Mercurial > libervia-pubsub
comparison sat_pubsub/mam.py @ 405:c56a728412f1
file organisation + setup refactoring:
- `/src` has been renamed to `/sat_pubsub`, this is the recommended naming convention
- revamped `setup.py` on the basis of SàT's `setup.py`
- added a `VERSION` which is the unique place where version number will now be set
- use same trick as in SàT to specify dev version (`D` at the end)
- use setuptools_scm to retrieve Mercurial hash when in dev version
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 16 Aug 2019 12:00:02 +0200 |
parents | src/mam.py@907b10480394 |
children | ccb2a22ea0fc |
comparison
equal
deleted
inserted
replaced
404:105a0772eedd | 405:c56a728412f1 |
---|---|
1 #!/usr/bin/python | |
2 #-*- coding: utf-8 -*- | |
3 | |
4 # Copyright (c) 2016 Jérôme Poisson | |
5 # Copyright (c) 2015-2016 Adrien Cossa | |
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 | |
20 """ | |
21 XMPP Message Archive Management protocol. | |
22 | |
23 This protocol is specified in | |
24 U{XEP-0313<http://xmpp.org/extensions/xep-0313.html>}. | |
25 """ | |
26 | |
27 | |
28 from zope.interface import implements | |
29 | |
30 from twisted.words.xish import domish | |
31 from twisted.python import log | |
32 from twisted.words.protocols.jabber import error | |
33 | |
34 from sat_pubsub import const | |
35 from sat_pubsub import backend | |
36 from wokkel import pubsub | |
37 | |
38 from wokkel import rsm | |
39 from wokkel import mam | |
40 from wokkel import delay | |
41 | |
42 | |
43 class MAMResource(object): | |
44 implements(mam.IMAMResource) | |
45 _errorMap = backend.PubSubResourceFromBackend._errorMap | |
46 | |
47 def __init__(self, backend_): | |
48 self.backend = backend_ | |
49 | |
50 def _mapErrors(self, failure): | |
51 # XXX: come from backend.PubsubResourceFromBackend | |
52 e = failure.trap(*self._errorMap.keys()) | |
53 | |
54 condition, pubsubCondition, feature = self._errorMap[e] | |
55 msg = failure.value.msg | |
56 | |
57 if pubsubCondition: | |
58 exc = pubsub.PubSubError(condition, pubsubCondition, feature, msg) | |
59 else: | |
60 exc = error.StanzaError(condition, text=msg) | |
61 | |
62 raise exc | |
63 | |
64 def onArchiveRequest(self, mam_request): | |
65 """ | |
66 | |
67 @param mam_request: The MAM archive request. | |
68 @type mam_request: L{MAMQueryReques<wokkel.mam.MAMRequest>} | |
69 | |
70 @return: A tuple with list of message data (id, element, data) and RSM element | |
71 @rtype: C{tuple} | |
72 """ | |
73 # FIXME: bad result ordering | |
74 try: | |
75 pep = mam_request.delegated | |
76 except AttributeError: | |
77 pep = False | |
78 ext_data = {'pep': pep} | |
79 if mam_request.form: | |
80 ext_data['filters'] = mam_request.form.fields.values() | |
81 if mam_request.rsm is None: | |
82 if const.VAL_RSM_MAX_DEFAULT != None: | |
83 log.msg("MAM request without RSM limited to {}".format(const.VAL_RSM_MAX_DEFAULT)) | |
84 ext_data['rsm'] = rsm.RSMRequest(const.VAL_RSM_MAX_DEFAULT) | |
85 else: | |
86 ext_data['rsm'] = mam_request.rsm | |
87 | |
88 if mam_request.orderBy: | |
89 ext_data['order_by'] = mam_request.orderBy | |
90 | |
91 d = self.backend.getItemsData(mam_request.node, mam_request.sender, | |
92 mam_request.recipient, None, None, ext_data) | |
93 | |
94 def make_message(elt): | |
95 # XXX: http://xmpp.org/extensions/xep-0297.html#sect-idp629952 (rule 3) | |
96 message = domish.Element((const.NS_CLIENT, "message")) | |
97 event = message.addElement((pubsub.NS_PUBSUB_EVENT, "event")) | |
98 items = event.addElement('items') | |
99 items["node"] = mam_request.node | |
100 items.addChild(elt) | |
101 return message | |
102 | |
103 def cb(items_data): | |
104 msg_data = [] | |
105 rsm_elt = None | |
106 attributes = {} | |
107 for item_data in items_data: | |
108 if item_data.item.name == 'set' and item_data.item.uri == rsm.NS_RSM: | |
109 assert rsm_elt is None | |
110 rsm_elt = item_data.item | |
111 if rsm_elt.first: | |
112 # XXX: we check if it is the last page using initial request data | |
113 # and RSM element data. In this case, we must have the | |
114 # "complete" | |
115 # attribute set to "true". | |
116 page_max = (int(rsm_elt.first['index']) + 1) * mam_request.rsm.max | |
117 count = int(unicode(rsm_elt.count)) | |
118 if page_max >= count: | |
119 # the maximum items which can be displayed is equal to or | |
120 # above the total number of items, which means we are complete | |
121 attributes['complete'] = "true" | |
122 else: | |
123 log.msg("WARNING: no <first> element in RSM request: {xml}".format( | |
124 xml = rsm_elt.toXml().encode('utf-8'))) | |
125 elif item_data.item.name == 'item': | |
126 msg_data.append([item_data.item['id'], make_message(item_data.item), | |
127 item_data.created]) | |
128 else: | |
129 log.msg("WARNING: unknown element: {}".format(item_data.item.name)) | |
130 if pep: | |
131 # we need to send privileged message | |
132 # so me manage the sending ourself, and return | |
133 # an empty msg_data list to avoid double sending | |
134 for data in msg_data: | |
135 self.forwardPEPMessage(mam_request, *data) | |
136 msg_data = [] | |
137 return (msg_data, rsm_elt, attributes) | |
138 | |
139 d.addErrback(self._mapErrors) | |
140 d.addCallback(cb) | |
141 return d | |
142 | |
143 def forwardPEPMessage(self, mam_request, id_, elt, created): | |
144 msg = domish.Element((None, 'message')) | |
145 msg['from'] = self.backend.privilege.server_jid.full() | |
146 msg['to'] = mam_request.sender.full() | |
147 result = msg.addElement((mam.NS_MAM, 'result')) | |
148 if mam_request.query_id is not None: | |
149 result['queryid'] = mam_request.query_id | |
150 result['id'] = id_ | |
151 forward = result.addElement((const.NS_FORWARD, 'forwarded')) | |
152 forward.addChild(delay.Delay(created).toElement()) | |
153 forward.addChild(elt) | |
154 self.backend.privilege.sendMessage(msg) | |
155 | |
156 def onPrefsGetRequest(self, requestor): | |
157 """ | |
158 | |
159 @param requestor: JID of the requestor. | |
160 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} | |
161 | |
162 @return: The current settings. | |
163 @rtype: L{wokkel.mam.MAMPrefs} | |
164 """ | |
165 # TODO: return the actual current settings | |
166 return mam.MAMPrefs() | |
167 | |
168 def onPrefsSetRequest(self, prefs, requestor): | |
169 """ | |
170 | |
171 @param prefs: The new settings to set. | |
172 @type prefs: L{wokkel.mam.MAMPrefs} | |
173 | |
174 @param requestor: JID of the requestor. | |
175 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} | |
176 | |
177 @return: The settings that have actually been set. | |
178 @rtype: L{wokkel.mam.MAMPrefs} | |
179 """ | |
180 # TODO: set the new settings and return them | |
181 return mam.MAMPrefs() |