Exemplo n.º 1
0
        public Program()
        {
            client = new IrcClient();
            client.OnConnectionEstablished += () =>
            {
                Console.WriteLine("Connection Established");
            };
            client.OnRawLineReceived += (line) =>
            {
                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.WriteLine("RAW: " + line);
                Console.ResetColor();
            };
            client.OnDebugLog += (message) =>
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("DEB: " + message);
                Console.ResetColor();
            };
            /*test.OnFormattedLineReceived += (line) =>
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("RAW: " + line.ToString());
                Console.ResetColor();
            };*/
            client.OnNickChanged += OnNickChanged;

            client.OnMessageReceived += (message) =>
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("{0} ({3}, {4}) in {1}: {2}", message.Sender.Nick, message.Channel, message.Message, message.Sender.Ident, message.Sender.Hostmask);
                Console.ResetColor();
                if (message.Message.StartsWith("-")) {
                    ProcessCommand(message);
                }
            };

            string channel = "#baggy";
            client.Connect("irc.esper.net", 6667, "Baggybot_test");

            client.JoinChannel(channel);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 16; i++) {
                sb.Append(GetColor(i % 99));
                sb.Append("=");
            }
            client.SendMessage(channel, GetColor(12) + sb.ToString());

            sb = new StringBuilder();
            sb.Append("test ");
            sb.Append(GetItalic());
            sb.Append("test ");
            sb.Append(GetUnderlined());
            sb.Append("test ");
            sb.Append(GetBold());
            sb.Append("test ");

            client.SendMessage(channel, GetColor(12) + sb.ToString());
        }
Exemplo n.º 2
0
 internal IrcClientWrapper(IrcClient client, StatsDatabaseManager database, string serverName)
 {
     ServerName = serverName;
     StatsDatabase = database;
     this.client = client;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Connects the bot to the IRC server
        /// </summary>
        public bool ConnectIrc(ServerCfg server)
        {
            var client = new IrcClient();
            var statsDatabaseManager = new StatsDatabaseManager(ConnectDatabase(server.Backend), server.UseNickserv);
            var wrapper = new IrcClientWrapper(client, statsDatabaseManager, server.ServerName);
            HookupIrcEvents(client, wrapper, server.ServerName);

            var host = server.Host;
            var port = server.Port;
            var nick = server.Identity.Nick;
            var ident = server.Identity.Ident;
            var realname = server.Identity.RealName;
            var password = server.Password;
            var tls = server.UseTls;
            var verify = server.VerifyCertificate;
            var slackCompatMode = server.CompatModes.Contains("slack");

            // Slack IRC messes up domain names by prepending http://<domain> to everything that looks like a domain name or incorrectly formatted URL,
            // despite the fact that such formatting should really be done by IRC clients, not servers.
            // BaggyBot doesn't like this; it messes up several of his commands, such as -resolve and -ping.
            // Therefore, when Slack Compatibility Mode is enabled, we add a SlackIrcProcessor which strips these misformatted domain names from the message.
            if (slackCompatMode)
            {
                client.DataProcessors.Add(new SlackIrcProcessor());
            }

            Logger.Log(this, "Connecting to the IRC server..");
            Logger.Log(this, "\tNick\t" + nick);
            Logger.Log(this, "\tIdent\t" + ident);
            Logger.Log(this, "\tName\t" + realname);
            Logger.Log(this, "\tHost\t" + host);
            Logger.Log(this, "\tPort\t" + port);
            try
            {
                client.Connect(new ConnectionInfo
                {
                    Host = host,
                    Port = port,
                    Nick = nick,
                    Ident = ident,
                    Password = password,
                    RealName = realname,
                    useTLS = tls,
                    verifyServerCertificate = verify
                });

                try
                {
                    clients.Add(server.ServerName, wrapper);
                }
                catch (ArgumentException)
                {
                    Logger.Log(this, "Failed to add the IRC server: a server with the same name already exists.", LogLevel.Error);
                    client.Disconnect();
                    return false;
                }
                return true;
            }
            catch (SocketException e)
            {
                Logger.Log(this, "Failed to connect to the IRC server: " + e.Message, LogLevel.Error);
                return false;
            }
            catch (ArgumentException e)
            {
                Logger.Log(this, $"Failed to connect to the IRC server: The settings file does not contain a value for \"{e.ParamName}\"", LogLevel.Error);
                return false;
            }
            catch (Exception e)
            {
                Logger.Log(this, "Failed to connect to the IRC server: " + e.Message, LogLevel.Error);
                return false;
            }
        }
Exemplo n.º 4
0
 private void HookupIrcEvents(IrcClient client, IrcClientWrapper wrapper, string serverName)
 {
     client.OnNickChange += (sender, newNick) => ircEventHandler.HandleNickChange(new IrcUser(wrapper, sender), newNick);
     client.OnMessageReceived += message => Task.Run(() =>
     {
         try
         {
             ircEventHandler.ProcessMessage(new IrcMessage(wrapper, message));
         }
         catch (Exception e)
         {
             Logger.LogException(this, e, "processing a message");
             if (Debugger.IsAttached)
             {
                 Debugger.Break();
             }
         }
     });
     client.OnFormattedLineReceived += ircEventHandler.ProcessFormattedLine;
     client.OnNoticeReceived += (sender, notice) => ircEventHandler.ProcessNotice(new IrcUser(wrapper, sender), notice);
     client.OnDisconnect += (reason, exception) => HandleDisconnect(client, serverName, reason, exception);
     client.OnKicked += (channel, reason, kicker) => Logger.Log(this, $"I was kicked from {channel} by {kicker.Nick} ({reason})", LogLevel.Warning);
     client.OnDebugLog += (sender, message) => Logger.Log(sender, "[IC#]" + message, LogLevel.Info);
     client.OnNetLibDebugLog += (sender, message) => Logger.Log(sender, "[NL#]" + message, LogLevel.Info);
     client.OnJoinChannel += (user, channel) => ircEventHandler.HandleJoin(new IrcUser(wrapper, user), channel);
     client.OnPartChannel += (user, channel) => ircEventHandler.HandlePart(new IrcUser(wrapper, user), channel);
     client.OnKick += (kickee, channel, reason, kicker) => ircEventHandler.HandleKick(kickee, channel, reason, new IrcUser(wrapper, kicker));
     client.OnQuit += (user, channel) => ircEventHandler.HandleQuit(new IrcUser(wrapper, user), channel);
 }
Exemplo n.º 5
0
        private void HandleDisconnect(IrcClient client, string serverName, DisconnectReason reason, Exception ex)
        {
            if (reason == DisconnectReason.DisconnectOnRequest)
            {
                Logger.Log(this, $"Disconnected from an IRC server ({serverName}).", LogLevel.Info);
            }
            else
            {
                var lostChannels = client.Channels;

                if (reason == DisconnectReason.Other)
                {
                    if (ex == null)
                    {
                        Logger.Log(client, $"Connection to {serverName} lost ({ex.GetType()}: {ex.Message}) Attempting to reconnect...", LogLevel.Error);
                    }
                    else
                    {
                        // If the client quits, we shouldn't try to reconnect.
                        Logger.Log(client, $"Connection to {serverName} lost (Client Quit).", LogLevel.Error);
                        return;
                    }
                }
                else
                {
                    Logger.Log(client, $"Connection to {serverName} lost ({reason}) Attempting to reconnect...", LogLevel.Warning);
                }
                bool success;

                do
                {
                    success = ConnectIrc(ConfigManager.Config.Servers.First(server => server.ServerName == serverName));
                    if (success) continue;
                    Logger.Log(this, "Reconnection attempt failed. Retrying in 5 seconds.", LogLevel.Warning);
                    Thread.Sleep(5000);
                } while (!success);

                Logger.Log(this, $"Successfully reconnected to {serverName}!", LogLevel.Warning);

                foreach (var channel in lostChannels)
                {
                    if (!client.JoinChannel(channel.Name, true))
                    {
                        Logger.Log(this, $"Failed to rejoin {channel} on {serverName}! Skipping this channel.", LogLevel.Error);
                    }
                }
            }
        }
Exemplo n.º 6
0
 public CommandProcessor(IrcClient client)
 {
     this.client = client;
 }