Ejemplo n.º 1
0
        public void SendToChannel(string channel, string message)
        {
            BotLogger.LogMessage(message);
            string data = IRCSymbols.FormatChannelMessage(channel, message);

            Socket.Send(data);
        }
Ejemplo n.º 2
0
        private void RequestTwitchMembershipStateEvents()
        {
            BotLogger.LogDebug("Requesting Membership capabilities.");
            string data = "CAP REQ :twitch.tv/membership twitch.tv/commands twitch.tv/tags";

            Socket.Send(data);
        }
Ejemplo n.º 3
0
 /**
  *  This methods cleanups the the running thread for the receiving socket
  */
 private void CleanUp()
 {
     BotLogger.LogDebug("Cleaning up the bot!");
     try
     {
         ReceivingThread.Interrupt();
         if (!ReceivingThread.Join(1000))
         {
             ReceivingThread.Abort();
         }
     }
     catch (Exception e)
     {
         if (e is ThreadAbortException tabex)
         {
             BotLogger.LogDebug("Couldn't terminate the thread in time. Forced it's shutdown.");
         }
         else
         {
             if (e is ThreadInterruptedException tiex)
             {
                 BotLogger.LogDebug("Interrupted the exception!");
             }
         }
     }
     BotLogger.LogDebug("Thread terminated!");
 }
Ejemplo n.º 4
0
 private void initTimer()
 {
     Timer_memory = new Timer((e) =>
     {
         long bytes       = GC.GetTotalMemory(false);
         double megabytes = Math.Round(bytes * 1E-6, 3);
         BotLogger.LogDebug($"Memory usage is : {megabytes}MB");
     }, null, 1000, 60 * 1000);
 }
Ejemplo n.º 5
0
 public void Connect(string oauth)
 {
     BotLogger.LogDebug("[ >>Connecting! ]");
     Socket.Connect();
     SendOauth(oauth);
     SendToIRC(IRCSymbols.FormatUsername(Username));
     SendToIRC(IRCSymbols.FormatJoin(DefaultChannel));
     RequestTwitchMembershipStateEvents();
     this.Running = true;
     ReceivingThread.Start();
 }
Ejemplo n.º 6
0
 private void ReceiveData()
 {
     while (Running)
     {
         string data = null;
         data = Socket.Receive();
         if (data != null)
         {
             /*  Parse
              *  decide what to do with data
              */
             BotLogger.LogDebug(data);
             CallbackHandler.RunCallbacks(data);
         }
     }
 }
Ejemplo n.º 7
0
        private void DefaultActions()
        {
            /*
             *  This responds to Ping requests
             */
            CallbackHandler.AddToplevelCallback((string data) =>
            {
                if (Regex.Match(data, IRCSymbols.Keywords.PING).Success)
                {
                    sendPONG();
                }
                return(CallbackAction.SKIP_OTHERS);
            });

            /*
             *  This parses the channel,username and message and fires the OnChannelMessage Event
             */
            CallbackHandler.AddToplevelCallback((string data) =>
            {
                /*
                 *  There are two cases,
                 *
                 * one is a regular privmsg
                 * second is privmsg with bits information
                 *
                 */
                if (data.Contains(IRCSymbols.Keywords.BITS))
                {
                    Match match = Regex.Match(data, @"@badges=([a-zA-Z]+/\d+).+;bits=(\d+).+:([a-zA-Z_0-9]+)!.+PRIVMSG\s#(\w+)\s:(.+)");
                    if (match.Success)
                    {
                        var tuple           = IRCSymbols.ParseBadge(match.Groups[1].Value);
                        int bits            = Int32.Parse(match.Groups[2].Value);
                        string username     = match.Groups[3].Value;
                        string channel      = match.Groups[4].Value;
                        string message      = match.Groups[5].Value;
                        BitsEvent bitsEvent = new BitsEvent(tuple.badge, tuple.version, bits, username, channel, message);

                        return(CallbackAction.SKIP_OTHERS);
                    }
                }
                else
                if (data.Contains(IRCSymbols.Keywords.PRIVMSG))
                {
                    Match match = Regex.Match(data, @":(\w+)!.+#(\w+) :(.+)$");
                    if (match.Success)
                    {
                        string username        = match.Groups[1].Value;
                        string channel         = match.Groups[2].Value;
                        string message         = match.Groups[3].Value;
                        string badge           = String.Empty;
                        int version            = -1;
                        Match match_message_id = Regex.Match(data, @";id=([a-zA-Z0-9-]+);");
                        string message_id      = null;
                        if (match_message_id.Success)
                        {
                            message_id = match_message_id.Groups[1].Value;
                        }

                        Match match_badge = Regex.Match(data, @"@badges=(\w+/\d+|);");
                        if (match_badge.Success)
                        {
                            badge = match_badge.Groups[1].Value;
                            if (!String.IsNullOrEmpty(badge))
                            {
                                string[] parsed = badge.Split('/');
                                badge           = parsed[0];
                                version         = Int32.Parse(parsed[1]);
                            }
                        }
                        ChannelMessageEvent channel_message = new ChannelMessageEvent(channel, username, message, message_id, badge, version);
                        CallbackHandler.RunOnChannelMessageCallbacks(channel_message);
                    }
                }
                return(CallbackAction.SKIP_OTHERS);
            });

            /*
             *  Callback that parses and triggers & logs bad error
             */
            CallbackHandler.AddToplevelCallback((string data) =>
            {
                if (Regex.Match(data, @":Unknown command").Success)
                {
                    BotLogger.LogError("[ >> Unknown command! ]");
                }
                return(CallbackAction.SKIP_OTHERS);
            });

            /*
             *  This parses that user joins the channel and fires onJoinChannel callbacks
             */
            CallbackHandler.AddToplevelCallback((string data) =>
            {
                Match match = Regex.Match(data, @":(.+)!.+ JOIN #(.+)");
                if (match.Success)
                {
                    UserActionUponChannel joinChannel = new UserActionUponChannel(channel: match.Groups[2].Value, username: match.Groups[1].Value);
                    CallbackHandler.RunOnJoinedChannelCallback(joinChannel);
                }
                return(CallbackAction.SKIP_OTHERS);
            });

            /*
             *  Callback to parse and trigger OnLeaveChannel event
             */
            CallbackHandler.AddToplevelCallback((string data) =>
            {
                Match match = Regex.Match(data, @":(.+)!.+ PART #(.+)");
                if (match.Success)
                {
                    UserActionUponChannel leaveChannel = new UserActionUponChannel(channel: match.Groups[2].Value, username: match.Groups[1].Value);
                    CallbackHandler.RunOnLeaveChannelCallback(leaveChannel);
                }
                return(CallbackAction.SKIP_OTHERS);
            });

            /* Usernotice */
            CallbackHandler.AddToplevelCallback((string data) =>
            {
                //If it matches a sub/resub
                Match match = Regex.Match(data, @"@badges=(\w+/\d+).+;display-name=([a-zA-Z0-9]+).+;msg-id=(resub|sub);msg-param-cumulative-months=(\d+);msg-param-streak-months=(\d+);.+;msg-param-sub-plan=([a-zA-Z0-9]+).+USERNOTICE\s#(\w+)\s:(.+)");
                if (match.Success)
                {
                    var badge_version      = match.Groups[1].Value.Split('/');
                    string badge           = badge_version[0];
                    string version         = badge_version[1];
                    string username        = match.Groups[2].Value;
                    string subtype         = match.Groups[3].Value;
                    int commulative_months = Int32.Parse(match.Groups[3].Value);
                    int consecutive_months = Int32.Parse(match.Groups[4].Value);
                    string subplan         = match.Groups[6].Value;
                    string channel         = match.Groups[7].Value;
                    string message         = match.Groups[8].Value;

                    SubscriptionEvent subs_event = new SubscriptionEvent
                                                       (badge, version, username,
                                                       subtype, commulative_months, consecutive_months,
                                                       subplan, channel, message);
                    CallbackHandler.RunOnSubscribeCallback(subs_event);
                    return(CallbackAction.SKIP_OTHERS);
                }
                return(CallbackAction.CONTINUE);
            });

            /*
             *  Parses a gift event
             */
            CallbackHandler.AddToplevelCallback((string data) =>
            {
                Match match = Regex.Match(data, @"@badges=(\w+/\d+).+;display-name=([a-zA-Z0-9]+).+;msg-id=(\w+);msg-param-months=(\d+);msg-param-recipient-display-name=([a-zA-Z0-9_]+);.+msg-param-sub-plan=([a-bA-Z0-9]+);");
                if (match.Success)
                {
                    var badge_version   = IRCSymbols.ParseBadge(match.Groups[1].Value);
                    string gifter       = match.Groups[2].Value;
                    string typeOfGift   = match.Groups[3].Value;
                    int total_months    = Int32.Parse(match.Groups[4].Value);
                    string recipient    = match.Groups[5].Value;
                    string subplan      = match.Groups[6].Value;
                    string message      = match.Groups[7].Value;
                    GiftEvent giftevent = new GiftEvent(badge_version.badge, badge_version.version, gifter, typeOfGift, total_months, subplan, recipient, message);
                    CallbackHandler.RunOnSubGiftCallback(giftevent);
                    return(CallbackAction.SKIP_OTHERS);
                }
                return(CallbackAction.CONTINUE);
            });

            /*
             * Parses a RaidingEvent
             */
            CallbackHandler.AddToplevelCallback((string data) =>
            {
                Match match = Regex.Match(data, @"@badges=([a-zA-Z0-9]+/\d+);.+login=([a-zA-Z0-9]+);.+;msg-param-viewerCount=(\d+).+;system-msg=(.+)\sUSERNOTICE\s#(.+)");
                if (match.Success)
                {
                    var tuple                  = IRCSymbols.ParseBadge(match.Groups[1].Value);
                    var raiderchannel          = match.Groups[2].Value;
                    var viewers                = Int32.Parse(match.Groups[3].Value);
                    var message                = match.Groups[4].Value;
                    var raidedchannel          = match.Groups[5].Value;
                    RaidingEvent raiding_event = new RaidingEvent(tuple.badge, tuple.version, raiderchannel, viewers, message, raidedchannel);
                    CallbackHandler.RunOnRaidingCallbacks(raiding_event);
                    return(CallbackAction.SKIP_OTHERS);
                }

                return(CallbackAction.CONTINUE);
            });

            /*
             *  Parses a RitualEvent
             */

            CallbackHandler.AddToplevelCallback((string data) =>
            {
                Match match = Regex.Match(data, @"display-name=([a-zA-Z_0-9]+).+;login=([a-zA-Z_0-9]+).+;msg-id=(ritual);msg-param-ritual-name=([a-zA-Z0-9_]+);.+;system-msg=(.+);tmi.+USERNOTICE\s#([a-zA-Z0-9]+)\s+:(.+)");
                if (match.Success)
                {
                    string username          = match.Groups[2].Value;
                    string ritual            = match.Groups[3].Value;
                    string typeOfRitual      = match.Groups[4].Value;
                    string event_message     = match.Groups[5].Value;
                    string channel           = match.Groups[6].Value;
                    string user_message      = match.Groups[7].Value;
                    RitualEvent ritual_event = new RitualEvent(username, ritual, typeOfRitual, event_message, channel, user_message);
                    CallbackHandler.RunOnRitualEvent(ritual_event);
                    return(CallbackAction.SKIP_OTHERS);
                }
                return(CallbackAction.CONTINUE);
            });

            /*
             *  Callback if user sends !github command, it responds back with github url
             */
            CallbackHandler.AddToChannelMessageCallback((ChannelMessageEvent channelMessage) =>
            {
                if (channelMessage.Message.ToLower().Contains(IRCSymbols.CustomChannelCommands.GITHUB))
                {
                    SendToChannel(channel: channelMessage.Channel, message: GITHUB_URL);
                    return(CallbackAction.SKIP_OTHERS);
                }
                return(CallbackAction.CONTINUE);
            });

            CallbackHandler.AddToSubscriptionCallback(
                (SubscriptionEvent subscription) =>
            {
                SendToChannel(subscription.Channel, "Thanks for the (re)subscription!");
                return(CallbackAction.SKIP_OTHERS);
            });
        }
Ejemplo n.º 8
0
 public void SendToIRC(string message)
 {
     BotLogger.LogMessage(message);
     Socket.Send(message);
 }
Ejemplo n.º 9
0
 private void SendOauth(string oauth)
 {
     BotLogger.LogDebug("[ >> Sending OAUTH ]");
     Socket.Send(IRCSymbols.FormatOAuth(oauth));
 }
Ejemplo n.º 10
0
 private void sendPONG()
 {
     BotLogger.LogDebug("[ >> Sending PONG ]");
     SendToIRC("PONG :tmi.twitch.tv");
 }
Ejemplo n.º 11
0
 public void Debug()
 {
     BotLogger.LogDebug("[ >>Sending HelloWorld! ]");
     SendToIRC(IRCSymbols.FormatChannelMessage(DefaultChannel, "HelloWorld!"));
     initTimer();
 }