Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0313.py @ 1988:3f0d22565684
primitivus (chat): fixed a crash when page-up was pressed on room contacts list
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 01 Jul 2016 00:00:12 +0200 |
parents | 2daf7b4c6756 |
children | 1d3f73e065e1 |
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 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # SAT plugin for Message Archive Management (XEP-0313) | |
1766 | 5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
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 | |
24 log = getLogger(__name__) | |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
25 from sat.core import exceptions |
1277 | 26 |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
27 from twisted.words.protocols.jabber import jid |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
28 |
1277 | 29 from zope.interface import implements |
30 | |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
31 from wokkel import disco |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
32 import uuid |
1277 | 33 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
34 # XXX: mam and rsm come from tmp.wokkel |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
35 from wokkel import rsm |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
36 from wokkel import mam |
1285
ed2c718bfe03
tmp, plugins: fixes the imports fron sat.tmp
souliane <souliane@mailoo.org>
parents:
1284
diff
changeset
|
37 |
1277 | 38 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
39 MESSAGE_RESULT = "/message/result[@xmlns='{mam_ns}' and @queryid='{query_id}']" |
1277 | 40 |
41 PLUGIN_INFO = { | |
42 "name": "Message Archive Management", | |
43 "import_name": "XEP-0313", | |
44 "type": "XEP", | |
45 "protocols": ["XEP-0313"], | |
46 "main": "XEP_0313", | |
47 "handler": "yes", | |
48 "description": _("""Implementation of Message Archive Management""") | |
49 } | |
50 | |
51 | |
52 class XEP_0313(object): | |
53 | |
54 def __init__(self, host): | |
55 log.info(_("Message Archive Management plugin initialization")) | |
56 self.host = host | |
57 | |
58 def getHandler(self, profile): | |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
59 client = self.host.getClient(profile) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
60 mam_client = client._mam = SatMAMClient() |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
61 return mam_client |
1277 | 62 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
63 def queryFields(self, client, service=None): |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
64 """Ask the server about supported fields. |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
65 |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
66 @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
|
67 @return (D(data_form.Form)): form with the implemented fields (cf XEP-0313 §4.1.5) |
1277 | 68 """ |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
69 return client._mam.queryFields(service) |
1277 | 70 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
71 def queryArchive(self, client, mam_query, service=None): |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
72 """Query a user, MUC or pubsub archive. |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
73 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
74 @param mam_query(mam.MAMRequest): MAM query instance |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
75 @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
|
76 None for user server |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
77 @return (D(domish.Element)): <IQ/> result |
1277 | 78 """ |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
79 return client._mam.queryArchive(mam_query, service) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
80 |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
81 def _appendMessage(self, elt_list, message_cb, message_elt): |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
82 if message_cb is not None: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
83 elt_list.append(message_cb(message_elt)) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
84 else: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
85 elt_list.append(message_elt) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
86 |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
87 def _queryFinished(self, iq_result, client, elt_list, event): |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
88 client.xmlstream.removeObserver(event, self._appendMessage) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
89 try: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
90 fin_elt = iq_result.elements(mam.NS_MAM, 'fin').next() |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
91 except StopIteration: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
92 raise exceptions.DataError(u"Invalid MAM result") |
1277 | 93 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
94 try: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
95 rsm_response = rsm.RSMResponse.fromElement(fin_elt) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
96 except rsm.RSMNotFoundError: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
97 rsm_response = None |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
98 |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
99 return (elt_list, rsm_response) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
100 |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
101 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
|
102 """Query archive then grab and return them all in the result |
1277 | 103 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
104 """ |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
105 if query.query_id is None: |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
106 query.query_id = unicode(uuid.uuid4()) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
107 elt_list = [] |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
108 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
|
109 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
|
110 d = self.queryArchive(client, query, service) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
111 d.addCallback(self._queryFinished, client, elt_list, event) |
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
112 return d |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
113 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
114 def getPrefs(self, client, service=None): |
1277 | 115 """Retrieve the current user preferences. |
116 | |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
117 @param service: entity offering the MAM service (None for user archives) |
1277 | 118 @return: the server response as a Deferred domish.Element |
119 """ | |
120 # http://xmpp.org/extensions/xep-0313.html#prefs | |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
121 return client._mam.queryPrefs(service) |
1277 | 122 |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
123 def _setPrefs(self, service_s=None, default='roster', always=None, never=None, profile_key=C.PROF_KEY_NONE): |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
124 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
|
125 always_jid = [jid.JID(entity) for entity in always] |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
126 never_jid = [jid.JID(entity) for entity in never] |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
127 #TODO: why not build here a MAMPrefs object instead of passing the args separately? |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
128 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
|
129 |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
130 def setPrefs(self, client, service=None, default='roster', always=None, never=None): |
1277 | 131 """Set news user preferences. |
132 | |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
133 @param service: entity offering the MAM service (None for user archives) |
1277 | 134 @param default (unicode): a value in ('always', 'never', 'roster') |
135 @param always (list): a list of JID instances | |
136 @param never (list): a list of JID instances | |
137 @param profile_key (unicode): %(doc_profile_key)s | |
138 @return: the server response as a Deferred domish.Element | |
139 """ | |
140 # http://xmpp.org/extensions/xep-0313.html#prefs | |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
141 return client._mam.setPrefs(service, default, always, never) |
1277 | 142 |
143 | |
1284
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
144 class SatMAMClient(mam.MAMClient): |
41ffe2c2dddc
plugin XEP-0313: update (still draft)
souliane <souliane@mailoo.org>
parents:
1277
diff
changeset
|
145 implements(disco.IDisco) |
1277 | 146 |
147 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
1776
4fc1bf1af48f
plugin XEP-0313: cleaning and improvments:
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
148 return [disco.DiscoFeature(mam.NS_MAM)] |
1277 | 149 |
150 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
151 return [] |