示例#1
0
        static void Main(string[] args)
        {
            Irc irc = new Irc
            {
                ThrowExceptions = false
            };

            irc.MessageReceived += Irc_MessageReceived;
            irc.MessageSent     += Irc_MessageSent;
            irc.StateChanged    += Irc_StateChanged;
            irc.Connect("irc.freenode.net", 6667);

            Console.ReadLine();
        }
示例#2
0
文件: IrcBot.cs 项目: SPDevDE/IrcBot
        private void GetData()
        {
            #region Lade Konfiguration wenn vorhanden. Ansonsten nutze Standartwerte
            if (!string.IsNullOrWhiteSpace(ConfigFile))
            {
                try
                {
                    clientConfig  = ClientConfiguration.LoadConfig(ConfigFile);
                    IrcConnection = new Irc(clientConfig);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Fehler beim Laden der Konfiguration: " + ex.Message);
                    Console.ReadKey();
                    return; // return beendet die Methode und damit das Programm.
                }
            }


            #endregion

            // Verbindung zum IRC-Server aufbauen
            IrcConnection.Connect();

            while (IrcConnection.Connected)
            {
                string Data = IrcConnection.readData();

                if (!string.IsNullOrWhiteSpace(Data))
                {
                    Console.WriteLine("> " + Data);

                    #region PING
                    if (Data.Substring(0, 6) == "PING :")
                    {
                        string[] tPing = Data.Split(Convert.ToChar(":"));
                        IrcConnection.sendData("PONG :" + tPing[1]);
                    }
                    #endregion

                    #region Server Messages
                    if (Regex.IsMatch(Data, @":(.+) ([\d]{3}) (.+) :(.+)"))
                    {
                        string[] tRegEx = Regex.Split(Data, @":(.+) ([\d]{3}) (.+) (:)?(.+)");
                        // tRegEx[0] = null
                        // tRegEx[1] = Server-Hostname
                        // tRegEx[2] = Numeric Response
                        // tRegEx[3] = Client Username
                        // tRegEx[4] = Server Message
                        // tRegEx[5] = null


                        switch (int.Parse(tRegEx[2]))
                        {
                        case (int)NumericResponses.RPL_WELCOME:
                            IrcConnection.ServerAdress = tRegEx[1];
                            break;

                        case (int)NumericResponses.RPL_ENDOFMOTD:
                            AutoJoinChannels(clientConfig.Channels);
                            break;

                        case (int)NumericResponses.ERR_NICKNAMEINUSE:     // Wenn Nickname bereits verwendet, eine zufällige dreistellige Ziffer hintendranhängen.
                            IrcConnection.ChangeNickname(clientConfig.ServerDetails.Nick + new Random().Next(100, 1000));
                            break;

                        case (int)NumericResponses.ERR_YOUWILLBEBANNED:
                            Console.WriteLine("IMPORTANT: Server is going to deny your access.");
                            break;

                            // ToDo: Weitere wichtige NumericReplies einbinden und verarbeiten.
                        }
                    }
                    #endregion

                    #region PRIVMSG
                    if (Regex.IsMatch(Data, @":([^@!\ ]*)(.)*PRIVMSG\ ([^@!\ ]*)\ :"))
                    {
                        // :|54H|DamianRyse!~|54H|[email protected] PRIVMSG #DamiansTestRoom :test
                        IrcMessage Message = new IrcMessage(Data);
                        Actions(Message);
                    }
                    #endregion
                }
            }
            Console.WriteLine("Reconnect in 5 Minuten...");
            System.Threading.Thread.Sleep(60000);
            Console.WriteLine("Reconnect in 4 Minuten...");
            System.Threading.Thread.Sleep(60000);
            Console.WriteLine("Reconnect in 3 Minuten...");
            System.Threading.Thread.Sleep(60000);
            Console.WriteLine("Reconnect in 2 Minuten...");
            System.Threading.Thread.Sleep(60000);
            Console.WriteLine("Reconnect in 1 Minute...");
            System.Threading.Thread.Sleep(60000);
            GetData();
        }