Mercurial > libervia-pubsub
annotate sat_pubsub/mam.py @ 324:e73e42b4f6ff
pgsql storage: getItemsIndex raise NodeNotFound in this make sense
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 08 Jan 2016 17:27:44 +0100 |
parents | 54d90c73b8b5 |
children | 8d939378f023 |
rev | line source |
---|---|
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
2 #-*- coding: utf-8 -*- |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
3 |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
4 # Copyright (c) 2016 Jérôme Poisson |
311 | 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 | |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
20 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
21 XMPP Message Archive Management protocol. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
22 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
23 This protocol is specified in |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
24 U{XEP-0313<http://xmpp.org/extensions/xep-0313.html>}. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
25 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
26 |
311 | 27 |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
28 from zope.interface import implements |
281
30895c49ebd2
fixes the imports from sat.tmp
souliane <souliane@mailoo.org>
parents:
280
diff
changeset
|
29 |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
30 from twisted.words.xish import domish |
322 | 31 from twisted.python import log |
32 from twisted.words.protocols.jabber import error | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
33 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
34 from sat_pubsub import const |
322 | 35 from sat_pubsub import backend |
36 from wokkel import pubsub | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
37 |
322 | 38 from wokkel import rsm |
39 from wokkel import mam | |
40 from wokkel import delay | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
41 |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
42 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
43 class MAMResource(object): |
322 | 44 implements(mam.IMAMResource) |
45 _errorMap = backend.PubSubResourceFromBackend._errorMap | |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
46 |
322 | 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()) | |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
53 |
322 | 54 condition, pubsubCondition, feature = self._errorMap[e] |
55 msg = failure.value.msg | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
56 |
322 | 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, requestor): | |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
65 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
66 |
322 | 67 @param mam_request: The MAM archive request. |
68 @type mam_request: L{MAMQueryReques<wokkel.mam.MAMRequest>} | |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
69 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
70 @param requestor: JID of the requestor. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
71 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
72 |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
73 @return: A tuple with list of message data (id, element, data) and RSM element |
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
74 @rtype: C{tuple} |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
75 """ |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
76 # FIXME: bad result ordering |
322 | 77 try: |
78 pep = mam_request.delegated | |
79 except AttributeError: | |
80 pep = False | |
81 ext_data = {'pep': pep} | |
82 if mam_request.form: | |
83 ext_data['filters'] = mam_request.form.fields.values() | |
84 if mam_request.rsm is None: | |
85 if const.VAL_RSM_MAX_DEFAULT != None: | |
86 log.msg("MAM request without RSM limited to {}".format(const.VAL_RSM_MAX_DEFAULT)) | |
87 ext_data['rsm'] = rsm.RSMRequest(const.VAL_RSM_MAX_DEFAULT) | |
88 else: | |
89 ext_data['rsm'] = mam_request.rsm | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
90 |
322 | 91 d = self.backend.getItemsData(mam_request.node, requestor, None, None, ext_data) |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
92 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
93 def make_message(elt): |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
94 # XXX: http://xmpp.org/extensions/xep-0297.html#sect-idp629952 (rule 3) |
322 | 95 message = domish.Element((const.NS_CLIENT, "message")) |
96 event = message.addElement((pubsub.NS_PUBSUB_EVENT, "event")) | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
97 items = event.addElement('items') |
322 | 98 items["node"] = mam_request.node |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
99 items.addChild(elt) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
100 return message |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
101 |
322 | 102 def cb(items_data): |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
103 msg_data = [] |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
104 rsm_elt = None |
322 | 105 for item_data in items_data: |
106 if item_data.item.name == 'set' and item_data.item.uri == rsm.NS_RSM: | |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
107 assert rsm_elt is None |
322 | 108 rsm_elt = item_data.item |
109 elif item_data.item.name == 'item': | |
110 msg_data.append([item_data.item['id'], make_message(item_data.item), item_data.date]) | |
111 else: | |
112 log.msg("WARNING: unknown element: {}".format(item_data.item.name)) | |
113 if pep: | |
114 # we need to send privileged message | |
115 # so me manage the sending ourself, and return | |
116 # an empty msg_data list to avoid double sending | |
117 for data in msg_data: | |
118 self.forwardPEPMessage(mam_request, requestor, *data) | |
119 msg_data = [] | |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
120 return (msg_data, rsm_elt) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
121 |
322 | 122 d.addErrback(self._mapErrors) |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
123 d.addCallback(cb) |
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
124 return d |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
125 |
322 | 126 def forwardPEPMessage(self, mam_request, requestor, id_, elt, date): |
127 msg = domish.Element((None, 'message')) | |
128 msg['from'] = self.backend.privilege.server_jid.full() | |
129 msg['to'] = requestor.full() | |
130 result = msg.addElement((mam.NS_MAM, 'result')) | |
131 if mam_request.query_id is not None: | |
132 result['queryid'] = mam_request.query_id | |
133 result['id'] = id_ | |
134 forward = result.addElement((const.NS_FORWARD, 'forwarded')) | |
135 forward.addChild(delay.Delay(date).toElement()) | |
136 forward.addChild(elt) | |
137 self.backend.privilege.sendMessage(msg) | |
138 | |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
139 def onPrefsGetRequest(self, requestor): |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
140 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
141 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
142 @param requestor: JID of the requestor. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
143 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
144 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
145 @return: The current settings. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
146 @rtype: L{wokkel.mam.MAMPrefs} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
147 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
148 # TODO: return the actual current settings |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
149 return mam.MAMPrefs() |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
150 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
151 def onPrefsSetRequest(self, prefs, requestor): |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
152 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
153 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
154 @param prefs: The new settings to set. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
155 @type prefs: L{wokkel.mam.MAMPrefs} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
156 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
157 @param requestor: JID of the requestor. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
158 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
159 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
160 @return: The settings that have actually been set. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
161 @rtype: L{wokkel.mam.MAMPrefs} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
162 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
163 # TODO: set the new settings and return them |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
164 return mam.MAMPrefs() |