comparison src/plugins/plugin_xep_0184.py @ 2023:224c8b0886bc

plugin XEP-0184: Implementation of XEP-0184 (Message Delivery Receipts)
author Geoffrey POUZET <chteufleur@kingpenguin.tk>
date Thu, 28 Jul 2016 19:11:31 +0200
parents
children 522c4c8903f9
comparison
equal deleted inserted replaced
2022:88c41a195728 2023:224c8b0886bc
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for managing xep-0184
5 # Copyright (C) 2009-2016 Geoffrey POUZET (chteufleur@kingpenguin.tk)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from sat.core.i18n import _
20 from sat.core.log import getLogger
21 from twisted.internet import reactor
22 from twisted.words.protocols.jabber import xmlstream
23 from twisted.words.xish import domish
24 log = getLogger(__name__)
25
26 from wokkel import disco, iwokkel
27 from zope.interface import implements
28 try:
29 from twisted.words.protocols.xmlstream import XMPPHandler
30 except ImportError:
31 from wokkel.subprotocols import XMPPHandler
32
33
34 NS_MESSAGE_DELIVERY_RECEIPTS = 'urn:xmpp:receipts'
35
36 MSG = 'message'
37
38 MSG_CHAT = '/'+MSG+'[@type="chat"]'
39 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST = MSG_CHAT+'/request[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]'
40 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = MSG_CHAT+'/received[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]'
41
42 MSG_NORMAL = '/'+MSG+'[@type="normal"]'
43 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST = MSG_NORMAL+'/request[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]'
44 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = MSG_NORMAL+'/received[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]'
45
46
47 PARAM_KEY = "Privacy"
48 PARAM_NAME = "Enable message delivery receipts"
49 ENTITY_KEY = PARAM_KEY + "_" + PARAM_NAME
50
51
52 PLUGIN_INFO = {
53 "name": "XEP-0184 Plugin",
54 "import_name": "XEP-0184",
55 "type": "XEP",
56 "protocols": ["XEP-0184"],
57 "dependencies": [],
58 "main": "XEP_0184",
59 "handler": "yes",
60 "description": _("""Implementation of Message Delivery Receipts""")
61 }
62
63
64 STATUS_MESSAGE_DELIVERY_RECEIVED = "delivered"
65 TEMPO_DELETE_WAITING_ACK_S = 300 # 5 min
66
67
68 class XEP_0184(object):
69 """
70 Implementation for XEP 0184.
71 """
72 params = """
73 <params>
74 <individual>
75 <category name="%(category_name)s" label="%(category_label)s">
76 <param name="%(param_name)s" label="%(param_label)s" value="false" type="bool" security="0"/>
77 </category>
78 </individual>
79 </params>
80 """ % {
81 'category_name': PARAM_KEY,
82 'category_label': _(PARAM_KEY),
83 'param_name': PARAM_NAME,
84 'param_label': _('Enable message delivery receipts')
85 }
86
87 def __init__(self, host):
88 log.info(_("Plugin XEP_0184 (message delivery receipts) initialization"))
89 self.host = host
90 self._dictRequest = dict()
91
92 # parameter value is retrieved before each use
93 host.memory.updateParams(self.params)
94
95 host.trigger.add("messageSend", self.messageSendTrigger)
96 host.bridge.addSignal("messageState", ".plugin", signature='sss') # message_uid, status, profile
97
98 def getHandler(self, profile):
99 return XEP_0184_handler(self, profile)
100
101 def messageSendTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments):
102 """Install SendMessage command hook """
103 def treatment(mess_data):
104 message = mess_data['xml']
105 message_type = message.getAttribute("type")
106
107 if self._isActif(client.profile) and (message_type == "chat" or message_type == "normal"):
108 message.addElement('request', NS_MESSAGE_DELIVERY_RECEIPTS)
109 uid = mess_data['uid']
110 msg_id = message.getAttribute("id")
111 self._dictRequest[msg_id] = uid
112 reactor.callLater(TEMPO_DELETE_WAITING_ACK_S, self._clearDictRequest, msg_id)
113 log.debug(_("[XEP-0184] Request acknowledgment for message id {}".format(msg_id)))
114
115 return mess_data
116
117 post_xml_treatments.addCallback(treatment)
118 return True
119
120
121 def onMessageDeliveryReceiptsRequest(self, msg_elt, client):
122 """This method is called on message delivery receipts **request** (XEP-0184 #7)
123 @param msg_elt: message element
124 @param client: %(doc_client)s"""
125
126 if self._isActif(client.profile):
127 received_elt_ret = domish.Element((NS_MESSAGE_DELIVERY_RECEIPTS, 'received'))
128 received_elt_ret["id"] = msg_elt["id"]
129
130 msg_result_elt = xmlstream.toResponse(msg_elt, 'result')
131 msg_result_elt.addChild(received_elt_ret)
132 client.xmlstream.send(msg_result_elt)
133
134 def onMessageDeliveryReceiptsReceived(self, msg_elt, client):
135 """This method is called on message delivery receipts **received** (XEP-0184 #7)
136 @param msg_elt: message element
137 @param client: %(doc_client)s"""
138 msg_elt.handled = True
139 rcv_elt = msg_elt.elements(NS_MESSAGE_DELIVERY_RECEIPTS, 'received').next()
140 msg_id = rcv_elt['id']
141
142 try:
143 uid = self._dictRequest[msg_id]
144 del self._dictRequest[msg_id]
145 self.host.bridge.messageState(uid, STATUS_MESSAGE_DELIVERY_RECEIVED, client.profile)
146 log.debug(_("[XEP-0184] Receive acknowledgment for message id {}".format(msg_id)))
147 except KeyError:
148 pass
149
150 def _clearDictRequest(self, msg_id):
151 try:
152 del self._dictRequest[msg_id]
153 log.debug(_("[XEP-0184] Delete waiting acknowledgment for message id {}".format(msg_id)))
154 except KeyError:
155 pass
156
157 def _isActif(self, profile):
158 return self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile)
159
160 class XEP_0184_handler(XMPPHandler):
161 implements(iwokkel.IDisco)
162
163 def __init__(self, plugin_parent, profile):
164 self.plugin_parent = plugin_parent
165 self.host = plugin_parent.host
166 self.profile = profile
167
168 def connectionInitialized(self):
169 self.xmlstream.addObserver(MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST, self.plugin_parent.onMessageDeliveryReceiptsRequest, client=self.parent)
170 self.xmlstream.addObserver(MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, self.plugin_parent.onMessageDeliveryReceiptsReceived, client=self.parent)
171
172 self.xmlstream.addObserver(MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST, self.plugin_parent.onMessageDeliveryReceiptsRequest, client=self.parent)
173 self.xmlstream.addObserver(MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, self.plugin_parent.onMessageDeliveryReceiptsReceived, client=self.parent)
174
175 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
176 return [disco.DiscoFeature(NS_MESSAGE_DELIVERY_RECEIPTS)]
177
178 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
179 return []