예제 #1
0
파일: Server.cs 프로젝트: davuxcom/IRCLib
        public void OpenConversation(string Name)
        {
            Channel cn = new Channel(this, Name);

            Channels.Add(cn);
            if (OnJoined != null)
            {
                OnJoined.Invoke(this, cn);
            }
        }
예제 #2
0
파일: Server.cs 프로젝트: davuxcom/IRCLib
        private void OnMessage(Message m)
        {
            Event.Invoke(this, Event.ServerEventType.Message, m);
            switch (m.Command.ToUpper())
            {
            case "NOTICE":
                if (OnNotice != null)
                {
                    OnNotice.Invoke(this, m);
                }
                break;

            case "TOPIC":
                // --> :[email protected] TOPIC #davux :Test topic
            {
                Channel c = FindChannel(m.Target);
                if (c != null)
                {
                    c.ChangeTopic(new User(this, m.Prefix), m.ListString);
                }
                else
                {
                    Trace.WriteLine("IRC/Server/OnMessage/TOPIC: Invalid Channel for TOPIC: " + m);
                }
            }
            break;

            case "PRIVMSG":
                if (m.ListString == "\x001VERSION\x001")
                {
                    Notice(m.PrefixNick, "\x001VERSION irc.net ALPHA " + Assembly.GetExecutingAssembly().GetName().Version + " " + Environment.OSVersion.Platform + "\x001");
                }
                else if (m.Target.ToLower() == Nick.ToLower())
                {
                    Channel cf = FindChannel(m.PrefixNick);
                    User    u  = new User(this, m.Prefix);
                    if (cf != null)
                    {
                        cf.MessageRcv(u, m.ListString);
                    }
                    else
                    {
                        Channel cn = new Channel(this, m.PrefixNick);
                        Channels.Add(cn);
                        if (OnJoined != null)
                        {
                            OnJoined.Invoke(this, cn);
                        }
                        cn.MessageRcv(u, m.ListString);
                    }
                }
                else
                {
                    Channel cf = FindChannel(m.Target);
                    User    u  = new User(this, m.Prefix);
                    if (cf != null)
                    {
                        cf.MessageRcv(u, m.ListString);
                    }
                    else
                    {
                        Channel cn = new Channel(this, m.Target);
                        cn.MessageRcv(u, m.ListString);
                        Channels.Add(cn);
                        if (OnJoined != null)
                        {
                            OnJoined.Invoke(this, cn);
                        }
                    }
                }

                break;

            case "JOIN":
            {
                if (m.PrefixNick.ToLower() == Nick.ToLower())
                {
                    // i joined a channel!
                    Channel c = new Channel(this, m.Target);
                    if (OnJoined != null)
                    {
                        OnJoined.Invoke(this, c);
                        // Thread.Sleep(1000);
                    }
                    Channels.Add(c);
                }
                else
                {
                    // someone joined a channel that im on
                    Channel c = FindChannel(m.Target);
                    if (c != null)
                    {
                        User ux = new User(this, m.Prefix);
                        if (ux != null)
                        {
                            c.JoinUser(ux, true);
                        }
                        else
                        {
                            Trace.WriteLine("IRC/Server/OnMessage: Invalid User for JOIN: " + m);
                        }
                    }
                    else
                    {
                        Trace.WriteLine("IRC/Server/OnMessage: Invalid Channel for JOIN: " + m);
                    }
                }
                break;
            }

            case "376":
                if (OnStatusMessage != null)
                {
                    OnStatusMessage.Invoke(this, m);
                }
                if (!autoJoined)
                {
                    autoJoined = true;
                    if (OnConnected != null)
                    {
                        OnConnected.Invoke(this);
                    }
                    JoinAutoChannels();
                }
                break;

            case "005":
            {
                string[] parts = m.AfterCommandString.Split(' ');

                foreach (string p in parts)
                {
                    if (p.ToUpper().StartsWith("NETWORK="))
                    {
                        // p is network
                        Network = p.Remove(0, 8);
                    }
                    else if (p.ToUpper().StartsWith("PREFIX="))
                    {
                        ChannelModes = new ChannelModes(p.Remove(0, 7));
                    }
                }
            }
            break;

            case "MODE":
                // :DaveTest MODE DaveTest :+iwx
                // :irc.aohell.org MODE #davux +nt
                if (!autoJoined)
                {
                    autoJoined = true;
                    if (OnConnected != null)
                    {
                        OnConnected.Invoke(this);
                    }
                    JoinAutoChannels();
                }

                if (m.Target == Nick)
                {
                    // TODO own modes
                    ParseOwnModes(m.ListString);
                }
                else if (m.TargetIsChannel)
                {
                    // setting chan modes
                    Channel ct = FindChannel(m.Target);

                    if (ct != null)
                    {
                        User u = ct.FindUser(m.PrefixNick);
                        if (u != null)
                        {
                            u.SetMask(m.Prefix);
                        }
                        else
                        {
                            u = new User(this, m.Prefix);
                        }
                        ct.SetModes(u, m.AfterCommandString.Substring(m.AfterCommandString.IndexOf(' ')).Trim());
                    }
                    else
                    {
                        Trace.WriteLine("IRC/Server/OnMessage: Invalid Channel to MODE: " + m);
                    }
                }
                break;

            case "KICK":
                // --> :[email protected] KICK #davux DavXn :DavX-
            {
                Channel c      = FindChannel(m.Target);
                User    kicker = c.FindUser(m.PrefixNick);
                User    kicked = c.FindUser(m.Arg2);

                if (kicker == null || kicked == null)
                {
                    Trace.WriteLine("IRC/Server/OnMessage/KICK: No kicker or kicked " + m);
                }
                else
                {
                    if (c != null)
                    {
                        c.KickUser(kicked, kicker, m.ListString);
                    }
                    else
                    {
                        Trace.WriteLine("IRC/Server/OnMessage/KICK No Channel: " + m);
                    }
                }
            }
            break;

            case "PART":
                if (m.PrefixNick.ToLower() == Nick.ToLower())
                {
                    // i left a channel!
                    Channel c = FindChannel(m.Target);
                    if (c != null)
                    {
                        //c.Part();
                        Channels.Remove(c);
                        if (OnParted != null)
                        {
                            OnParted.Invoke(this, c);
                        }
                    }
                    else
                    {
                        Trace.WriteLine("IRC/Server/OnMessage: Invalid Channel for PART: " + m);
                    }
                }
                else
                {
                    // someone left a channel that im on
                    Channel c = FindChannel(m.Target);
                    if (c != null)
                    {
                        User ud = c.FindUser(m.PrefixNick);
                        ud.SetMask(m.Prefix);
                        if (ud != null)
                        {
                            c.PartUser(ud, m.ListString, true);
                        }
                        else
                        {
                            Trace.WriteLine("IRC/Server/OnMessage: Invalid User for PART: " + m);
                        }
                    }
                    else
                    {
                        Trace.WriteLine("IRC/Server/OnMessage: Invalid Channel for PART: " + m);
                    }
                }
                break;

            case "353":
            {
                string[] parts = m.ListString.Split(' ');

                Channel c = FindChannel(m.Arg3);
                if (c != null)
                {
                    foreach (string p in parts)
                    {
                        if (!string.IsNullOrEmpty(p))
                        {
                            string px = p;

                            if ("" != ChannelModes.ModeForSymbol(px.Substring(0, 1)))
                            {
                                px = px.Remove(0, 1);
                            }
                            bool add = true;
                            foreach (User u in c.Users)
                            {
                                if (u.Nick.ToLower() == px)
                                {
                                    add = false;
                                    break;
                                }
                            }
                            if (add)
                            {
                                c.JoinUser(new User(this, p), false);
                            }
                        }
                    }
                }
                else
                {
                    Trace.WriteLine("IRC/Server/OnMessage/353: Invalid Channel: " + m);
                }
            }
            break;

            // --> :irc.aohell.org 332 DavXn #davux :This is the topic message
            // --> :irc.aohell.org 333 DavXn #davux DavX- 1260079684
            case "332":
            {
                Channel chan = FindChannel(m.Arg2);
                if (chan != null)
                {
                    chan.Topic = m.ListString;
                }
                else
                {
                    Trace.WriteLine("IRC/Server/OnMessage/332: Invalid channel: " + m);
                }
            }
            break;

            case "333":
            {
                Channel c = FindChannel(m.Arg2);
                if (c != null)
                {
                    User u = new User(this, m.Arg3);
                    c.TopicDetails(u, m.Arg4);
                }
                else
                {
                    Trace.WriteLine("IRC/Server/OnMessage/333: Invalid channel: " + m);
                }
            }
            break;

            case "366":
                // no action.
                break;

            case "433":
                // --> :irc.redwolfs.net 433 * DavXn :Nickname is already in use.
                if (OnStatusMessage != null)
                {
                    OnStatusMessage.Invoke(this, m);
                }
                Send("NICK " + Profile.GetNick(m.Arg2));
                break;

            case "NICK":
            {
                string oldNick = m.PrefixNick;
                string newNick = m.Target;

                if (oldNick.ToLower() == Nick.ToLower())
                {
                    Nick = newNick;
                }

                foreach (Channel c in Channels)
                {
                    c.NickChanged(oldNick, newNick);
                }


                /*
                 * if (OnNickChanged != null)
                 * {
                 *  OnNickChanged.Invoke(oldNick, newNick);
                 * }
                 */
            }
            break;

            case "001":
                if (m.Target != Nick)
                {
                    string oldNick = Nick;
                    Nick = m.Target;
                    if (OnNickChanged != null)
                    {
                        OnNickChanged.Invoke(oldNick, Nick);
                    }
                }
                if (OnStatusMessage != null)
                {
                    OnStatusMessage.Invoke(this, m);
                }
                break;

            case "QUIT":
                // :[email protected] QUIT :Quit: Clfloat
            {
                User u = new User(this, m.Prefix);

                if (OnUserQuit != null)
                {
                    OnUserQuit(this, u, m.ListString);
                }

                foreach (Channel c in Channels)
                {
                    c.UserQuit(u, m.ListString);
                }
            }
            break;

            default:
                if (OnStatusMessage != null)
                {
                    OnStatusMessage.Invoke(this, m);
                }
                break;
            }
        }