comparison 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
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
20 from sat.core.constants import Const as C 20 from sat.core.constants import Const as C
21 from sat.core.log import getLogger 21 from sat.core.log import getLogger
22 from twisted.internet import reactor 22 from twisted.internet import reactor
23 from twisted.words.protocols.jabber import xmlstream, jid 23 from twisted.words.protocols.jabber import xmlstream, jid
24 from twisted.words.xish import domish 24 from twisted.words.xish import domish
25
25 log = getLogger(__name__) 26 log = getLogger(__name__)
26 27
27 from wokkel import disco, iwokkel 28 from wokkel import disco, iwokkel
28 from zope.interface import implements 29 from zope.interface import implements
30
29 try: 31 try:
30 from twisted.words.protocols.xmlstream import XMPPHandler 32 from twisted.words.protocols.xmlstream import XMPPHandler
31 except ImportError: 33 except ImportError:
32 from wokkel.subprotocols import XMPPHandler 34 from wokkel.subprotocols import XMPPHandler
33 35
34 36
35 NS_MESSAGE_DELIVERY_RECEIPTS = 'urn:xmpp:receipts' 37 NS_MESSAGE_DELIVERY_RECEIPTS = "urn:xmpp:receipts"
36 38
37 MSG = 'message' 39 MSG = "message"
38 40
39 MSG_CHAT = '/'+MSG+'[@type="chat"]' 41 MSG_CHAT = "/" + MSG + '[@type="chat"]'
40 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST = MSG_CHAT+'/request[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]' 42 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST = (
41 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = MSG_CHAT+'/received[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]' 43 MSG_CHAT + '/request[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]'
42 44 )
43 MSG_NORMAL = '/'+MSG+'[@type="normal"]' 45 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = (
44 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST = MSG_NORMAL+'/request[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]' 46 MSG_CHAT + '/received[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]'
45 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = MSG_NORMAL+'/received[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]' 47 )
48
49 MSG_NORMAL = "/" + MSG + '[@type="normal"]'
50 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST = (
51 MSG_NORMAL + '/request[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]'
52 )
53 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = (
54 MSG_NORMAL + '/received[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]'
55 )
46 56
47 57
48 PARAM_KEY = "Privacy" 58 PARAM_KEY = "Privacy"
49 PARAM_NAME = "Enable message delivery receipts" 59 PARAM_NAME = "Enable message delivery receipts"
50 ENTITY_KEY = PARAM_KEY + "_" + PARAM_NAME 60 ENTITY_KEY = PARAM_KEY + "_" + PARAM_NAME
51 61
52 62
53 PLUGIN_INFO = { 63 PLUGIN_INFO = {
54 C.PI_NAME: "XEP-0184 Plugin", 64 C.PI_NAME: "XEP-0184 Plugin",
55 C.PI_IMPORT_NAME: "XEP-0184", 65 C.PI_IMPORT_NAME: "XEP-0184",
56 C.PI_TYPE: "XEP", 66 C.PI_TYPE: "XEP",
57 C.PI_PROTOCOLS: ["XEP-0184"], 67 C.PI_PROTOCOLS: ["XEP-0184"],
58 C.PI_DEPENDENCIES: [], 68 C.PI_DEPENDENCIES: [],
59 C.PI_MAIN: "XEP_0184", 69 C.PI_MAIN: "XEP_0184",
60 C.PI_HANDLER: "yes", 70 C.PI_HANDLER: "yes",
61 C.PI_DESCRIPTION: _("""Implementation of Message Delivery Receipts""") 71 C.PI_DESCRIPTION: _("""Implementation of Message Delivery Receipts"""),
62 } 72 }
63 73
64 74
65 STATUS_MESSAGE_DELIVERY_RECEIVED = "delivered" 75 STATUS_MESSAGE_DELIVERY_RECEIVED = "delivered"
66 TEMPO_DELETE_WAITING_ACK_S = 300 # 5 min 76 TEMPO_DELETE_WAITING_ACK_S = 300 # 5 min
67 77
68 78
69 class XEP_0184(object): 79 class XEP_0184(object):
70 """ 80 """
71 Implementation for XEP 0184. 81 Implementation for XEP 0184.
72 """ 82 """
83
73 params = """ 84 params = """
74 <params> 85 <params>
75 <individual> 86 <individual>
76 <category name="%(category_name)s" label="%(category_label)s"> 87 <category name="%(category_name)s" label="%(category_label)s">
77 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" security="0"/> 88 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" security="0"/>
78 </category> 89 </category>
79 </individual> 90 </individual>
80 </params> 91 </params>
81 """ % { 92 """ % {
82 'category_name': PARAM_KEY, 93 "category_name": PARAM_KEY,
83 'category_label': _(PARAM_KEY), 94 "category_label": _(PARAM_KEY),
84 'param_name': PARAM_NAME, 95 "param_name": PARAM_NAME,
85 'param_label': _('Enable message delivery receipts') 96 "param_label": _("Enable message delivery receipts"),
86 } 97 }
87 98
88 def __init__(self, host): 99 def __init__(self, host):
89 log.info(_("Plugin XEP_0184 (message delivery receipts) initialization")) 100 log.info(_("Plugin XEP_0184 (message delivery receipts) initialization"))
90 self.host = host 101 self.host = host
92 103
93 # parameter value is retrieved before each use 104 # parameter value is retrieved before each use
94 host.memory.updateParams(self.params) 105 host.memory.updateParams(self.params)
95 106
96 host.trigger.add("sendMessage", self.sendMessageTrigger) 107 host.trigger.add("sendMessage", self.sendMessageTrigger)
97 host.bridge.addSignal("messageState", ".plugin", signature='sss') # message_uid, status, profile 108 host.bridge.addSignal(
109 "messageState", ".plugin", signature="sss"
110 ) # message_uid, status, profile
98 111
99 def getHandler(self, client): 112 def getHandler(self, client):
100 return XEP_0184_handler(self, client.profile) 113 return XEP_0184_handler(self, client.profile)
101 114
102 def sendMessageTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments): 115 def sendMessageTrigger(
116 self, client, mess_data, pre_xml_treatments, post_xml_treatments
117 ):
103 """Install SendMessage command hook """ 118 """Install SendMessage command hook """
119
104 def treatment(mess_data): 120 def treatment(mess_data):
105 message = mess_data['xml'] 121 message = mess_data["xml"]
106 message_type = message.getAttribute("type") 122 message_type = message.getAttribute("type")
107 123
108 if self._isActif(client.profile) and (message_type == "chat" or message_type == "normal"): 124 if self._isActif(client.profile) and (
109 message.addElement('request', NS_MESSAGE_DELIVERY_RECEIPTS) 125 message_type == "chat" or message_type == "normal"
110 uid = mess_data['uid'] 126 ):
127 message.addElement("request", NS_MESSAGE_DELIVERY_RECEIPTS)
128 uid = mess_data["uid"]
111 msg_id = message.getAttribute("id") 129 msg_id = message.getAttribute("id")
112 self._dictRequest[msg_id] = uid 130 self._dictRequest[msg_id] = uid
113 reactor.callLater(TEMPO_DELETE_WAITING_ACK_S, self._clearDictRequest, msg_id) 131 reactor.callLater(
114 log.debug(_("[XEP-0184] Request acknowledgment for message id {}".format(msg_id))) 132 TEMPO_DELETE_WAITING_ACK_S, self._clearDictRequest, msg_id
133 )
134 log.debug(
135 _(
136 "[XEP-0184] Request acknowledgment for message id {}".format(
137 msg_id
138 )
139 )
140 )
115 141
116 return mess_data 142 return mess_data
117 143
118 post_xml_treatments.addCallback(treatment) 144 post_xml_treatments.addCallback(treatment)
119 return True 145 return True
120 146
121 def onMessageDeliveryReceiptsRequest(self, msg_elt, client): 147 def onMessageDeliveryReceiptsRequest(self, msg_elt, client):
122 """This method is called on message delivery receipts **request** (XEP-0184 #7) 148 """This method is called on message delivery receipts **request** (XEP-0184 #7)
123 @param msg_elt: message element 149 @param msg_elt: message element
124 @param client: %(doc_client)s""" 150 @param client: %(doc_client)s"""
125 from_jid = jid.JID(msg_elt['from']) 151 from_jid = jid.JID(msg_elt["from"])
126 152
127 if self._isActif(client.profile) and client.roster.isPresenceAuthorised(from_jid): 153 if self._isActif(client.profile) and client.roster.isPresenceAuthorised(from_jid):
128 received_elt_ret = domish.Element((NS_MESSAGE_DELIVERY_RECEIPTS, 'received')) 154 received_elt_ret = domish.Element((NS_MESSAGE_DELIVERY_RECEIPTS, "received"))
129 received_elt_ret["id"] = msg_elt["id"] 155 received_elt_ret["id"] = msg_elt["id"]
130 156
131 msg_result_elt = xmlstream.toResponse(msg_elt, 'result') 157 msg_result_elt = xmlstream.toResponse(msg_elt, "result")
132 msg_result_elt.addChild(received_elt_ret) 158 msg_result_elt.addChild(received_elt_ret)
133 client.send(msg_result_elt) 159 client.send(msg_result_elt)
134 160
135 def onMessageDeliveryReceiptsReceived(self, msg_elt, client): 161 def onMessageDeliveryReceiptsReceived(self, msg_elt, client):
136 """This method is called on message delivery receipts **received** (XEP-0184 #7) 162 """This method is called on message delivery receipts **received** (XEP-0184 #7)
137 @param msg_elt: message element 163 @param msg_elt: message element
138 @param client: %(doc_client)s""" 164 @param client: %(doc_client)s"""
139 msg_elt.handled = True 165 msg_elt.handled = True
140 rcv_elt = msg_elt.elements(NS_MESSAGE_DELIVERY_RECEIPTS, 'received').next() 166 rcv_elt = msg_elt.elements(NS_MESSAGE_DELIVERY_RECEIPTS, "received").next()
141 msg_id = rcv_elt['id'] 167 msg_id = rcv_elt["id"]
142 168
143 try: 169 try:
144 uid = self._dictRequest[msg_id] 170 uid = self._dictRequest[msg_id]
145 del self._dictRequest[msg_id] 171 del self._dictRequest[msg_id]
146 self.host.bridge.messageState(uid, STATUS_MESSAGE_DELIVERY_RECEIVED, client.profile) 172 self.host.bridge.messageState(
147 log.debug(_("[XEP-0184] Receive acknowledgment for message id {}".format(msg_id))) 173 uid, STATUS_MESSAGE_DELIVERY_RECEIVED, client.profile
174 )
175 log.debug(
176 _("[XEP-0184] Receive acknowledgment for message id {}".format(msg_id))
177 )
148 except KeyError: 178 except KeyError:
149 pass 179 pass
150 180
151 def _clearDictRequest(self, msg_id): 181 def _clearDictRequest(self, msg_id):
152 try: 182 try:
153 del self._dictRequest[msg_id] 183 del self._dictRequest[msg_id]
154 log.debug(_("[XEP-0184] Delete waiting acknowledgment for message id {}".format(msg_id))) 184 log.debug(
185 _(
186 "[XEP-0184] Delete waiting acknowledgment for message id {}".format(
187 msg_id
188 )
189 )
190 )
155 except KeyError: 191 except KeyError:
156 pass 192 pass
157 193
158 def _isActif(self, profile): 194 def _isActif(self, profile):
159 return self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile) 195 return self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile)
196
160 197
161 class XEP_0184_handler(XMPPHandler): 198 class XEP_0184_handler(XMPPHandler):
162 implements(iwokkel.IDisco) 199 implements(iwokkel.IDisco)
163 200
164 def __init__(self, plugin_parent, profile): 201 def __init__(self, plugin_parent, profile):
165 self.plugin_parent = plugin_parent 202 self.plugin_parent = plugin_parent
166 self.host = plugin_parent.host 203 self.host = plugin_parent.host
167 self.profile = profile 204 self.profile = profile
168 205
169 def connectionInitialized(self): 206 def connectionInitialized(self):
170 self.xmlstream.addObserver(MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST, self.plugin_parent.onMessageDeliveryReceiptsRequest, client=self.parent) 207 self.xmlstream.addObserver(
171 self.xmlstream.addObserver(MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, self.plugin_parent.onMessageDeliveryReceiptsReceived, client=self.parent) 208 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST,
172 209 self.plugin_parent.onMessageDeliveryReceiptsRequest,
173 self.xmlstream.addObserver(MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST, self.plugin_parent.onMessageDeliveryReceiptsRequest, client=self.parent) 210 client=self.parent,
174 self.xmlstream.addObserver(MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, self.plugin_parent.onMessageDeliveryReceiptsReceived, client=self.parent) 211 )
175 212 self.xmlstream.addObserver(
176 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): 213 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED,
214 self.plugin_parent.onMessageDeliveryReceiptsReceived,
215 client=self.parent,
216 )
217
218 self.xmlstream.addObserver(
219 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST,
220 self.plugin_parent.onMessageDeliveryReceiptsRequest,
221 client=self.parent,
222 )
223 self.xmlstream.addObserver(
224 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED,
225 self.plugin_parent.onMessageDeliveryReceiptsReceived,
226 client=self.parent,
227 )
228
229 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
177 return [disco.DiscoFeature(NS_MESSAGE_DELIVERY_RECEIPTS)] 230 return [disco.DiscoFeature(NS_MESSAGE_DELIVERY_RECEIPTS)]
178 231
179 def getDiscoItems(self, requestor, target, nodeIdentifier=''): 232 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
180 return [] 233 return []