From 5f0afab060aa84a1cd1a81ade027f80b580a291d Mon Sep 17 00:00:00 2001 From: mathieui Date: Tue, 29 Apr 2014 22:14:03 +0200 Subject: Do not ask for receipts in messages without a body --- src/connection.py | 7 ++++++- src/core/handlers.py | 10 ++++++---- src/fixes.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/connection.py b/src/connection.py index 0af2b228..649cd0a2 100644 --- a/src/connection.py +++ b/src/connection.py @@ -15,10 +15,12 @@ log = logging.getLogger(__name__) import getpass import sleekxmpp +from sleekxmpp.plugins.xep_0184 import XEP_0184 -from config import config, options import common +import fixes from common import safeJID +from config import config, options class Connection(sleekxmpp.ClientXMPP): """ @@ -85,6 +87,9 @@ class Connection(sleekxmpp.ClientXMPP): self.register_plugin('xep_0085') self.register_plugin('xep_0115') + # monkey-patch xep_0184 to avoid requesting receipts for messages + # without a body + XEP_0184._filter_add_receipt_request = fixes._filter_add_receipt_request self.register_plugin('xep_0184') self.plugin['xep_0184'].auto_ack = config.get('ack_message_receipts', True) diff --git a/src/core/handlers.py b/src/core/handlers.py index 12e07201..ad696eb1 100644 --- a/src/core/handlers.py +++ b/src/core/handlers.py @@ -12,6 +12,7 @@ from hashlib import sha1 from gettext import gettext as _ from sleekxmpp import InvalidJID +from sleekxmpp.stanza import Message from sleekxmpp.xmlstream.stanzabase import StanzaBase import bookmark @@ -584,7 +585,7 @@ def on_chatstate_normal_conversation(self, message, state): tab.refresh_info_header() self.doupdate() else: - composing_tab_state(tab, state) + _composing_tab_state(tab, state) self.refresh_tab_win() return True @@ -602,7 +603,7 @@ def on_chatstate_private_conversation(self, message, state): tab.refresh_info_header() self.doupdate() else: - composing_tab_state(tab, state) + _composing_tab_state(tab, state) self.refresh_tab_win() return True @@ -622,7 +623,7 @@ def on_chatstate_groupchat_conversation(self, message, state): tab.input.refresh() self.doupdate() else: - composing_tab_state(tab, state) + _composing_tab_state(tab, state) self.refresh_tab_win() ### subscription-related handlers ### @@ -1098,7 +1099,7 @@ def validate_ssl(self, pem): self.information(_('Unable to write in the config file'), 'Error') -def composing_tab_state(tab, state): +def _composing_tab_state(tab, state): """ Set a tab state to or from the "composing" state according to the config and the current tab state @@ -1124,3 +1125,4 @@ def composing_tab_state(tab, state): elif tab.state == 'composing' and state != 'composing': tab.restore_state() + diff --git a/src/fixes.py b/src/fixes.py index b7c8d03b..75ac6343 100644 --- a/src/fixes.py +++ b/src/fixes.py @@ -6,7 +6,9 @@ TODO: Check that they are fixed and remove those hacks """ +from sleekxmpp.stanza import Message from sleekxmpp.xmlstream import ET + import logging log = logging.getLogger(__name__) @@ -51,4 +53,46 @@ def get_room_form(xmpp, room): return form +def _filter_add_receipt_request(self, stanza): + """ + Auto add receipt requests to outgoing messages, if: + + - ``self.auto_request`` is set to ``True`` + - The message is not for groupchat + - The message does not contain a receipt acknowledgment + - The recipient is a bare JID or, if a full JID, one + that has the ``urn:xmpp:receipts`` feature enabled + - The message has a body + + The disco cache is checked if a full JID is specified in + the outgoing message, which may mean a round-trip disco#info + delay for the first message sent to the JID if entity caps + are not used. + """ + + if not self.auto_request: + return stanza + + if not isinstance(stanza, Message): + return stanza + + if stanza['request_receipt']: + return stanza + + if not stanza['type'] in self.ack_types: + return stanza + + if stanza['receipt']: + return stanza + + if not stanza['body']: + return stanza + + if stanza['to'].resource: + if not self.xmpp['xep_0030'].supports(stanza['to'], + feature='urn:xmpp:receipts', + cached=True): + return stanza + stanza['request_receipt'] = True + return stanza -- cgit v1.2.3