Пример #1
0
        internal ChatClient(Plugin plugin, ServerCfg configuration)
        {
            this.plugin        = plugin;
            this.configuration = configuration;
            StatsDatabase      = new StatsDatabaseManager(ConnectDatabase(configuration.Backend));

            var handlers = new List <ChatClientEventHandler>
            {
                // This handler binds ChatUsers to database users and should therefore run first
                new BindHandler(),
                // Logs client events to stdout and optionally the log file
                new LogHandler(),
                // Generates statistics
                new StatsHandler(),
                // Performs channel-administrative actions
                new AdministrationHandler(),
                // Processes commands
                new CommandHandler()
            };

            foreach (var handler in handlers)
            {
                handler.BindClient(this);
                handler.Initialise();
            }
            var eventManager = new ChatClientEventManager(handlers);

            AttachEventHandlers(eventManager);
        }
Пример #2
0
        public static void ProcessBatch(StatsDatabaseManager database, IEnumerable <ChatMessage> messages)
        {
            //var msgs = messages.ToList();
            //var grouped = msgs.GroupBy(m => m.Sender.DbUser.Id).ToList();

            //var userActions = grouped.ToDictionary(g => g.Key, g => g.Count(m => m.Action));
            //var userLines = grouped.ToDictionary(g => g.Key, g => g.Count(m => !m.Action));
            //var userWords = grouped.ToDictionary(g => g.Key, g => g.SelectMany(m => WordTools.GetWords(m.Body)));

            //var allWords = msgs.SelectMany(m => WordTools.GetWords(m.Body).Select(w => new UsedWord(m.Sender.DbUser.Id, w))).ToList();
            //var emoticons = allWords.Where(w => Emoticons.List.Contains(w.Word)).GroupBy(w => w.Word);


            // Increment actions/lines for the user
            //database.IncrementActions(userActions);
            ///database.IncrementLineCount(userLines);
            //database.IncrementWordCount(userWords.ToDictionary(p => p.Key, p => p.Value.Count()));

            // Increment global line/word count
            //database.IncrementVar("global_line_count", msgs.Count);
            //database.IncrementVar("global_word_count", allWords.Count);

            // increment emoticons
            //database.IncrementEmoticons(emoticons);
        }
Пример #3
0
 internal IrcClientWrapper(IrcClient client, StatsDatabaseManager database, string serverName)
 {
     ServerName = serverName;
     StatsDatabase = database;
     this.client = client;
 }
Пример #4
0
 public string GetAlias(StatsDatabaseManager db, string key)
 {
     return db.GetMiscData("alias", key);
 }
Пример #5
0
 public bool ContainsKey(StatsDatabaseManager db, string key)
 {
     return db.MiscDataContainsKey("alias", key);
 }
Пример #6
0
 private void GetEmoticons(StatsDatabaseManager statsDb, int userId, IEnumerable<string> words)
 {
     foreach (var word in words)
     {
         if (Emoticons.List.Contains(word))
         {
             statsDb.IncrementEmoticon(word, userId);
         }
     }
 }
Пример #7
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;
            }
        }