comparison sat/plugins/plugin_xep_0313.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_xep_0313.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for Message Archive Management (XEP-0313)
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
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__)
25 from sat.core import exceptions
26
27 from twisted.words.protocols.jabber import jid
28
29 from zope.interface import implements
30
31 from wokkel import disco
32 import uuid
33
34 # XXX: mam and rsm come from sat_tmp.wokkel
35 from wokkel import rsm
36 from wokkel import mam
37
38
39 MESSAGE_RESULT = "/message/result[@xmlns='{mam_ns}' and @queryid='{query_id}']"
40
41 PLUGIN_INFO = {
42 C.PI_NAME: "Message Archive Management",
43 C.PI_IMPORT_NAME: "XEP-0313",
44 C.PI_TYPE: "XEP",
45 C.PI_PROTOCOLS: ["XEP-0313"],
46 C.PI_MAIN: "XEP_0313",
47 C.PI_HANDLER: "yes",
48 C.PI_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, client):
59 mam_client = client._mam = SatMAMClient()
60 return mam_client
61
62 def queryFields(self, client, service=None):
63 """Ask the server about supported fields.
64
65 @param service: entity offering the MAM service (None for user archives)
66 @return (D(data_form.Form)): form with the implemented fields (cf XEP-0313 §4.1.5)
67 """
68 return client._mam.queryFields(service)
69
70 def queryArchive(self, client, mam_query, service=None):
71 """Query a user, MUC or pubsub archive.
72
73 @param mam_query(mam.MAMRequest): MAM query instance
74 @param service(jid.JID, None): entity offering the MAM service
75 None for user server
76 @return (D(domish.Element)): <IQ/> result
77 """
78 return client._mam.queryArchive(mam_query, service)
79
80 def _appendMessage(self, elt_list, message_cb, message_elt):
81 if message_cb is not None:
82 elt_list.append(message_cb(message_elt))
83 else:
84 elt_list.append(message_elt)
85
86 def _queryFinished(self, iq_result, client, elt_list, event):
87 client.xmlstream.removeObserver(event, self._appendMessage)
88 try:
89 fin_elt = iq_result.elements(mam.NS_MAM, 'fin').next()
90 except StopIteration:
91 raise exceptions.DataError(u"Invalid MAM result")
92
93 try:
94 rsm_response = rsm.RSMResponse.fromElement(fin_elt)
95 except rsm.RSMNotFoundError:
96 rsm_response = None
97
98 return (elt_list, rsm_response)
99
100 def getArchives(self, client, query, service=None, message_cb=None):
101 """Query archive then grab and return them all in the result
102
103 """
104 if query.query_id is None:
105 query.query_id = unicode(uuid.uuid4())
106 elt_list = []
107 event = MESSAGE_RESULT.format(mam_ns=mam.NS_MAM, query_id=query.query_id)
108 client.xmlstream.addObserver(event, self._appendMessage, 0, elt_list, message_cb)
109 d = self.queryArchive(client, query, service)
110 d.addCallback(self._queryFinished, client, elt_list, event)
111 return d
112
113 def getPrefs(self, client, service=None):
114 """Retrieve the current user preferences.
115
116 @param service: entity offering the MAM service (None for user archives)
117 @return: the server response as a Deferred domish.Element
118 """
119 # http://xmpp.org/extensions/xep-0313.html#prefs
120 return client._mam.queryPrefs(service)
121
122 def _setPrefs(self, service_s=None, default='roster', always=None, never=None, profile_key=C.PROF_KEY_NONE):
123 service = jid.JID(service_s) if service_s else None
124 always_jid = [jid.JID(entity) for entity in always]
125 never_jid = [jid.JID(entity) for entity in never]
126 #TODO: why not build here a MAMPrefs object instead of passing the args separately?
127 return self.setPrefs(service, default, always_jid, never_jid, profile_key)
128
129 def setPrefs(self, client, service=None, default='roster', always=None, never=None):
130 """Set news user preferences.
131
132 @param service: entity offering the MAM service (None for user archives)
133 @param default (unicode): a value in ('always', 'never', 'roster')
134 @param always (list): a list of JID instances
135 @param never (list): a list of JID instances
136 @param profile_key (unicode): %(doc_profile_key)s
137 @return: the server response as a Deferred domish.Element
138 """
139 # http://xmpp.org/extensions/xep-0313.html#prefs
140 return client._mam.setPrefs(service, default, always, never)
141
142
143 class SatMAMClient(mam.MAMClient):
144 implements(disco.IDisco)
145
146 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
147 return [disco.DiscoFeature(mam.NS_MAM)]
148
149 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
150 return []