Mercurial > libervia-backend
diff sat/plugins/plugin_xep_0184.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | ab2696e34d29 |
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0184.py Wed Jun 27 07:51:29 2018 +0200 +++ b/sat/plugins/plugin_xep_0184.py Wed Jun 27 20:14:46 2018 +0200 @@ -22,27 +22,37 @@ from twisted.internet import reactor from twisted.words.protocols.jabber import xmlstream, jid from twisted.words.xish import domish + log = getLogger(__name__) from wokkel import disco, iwokkel from zope.interface import implements + try: from twisted.words.protocols.xmlstream import XMPPHandler except ImportError: from wokkel.subprotocols import XMPPHandler -NS_MESSAGE_DELIVERY_RECEIPTS = 'urn:xmpp:receipts' +NS_MESSAGE_DELIVERY_RECEIPTS = "urn:xmpp:receipts" -MSG = 'message' +MSG = "message" -MSG_CHAT = '/'+MSG+'[@type="chat"]' -MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST = MSG_CHAT+'/request[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]' -MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = MSG_CHAT+'/received[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]' +MSG_CHAT = "/" + MSG + '[@type="chat"]' +MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST = ( + MSG_CHAT + '/request[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]' +) +MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = ( + MSG_CHAT + '/received[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]' +) -MSG_NORMAL = '/'+MSG+'[@type="normal"]' -MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST = MSG_NORMAL+'/request[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]' -MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = MSG_NORMAL+'/received[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]' +MSG_NORMAL = "/" + MSG + '[@type="normal"]' +MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST = ( + MSG_NORMAL + '/request[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]' +) +MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = ( + MSG_NORMAL + '/received[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]' +) PARAM_KEY = "Privacy" @@ -51,25 +61,26 @@ PLUGIN_INFO = { -C.PI_NAME: "XEP-0184 Plugin", -C.PI_IMPORT_NAME: "XEP-0184", -C.PI_TYPE: "XEP", -C.PI_PROTOCOLS: ["XEP-0184"], -C.PI_DEPENDENCIES: [], -C.PI_MAIN: "XEP_0184", -C.PI_HANDLER: "yes", -C.PI_DESCRIPTION: _("""Implementation of Message Delivery Receipts""") + C.PI_NAME: "XEP-0184 Plugin", + C.PI_IMPORT_NAME: "XEP-0184", + C.PI_TYPE: "XEP", + C.PI_PROTOCOLS: ["XEP-0184"], + C.PI_DEPENDENCIES: [], + C.PI_MAIN: "XEP_0184", + C.PI_HANDLER: "yes", + C.PI_DESCRIPTION: _("""Implementation of Message Delivery Receipts"""), } STATUS_MESSAGE_DELIVERY_RECEIVED = "delivered" -TEMPO_DELETE_WAITING_ACK_S = 300 # 5 min +TEMPO_DELETE_WAITING_ACK_S = 300 # 5 min class XEP_0184(object): """ Implementation for XEP 0184. """ + params = """ <params> <individual> @@ -79,10 +90,10 @@ </individual> </params> """ % { - 'category_name': PARAM_KEY, - 'category_label': _(PARAM_KEY), - 'param_name': PARAM_NAME, - 'param_label': _('Enable message delivery receipts') + "category_name": PARAM_KEY, + "category_label": _(PARAM_KEY), + "param_name": PARAM_NAME, + "param_label": _("Enable message delivery receipts"), } def __init__(self, host): @@ -94,24 +105,39 @@ host.memory.updateParams(self.params) host.trigger.add("sendMessage", self.sendMessageTrigger) - host.bridge.addSignal("messageState", ".plugin", signature='sss') # message_uid, status, profile + host.bridge.addSignal( + "messageState", ".plugin", signature="sss" + ) # message_uid, status, profile def getHandler(self, client): return XEP_0184_handler(self, client.profile) - def sendMessageTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments): + def sendMessageTrigger( + self, client, mess_data, pre_xml_treatments, post_xml_treatments + ): """Install SendMessage command hook """ + def treatment(mess_data): - message = mess_data['xml'] + message = mess_data["xml"] message_type = message.getAttribute("type") - if self._isActif(client.profile) and (message_type == "chat" or message_type == "normal"): - message.addElement('request', NS_MESSAGE_DELIVERY_RECEIPTS) - uid = mess_data['uid'] + if self._isActif(client.profile) and ( + message_type == "chat" or message_type == "normal" + ): + message.addElement("request", NS_MESSAGE_DELIVERY_RECEIPTS) + uid = mess_data["uid"] msg_id = message.getAttribute("id") self._dictRequest[msg_id] = uid - reactor.callLater(TEMPO_DELETE_WAITING_ACK_S, self._clearDictRequest, msg_id) - log.debug(_("[XEP-0184] Request acknowledgment for message id {}".format(msg_id))) + reactor.callLater( + TEMPO_DELETE_WAITING_ACK_S, self._clearDictRequest, msg_id + ) + log.debug( + _( + "[XEP-0184] Request acknowledgment for message id {}".format( + msg_id + ) + ) + ) return mess_data @@ -122,13 +148,13 @@ """This method is called on message delivery receipts **request** (XEP-0184 #7) @param msg_elt: message element @param client: %(doc_client)s""" - from_jid = jid.JID(msg_elt['from']) + from_jid = jid.JID(msg_elt["from"]) if self._isActif(client.profile) and client.roster.isPresenceAuthorised(from_jid): - received_elt_ret = domish.Element((NS_MESSAGE_DELIVERY_RECEIPTS, 'received')) + received_elt_ret = domish.Element((NS_MESSAGE_DELIVERY_RECEIPTS, "received")) received_elt_ret["id"] = msg_elt["id"] - msg_result_elt = xmlstream.toResponse(msg_elt, 'result') + msg_result_elt = xmlstream.toResponse(msg_elt, "result") msg_result_elt.addChild(received_elt_ret) client.send(msg_result_elt) @@ -137,27 +163,38 @@ @param msg_elt: message element @param client: %(doc_client)s""" msg_elt.handled = True - rcv_elt = msg_elt.elements(NS_MESSAGE_DELIVERY_RECEIPTS, 'received').next() - msg_id = rcv_elt['id'] + rcv_elt = msg_elt.elements(NS_MESSAGE_DELIVERY_RECEIPTS, "received").next() + msg_id = rcv_elt["id"] try: uid = self._dictRequest[msg_id] del self._dictRequest[msg_id] - self.host.bridge.messageState(uid, STATUS_MESSAGE_DELIVERY_RECEIVED, client.profile) - log.debug(_("[XEP-0184] Receive acknowledgment for message id {}".format(msg_id))) + self.host.bridge.messageState( + uid, STATUS_MESSAGE_DELIVERY_RECEIVED, client.profile + ) + log.debug( + _("[XEP-0184] Receive acknowledgment for message id {}".format(msg_id)) + ) except KeyError: pass def _clearDictRequest(self, msg_id): try: del self._dictRequest[msg_id] - log.debug(_("[XEP-0184] Delete waiting acknowledgment for message id {}".format(msg_id))) + log.debug( + _( + "[XEP-0184] Delete waiting acknowledgment for message id {}".format( + msg_id + ) + ) + ) except KeyError: pass def _isActif(self, profile): return self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile) + class XEP_0184_handler(XMPPHandler): implements(iwokkel.IDisco) @@ -167,14 +204,30 @@ self.profile = profile def connectionInitialized(self): - self.xmlstream.addObserver(MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST, self.plugin_parent.onMessageDeliveryReceiptsRequest, client=self.parent) - self.xmlstream.addObserver(MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, self.plugin_parent.onMessageDeliveryReceiptsReceived, client=self.parent) + self.xmlstream.addObserver( + MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST, + self.plugin_parent.onMessageDeliveryReceiptsRequest, + client=self.parent, + ) + self.xmlstream.addObserver( + MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, + self.plugin_parent.onMessageDeliveryReceiptsReceived, + client=self.parent, + ) - self.xmlstream.addObserver(MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST, self.plugin_parent.onMessageDeliveryReceiptsRequest, client=self.parent) - self.xmlstream.addObserver(MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, self.plugin_parent.onMessageDeliveryReceiptsReceived, client=self.parent) + self.xmlstream.addObserver( + MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST, + self.plugin_parent.onMessageDeliveryReceiptsRequest, + client=self.parent, + ) + self.xmlstream.addObserver( + MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, + self.plugin_parent.onMessageDeliveryReceiptsReceived, + client=self.parent, + ) - def getDiscoInfo(self, requestor, target, nodeIdentifier=''): + def getDiscoInfo(self, requestor, target, nodeIdentifier=""): return [disco.DiscoFeature(NS_MESSAGE_DELIVERY_RECEIPTS)] - def getDiscoItems(self, requestor, target, nodeIdentifier=''): + def getDiscoItems(self, requestor, target, nodeIdentifier=""): return []