Esempio n. 1
0
 void irc_OnArchMsg(RawMsg amsg)
 {
     backlog.Enqueue(amsg);
     if (newMessage != null)
     {
         newMessage(amsg, server);
     }
 }
 public JoinMsg(string who, string whoUser, string whoHost, string channel)
 {
     raw          = new RawMsg();
     this.who     = who;
     this.whoUser = whoUser;
     this.whoHost = whoHost;
     this.channel = channel;
 }
 public JoinMsg(RawMsg raw)
 {
     this.raw = raw;
     //who = raw.subject.nick;
     //whoUser = raw.subject.user;
     //whoHost = raw.subject.host;
     //channel = raw.payload;
 }
 public MSG(string from, string fromUser, string fromHost, string to, string message)
 {
     raw           = new RawMsg();
     when          = DateTime.Now;
     this.from     = from;
     this.fromHost = fromHost;
     this.fromUser = fromUser;
     this.to       = to;
     this.message  = message;
 }
 public KickMsg(string from, string fromUser, string fromHost, string to, string channel, string message)
 {
     raw           = new RawMsg();
     this.from     = from;
     this.fromHost = fromHost;
     this.fromUser = fromUser;
     this.to       = to;
     this.message  = message;
     this.channel  = channel;
 }
 public CTCP(string from, string fromUser, string fromHost, string to, string command, string message)
 {
     raw           = new RawMsg();
     when          = DateTime.Now;
     this.from     = from;
     this.fromHost = fromHost;
     this.fromUser = fromUser;
     this.to       = to;
     this.message  = message;
     this.command  = command;
 }
        public MSG(string line)
        {
            raw = new RawMsg();
            Regex msg = new Regex(@"(\[)(?<date>.*?)(\]\()(?<channel>.*?)(\)<)(?<nick>.*?)(> )(?<message>.*)");
            Match m   = msg.Match(line);

            if (m.Success)
            {
                from    = m.Groups["nick"].Value;
                to      = m.Groups["channel"].Value;
                message = m.Groups["message"].Value;
                when    = DateTime.Parse(m.Groups["date"].Value);
            }
        }
 public bool SendMessage(string target, string message)
 {
     lock (padlock)
     {
         sendQueue.Enqueue("PRIVMSG " + target + " :" + message);
         RawMsg rm = new RawMsg(nick + "!you@core PRIVMSG " + target + " :" + message);
         if (OnArchMsg != null)
         {
             OnArchMsg(rm);
         }
     }
     //stream.Write(ASCIIEncoding.ASCII.GetBytes("PRIVMSG " + target + " :" + message + "\n"), 0, ASCIIEncoding.ASCII.GetByteCount("PRIVMSG " + target + " :" + message + "\n"));
     return(true);
 }
 public MSG(RawMsg raw)
 {
     this.raw = raw;
 }
Esempio n. 10
0
 public CTCP(RawMsg raw)
 {
     this.raw = raw;
 }
Esempio n. 11
0
 public KickMsg(RawMsg raw)
 {
     this.raw = raw;
 }
Esempio n. 12
0
        void ReceiveThread()
        {
            byte[] receiveBuf    = new byte[1024];
            string lastOverdraft = "";

            while (true)
            {
                if (!client.Connected)
                {
                    if (quitOnPurpose)
                    {
                        return;
                    }
                    else
                    {
                        if (OnDisconnect != null)
                        {
                            OnDisconnect();
                        }
                        return;
                    }
                }
                int c = 0;
                try
                {
                    c = stream.Read(receiveBuf, 0, 1024);
                }
                catch
                {
                    if (quitOnPurpose)
                    {
                        return;
                    }
                    else
                    {
                        if (OnDisconnect != null)
                        {
                            OnDisconnect();
                        }
                        return;
                    }
                }
                if (c != 0)
                {
                    string        receivedTotal = lastOverdraft + Encoding.UTF8.GetString(receiveBuf, 0, c);
                    List <string> receivedLines = new List <string>();
                    for (int x = 0; x < receivedTotal.Length; x++)
                    {
                        if (receivedTotal[x] != "\n".ToCharArray()[0])
                        {
                            lastOverdraft += receivedTotal[x];
                        }
                        else
                        {
                            receivedLines.Add(lastOverdraft);
                            lastOverdraft = "";
                        }
                    }
                    foreach (string received in receivedLines)
                    {
                        try
                        {
                            if (received.StartsWith("PING :"))
                            {
                                stream.Write(ASCIIEncoding.ASCII.GetBytes("PONG :" + received.Substring("PING :".Length) + "\n"), 0, ASCIIEncoding.ASCII.GetByteCount("PONG :" + received.Substring("PING :".Length) + "\n"));
                            }
                            else if (received.Contains(":End of /MOTD command.") || received.Contains("MOTD File is missing"))
                            {
                                ready = true;
                            }
                            else if (received.StartsWith(":"))
                            {
                                if (received.Contains("!"))
                                {
                                    RawMsg msg = null;
                                    try
                                    {
                                        msg = new RawMsg(received.Replace("\r", "").Replace("\n", ""));
                                    }
                                    catch (Exception)
                                    {
                                        continue;
                                    }
                                    switch (msg.command)
                                    {
                                    case "PRIVMSG":
                                    {
                                        //PRIVMSG
                                        //string header = received.Substring(1, received.IndexOf(':', 1));
                                        //string message = received.Substring(received.IndexOf(':', 1) + 1).Replace("\r", "");
                                        //string from = header.Substring(0, header.IndexOf('!'));
                                        //string fromUser = header.Substring(header.IndexOf('!') + 1, header.IndexOf('@') - (header.IndexOf('!') + 1));
                                        //string fromHost = header.Substring(header.IndexOf('@') + 1, header.IndexOf(' ') - (header.IndexOf('@') + 1));
                                        //string to = header.Split(' ')[2];
                                        if (msg.payload.StartsWith("\x01"))
                                        {
                                            if (OnCTCPRecvd != null)
                                            {
                                                OnCTCPRecvd(new CTCP(msg));
                                            }
                                            if (OnArchMsg != null)
                                            {
                                                OnArchMsg(msg);
                                            }
                                        }
                                        else if (msg.location.Contains("#"))
                                        {
                                            //Message to Channel
                                            if (OnMSGRecvd != null)
                                            {
                                                OnMSGRecvd(new MSG(msg));
                                            }
                                            if (OnArchMsg != null)
                                            {
                                                OnArchMsg(msg);
                                            }
                                        }
                                        else
                                        {
                                            //Private Message
                                            if (OnPMRecvd != null)
                                            {
                                                OnPMRecvd(new MSG(msg));
                                            }
                                            if (OnArchMsg != null)
                                            {
                                                OnArchMsg(msg);
                                            }
                                        }
                                        break;
                                    }

                                    case "NOTICE":
                                    {
                                        //NOTICE
                                        //string header = received.Substring(1, received.IndexOf(':', 1));
                                        //string message = received.Substring(received.IndexOf(':', 1) + 1).Replace("\r", "");
                                        //string from = header.Substring(0, header.IndexOf('!'));
                                        //string fromUser = header.Substring(header.IndexOf('!') + 1, header.IndexOf('@') - (header.IndexOf('!') + 1));
                                        //string fromHost = header.Substring(header.IndexOf('@') + 1, header.IndexOf(' ') - (header.IndexOf('@') + 1));
                                        //string to = header.Split(' ')[2];
                                        if (OnNoticeRecvd != null)
                                        {
                                            OnNoticeRecvd(new MSG(msg));
                                        }
                                        if (OnArchMsg != null)
                                        {
                                            OnArchMsg(msg);
                                        }
                                        break;
                                    }

                                    case "JOIN":
                                    {
                                        //Response: :[email protected] JOIN :#chat
                                        //Add channel to list of channels
                                        if (msg.subject.nick == nick)
                                        {
                                            channels.Add(msg.payload);
                                        }
                                        //string who = received.Substring(1, received.IndexOf('!') - 1);
                                        //string whoUser = received.Substring(received.IndexOf('!') + 1, received.IndexOf('@') - (received.IndexOf('!') + 1));
                                        //string whoHost = received.Substring(received.IndexOf('@') + 1, received.IndexOf(' ') - (received.IndexOf('@') + 1));
                                        //string channel = received.Substring(received.IndexOf(':', 1) + 1).Replace("\r", "");
                                        if (OnJoinRecvd != null)
                                        {
                                            OnJoinRecvd(new JoinMsg(msg));
                                        }
                                        if (OnArchMsg != null)
                                        {
                                            OnArchMsg(msg);
                                        }
                                        break;
                                    }

                                    case "NICK":
                                    {
                                        //string who = received.Substring(1, received.IndexOf('!') - 1);
                                        //string whoUser = received.Substring(received.IndexOf('!') + 1, received.IndexOf('@') - (received.IndexOf('!') + 1));
                                        //string whoHost = received.Substring(received.IndexOf('@') + 1, received.IndexOf(' ') - (received.IndexOf('@') + 1));
                                        //string channel = received.Substring(received.IndexOf(':', 1) + 1).Replace("\r", "");
                                        if (OnNickRecvd != null)
                                        {
                                            OnNickRecvd(new JoinMsg(msg));
                                        }
                                        if (OnArchMsg != null)
                                        {
                                            OnArchMsg(msg);
                                        }
                                        break;
                                    }

                                    case "QUIT":
                                    {
                                        //string who = received.Substring(1, received.IndexOf('!') - 1);
                                        if (msg.subject.nick == nick)
                                        {
                                            if (OnDisconnect != null)
                                            {
                                                OnDisconnect();
                                            }
                                            client.Close();
                                            return;
                                        }
                                        //string whoUser = received.Substring(received.IndexOf('!') + 1, received.IndexOf('@') - (received.IndexOf('!') + 1));
                                        //string whoHost = received.Substring(received.IndexOf('@') + 1, received.IndexOf(' ') - (received.IndexOf('@') + 1));
                                        //string message = received.Substring(received.IndexOf(':', 1) + 1).Replace("\r", "");
                                        if (OnQuitRecvd != null)
                                        {
                                            OnQuitRecvd(new JoinMsg(msg));
                                        }
                                        if (OnArchMsg != null)
                                        {
                                            OnArchMsg(msg);
                                        }
                                        break;
                                    }

                                    case "PART":
                                    {
                                        if (msg.subject.nick == nick)
                                        {
                                            channels.Remove(msg.location);
                                        }
                                        //string header = received.Substring(1);
                                        //string message = received.Substring(received.IndexOf(':', 1) + 1);
                                        //string from = header.Substring(0, header.IndexOf('!'));
                                        //string fromUser = header.Substring(header.IndexOf('!') + 1, header.IndexOf('@') - (header.IndexOf('!') + 1));
                                        //string fromHost = header.Substring(header.IndexOf('@') + 1, header.IndexOf(' ') - (header.IndexOf('@') + 1));
                                        //string to = header.Split(' ')[2];
                                        if (OnPartRecvd != null)
                                        {
                                            OnPartRecvd(new MSG(msg));
                                        }
                                        if (OnArchMsg != null)
                                        {
                                            OnArchMsg(msg);
                                        }
                                        break;
                                    }

                                    case "KICK":
                                    {
                                        if (msg.target == nick)
                                        {
                                            channels.Remove(msg.location);
                                        }
                                        //string from = received.Substring(1, received.IndexOf('!') - 1);
                                        //string fromUser = received.Substring(received.IndexOf('!') + 1, received.IndexOf('@') - (received.IndexOf('!') + 1));
                                        //string fromHost = received.Substring(received.IndexOf('@') + 1, received.IndexOf(' ') - (received.IndexOf('@') + 1));
                                        //string message = "";
                                        if (OnKickRecvd != null)
                                        {
                                            OnKickRecvd(new KickMsg(msg));
                                        }
                                        if (OnArchMsg != null)
                                        {
                                            OnArchMsg(msg);
                                        }
                                        break;
                                    }

                                    case "MODE":
                                    {
                                        //Do modes later
                                        if (OnArchMsg != null)
                                        {
                                            OnArchMsg(msg);
                                        }
                                        break;
                                    }
                                    }
                                }
                                else
                                {
                                    switch (received.Split(' ')[1])
                                    {
                                    case "433":
                                        stream.Write(ASCIIEncoding.ASCII.GetBytes("NICK " + server.othernick + "\n"), 0, ASCIIEncoding.ASCII.GetByteCount("NICK " + server.othernick + "\n"));
                                        break;
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(DateTime.Now.ToLongTimeString() + ": Client error: (" + e.Message + ") Errored netcode: " + received);
                        }
                    }
                }
            }
        }