annotate sat/plugins/plugin_xep_0313.py @ 2701:2ea2369ae7de

plugin XEP-0313: implementation of MAM for messages: - (core/xmpp): new messageGetBridgeArgs to easily retrieve arguments used in bridge from message data - : parseMessage is not static anymore - : new "message_parse" trigger point - (xep-0313) : new "MAMGet" bridge method to retrieve history from MAM instead of local one - : on profileConnected, if previous MAM message is found (i.e. message with a stanza_id), message received while offline are retrieved and injected in message workflow. In other words, one2one history is synchronised on connection. - : new "parseExtra" method which parse MAM (and optionally RSM) option from extra dictionary used in bridge.
author Goffi <goffi@goffi.org>
date Sat, 01 Dec 2018 10:33:43 +0100
parents 56f94936df1e
children 19000c506d0c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1776
diff changeset
1 #!/usr/bin/env python2
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
3
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT plugin for Message Archive Management (XEP-0313)
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
7
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
12
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
17
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
20
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
22 from sat.core.i18n import _
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
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
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
28 from zope.interface import implements
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
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
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
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
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
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
42
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
43 MESSAGE_RESULT = "/message/result[@xmlns='{mam_ns}' and @queryid='{query_id}']"
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
44
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
45 PLUGIN_INFO = {
2701
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
46 C.PI_NAME: u"Message Archive Management",
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
47 C.PI_IMPORT_NAME: u"XEP-0313",
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
48 C.PI_TYPE: u"XEP",
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
49 C.PI_PROTOCOLS: [u"XEP-0313"],
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
50 C.PI_DEPENDENCIES: [u"XEP-0059", u"XEP-0359"],
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
51 C.PI_MAIN: u"XEP_0313",
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
52 C.PI_HANDLER: u"yes",
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
53 C.PI_DESCRIPTION: _(u"""Implementation of Message Archive Management"""),
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
54 }
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
55
2701
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
56 MAM_PREFIX = u"mam_"
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
57 FILTER_PREFIX = MAM_PREFIX + "filter_"
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
58
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
59
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
60 class XEP_0313(object):
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
61 def __init__(self, host):
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
62 log.info(_("Message Archive Management plugin initialization"))
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
63 self.host = host
2701
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
64 self.host.registerNamespace(u"mam", mam.NS_MAM)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
65 self._rsm = host.plugins[u"XEP-0059"]
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
66 self._sid = host.plugins[u"XEP-0359"]
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
67 host.bridge.addMethod(
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
68 "MAMGet", ".plugin", in_sign='sss', out_sign='(a(sdssa{ss}a{ss}sa{ss})s)', method=self._getArchives,
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
69 async=True)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
70
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
71 @defer.inlineCallbacks
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
72 def profileConnected(self, client):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
73 last_mess = yield self.host.memory.historyGet(
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
74 None, None, limit=1, filters={u'last_stanza_id': True},
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
75 profile=client.profile)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
76 if not last_mess:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
77 log.info(_(u"It seems that we have no MAM history yet"))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
78 return
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
79 stanza_id = last_mess[0][-1][u'stanza_id']
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
80 # XXX: test
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
81 # stanza_id = "IIheJOfiIhkPYkw6"
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
82 rsm_req = rsm.RSMRequest(after=stanza_id)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
83 mam_req = mam.MAMRequest(rsm_=rsm_req)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
84 mam_data = yield self.getArchives(client, mam_req,
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
85 service=client.jid.userhostJID())
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
86 elt_list, rsm_response = mam_data
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
87 if not elt_list:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
88 log.info(_(u"We have received no message while offline"))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
89 return
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
90 else:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
91 log.info(_(u"We have received {num_mess} message(s) while offline.").format(
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
92 num_mess=len(elt_list)))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
93
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
94 for mess_elt in elt_list:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
95 try:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
96 fwd_message_elt = self.getMessageFromResult(client, mess_elt, mam_req)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
97 except exceptions.DataError:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
98 continue
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
99
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
100 client.messageProt.onMessage(fwd_message_elt)
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
101
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
102 def getHandler(self, client):
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
103 mam_client = client._mam = SatMAMClient()
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
104 return mam_client
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
105
2701
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
106 def parseExtra(self, extra, with_rsm=True):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
107 """Parse extra dictionnary to retrieve MAM arguments
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
108
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
109 @param extra(dict): data for parse
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
110 @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
111 @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
112 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
113 """
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
114 mam_args = {}
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
115 form_args = {}
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
116 for arg in (u"start", u"end"):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
117 try:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
118 value = extra.pop(MAM_PREFIX + arg)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
119 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
120 except (TypeError, ValueError):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
121 log.warning(u"Bad value for {arg} filter ({value}), ignoring".format(
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
122 arg=arg, value=value))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
123 except KeyError:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
124 continue
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
125
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
126 try:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
127 form_args[u"with_jid"] = jid.JID(extra.pop(
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
128 MAM_PREFIX + u"with"))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
129 except (jid.InvalidFormat):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
130 log.warning(u"Bad value for jid filter")
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
131 except KeyError:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
132 pass
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
133
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
134 for name, value in extra.iteritems():
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
135 if name.startswith(FILTER_PREFIX):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
136 var = name[len(FILTER_PREFIX) :]
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
137 extra_fields = form_args.setdefault(u"extra_fields", [])
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
138 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
139
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
140 for arg in (u"node", u"query_id"):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
141 try:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
142 value = extra.pop(MAM_PREFIX + arg)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
143 mam_args[arg] = value
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
144 except KeyError:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
145 continue
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
146
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
147 if with_rsm:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
148 rsm_request = self._rsm.parseExtra(extra)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
149 if rsm_request is not None:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
150 mam_args["rsm_"] = rsm_request
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
151
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
152 if form_args:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
153 mam_args["form"] = mam.buildForm(**form_args)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
154
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
155 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
156
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
157 def getMessageFromResult(self, client, mess_elt, mam_req):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
158 """Extract usable <message/> from MAM query result
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
159
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
160 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
161 @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
162 to retrieve
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
163 @param mam_req(mam.MAMRequest): request used
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
164 @return (domish.Element): <message/> that can be used directly with onMessage
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
165 """
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
166 if mess_elt.name != u"message":
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
167 log.warning(u"unexpected stanza in archive: {xml}".format(
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
168 xml=mess_elt.toXml()))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
169 raise exceptions.DataError(u"Invalid element")
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
170 mess_from = mess_elt[u"from"]
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
171 if mess_from != client.jid.host and mess_from != client.jid.userhost():
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
172 log.error(u"Message is not from our server, something went wrong: "
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
173 u"{xml}".format(xml=mess_elt.toXml()))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
174 raise exceptions.DataError(u"Invalid element")
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
175 try:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
176 result_elt = next(mess_elt.elements(mam.NS_MAM, u"result"))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
177 forwarded_elt = next(result_elt.elements(C.NS_FORWARD, u"forwarded"))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
178 try:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
179 delay_elt = next(forwarded_elt.elements(C.NS_DELAY, u"delay"))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
180 except StopIteration:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
181 # delay_elt is not mandatory
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
182 delay_elt = None
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
183 fwd_message_elt = next(forwarded_elt.elements(C.NS_CLIENT, u"message"))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
184 except StopIteration:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
185 log.warning(u"Invalid message received from MAM: {xml}".format(
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
186 xml=mess_elt.toXml()))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
187 raise exceptions.DataError(u"Invalid element")
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
188 else:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
189 if not result_elt[u"queryid"] == mam_req.query_id:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
190 log.error(u"Unexpected query id (was expecting {query_id}): {xml}"
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
191 .format(query_id=mam.query_id, xml=mess_elt.toXml()))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
192 raise exceptions.DataError(u"Invalid element")
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
193 stanza_id = self._sid.getStanzaId(fwd_message_elt,
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
194 client.jid.userhostJID())
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
195 if stanza_id is None:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
196 # 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
197 # 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
198 # the same MAM achive
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
199 try:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
200 stanza_id = result_elt[u"id"]
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
201 except AttributeError:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
202 log.warning(u'Invalid MAM result: missing "id" attribute: {xml}'
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
203 .format(xml=result_elt.toXml()))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
204 raise exceptions.DataError(u"Invalid element")
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
205 self._sid.addStanzaId(client, fwd_message_elt, stanza_id)
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 delay_elt is not None:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
208 fwd_message_elt.addChild(delay_elt)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
209
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
210 return fwd_message_elt
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
211
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
212 def queryFields(self, client, service=None):
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
213 """Ask the server about supported fields.
1284
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
214
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
215 @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
216 @return (D(data_form.Form)): form with the implemented fields (cf XEP-0313 §4.1.5)
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
217 """
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
218 return client._mam.queryFields(service)
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
219
2701
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
220 def queryArchive(self, client, mam_req, service=None):
1284
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
221 """Query a user, MUC or pubsub archive.
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
222
2701
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
223 @param mam_req(mam.MAMRequest): MAM query instance
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
224 @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
225 None for user server
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
226 @return (D(domish.Element)): <IQ/> result
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
227 """
2701
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
228 return client._mam.queryArchive(mam_req, service)
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
229
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
230 def _appendMessage(self, elt_list, message_cb, message_elt):
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
231 if message_cb is not None:
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
232 elt_list.append(message_cb(message_elt))
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
233 else:
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
234 elt_list.append(message_elt)
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
235
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
236 def _queryFinished(self, iq_result, client, elt_list, event):
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
237 client.xmlstream.removeObserver(event, self._appendMessage)
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
238 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
239 fin_elt = iq_result.elements(mam.NS_MAM, "fin").next()
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
240 except StopIteration:
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
241 raise exceptions.DataError(u"Invalid MAM result")
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
242
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
243 try:
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
244 rsm_response = rsm.RSMResponse.fromElement(fin_elt)
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
245 except rsm.RSMNotFoundError:
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
246 rsm_response = None
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
247
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
248 return (elt_list, rsm_response)
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
249
2701
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
250 def serializeArchiveResult(self, data, client, mam_req):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
251 elt_list, rsm_response = data
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
252 mess_list = []
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
253 for elt in elt_list:
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
254 fwd_message_elt = self.getMessageFromResult(client, elt, mam_req)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
255 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
256 mess_list.append(client.messageGetBridgeArgs(mess_data))
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
257 return mess_list, client.profile
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
258
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
259 def _getArchives(self, service, extra_ser, profile_key):
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
260 client = self.host.getClient(profile_key)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
261 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
262 extra = data_format.deserialise(extra_ser, {})
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
263 mam_req = self.parseExtra(extra)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
264
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
265 d = self.getArchives(client, mam_req, service=service)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
266 d.addCallback(self.serializeArchiveResult, client, mam_req)
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
267 return d
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
268
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
269 def getArchives(self, client, query, service=None, message_cb=None):
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
270 """Query archive then grab and return them all in the result
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
271
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
272 """
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
273 if query.query_id is None:
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
274 query.query_id = unicode(uuid.uuid4())
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
275 elt_list = []
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
276 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
277 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
278 d = self.queryArchive(client, query, service)
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
279 d.addCallback(self._queryFinished, client, elt_list, event)
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
280 return d
1284
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
281
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
282 def getPrefs(self, client, service=None):
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
283 """Retrieve the current user preferences.
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
284
1284
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
285 @param service: entity offering the MAM service (None for user archives)
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
286 @return: the server response as a Deferred domish.Element
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
287 """
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
288 # http://xmpp.org/extensions/xep-0313.html#prefs
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
289 return client._mam.queryPrefs(service)
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
290
2701
2ea2369ae7de plugin XEP-0313: implementation of MAM for messages:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
291 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
292 profile_key=C.PROF_KEY_NONE):
1284
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
293 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
294 always_jid = [jid.JID(entity) for entity in always]
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
295 never_jid = [jid.JID(entity) for entity in never]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
296 # 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
297 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
298
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
299 def setPrefs(self, client, service=None, default="roster", always=None, never=None):
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
300 """Set news user preferences.
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
301
1284
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
302 @param service: entity offering the MAM service (None for user archives)
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
303 @param default (unicode): a value in ('always', 'never', 'roster')
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
304 @param always (list): a list of JID instances
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
305 @param never (list): a list of JID instances
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
306 @param profile_key (unicode): %(doc_profile_key)s
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
307 @return: the server response as a Deferred domish.Element
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
308 """
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
309 # http://xmpp.org/extensions/xep-0313.html#prefs
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
310 return client._mam.setPrefs(service, default, always, never)
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
311
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
312
1284
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
313 class SatMAMClient(mam.MAMClient):
41ffe2c2dddc plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents: 1277
diff changeset
314 implements(disco.IDisco)
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
315
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
316 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
1776
4fc1bf1af48f plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
317 return [disco.DiscoFeature(mam.NS_MAM)]
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
318
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
319 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
1277
3a3e3014f9f8 plugin XEP-0313: first draft:
souliane <souliane@mailoo.org>
parents:
diff changeset
320 return []