comparison sat/plugins/plugin_xep_0184.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_xep_0184.py@33c8c4973743
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
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.constants import Const as C
21 from sat.core.log import getLogger
22 from twisted.internet import reactor
23 from twisted.words.protocols.jabber import xmlstream, jid
24 from twisted.words.xish import domish
25 log = getLogger(__name__)
26
27 from wokkel import disco, iwokkel
28 from zope.interface import implements
29 try:
30 from twisted.words.protocols.xmlstream import XMPPHandler
31 except ImportError:
32 from wokkel.subprotocols import XMPPHandler
33
34
35 NS_MESSAGE_DELIVERY_RECEIPTS = 'urn:xmpp:receipts'
36
37 MSG = 'message'
38
39 MSG_CHAT = '/'+MSG+'[@type="chat"]'
40 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST = MSG_CHAT+'/request[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]'
41 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = MSG_CHAT+'/received[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]'
42
43 MSG_NORMAL = '/'+MSG+'[@type="normal"]'
44 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST = MSG_NORMAL+'/request[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]'
45 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = MSG_NORMAL+'/received[@xmlns="'+NS_MESSAGE_DELIVERY_RECEIPTS+'"]'
46
47
48 PARAM_KEY = "Privacy"
49 PARAM_NAME = "Enable message delivery receipts"
50 ENTITY_KEY = PARAM_KEY + "_" + PARAM_NAME
51
52
53 PLUGIN_INFO = {
54 C.PI_NAME: "XEP-0184 Plugin",
55 C.PI_IMPORT_NAME: "XEP-0184",
56 C.PI_TYPE: "XEP",
57 C.PI_PROTOCOLS: ["XEP-0184"],
58 C.PI_DEPENDENCIES: [],
59 C.PI_MAIN: "XEP_0184",
60 C.PI_HANDLER: "yes",
61 C.PI_DESCRIPTION: _("""Implementation of Message Delivery Receipts""")
62 }
63
64
65 STATUS_MESSAGE_DELIVERY_RECEIVED = "delivered"
66 TEMPO_DELETE_WAITING_ACK_S = 300 # 5 min
67
68
69 class XEP_0184(object):
70 """
71 Implementation for XEP 0184.
72 """
73 params = """
74 <params>
75 <individual>
76 <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"/>
78 </category>
79 </individual>
80 </params>
81 """ % {
82 'category_name': PARAM_KEY,
83 'category_label': _(PARAM_KEY),
84 'param_name': PARAM_NAME,
85 'param_label': _('Enable message delivery receipts')
86 }
87
88 def __init__(self, host):
89 log.info(_("Plugin XEP_0184 (message delivery receipts) initialization"))
90 self.host = host
91 self._dictRequest = dict()
92
93 # parameter value is retrieved before each use
94 host.memory.updateParams(self.params)
95
96 host.trigger.add("sendMessage", self.sendMessageTrigger)
97 host.bridge.addSignal("messageState", ".plugin", signature='sss') # message_uid, status, profile
98
99 def getHandler(self, client):
100 return XEP_0184_handler(self, client.profile)
101
102 def sendMessageTrigger(self, client, mess_data, pre_xml_treatments, post_xml_treatments):
103 """Install SendMessage command hook """
104 def treatment(mess_data):
105 message = mess_data['xml']
106 message_type = message.getAttribute("type")
107
108 if self._isActif(client.profile) and (message_type == "chat" or message_type == "normal"):
109 message.addElement('request', NS_MESSAGE_DELIVERY_RECEIPTS)
110 uid = mess_data['uid']
111 msg_id = message.getAttribute("id")
112 self._dictRequest[msg_id] = uid
113 reactor.callLater(TEMPO_DELETE_WAITING_ACK_S, self._clearDictRequest, msg_id)
114 log.debug(_("[XEP-0184] Request acknowledgment for message id {}".format(msg_id)))
115
116 return mess_data
117
118 post_xml_treatments.addCallback(treatment)
119 return True
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 from_jid = jid.JID(msg_elt['from'])
126
127 if self._isActif(client.profile) and client.roster.isPresenceAuthorised(from_jid):
128 received_elt_ret = domish.Element((NS_MESSAGE_DELIVERY_RECEIPTS, 'received'))
129 received_elt_ret["id"] = msg_elt["id"]
130
131 msg_result_elt = xmlstream.toResponse(msg_elt, 'result')
132 msg_result_elt.addChild(received_elt_ret)
133 client.send(msg_result_elt)
134
135 def onMessageDeliveryReceiptsReceived(self, msg_elt, client):
136 """This method is called on message delivery receipts **received** (XEP-0184 #7)
137 @param msg_elt: message element
138 @param client: %(doc_client)s"""
139 msg_elt.handled = True
140 rcv_elt = msg_elt.elements(NS_MESSAGE_DELIVERY_RECEIPTS, 'received').next()
141 msg_id = rcv_elt['id']
142
143 try:
144 uid = self._dictRequest[msg_id]
145 del self._dictRequest[msg_id]
146 self.host.bridge.messageState(uid, STATUS_MESSAGE_DELIVERY_RECEIVED, client.profile)
147 log.debug(_("[XEP-0184] Receive acknowledgment for message id {}".format(msg_id)))
148 except KeyError:
149 pass
150
151 def _clearDictRequest(self, msg_id):
152 try:
153 del self._dictRequest[msg_id]
154 log.debug(_("[XEP-0184] Delete waiting acknowledgment for message id {}".format(msg_id)))
155 except KeyError:
156 pass
157
158 def _isActif(self, profile):
159 return self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile)
160
161 class XEP_0184_handler(XMPPHandler):
162 implements(iwokkel.IDisco)
163
164 def __init__(self, plugin_parent, profile):
165 self.plugin_parent = plugin_parent
166 self.host = plugin_parent.host
167 self.profile = profile
168
169 def connectionInitialized(self):
170 self.xmlstream.addObserver(MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST, self.plugin_parent.onMessageDeliveryReceiptsRequest, client=self.parent)
171 self.xmlstream.addObserver(MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, self.plugin_parent.onMessageDeliveryReceiptsReceived, client=self.parent)
172
173 self.xmlstream.addObserver(MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST, self.plugin_parent.onMessageDeliveryReceiptsRequest, client=self.parent)
174 self.xmlstream.addObserver(MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, self.plugin_parent.onMessageDeliveryReceiptsReceived, client=self.parent)
175
176 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
177 return [disco.DiscoFeature(NS_MESSAGE_DELIVERY_RECEIPTS)]
178
179 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
180 return []