Ignore received message duplicates using sender jid, message id and message content. See #8258

This commit is contained in:
tmolitor 2016-02-08 23:08:26 +01:00
parent 0e80db2be6
commit c5c1e5d235
2 changed files with 19 additions and 0 deletions

View File

@ -2168,6 +2168,8 @@ ConnectionHandlersBase, ConnectionJingle, ConnectionIBBytestream):
gajim.nec.push_incoming_event(SignedInEvent(None, conn=self)) gajim.nec.push_incoming_event(SignedInEvent(None, conn=self))
self.send_awaiting_pep() self.send_awaiting_pep()
self.continue_connect_info = None self.continue_connect_info = None
# hashes of already received messages
self.received_message_hashes = []
def request_gmail_notifications(self): def request_gmail_notifications(self):
if not self.connection or self.connected < 2: if not self.connection or self.connected < 2:

View File

@ -1391,10 +1391,12 @@ class DecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
self.encrypted = self.msg_obj.encrypted self.encrypted = self.msg_obj.encrypted
self.forwarded = self.msg_obj.forwarded self.forwarded = self.msg_obj.forwarded
self.sent = self.msg_obj.sent self.sent = self.msg_obj.sent
self.conn = self.msg_obj.conn
self.popup = False self.popup = False
self.msg_id = None # id in log database self.msg_id = None # id in log database
self.attention = False # XEP-0224 self.attention = False # XEP-0224
self.correct_id = None # XEP-0308 self.correct_id = None # XEP-0308
self.msghash = None
self.receipt_request_tag = self.stanza.getTag('request', self.receipt_request_tag = self.stanza.getTag('request',
namespace=nbxmpp.NS_RECEIPTS) namespace=nbxmpp.NS_RECEIPTS)
@ -1445,6 +1447,21 @@ class DecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
if replace: if replace:
self.correct_id = replace.getAttr('id') self.correct_id = replace.getAttr('id')
# ignore message duplicates
if self.msgtxt and self.id_ and self.jid:
self.msghash = hashlib.sha256("%s|%s|%s" % (
hashlib.sha256(str(self.msgtxt)).hexdigest(),
hashlib.sha256(str(self.id_)).hexdigest(),
hashlib.sha256(str(self.jid)).hexdigest())).digest()
if self.msghash in self.conn.received_message_hashes:
log.info("Ignoring duplicated message from '%s' with id '%s'" % (str(self.jid), str(self.id_)))
return False
else:
log.debug("subhashes: msgtxt, id_, jid = ('%s', '%s', '%s')" % (hashlib.sha256(str(self.msgtxt)).hexdigest(), hashlib.sha256(str(self.id_)).hexdigest(), hashlib.sha256(str(self.jid)).hexdigest()))
self.conn.received_message_hashes.append(self.msghash)
# only record the last 20000 hashes (should be about 1MB [32 bytes per hash]
# and about 24 hours if you receive a message every 5 seconds)
self.conn.received_message_hashes = self.conn.received_message_hashes[-20000:]
return True return True
class ChatstateReceivedEvent(nec.NetworkIncomingEvent): class ChatstateReceivedEvent(nec.NetworkIncomingEvent):