Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0313.py @ 3340:2ad14b834730
component file sharing: added `Access-Control-Expose-Headers` CORS header:
without this header, the range header in response was not exposed to client. This could
cause trouble is a javascript application was needing to access it.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 15 Aug 2020 20:28:07 +0200 |
parents | 384283adcce1 |
children | 569f4cf7183b |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
1277 | 3 |
4 # SAT plugin for Message Archive Management (XEP-0313) | |
3136 | 5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
1766 | 6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) |
1277 | 7 |
8 # This program is free software: you can redistribute it and/or modify | |
9 # it under the terms of the GNU Affero General Public License as published by | |
10 # the Free Software Foundation, either version 3 of the License, or | |
11 # (at your option) any later version. | |
12 | |
13 # This program is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 # GNU Affero General Public License for more details. | |
17 | |
18 # You should have received a copy of the GNU Affero General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
21 from sat.core.constants import Const as C | |
22 from sat.core.i18n import _ | |
23 from sat.core.log import getLogger | |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
24 from sat.core import exceptions |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
25 from sat.tools.common import data_format |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
26 from twisted.words.protocols.jabber import jid |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
27 from twisted.internet import defer |
3028 | 28 from zope.interface import implementer |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
29 from datetime import datetime |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
30 from dateutil import tz |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
31 from wokkel import disco |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
32 from wokkel import data_form |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
33 import uuid |
1277 | 34 |
2413
70399d1acb47
tmp: removed sat.tmp hierarchy and fixed references to it as it is now an independant sat_tmp repository
Goffi <goffi@goffi.org>
parents:
2145
diff
changeset
|
35 # XXX: mam and rsm come from sat_tmp.wokkel |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
36 from wokkel import rsm |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
37 from wokkel import mam |
1285
ed2c718bfe03
tmp, plugins: fixes the imports fron sat.tmp
souliane <souliane@mailoo.org>
parents:
1284
diff
changeset
|
38 |
1277 | 39 |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
40 log = getLogger(__name__) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
41 |
1277 | 42 PLUGIN_INFO = { |
3028 | 43 C.PI_NAME: "Message Archive Management", |
44 C.PI_IMPORT_NAME: "XEP-0313", | |
45 C.PI_TYPE: "XEP", | |
46 C.PI_PROTOCOLS: ["XEP-0313"], | |
47 C.PI_DEPENDENCIES: ["XEP-0059", "XEP-0359"], | |
48 C.PI_MAIN: "XEP_0313", | |
49 C.PI_HANDLER: "yes", | |
50 C.PI_DESCRIPTION: _("""Implementation of Message Archive Management"""), | |
1277 | 51 } |
52 | |
3028 | 53 MAM_PREFIX = "mam_" |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
54 FILTER_PREFIX = MAM_PREFIX + "filter_" |
3028 | 55 KEY_LAST_STANZA_ID = "last_stanza_id" |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
56 MESSAGE_RESULT = "/message/result[@xmlns='{mam_ns}' and @queryid='{query_id}']" |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
57 MESSAGE_STANZA_ID = '/message/stanza-id[@xmlns="{ns_stanza_id}"]' |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
58 |
1277 | 59 |
60 class XEP_0313(object): | |
61 def __init__(self, host): | |
62 log.info(_("Message Archive Management plugin initialization")) | |
63 self.host = host | |
3028 | 64 self.host.registerNamespace("mam", mam.NS_MAM) |
65 self._rsm = host.plugins["XEP-0059"] | |
66 self._sid = host.plugins["XEP-0359"] | |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
67 # Deferred used to store last stanza id in order of reception |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
68 self._last_stanza_id_d = defer.Deferred() |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
69 self._last_stanza_id_d.callback(None) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
70 host.bridge.addMethod( |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
71 "MAMGet", ".plugin", in_sign='sss', |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
72 out_sign='(a(sdssa{ss}a{ss}sa{ss})a{ss}s)', method=self._getArchives, |
3028 | 73 async_=True) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
74 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
75 @defer.inlineCallbacks |
3011
93da7c6f8e0c
plugin XEP-0198: retrieve missing messages + send buffered ones on hot reconnection:
Goffi <goffi@goffi.org>
parents:
2930
diff
changeset
|
76 def resume(self, client): |
93da7c6f8e0c
plugin XEP-0198: retrieve missing messages + send buffered ones on hot reconnection:
Goffi <goffi@goffi.org>
parents:
2930
diff
changeset
|
77 """Retrieve one2one messages received since the last we have in local storage""" |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
78 stanza_id_data = yield self.host.memory.storage.getPrivates( |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
79 mam.NS_MAM, [KEY_LAST_STANZA_ID], profile=client.profile) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
80 stanza_id = stanza_id_data.get(KEY_LAST_STANZA_ID) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
81 if stanza_id is None: |
3028 | 82 log.info("can't retrieve last stanza ID, checking history") |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
83 last_mess = yield self.host.memory.historyGet( |
3028 | 84 None, None, limit=1, filters={'not_types': C.MESS_TYPE_GROUPCHAT, |
85 'last_stanza_id': True}, | |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
86 profile=client.profile) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
87 if not last_mess: |
3028 | 88 log.info(_("It seems that we have no MAM history yet")) |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
89 return |
3028 | 90 stanza_id = last_mess[0][-1]['stanza_id'] |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
91 rsm_req = rsm.RSMRequest(max_=100, after=stanza_id) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
92 mam_req = mam.MAMRequest(rsm_=rsm_req) |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
93 complete = False |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
94 count = 0 |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
95 while not complete: |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
96 mam_data = yield self.getArchives(client, mam_req, |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
97 service=client.jid.userhostJID()) |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
98 elt_list, rsm_response, mam_response = mam_data |
3028 | 99 complete = mam_response["complete"] |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
100 # we update MAM request for next iteration |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
101 mam_req.rsm.after = rsm_response.last |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
102 if not elt_list: |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
103 break |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
104 else: |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
105 count += len(elt_list) |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
106 |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
107 for mess_elt in elt_list: |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
108 try: |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
109 fwd_message_elt = self.getMessageFromResult( |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
110 client, mess_elt, mam_req) |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
111 except exceptions.DataError: |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
112 continue |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
113 |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
114 try: |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
115 destinee = jid.JID(fwd_message_elt['to']) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
116 except KeyError: |
3028 | 117 log.warning(_('missing "to" attribute in forwarded message')) |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
118 destinee = client.jid |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
119 if destinee.userhostJID() == client.jid.userhostJID(): |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
120 # message to use, we insert the forwarded message in the normal |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
121 # workflow |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
122 client.xmlstream.dispatch(fwd_message_elt) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
123 else: |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
124 # this message should be from us, we just add it to history |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
125 try: |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
126 from_jid = jid.JID(fwd_message_elt['from']) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
127 except KeyError: |
3028 | 128 log.warning(_('missing "from" attribute in forwarded message')) |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
129 from_jid = client.jid |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
130 if from_jid.userhostJID() != client.jid.userhostJID(): |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
131 log.warning(_( |
3028 | 132 'was expecting a message sent by our jid, but this one if ' |
133 'from {from_jid}, ignoring\n{xml}').format( | |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
134 from_jid=from_jid.full(), xml=mess_elt.toXml())) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
135 continue |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
136 # adding message to history |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
137 mess_data = client.messageProt.parseMessage(fwd_message_elt) |
3023
1f74cd0f22c3
plugin XEP-0313: log issues in addToHistory instead of raising error
Goffi <goffi@goffi.org>
parents:
3011
diff
changeset
|
138 try: |
1f74cd0f22c3
plugin XEP-0313: log issues in addToHistory instead of raising error
Goffi <goffi@goffi.org>
parents:
3011
diff
changeset
|
139 yield client.messageProt.addToHistory(mess_data) |
1f74cd0f22c3
plugin XEP-0313: log issues in addToHistory instead of raising error
Goffi <goffi@goffi.org>
parents:
3011
diff
changeset
|
140 except exceptions.CancelError as e: |
1f74cd0f22c3
plugin XEP-0313: log issues in addToHistory instead of raising error
Goffi <goffi@goffi.org>
parents:
3011
diff
changeset
|
141 log.warning( |
3028 | 142 "message has not been added to history: {e}".format(e=e)) |
3023
1f74cd0f22c3
plugin XEP-0313: log issues in addToHistory instead of raising error
Goffi <goffi@goffi.org>
parents:
3011
diff
changeset
|
143 except Exception as e: |
1f74cd0f22c3
plugin XEP-0313: log issues in addToHistory instead of raising error
Goffi <goffi@goffi.org>
parents:
3011
diff
changeset
|
144 log.error( |
3028 | 145 "can't add message to history: {e}\n{xml}" |
3023
1f74cd0f22c3
plugin XEP-0313: log issues in addToHistory instead of raising error
Goffi <goffi@goffi.org>
parents:
3011
diff
changeset
|
146 .format(e=e, xml=mess_elt.toXml())) |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
147 |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
148 if not count: |
3028 | 149 log.info(_("We have received no message while offline")) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
150 else: |
3028 | 151 log.info(_("We have received {num_mess} message(s) while offline.") |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
152 .format(num_mess=count)) |
1277 | 153 |
3011
93da7c6f8e0c
plugin XEP-0198: retrieve missing messages + send buffered ones on hot reconnection:
Goffi <goffi@goffi.org>
parents:
2930
diff
changeset
|
154 def profileConnected(self, client): |
93da7c6f8e0c
plugin XEP-0198: retrieve missing messages + send buffered ones on hot reconnection:
Goffi <goffi@goffi.org>
parents:
2930
diff
changeset
|
155 return self.resume(client) |
93da7c6f8e0c
plugin XEP-0198: retrieve missing messages + send buffered ones on hot reconnection:
Goffi <goffi@goffi.org>
parents:
2930
diff
changeset
|
156 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
157 def getHandler(self, client): |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
158 mam_client = client._mam = SatMAMClient(self) |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
159 return mam_client |
1277 | 160 |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
161 def parseExtra(self, extra, with_rsm=True): |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
162 """Parse extra dictionnary to retrieve MAM arguments |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
163 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
164 @param extra(dict): data for parse |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
165 @param with_rsm(bool): if True, RSM data will be parsed too |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
166 @return (data_form, None): request with parsed arguments |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
167 or None if no MAM arguments have been found |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
168 """ |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
169 mam_args = {} |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
170 form_args = {} |
3028 | 171 for arg in ("start", "end"): |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
172 try: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
173 value = extra.pop(MAM_PREFIX + arg) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
174 form_args[arg] = datetime.fromtimestamp(float(value), tz.tzutc()) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
175 except (TypeError, ValueError): |
3028 | 176 log.warning("Bad value for {arg} filter ({value}), ignoring".format( |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
177 arg=arg, value=value)) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
178 except KeyError: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
179 continue |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
180 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
181 try: |
3028 | 182 form_args["with_jid"] = jid.JID(extra.pop( |
183 MAM_PREFIX + "with")) | |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
184 except (jid.InvalidFormat): |
3028 | 185 log.warning("Bad value for jid filter") |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
186 except KeyError: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
187 pass |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
188 |
3028 | 189 for name, value in extra.items(): |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
190 if name.startswith(FILTER_PREFIX): |
2763 | 191 var = name[len(FILTER_PREFIX):] |
3028 | 192 extra_fields = form_args.setdefault("extra_fields", []) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
193 extra_fields.append(data_form.Field(var=var, value=value)) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
194 |
3028 | 195 for arg in ("node", "query_id"): |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
196 try: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
197 value = extra.pop(MAM_PREFIX + arg) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
198 mam_args[arg] = value |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
199 except KeyError: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
200 continue |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
201 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
202 if with_rsm: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
203 rsm_request = self._rsm.parseExtra(extra) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
204 if rsm_request is not None: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
205 mam_args["rsm_"] = rsm_request |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
206 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
207 if form_args: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
208 mam_args["form"] = mam.buildForm(**form_args) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
209 |
2760
3480d4fdf83a
plugins XEP-0060, XEP-0313: implemented Order-By protoXEP:
Goffi <goffi@goffi.org>
parents:
2718
diff
changeset
|
210 # we only set orderBy if we have other MAM args |
3480d4fdf83a
plugins XEP-0060, XEP-0313: implemented Order-By protoXEP:
Goffi <goffi@goffi.org>
parents:
2718
diff
changeset
|
211 # else we would make a MAM query while it's not expected |
3028 | 212 if "order_by" in extra and mam_args: |
213 order_by = extra.pop("order_by") | |
2760
3480d4fdf83a
plugins XEP-0060, XEP-0313: implemented Order-By protoXEP:
Goffi <goffi@goffi.org>
parents:
2718
diff
changeset
|
214 assert isinstance(order_by, list) |
3480d4fdf83a
plugins XEP-0060, XEP-0313: implemented Order-By protoXEP:
Goffi <goffi@goffi.org>
parents:
2718
diff
changeset
|
215 mam_args["orderBy"] = order_by |
3480d4fdf83a
plugins XEP-0060, XEP-0313: implemented Order-By protoXEP:
Goffi <goffi@goffi.org>
parents:
2718
diff
changeset
|
216 |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
217 return mam.MAMRequest(**mam_args) if mam_args else None |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
218 |
2713
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
219 def getMessageFromResult(self, client, mess_elt, mam_req, service=None): |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
220 """Extract usable <message/> from MAM query result |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
221 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
222 The message will be validated, and stanza-id/delay will be added if necessary. |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
223 @param mess_elt(domish.Element): result <message/> element wrapping the message |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
224 to retrieve |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
225 @param mam_req(mam.MAMRequest): request used (needed to get query_id) |
2713
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
226 @param service(jid.JID, None): MAM service where the request has been sent |
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
227 None if it's user server |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
228 @return domish.Element): <message/> that can be used directly with onMessage |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
229 """ |
3028 | 230 if mess_elt.name != "message": |
231 log.warning("unexpected stanza in archive: {xml}".format( | |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
232 xml=mess_elt.toXml())) |
3028 | 233 raise exceptions.DataError("Invalid element") |
2713
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
234 service_jid = client.jid.userhostJID() if service is None else service |
3028 | 235 mess_from = mess_elt["from"] |
2713
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
236 # we check that the message has been sent by the right service |
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
237 # if service is None (i.e. message expected from our own server) |
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
238 # from can be server jid or user's bare jid |
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
239 if (mess_from != service_jid.full() |
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
240 and not (service is None and mess_from == client.jid.host)): |
3028 | 241 log.error("Message is not from our server, something went wrong: " |
242 "{xml}".format(xml=mess_elt.toXml())) | |
243 raise exceptions.DataError("Invalid element") | |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
244 try: |
3028 | 245 result_elt = next(mess_elt.elements(mam.NS_MAM, "result")) |
246 forwarded_elt = next(result_elt.elements(C.NS_FORWARD, "forwarded")) | |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
247 try: |
3028 | 248 delay_elt = next(forwarded_elt.elements(C.NS_DELAY, "delay")) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
249 except StopIteration: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
250 # delay_elt is not mandatory |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
251 delay_elt = None |
3028 | 252 fwd_message_elt = next(forwarded_elt.elements(C.NS_CLIENT, "message")) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
253 except StopIteration: |
3028 | 254 log.warning("Invalid message received from MAM: {xml}".format( |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
255 xml=mess_elt.toXml())) |
3028 | 256 raise exceptions.DataError("Invalid element") |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
257 else: |
3028 | 258 if not result_elt["queryid"] == mam_req.query_id: |
259 log.error("Unexpected query id (was expecting {query_id}): {xml}" | |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
260 .format(query_id=mam.query_id, xml=mess_elt.toXml())) |
3028 | 261 raise exceptions.DataError("Invalid element") |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
262 stanza_id = self._sid.getStanzaId(fwd_message_elt, |
2713
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
263 service_jid) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
264 if stanza_id is None: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
265 # not stanza-id element is present, we add one so message |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
266 # will be archived with it, and we won't request several times |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
267 # the same MAM achive |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
268 try: |
3028 | 269 stanza_id = result_elt["id"] |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
270 except AttributeError: |
3028 | 271 log.warning('Invalid MAM result: missing "id" attribute: {xml}' |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
272 .format(xml=result_elt.toXml())) |
3028 | 273 raise exceptions.DataError("Invalid element") |
2713
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
274 self._sid.addStanzaId(client, fwd_message_elt, stanza_id, by=service_jid) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
275 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
276 if delay_elt is not None: |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
277 fwd_message_elt.addChild(delay_elt) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
278 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
279 return fwd_message_elt |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
280 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
281 def queryFields(self, client, service=None): |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
282 """Ask the server about supported fields. |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
283 |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
284 @param service: entity offering the MAM service (None for user archives) |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
285 @return (D(data_form.Form)): form with the implemented fields (cf XEP-0313 §4.1.5) |
1277 | 286 """ |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
287 return client._mam.queryFields(service) |
1277 | 288 |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
289 def queryArchive(self, client, mam_req, service=None): |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
290 """Query a user, MUC or pubsub archive. |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
291 |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
292 @param mam_req(mam.MAMRequest): MAM query instance |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
293 @param service(jid.JID, None): entity offering the MAM service |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
294 None for user server |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
295 @return (D(domish.Element)): <IQ/> result |
1277 | 296 """ |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
297 return client._mam.queryArchive(mam_req, service) |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
298 |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
299 def _appendMessage(self, elt_list, message_cb, message_elt): |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
300 if message_cb is not None: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
301 elt_list.append(message_cb(message_elt)) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
302 else: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
303 elt_list.append(message_elt) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
304 |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
305 def _queryFinished(self, iq_result, client, elt_list, event): |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
306 client.xmlstream.removeObserver(event, self._appendMessage) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
307 try: |
3028 | 308 fin_elt = next(iq_result.elements(mam.NS_MAM, "fin")) |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
309 except StopIteration: |
3028 | 310 raise exceptions.DataError("Invalid MAM result") |
1277 | 311 |
3028 | 312 mam_response = {"complete": C.bool(fin_elt.getAttribute("complete", C.BOOL_FALSE)), |
313 "stable": C.bool(fin_elt.getAttribute("stable", C.BOOL_TRUE))} | |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
314 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
315 try: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
316 rsm_response = rsm.RSMResponse.fromElement(fin_elt) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
317 except rsm.RSMNotFoundError: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
318 rsm_response = None |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
319 |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
320 return (elt_list, rsm_response, mam_response) |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
321 |
2713
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
322 def serializeArchiveResult(self, data, client, mam_req, service): |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
323 elt_list, rsm_response, mam_response = data |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
324 mess_list = [] |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
325 for elt in elt_list: |
2713
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
326 fwd_message_elt = self.getMessageFromResult(client, elt, mam_req, |
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
327 service=service) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
328 mess_data = client.messageProt.parseMessage(fwd_message_elt) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
329 mess_list.append(client.messageGetBridgeArgs(mess_data)) |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
330 metadata = { |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
331 'rsm': self._rsm.response2dict(rsm_response), |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
332 'mam': mam_response |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
333 } |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
334 return mess_list, metadata, client.profile |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
335 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
336 def _getArchives(self, service, extra_ser, profile_key): |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
337 """ |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
338 @return: tuple with: |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
339 - list of message with same data as in bridge.messageNew |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
340 - response metadata with: |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
341 - rsm data (first, last, count, index) |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
342 - mam data (complete, stable) |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
343 - profile |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
344 """ |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
345 client = self.host.getClient(profile_key) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
346 service = jid.JID(service) if service else None |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
347 extra = data_format.deserialise(extra_ser, {}) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
348 mam_req = self.parseExtra(extra) |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
349 |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
350 d = self.getArchives(client, mam_req, service=service) |
2713
19000c506d0c
plugin XEP-0313: improvments to prepare MUC MAM:
Goffi <goffi@goffi.org>
parents:
2701
diff
changeset
|
351 d.addCallback(self.serializeArchiveResult, client, mam_req, service) |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
352 return d |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
353 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
354 def getArchives(self, client, query, service=None, message_cb=None): |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
355 """Query archive and gather page result |
1277 | 356 |
2718
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
357 @param query(mam.MAMRequest): MAM request |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
358 @param service(jid.JID, None): MAM service to use |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
359 None to use our own server |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
360 @param message_cb(callable, None): callback to use on each message |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
361 this method can be used to unwrap messages |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
362 @return (tuple[list[domish.Element], rsm.RSMResponse, dict): result data with: |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
363 - list of found elements |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
364 - RSM response |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
365 - MAM response, which is a dict with following value: |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
366 - complete: a boolean which is True if all items have been received |
bb6adaa580ee
plugin XEP-0313, XEP-0045: loop MAM requests until whole archive is retrieved:
Goffi <goffi@goffi.org>
parents:
2713
diff
changeset
|
367 - stable: a boolean which is False if items order may be changed |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
368 """ |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
369 if query.query_id is None: |
3028 | 370 query.query_id = str(uuid.uuid4()) |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
371 elt_list = [] |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
372 event = MESSAGE_RESULT.format(mam_ns=mam.NS_MAM, query_id=query.query_id) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
373 client.xmlstream.addObserver(event, self._appendMessage, 0, elt_list, message_cb) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
374 d = self.queryArchive(client, query, service) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
375 d.addCallback(self._queryFinished, client, elt_list, event) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
376 return d |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
377 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
378 def getPrefs(self, client, service=None): |
1277 | 379 """Retrieve the current user preferences. |
380 | |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
381 @param service: entity offering the MAM service (None for user archives) |
1277 | 382 @return: the server response as a Deferred domish.Element |
383 """ | |
384 # http://xmpp.org/extensions/xep-0313.html#prefs | |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
385 return client._mam.queryPrefs(service) |
1277 | 386 |
2701
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
387 def _setPrefs(self, service_s=None, default="roster", always=None, never=None, |
2ea2369ae7de
plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
388 profile_key=C.PROF_KEY_NONE): |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
389 service = jid.JID(service_s) if service_s else None |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
390 always_jid = [jid.JID(entity) for entity in always] |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
391 never_jid = [jid.JID(entity) for entity in never] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
392 # TODO: why not build here a MAMPrefs object instead of passing the args separately? |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
393 return self.setPrefs(service, default, always_jid, never_jid, profile_key) |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
394 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
395 def setPrefs(self, client, service=None, default="roster", always=None, never=None): |
1277 | 396 """Set news user preferences. |
397 | |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
398 @param service: entity offering the MAM service (None for user archives) |
1277 | 399 @param default (unicode): a value in ('always', 'never', 'roster') |
400 @param always (list): a list of JID instances | |
401 @param never (list): a list of JID instances | |
402 @param profile_key (unicode): %(doc_profile_key)s | |
403 @return: the server response as a Deferred domish.Element | |
404 """ | |
405 # http://xmpp.org/extensions/xep-0313.html#prefs | |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
406 return client._mam.setPrefs(service, default, always, never) |
1277 | 407 |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
408 def onMessageStanzaId(self, message_elt, client): |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
409 """Called when a message with a stanza-id is received |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
410 |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
411 the messages' stanza ids are stored when received, so the last one can be used |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
412 to retrieve missing history on next connection |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
413 @param message_elt(domish.Element): <message> with a stanza-id |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
414 """ |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
415 service_jid = client.jid.userhostJID() |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
416 stanza_id = self._sid.getStanzaId(message_elt, service_jid) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
417 if stanza_id is None: |
3028 | 418 log.debug("Ignoring <message>, stanza id is not from our server") |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
419 else: |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
420 # we use self._last_stanza_id_d do be sure that last_stanza_id is stored in |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
421 # the order of reception |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
422 self._last_stanza_id_d.addCallback( |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
423 lambda __: self.host.memory.storage.setPrivateValue( |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
424 namespace=mam.NS_MAM, |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
425 key=KEY_LAST_STANZA_ID, |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
426 value=stanza_id, |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
427 profile=client.profile)) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
428 |
1277 | 429 |
3028 | 430 @implementer(disco.IDisco) |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
431 class SatMAMClient(mam.MAMClient): |
1277 | 432 |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
433 def __init__(self, plugin_parent): |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
434 self.plugin_parent = plugin_parent |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
435 |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
436 @property |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
437 def host(self): |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
438 return self.parent.host_app |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
439 |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
440 def connectionInitialized(self): |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
441 observer_xpath = MESSAGE_STANZA_ID.format( |
3028 | 442 ns_stanza_id=self.host.ns_map['stanza_id']) |
2930
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
443 self.xmlstream.addObserver( |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
444 observer_xpath, self.plugin_parent.onMessageStanzaId, client=self.parent |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
445 ) |
32b6893240e0
plugin XEP-0313: fixed archive retrieval on connection:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
446 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
447 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
448 return [disco.DiscoFeature(mam.NS_MAM)] |
1277 | 449 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
450 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
1277 | 451 return [] |