Example #1
0
        private bool IsDuplicateInput(PublicMessageEventArgs e)
        {
            // clean up the input, lowercase it and replace all consecutive non-readable characters to a single _
            string str = Regex.Replace($"{e.Message?.Text}{e.Message?.Caption}".ToLowerInvariant(), "[^a-zA-Z0-9]*", "_");

            if (str.Length < 256)
            {
                return(false);
            }
            var hash           = HashUtils.CalculateSHA1Hash(str);
            var hashCollection = DB.GetCollection <MessageHash>();

            hashCollection.Delete(x => (DateTime.UtcNow - x.UtcWhen) > TimeSpan.FromDays(3)); // keep it clean
            var hashRecord = hashCollection.Find(x => x.Hash == hash).FirstOrDefault();

            if (hashRecord != null)
            {
                InsultUserForCopyPasting(e, hashRecord);
                return(true);
            }
            else
            {
                hashRecord = new MessageHash
                {
                    Hash      = hash,
                    User      = e.Message.From,
                    UtcWhen   = DateTime.UtcNow,
                    MessageID = e.Message.MessageID,
                    ChatID    = e.Message.Chat.ID
                };
                hashCollection.Insert(hashRecord);
                _log.Info($"{e.Message.From.ShortName()} just sent a long message with hash {hash}, which I promptly stored.");
                return(false);
            }
        }
Example #2
0
        private void InsultUserForCopyPasting(PublicMessageEventArgs e, MessageHash hashRecord)
        {
            string[] allReplies = System.IO.File.ReadAllLines("taunts.duplicate.txt").Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
            string   replyFmt   = allReplies[_rnd.Next(allReplies.Length)];
            string   replyStr   = String.Format(replyFmt,
                                                MessageUtils.HtmlEscape(hashRecord.User.ShortName()),                 // {0}
                                                TimeService.AsReadableTimespan(DateTime.UtcNow - hashRecord.UtcWhen), // {1}
                                                MessageUtils.HtmlEscape(e.Message.From.ShortName()),                  // {2}
                                                hashRecord.MessageID,                                                 // {3}
                                                hashRecord.ChatID                                                     // {4}
                                                );

            _log.Info($"{e.Message.From.ShortName()} just duplicated {hashRecord.User.ShortName()}'s message and got abused with reply {replyStr}");
            Client.SendMessageToChat(e.Message.Chat.ID, replyStr, "HTML", true, false, e.Message.MessageID);
        }
Example #3
0
 private Message(
     ImmutableList<string> topics,
     string type,
     ImmutableList<Predecessor> predecessors,
     Guid objectId,
     ExpandoObject body,
     MessageHash hash)
 {
     _topics = topics;
     _type = type;
     _predecessors = predecessors;
     _objectId = objectId;
     _body = body;
     _hash = hash;
 }
Example #4
0
 public Subscription(
     Uri feedUrl,
     Uri imageUri,
     string title,
     string subtitle,
     string author,
     MessageHash hash)
 {
     FeedUrl = feedUrl;
     ImageUri = imageUri;
     Title = title;
     Subtitle = subtitle;
     Author = author;
     Hash = hash;
 }
Example #5
0
 private SearchResult(
     Uri feedUrl,
     string title,
     string subtitle,
     string author,
     Uri imageUri,
     MessageHash hash)
 {
     _feedUrl = feedUrl;
     _title = title;
     _subtitle = subtitle;
     _author = author;
     _imageUri = imageUri;
     _hash = hash;
 }
Example #6
0
 private static Message CreateSearchResultMessage(
     MessageHash searchHash,
     Guid searchTermId,
     SearchResult searchResult)
 {
     var searchResultMessage = Message.CreateMessage(
         searchTermId.ToCanonicalString(),
         "SearchResult",
         Predecessors.Set
             .In("Search", searchHash),
         searchTermId,
         new
         {
             ProviderId = "digitalpodcast.com",
             FeedUrl = searchResult.FeedUrl,
             Title = searchResult.Title,
             Subtitle = searchResult.Subtitle,
             Author = searchResult.Author,
             ImageUri = searchResult.ImageUri
         });
     return searchResultMessage;
 }
Example #7
0
 public Predecessor(string role, MessageHash hash)
 {
     _role = role;
     _hash = hash;
 }
Example #8
0
 public Predecessors In(string role, MessageHash hash)
 {
     _predecessors.Add(new Predecessor(role, hash));
     return this;
 }
Example #9
0
 public bool IncludesSearchResult(MessageHash hash)
 {
     return _hashes.Contains(hash);
 }
Example #10
0
        public static Message CreateMessage(
            TopicSet topicSet,
            string messageType,
            Predecessors predecessors,
            Guid objectId,
            object body)
        {
            // Convert the anonymous typed object to an ExpandoObject.
            var expandoBody = JsonConvert.DeserializeObject<ExpandoObject>(
                JsonConvert.SerializeObject(body));
            var predecessorList = predecessors.ToImmutableList();
            object document = new
            {
                MessageType = messageType,
                Predecessors = predecessorList
                    .Select(p => new
                    {
                        Role = p.Role,
                        Hash = p.Hash.ToString()
                    })
                    .ToArray(),
                ObjectId = objectId,
                Body = expandoBody
            };
            var messageHash = new MessageHash(ComputeHash(document));

            return new Message(
                topicSet.ToImmutableList(),
                messageType,
                predecessorList,
                objectId,
                expandoBody,
                messageHash);
        }