Mercurial > libervia-pubsub
annotate sat_pubsub/mam.py @ 316:cca47e9977a5
mam: minor improvments (module import and some checks)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 03 Jan 2016 18:33:22 +0100 |
parents | a776544d84e5 |
children | 54d90c73b8b5 |
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 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
31 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
32 from sat_pubsub import const |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
33 from sat_pubsub.backend import PubSubResourceFromBackend |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
34 from wokkel.pubsub import NS_PUBSUB_EVENT |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
35 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
36 from dateutil import parser |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
37 |
281
30895c49ebd2
fixes the imports from sat.tmp
souliane <souliane@mailoo.org>
parents:
280
diff
changeset
|
38 # TODO: change this when RSM and MAM are in wokkel |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
39 from sat.tmp.wokkel import rsm |
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
40 from sat.tmp.wokkel import mam |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
41 |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
42 NS_CLIENT = 'jabber:client' |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
43 |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
44 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
45 class MAMResource(object): |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
46 |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
47 implements(mam.IMAMResource) |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
48 |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
49 def __init__(self, backend): |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
50 self.backend = backend |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
51 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
52 def onArchiveRequest(self, mam, requestor): |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
53 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
54 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
55 @param mam: The MAM <query/> request. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
56 @type mam: L{MAMQueryReques<wokkel.mam.MAMQueryRequest>} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
57 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
58 @param requestor: JID of the requestor. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
59 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
60 |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
61 @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
|
62 @rtype: C{tuple} |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
63 """ |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
64 # FIXME: bad result ordering |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
65 ext_data = {} |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
66 if mam.form: |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
67 ext_data['filters'] = mam.form.fields.values() |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
68 if mam.rsm is None: |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
69 mam.rsm = rsm.RSMRequest(const.VAL_RSM_MAX_DEFAULT) |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
70 ext_data['rsm'] = mam.rsm |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
71 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
72 d = self.backend.getItems(mam.node, requestor, mam.rsm.max, None, ext_data) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
73 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
74 def make_message(elt): |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
75 # XXX: http://xmpp.org/extensions/xep-0297.html#sect-idp629952 (rule 3) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
76 message = domish.Element((NS_CLIENT, "message")) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
77 event = message.addElement((NS_PUBSUB_EVENT, "event")) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
78 items = event.addElement('items') |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
79 items["node"] = mam.node |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
80 items.addChild(elt) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
81 return message |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
82 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
83 def cb(elts): |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
84 msg_data = [] |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
85 rsm_elt = None |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
86 for elt in elts: |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
87 if elt.name == 'set' and elt.uri == rsm.NS_RSM: |
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
88 assert rsm_elt is None |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
89 rsm_elt = elt |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
90 elif elt.name == 'item': |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
91 # FIXME: this is not good as it is dependant on payload |
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
92 # TODO: remove this and use date field in database |
282
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
93 date = parser.parse(''.join(elt.entry.published.children)) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
94 msg_data.append([elt['id'], make_message(elt), date]) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
95 return (msg_data, rsm_elt) |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
96 |
7d54ff2eeaf2
actually retrieve the MAM messages
souliane <souliane@mailoo.org>
parents:
281
diff
changeset
|
97 d.addErrback(PubSubResourceFromBackend._mapErrors) |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
98 d.addCallback(cb) |
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
99 return d |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
100 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
101 def onPrefsGetRequest(self, requestor): |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
102 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
103 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
104 @param requestor: JID of the requestor. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
105 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
106 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
107 @return: The current settings. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
108 @rtype: L{wokkel.mam.MAMPrefs} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
109 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
110 # TODO: return the actual current settings |
316
cca47e9977a5
mam: minor improvments (module import and some checks)
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
111 return mam.MAMPrefs() |
280
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
112 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
113 def onPrefsSetRequest(self, prefs, requestor): |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
114 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
115 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
116 @param prefs: The new settings to set. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
117 @type prefs: L{wokkel.mam.MAMPrefs} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
118 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
119 @param requestor: JID of the requestor. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
120 @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
121 |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
122 @return: The settings that have actually been set. |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
123 @rtype: L{wokkel.mam.MAMPrefs} |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
124 """ |
798cb8962c0b
MAM (XEP-0313) support: first draft
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
125 # 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
|
126 return mam.MAMPrefs() |