コード例 #1
0
 // Main method, initiates all events and makes the connection
 public static void Main()
 {
     Button.OnInterrupt += new NativeEventHandler(Button_OnInterrupt);
     Client = new IRC_Client(new IntegratedSocket("irc.netmftoolbox.com", 6667), "Guest");
     Client.OnAuthenticated += new IRC_Client.OnStringReceived(Client_OnAuthenticated);
     Client.OnMessage += new IRC_Client.OnStringReceived(Client_OnMessage);
     Client.Connect();
 }
コード例 #2
0
        /// <summary>Triggered for every line of data received by the server</summary>
        /// <param name="Data">The received line of data</param>
        private void _DataReceived(string Data)
        {
            // Triggers the OnRawReceived event for all data
            if (this.OnRawReceived != null)
            {
                this.OnRawReceived("", "", Data);
            }

            // Splits on spaces to detect the command
            string[] DataSplit = SplitRawData(Data);

            // Default ping/pong, just send back the full response
            if (DataSplit[0] == "PING")
            {
                this.SendRaw("PONG " + Data.Substring(5));
                return;
            }

            // There are two commands, commands directly from the server,
            // and commands from another party
            if (DataSplit[0].Substring(0, 1) == ":")
            {
                // We got a command from another party
                DataSplit[0] = DataSplit[0].Substring(1);
            }
            else
            {
                // We got data from the server, lets shift some stuff
                string[] NewSplit = new string[DataSplit.Length + 1];
                DataSplit.CopyTo(NewSplit, 1);
                DataSplit = NewSplit;
            }

            // Now we can safely assume the 1st value is empty or the sender,
            // and the 2nd one is the command.
            switch (DataSplit[1])
            {
            case "001":     // Welcome message, confirms the current nickname
                this._ServerName    = DataSplit[0];
                this._Nickname      = DataSplit[2];
                this._Authenticated = true;
                if (this.OnAuthenticated != null)
                {
                    this.OnAuthenticated(DataSplit[0], DataSplit[2], DataSplit[3]);
                }
                break;

            case "433":     // Nickname already in use
                int Counter = 0;
                if (DataSplit[3] == this._Nickname)
                {
                    Counter = 1;
                }
                else
                {
                    Counter = int.Parse(DataSplit[3].Substring(this._Nickname.Length)) + 1;
                }
                this.SendRaw("NICK " + this._Nickname + Counter.ToString());
                break;

            case "NICK":     // Someone changes his name
                // Is it my own name?
                if (this._Nickname == SplitName(DataSplit[0])[0])
                {
                    this._Nickname = DataSplit[2];
                }
                // Do we need to send back an event?
                if (this.OnNick != null)
                {
                    this.OnNick(DataSplit[0], DataSplit[2], "");
                }
                break;

            case "ACTION":
                if (this.OnAction != null)
                {
                    this.OnAction(DataSplit[0], DataSplit[2], DataSplit[3]);
                }
                break;

            case "NOTICE":
                if (DataSplit[3].Substring(0, 1) == "\x01" && DataSplit[3].Substring(DataSplit[3].Length - 1, 1) == "\x01")
                {
                    // CTCP Reply
                    DataSplit[3] = DataSplit[3].Trim(new char[] { (char)1 });
                    if (this.OnCtcpReply != null)
                    {
                        this.OnCtcpReply(DataSplit[0], DataSplit[2], DataSplit[3]);
                    }
                }
                else
                {
                    // Notice
                    if (this.OnNotice != null)
                    {
                        this.OnNotice(DataSplit[0], DataSplit[2], DataSplit[3]);
                    }
                }
                break;

            case "PRIVMSG":
                if (DataSplit[3].Substring(0, 1) == "\x01" && DataSplit[3].Substring(DataSplit[3].Length - 1, 1) == "\x01")
                {
                    // CTCP Request
                    DataSplit[3] = DataSplit[3].Trim(new char[] { (char)1 }).ToUpper();
                    if (this.OnCtcpRequest == null)
                    {
                        // No CTCP Request event programmed, we're going to do it ourselves
                        if (DataSplit[3] == "VERSION")
                        {
                            this.CtcpResponse(SplitName(DataSplit[0])[0], "VERSION " + this.ClientVersion);
                        }
                        if (DataSplit[3] == "TIME")
                        {
                            this.CtcpResponse(SplitName(DataSplit[0])[0], "TIME " + IRC_Client.Time);
                        }
                        if (DataSplit[3].Substring(0, 4) == "PING")
                        {
                            this.CtcpResponse(SplitName(DataSplit[0])[0], "PING" + DataSplit[3].Substring(4));
                        }
                    }
                    else
                    {
                        this.OnCtcpRequest(DataSplit[0], DataSplit[2], DataSplit[3]);
                    }
                }
                else
                {
                    // Message
                    if (this.OnMessage != null)
                    {
                        this.OnMessage(DataSplit[0], DataSplit[2], DataSplit[3]);
                    }
                }
                break;

            case "JOIN":     // User joins a channel
                if (this.OnJoin != null)
                {
                    this.OnJoin(DataSplit[0], DataSplit[2], "");
                }
                break;

            case "PART":     // User parts a channel
                if (this.OnPart != null)
                {
                    this.OnPart(DataSplit[0], DataSplit[2], DataSplit.Length > 3 ? DataSplit[3] : "");
                }
                break;

            case "KICK":     // User has been kicked from a channel
                if (this.OnKick != null)
                {
                    this.OnKick(DataSplit[0], DataSplit[3], DataSplit[2] + " " + DataSplit[4]);
                }
                break;

            case "QUIT":     // User leaves the server
                if (this.OnQuit != null)
                {
                    this.OnQuit(DataSplit[0], DataSplit[2], DataSplit[3]);
                }
                // Its us! Lets close the rest
                if (this._Username == IRC_Client.SplitName(DataSplit[0])[0])
                {
                    this.Disconnect();
                }
                break;
            }
        }