Esempio n. 1
0
 public IrcPacket(IrcClient irc, string prefix, string action, StringStream content, string origString)
     : base(action, content)
 {
     this.irc = irc;
     this.prefix = prefix;
     this.origString = origString;
 }
Esempio n. 2
0
 protected override void OnText(IrcUser user, IrcChannel chan, StringStream text)
 {
     if (HideChatting != true)
     {
         Console.ForegroundColor = ConsoleColor.Green;
         Console.WriteLine("({0}) <IRC Interface> {1} <{2}> {3}", DateTime.Now.ToString("hh:mm"), chan, user, text);
     }
 }
Esempio n. 3
0
 public static void OnConsoleText(StringStream cText)
 {
     try
     {
         switch (cText.NextWord().ToLower())
         {
             case "join":
                 {
                     if (cText.Remainder.Contains(","))
                     {
                         var chaninfo = cText.Remainder.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
                         if (chaninfo.Length > 1)
                             JadBot.Irc.CommandHandler.Join(chaninfo[0], chaninfo[1]);
                         else
                             JadBot.Irc.CommandHandler.Join(chaninfo[0]);
                     }
                     else
                     {
                         JadBot.Irc.CommandHandler.Join(cText.Remainder);
                     }
                 }
                 break;
             case "say":
                 {
                     var chan = cText.NextWord();
                     var msg = cText.Remainder;
                     JadBot.Irc.CommandHandler.Msg(chan, msg);
                 }
                 break;
             case "quit":
                 {
                     JadBot.Parser.Kill();
                     JadBot.IrcLog.WriteLine("Shutting down due to console quit command..");
                     foreach (var chan in JadBot.ChannelList)
                     {
                         JadBot.Irc.CommandHandler.Msg(chan, "Shutting down in 5 seconds due to console quit command..");
                     }
                     Thread.Sleep(5000);
                     JadBot.Irc.Client.DisconnectNow();
                     Environment.Exit(0);
                 }
                 break;
         }
     }
     catch(Exception e)
     {
         WriteErrorSystem.WriteError(e);
     }
 }
 public NoticeCmdTrigger(StringStream args, IrcUser user)
     : this(args, user, null)
 {
 }
 public DccChatCmdTrigger(StringStream args, IrcUser user)
     : base(args, user, null)
 {
 }
Esempio n. 6
0
        internal void TextNotify(IrcUser user, IrcChannel chan, StringStream text)
        {
            if (chan != null)
                chan.TextNotify(user, text);
            OnText(user, chan, text);

            if (TriggersCommand(user, chan, text))
            {
                m_CommandHandler.ReactTo(new MsgCmdTrigger(text, user, chan));
            }
        }
Esempio n. 7
0
 internal void NoticeNotify(IrcUser user, IrcChannel chan, StringStream text)
 {
     if (chan != null)
         chan.NoticeReceivedNotify(user, text);
     OnNotice(user, chan, text);
     if (TriggersCommand(user, chan, text))
     {
         m_CommandHandler.ReactTo(new NoticeCmdTrigger(text, user, chan));
     }
 }
Esempio n. 8
0
 public virtual bool TriggersCommand(IrcUser user, IrcChannel chan, StringStream input)
 {
     return input.ConsumeNext(CommandHandler.RemoteCommandPrefix);
 }
Esempio n. 9
0
 private void ReceivedNotify(ByteBuffer buf)
 {
     if (InternalBytesReceived != null)
     {
         InternalBytesReceived(this, buf);
         buf.offset = 0;
     }
     if (BytesReceived != null)
     {
         BytesReceived(this, buf);
     }
     else if (RawTextReceived != null || LineReceived != null)
     {
         string text = lastLine + encoding.GetString(buf.Data, 0, buf.length);
         if (RawTextReceived != null)
             RawTextReceived(this, new StringStream(text));
         if (LineReceived != null)
         {
             var ss = new StringStream(text);
             while (ss.HasNext)
             {
                 if (BytesReceived != null)
                 {
                     // Handler might change
                     int count;
                     if (encoding.IsSingleByte)
                         count = ss.Position;
                     else
                         count = encoding.GetByteCount(ss.String.Substring(0, ss.Position));
                     buf.offset = count;
                     buf.length -= count;
                     BytesReceived(this, buf);
                     break;
                 }
                 if (LineReceived != null)
                 {
                     string line = ss.NextWord(lineTerminator);
                     if (!ss.HasNext && !text.EndsWith(lineTerminator))
                     {
                         lastLine += line;
                         return;
                     }
                     LineReceived(this, new StringStream(line));
                 }
             }
             lastLine = "";
         }
     }
 }
Esempio n. 10
0
 public StringStream(StringStream stream)
     : this(stream.str, stream.pos)
 {
 }
Esempio n. 11
0
 /// <summary>
 /// Fires when the Client receives any kind of NOTICE.
 /// </summary>
 /// <param name="user">The User who sent the text</param>
 /// <param name="channel">The Channel where it was sent (can be null)</param>
 /// <param name="text">The text which was sent</param>
 protected virtual void OnNotice(IrcUser user, IrcChannel channel, StringStream text)
 {
 }
Esempio n. 12
0
 internal void TextNotify(IrcUser user, StringStream text)
 {
     if (TextReceived != null)
         TextReceived(user, text);
 }
Esempio n. 13
0
 internal void NoticeReceivedNotify(IrcUser user, StringStream text)
 {
     if (NoticeReceived != null)
         NoticeReceived(user, text);
 }
Esempio n. 14
0
 internal void NoticeNotify(IrcUser user, IrcChannel channel, StringStream text)
 {
     OnNotice(user, channel, text);
 }
Esempio n. 15
0
 internal void MsgReceivedNotify(IrcUser user, StringStream text)
 {
     if (MsgReceived != null)
         MsgReceived(user, text);
 }
Esempio n. 16
0
 public Object Clone()
 {
     var ss = new StringStream(str);
     ss.pos = pos;
     return ss;
 }
Esempio n. 17
0
        /// <summary>
        /// 
        /// </summary>
        private void SetupHandlers()
        {
            // If its not recognized:
            defaultPacketHandler = packet =>
            {
                // PING is special
                if (packet.Prefix == "PING")
                {
                    packet.IrcClient.Send("PONG " + packet.Key);
                }
                else if (packet.Key.StartsWith("4") || packet.Key.StartsWith("5"))
                {
                    // Error Replies
                    packet.IrcClient.ErrorNotify(packet);
                }
                else
                {
                    // Normal replies
                    packet.IrcClient.UnspecifiedInfoNotify(packet);
                }
            };

            // Connection Handling
            AddPacketHandler("001", packet =>
            {
                // :Serv 001 Me :<Welcomemessage>
                packet.IrcClient.Me.ChangeNick(packet.Content.NextWord());
                defaultPacketHandler(packet);
            });

            AddPacketHandler("005", packet => { packet.IrcClient.ConnectionInfoNotify(packet); });
            AddPacketHandler("376", packet => { packet.IrcClient.PerformNotify(); });

            // Messaging
            AddPacketHandler("PRIVMSG", packet =>
            {
                var strStream = new StringStream(packet.Content);
                packet.channel = packet.IrcClient.GetChannel(strStream.NextWord());

                var args = packet.Args;
                packet.IrcClient.TextNotify(packet.User, packet.channel, new StringStream(args));

                if (args.StartsWith("") && args.EndsWith(""))
                {
                    // CTCP
                    args = packet.Args.Split('')[1];

                    var argsStream = new StringStream(args);
                    packet.IrcClient.CtcpRequestNotify(packet.User, packet.channel, argsStream.NextWord(),
                                                       argsStream.Remainder);
                }
                else
                {
                    // Text message
                    var argsStream = new StringStream(args);
                    if (packet.channel == null)
                    {
                        packet.IrcClient.QueryMsgNotify(packet.User, argsStream);
                    }
                    else
                    {
                        packet.IrcClient.ChannelMsgNotify(packet.User, packet.channel, argsStream);
                    }
                }
            });

            AddPacketHandler("NOTICE", packet =>
            {
                var strStream = new StringStream(packet.Content);
                packet.channel = packet.IrcClient.GetChannel(strStream.NextWord());

                string args = packet.Args;
                var argsStream = new StringStream(strStream.Remainder.Trim());
                packet.IrcClient.TextNotify(packet.User, packet.channel, argsStream);
                if (packet.Args.StartsWith("") && packet.Args.EndsWith(""))
                {
                    args = packet.Args.Substring(1, packet.Args.Length - 2);

                    // TODO: Check if this is right?
                    packet.IrcClient.CtcpReplyNotify(packet.User, packet.channel, argsStream.NextWord(),
                                                     packet.Args);
                }
                else
                {
                    packet.IrcClient.NoticeNotify(packet.User, packet.channel, new StringStream(packet.Args));
                }
            });

            // Nick / Quit
            AddPacketHandler("NICK", packet => { packet.IrcClient.NickNotify(packet.User, packet.Args); });
            AddPacketHandler("QUIT", packet => { packet.IrcClient.QuitNotify(packet.User, packet.Args); });
            AddPacketHandler("431", packet => { packet.IrcClient.InvalidNickNotify("431", "", packet.Args); }); // :Serv 431 Me :No nickname given
            AddPacketHandler("433", packet => // :Serv 433 Me <Nick> :Nickname is already in use.
                                    {
                                        var strStream = new StringStream(packet.Content);
                                        strStream.SkipWord();
                                        packet.IrcClient.InvalidNickNotify(packet.Key, strStream.NextWord(), packet.Args);
                                    });

            AddPacketHandler("432", packetHandlers["433"][0]); // :Serv 432 Me <Nick> :Invalid nickname: ...

            // Channel related Stuff
            AddPacketHandler("INVITE", packet => { packet.IrcClient.InviteNotify(packet.User, packet.ArgsOrFirstWord); });
            AddPacketHandler("JOIN", packet => { packet.IrcClient.JoinNotify(packet.User, packet.ArgsOrFirstWord); });
            AddPacketHandler("485", packet =>										// cannot join channel
            {
                packet.channel = packet.IrcClient.GetChannel(packet.Content.NextWord());
                packet.IrcClient.CannotJoinNotify(packet.channel, packet.Args);
            });
            AddPacketHandler("471", delegate { });
            AddPacketHandler("473", delegate { });
            AddPacketHandler("474", delegate { });
            AddPacketHandler("475", delegate { });
            AddPacketHandler("477", delegate { });

            AddPacketHandler("TOPIC", packet =>
            {
                // User TOPIC Channel :Topic
                var content = packet.Content;

                packet.channel = packet.IrcClient.GetOrCreateChannel(content.NextWord());
                packet.channel.TopicSetter = packet.User;
                packet.channel.TopicSetTime = DateTime.Now;
                packet.IrcClient.TopicNotify(packet.User, packet.channel, packet.Args, false);
                packet.channel.SetTopic(packet.Args);
            });
            AddPacketHandler("332", packet =>
            {
                // :Sender 332 Me Channel :Topic
                var content = packet.Content;
                //packet.User = packet.IrcClient.GetOrCreateUser(content.NextWord());
                content.SkipWord();
                packet.channel = packet.IrcClient.GetOrCreateChannel(content.NextWord());
                packet.IrcClient.TopicNotify(packet.User, packet.channel, packet.Args, true);
                packet.channel.SetTopic(packet.Args);
            });

            AddPacketHandler("333", packet =>
            {
                // :Sender 333 Me Channel TopicSetter TopicTimestamp
                StringStream content = packet.Content;
                content.SkipWord();
                packet.channel = packet.IrcClient.GetOrCreateChannel(content.NextWord());
                packet.User = packet.IrcClient.GetOrCreateUser(content.NextWord());
                packet.channel.TopicSetter = packet.User;
                packet.channel.TopicSetTime = new DateTime(1970, 1, 1) +
                                              TimeSpan.FromSeconds(content.NextInt());
            });

            AddPacketHandler("353", packet => // :Serv 353 Me = Channel :Namelist
                                    {
                                        var strStream = new StringStream(packet.Content);
                                        strStream.SkipWords(2);
                                        packet.channel = packet.IrcClient.GetChannel(strStream.NextWord());
                                        IrcUser[] users = packet.channel.AddNames(packet.Args);
                                        packet.IrcClient.UsersAddedNotify(packet.channel, users);
                                    });
            AddPacketHandler("MODE", packet => // :Serv MODE Channel Modes [ModeArgs]
                                     {
                                         var strStream = new StringStream(packet.Content);
                                         packet.channel = packet.IrcClient.GetChannel(strStream.NextWord());
                                         strStream.Consume(':', 1);
                                         string modes = strStream.NextWord();
                                         strStream.Consume(':', 1);
                                         string[] prms = strStream.RemainingWords();
                                         packet.ProtHandler.ParseModes(packet.User, packet.channel, modes, prms);
                                     });
            AddPacketHandler("PART", packet =>
            {
                var strStream = new StringStream(packet.Content);
                packet.channel = packet.IrcClient.GetChannel(strStream.NextWord());
                packet.IrcClient.PartNotify(packet.User, packet.channel, packet.Args);
            });
            AddPacketHandler("KICK", packet =>
            {
                var strStream = new StringStream(packet.Content);
                packet.channel = packet.IrcClient.GetChannel(strStream.NextWord());
                IrcUser user = packet.IrcClient.GetOrCreateUser(strStream.NextWord());
                packet.IrcClient.KickNotify(packet.User, packet.channel, user, packet.Args);
            });
            AddPacketHandler("324", packet => // :Serv 324 Me Channel Modes [ModeArgs]
                                    {
                                        var strStream = new StringStream(packet.Content);
                                        strStream.SkipWord(); // skip "Me"
                                        packet.channel = packet.IrcClient.GetChannel(strStream.NextWord());

                                        strStream.Consume(':', 1);
                                        string modes = strStream.NextWord();
                                        strStream.Consume(':', 1);
                                        string[] prms = strStream.RemainingWords();
                                        packet.ProtHandler.ParseModes(packet.User, packet.channel, modes, prms);
                                    });
            AddPacketHandler("329", packet => // :Serv 329 [Me] #Channel CreationTime
                                    {
                                        var strStream = new StringStream(packet.Content);
                                        String chanName = strStream.NextWord();
                                        if (!chanName.StartsWith("#"))
                                        {
                                            chanName = strStream.NextWord();
                                        }
                                        packet.channel = packet.IrcClient.GetChannel(chanName);
                                        DateTime time = new DateTime(1970, 1, 1) +
                                                        TimeSpan.FromSeconds(Convert.ToInt32(strStream.NextWord()));
                                        if (packet.channel != null)
                                            packet.channel.SetCreationTime(time);
                                        packet.IrcClient.ChanCreationTimeNotify(packet.channel, time);
                                    });
            AddPacketHandler("352", packet =>
            {
                // who info
                //strStream.SkipWord();
                var strStream = new StringStream(packet.Content);
                string channame = strStream.NextWord();
                string username = strStream.NextWord();
                string host = strStream.NextWord();
                string server = strStream.NextWord();
                string nick = strStream.NextWord();
                string flags = strStream.NextWord();
                string hops = strStream.NextWord().Remove(1, 1);
                string info = packet.Args.Substring(packet.Args.IndexOf(" ") + 1);
                packet.IrcClient.GetOrCreateUser(nick).SetInfo(username, host, info);
                packet.IrcClient.WhoReplyNotify(channame, username, host, server, nick, flags, hops, info);
            });
            AddPacketHandler("367", packet => // :Serv 367 Me Channel banMask bannerName banTime
                                    {
                                        var content = packet.Content;
                                        content.SkipWord();
                                        packet.channel = packet.IrcClient.GetChannel(content.NextWord());

                                        var banmask = content.NextWord();
                                        var banner = content.NextWord();
                                        var bantime = content.NextWord();
                                        var entry = new BanEntry(banmask, banner, new DateTime(1970, 1, 1) +
                                                                                  TimeSpan.FromSeconds(Convert.ToInt32(bantime)));
                                        if (packet.channel != null && !packet.channel.BanMasks.ContainsKey(banmask))
                                        {
                                            packet.channel.BanMasks.Add(banmask, entry);
                                        }
                                        packet.IrcClient.BanListEntryNotify(packet.channel, entry);
                                    });
            AddPacketHandler("368", packet =>
            {
                packet.channel = packet.IrcClient.GetChannel(packet.Content.NextWord());
                packet.IrcClient.BanListCompleteNotify(packet.channel);
            });
        }
 /// <summary>
 /// Calls <code>return ReactTo(new CmdTrigger(text));</code>.
 /// </summary>
 /// <param name="text"></param>
 /// <returns></returns>
 public bool ReactTo(StringStream text)
 {
     return ReactTo(new ExecuteCmdTrigger(text));
 }
Esempio n. 19
0
 /// <summary>
 /// Is called on a response from the server
 /// </summary>
 /// 
 public virtual bool OnServerResponse(string sender, string action, StringStream remainder)
 {
     return false;
 }
Esempio n. 20
0
 /// <summary>
 /// Fires when the Client receives a PRIVMSG, directed to this Client itself.
 /// </summary>
 protected virtual void OnQueryMsg(IrcUser user, StringStream text)
 {
 }
Esempio n. 21
0
 public CmdTrigger(StringStream args)
     : this(args, null, null)
 {
 }
Esempio n. 22
0
 internal void ChannelMsgNotify(IrcUser user, IrcChannel chan, StringStream text)
 {
     chan.MsgReceivedNotify(user, text);
     OnChannelMsg(user, chan, text);
 }
Esempio n. 23
0
 public CmdTrigger(StringStream args, IrcUser user, IrcChannel chan)
 {
     this.args = args;
     this.user = user;
     this.chan = chan;
 }
Esempio n. 24
0
 internal void QueryMsgNotify(IrcUser user, StringStream text)
 {
     OnQueryMsg(user, text);
 }
Esempio n. 25
0
 static void CheckMail()
 {
     try
     {
         Console.WriteLine("Checking Mailbox");
         POPClient pop3 = new POPClient();
         pop3.Connect(settings.Default.EmailHost, 110, false);
         pop3.Authenticate(settings.Default.EmailUsername, settings.Default.EmailPassword);
         int i = 0;
         while (i <= pop3.GetMessageCount())
         {
             Console.WriteLine(pop3.GetMessageCount());
             var msg = pop3.GetMessage(i);
             if (msg != null && msg.MessageBody.Capacity > 0 && !string.IsNullOrEmpty(msg.MessageBody[0]) && msg.Headers.From.ToString() == settings.Default.EmailFromAddress)
             {
                 Console.WriteLine("Marking message for deletion..");
                 pop3.DeleteMessage(i);
                 string msgbody = msg.MessageBody[0].Replace("\n", " ");
                 msgbody = msgbody.Replace("\r", " ");
                 var msgsplit = new StringStream(msgbody);
                 msgbody = msgsplit.NextWord("=====");
                 BuildNotifier.Irc.CommandHandler.Msg(settings.Default.NotificationChannel, msgbody);
                 Console.WriteLine(msgbody);
             }
             i++;
         }
         Console.WriteLine("Closing connection, deleting messages");
         pop3.Disconnect();
     }
     catch (Exception e)
     {
         WriteErrorSystem.WriteError(null, e.Message + e.Data + e.Source + e.StackTrace);
     }
 }
Esempio n. 26
0
 /// <summary>
 /// Fires when the Client receives a PRIVMSG which was directed to a Channel.
 /// </summary>
 protected virtual void OnChannelMsg(IrcUser user, IrcChannel chan, StringStream text)
 {
 }
        public IrcPacket[] ExtractPackets(ByteBuffer partialResponse)
        {
            var str = partialResponse.GetString(encoding);
            var response = lastResponsePart + str;
            var ss = new StringStream(response);
            var packets = new List<IrcPacket>(3);

            while (ss.HasNext)
            {
                var content = ss.NextWord(PacketTerminator);
                if (!ss.HasNext && !response.EndsWith(PacketTerminator))
                {
                    lastResponsePart = content;
                }
                else
                {
                    try
                    {
                        var packet = CreatePacket(content);
                        packets.Add(packet);
                        if (!ss.HasNext)
                        {
                            lastResponsePart = "";
                        }
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Unable to parse packet: " + content, e);
                    }
                }
            }
            return packets.ToArray();
        }
        /// <summary>
        /// Build a packet from a new line of content from the server.
        /// Do as much parsing as possible here before the packet-handler
        /// will then work with the gathered information.
        /// </summary>
        public IrcPacket CreatePacket(string content)
        {
            var line = new StringStream(content.Trim());

            string prefix;
            if (content[0] == ':')
            {
                prefix = line.NextWord().Substring(1);
            }
            else
            {
                prefix = line.NextWord();
            }

            var action = line.NextWord();
            var packet = new IrcPacket(irc, prefix, action, new StringStream(line.Remainder.Trim()), line.String)
            {
                protHandler = this
            };

            return packet;
        }
 public ExecuteCmdTrigger(StringStream args)
     : base(args)
 {
 }
Esempio n. 30
0
 public NoticeCmdTrigger(StringStream args, IrcUser user, IrcChannel chan)
     : base(args, user, chan)
 {
 }
Esempio n. 31
0
 protected override void OnText(IrcUser user, IrcChannel chan, StringStream text)
 {
     if (HideChatting != true)
     {
         Console.ForegroundColor = ConsoleColor.Green;
         Console.WriteLine("({0}) <IRC Interface> {1} <{2}> {3}", DateTime.Now.ToString("hh:mm"), chan, user, text);
     }
 }