Example #1
0
        public void handle(RegistrationInfoFactory infoFactory, IrcDotNet.IrcClient client, FehBot bot, MongoDB.Driver.IMongoDatabase db, IrcDotNet.IrcUser from, IrcDotNet.IrcChannel to, string message)
        {
            var nickName          = infoFactory.NickName;
            var messageCollection = db.GetCollection <BsonDocument> ("Message");

            if (isTellMessage(message, nickName))
            {
                Tuple <string, string> request = parseRequest(HandlerUtils.removeMention(message, nickName));
                if (!request.Item1.Equals(nickName))
                {
                    messageCollection.InsertOne(new BsonDocument {
                        { "network", infoFactory.Server },
                        { "channel", to.Name.ToLower() },
                        { "recipient", request.Item1 },
                        { "sender", from.NickName },
                        { "message", request.Item2 }
                    });
                    client.LocalUser.SendMessage(to, from.NickName + ": kk");
                    var messageObject = new JObject();

                    messageObject.Add("channel", to.Name.ToLower());
                    messageObject.Add("recipient", request.Item1);
                    messageObject.Add("sender", from.NickName);
                    messageObject.Add("message", request.Item2);
                    callWebHook(db, messageObject);
                }
            }
            else
            {
                var builder = Builders <BsonDocument> .Filter;
                var filter  = builder.Eq("recipient", from.NickName) & builder.Eq("channel", to.Name.ToLower()) & builder.Eq("network", infoFactory.Server);
                messageCollection.Find(filter).ForEachAsync((document) => {
                    var objectId    = document.GetValue("_id").AsObjectId;
                    var createdDate = objectId.CreationTime;
                    DateDiff diff   = new DateDiff(createdDate, DateTime.Now.ToUniversalTime());
                    client.LocalUser.SendMessage(to, String.Format("({0} ago) {1} => {2}, {3}", diff.GetDescription(6), document.GetValue("sender").AsString, document.GetValue("recipient").AsString, document.GetValue("message").AsString));
                    messageCollection.DeleteOne(document);
                });
            }
        }
Example #2
0
        public void handle(RegistrationInfoFactory infoFactory, IrcDotNet.IrcClient client, FehBot bot, MongoDB.Driver.IMongoDatabase db, IrcDotNet.IrcUser from, IrcDotNet.IrcChannel to, string message)
        {
            if (isFactoidMentionRequest(message))
            {
                Tuple <string, string> request = ParseFactionMention(message);
                var term   = request.Item1.Trim();
                var target = request.Item2.Trim();

                var factoid = db.GetCollection <BsonDocument>("factoid");
                var builder = Builders <BsonDocument> .Filter;
                var filter  = builder.Eq("term", term) & builder.Eq("channel", to.Name.ToLower()) & builder.Eq("network", infoFactory.Server);

                var document = factoid.Find(filter).FirstOrDefault();

                if (document == null)
                {
                    string template = "{0}, Huh?";
                    client.LocalUser.SendMessage(to, String.Format(template, from.NickName));
                }
                else
                {
                    string template = "{0}, {1}.";
                    client.LocalUser.SendMessage(to, String.Format(template, target, document.GetValue("meaning").AsString));
                }
            }
            else if (isFactoidGetRequest(message))
            {
                var term    = parseFactoidGet(message);
                var factoid = db.GetCollection <BsonDocument>("factoid");
                var builder = Builders <BsonDocument> .Filter;
                var filter  = builder.Eq("term", term) & builder.Eq("channel", to.Name.ToLower()) & builder.Eq("network", infoFactory.Server);

                var document = factoid.Find(filter).FirstOrDefault();

                if (document == null)
                {
                    string template = "{0}, Huh?";
                    client.LocalUser.SendMessage(to, String.Format(template, from.NickName));
                }
                else
                {
                    string template = "{0}, {1}";
                    client.LocalUser.SendMessage(to, String.Format(template, from.NickName, document.GetValue("meaning").AsString));
                }
            }
            else if (isFactoidStoreRequest(message, infoFactory.NickName))
            {
                message = HandlerUtils.removeMention(message, infoFactory.NickName);
                var request    = parseFactoidStoreRequest(message);
                var term       = request.Item1;
                var definition = request.Item2;

                var factoid = db.GetCollection <BsonDocument>("factoid");
                var builder = Builders <BsonDocument> .Filter;
                var filter  = builder.Eq("term", term) & builder.Eq("channel", to.Name.ToLower()) & builder.Eq("network", infoFactory.Server);

                var document = factoid.Find(filter).FirstOrDefault();
                if (document == null)
                {
                    factoid.InsertOne(new BsonDocument {
                        { "term", term },
                        { "channel", to.Name.ToLower() },
                        { "network", infoFactory.Server },
                        { "meaning", definition }
                    });
                }
                else
                {
                    var update = Builders <BsonDocument> .Update.Set("meaning", definition);

                    factoid.UpdateOne(filter, update);
                }
                string template = "{0}, Got it!";
                client.LocalUser.SendMessage(to, String.Format(template, from.NickName));
            }
            else if (isFactoidRemoveRequest(message, infoFactory.NickName))
            {
                message = HandlerUtils.removeMention(message, infoFactory.NickName);
                string term = parseFactoidRemoveRequest(message);

                var factoid = db.GetCollection <BsonDocument>("factoid");
                var builder = Builders <BsonDocument> .Filter;
                var filter  = builder.Eq("term", term) & builder.Eq("channel", to.Name.ToLower()) & builder.Eq("network", infoFactory.Server);

                var document = factoid.Find(filter).FirstOrDefault();
                if (document == null)
                {
                    string template = "{0}, Huh?";
                    client.LocalUser.SendMessage(to, String.Format(template, from.NickName));
                }
                else
                {
                    factoid.DeleteOne(filter);
                    string template = "{0}, It never happened.";
                    client.LocalUser.SendMessage(to, String.Format(template, from.NickName));
                }
            }
        }
Example #3
0
        private bool isFactoidRemoveRequest(string message, string nick)
        {
            string noMention = HandlerUtils.removeMention(message, nick);

            return(FactoidRemoveExpression.IsMatch(noMention) && HandlerUtils.addressedToMe(message, nick));
        }
Example #4
0
        private bool isTellMessage(string message, string nickName)
        {
            string normalizedMessage = HandlerUtils.removeMention(message, nickName);

            return(HandlerUtils.addressedToMe(message, nickName) && TellExpression.IsMatch(normalizedMessage));
        }