Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0280.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_0280.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 managing xep-0280 | |
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _, D_ | |
21 from sat.core.log import getLogger | |
22 log = getLogger(__name__) | |
23 from sat.core import exceptions | |
24 from sat.core.constants import Const as C | |
25 from sat.core.xmpp import SatMessageProtocol | |
26 from twisted.words.protocols.jabber.error import StanzaError | |
27 from twisted.internet import defer | |
28 from wokkel import disco, iwokkel | |
29 from zope.interface import implements | |
30 try: | |
31 from twisted.words.protocols.xmlstream import XMPPHandler | |
32 except ImportError: | |
33 from wokkel.subprotocols import XMPPHandler | |
34 | |
35 | |
36 PARAM_CATEGORY = "Misc" | |
37 PARAM_NAME = "carbon" | |
38 PARAM_LABEL = D_(u"Message carbons") | |
39 NS_CARBONS = 'urn:xmpp:carbons:2' | |
40 | |
41 PLUGIN_INFO = { | |
42 C.PI_NAME: u"XEP-0280 Plugin", | |
43 C.PI_IMPORT_NAME: u"XEP-0280", | |
44 C.PI_TYPE: u"XEP", | |
45 C.PI_PROTOCOLS: [u"XEP-0280"], | |
46 C.PI_DEPENDENCIES: [], | |
47 C.PI_MAIN: u"XEP_0280", | |
48 C.PI_HANDLER: u"yes", | |
49 C.PI_DESCRIPTION: D_(u"""Implementation of Message Carbons""") | |
50 } | |
51 | |
52 | |
53 class XEP_0280(object): | |
54 # TODO: param is only checked at profile connection | |
55 # activate carbons on param change even after profile connection | |
56 # TODO: chat state notifications are not handled yet (and potentially other XEPs?) | |
57 | |
58 params = """ | |
59 <params> | |
60 <individual> | |
61 <category name="{category_name}" label="{category_label}"> | |
62 <param name="{param_name}" label="{param_label}" value="true" type="bool" security="0" /> | |
63 </category> | |
64 </individual> | |
65 </params> | |
66 """.format( | |
67 category_name = PARAM_CATEGORY, | |
68 category_label = D_(PARAM_CATEGORY), | |
69 param_name = PARAM_NAME, | |
70 param_label = PARAM_LABEL, | |
71 ) | |
72 | |
73 def __init__(self, host): | |
74 log.info(_("Plugin XEP_0280 initialization")) | |
75 self.host = host | |
76 host.memory.updateParams(self.params) | |
77 host.trigger.add("MessageReceived", self.messageReceivedTrigger, priority=1000) | |
78 | |
79 def getHandler(self, client): | |
80 return XEP_0280_handler() | |
81 | |
82 def setPrivate(self, message_elt): | |
83 """Add a <private/> element to a message | |
84 | |
85 this method is intented to be called on final domish.Element by other plugins | |
86 (in particular end 2 end encryption plugins) | |
87 @param message_elt(domish.Element): <message> stanza | |
88 """ | |
89 if message_elt.name != u'message': | |
90 log.error(u"addPrivateElt must be used with <message> stanzas") | |
91 return | |
92 message_elt.addElement((NS_CARBONS, u'private')) | |
93 | |
94 @defer.inlineCallbacks | |
95 def profileConnected(self, client): | |
96 """activate message carbons on connection if possible and activated in config""" | |
97 activate = self.host.memory.getParamA(PARAM_NAME, PARAM_CATEGORY, profile_key=client.profile) | |
98 if not activate: | |
99 log.info(_(u"Not activating message carbons as requested in params")) | |
100 return | |
101 try: | |
102 yield self.host.checkFeatures(client, (NS_CARBONS,)) | |
103 except exceptions.FeatureNotFound: | |
104 log.warning(_(u"server doesn't handle message carbons")) | |
105 else: | |
106 log.info(_(u"message carbons available, enabling it")) | |
107 iq_elt = client.IQ() | |
108 iq_elt.addElement((NS_CARBONS, 'enable')) | |
109 try: | |
110 yield iq_elt.send() | |
111 except StanzaError as e: | |
112 log.warning(u"Can't activate message carbons: {}".format(e)) | |
113 else: | |
114 log.info(_(u"message carbons activated")) | |
115 | |
116 def messageReceivedTrigger(self, client, message_elt, post_treat): | |
117 """get message and handle it if carbons namespace is present""" | |
118 carbons_elt = None | |
119 for e in message_elt.elements(): | |
120 if e.uri == NS_CARBONS: | |
121 carbons_elt = e | |
122 break | |
123 | |
124 if carbons_elt is None: | |
125 # this is not a message carbons, | |
126 # we continue normal behaviour | |
127 return True | |
128 | |
129 if message_elt['from'] != client.jid.userhost(): | |
130 log.warning(u"The message carbon received is not from our server, hack attempt?\n{xml}".format( | |
131 xml = message_elt.toXml(), | |
132 )) | |
133 return | |
134 forwarded_elt = next(carbons_elt.elements(C.NS_FORWARD, 'forwarded')) | |
135 cc_message_elt = next(forwarded_elt.elements(C.NS_CLIENT, 'message')) | |
136 if carbons_elt.name == 'received': | |
137 # on receive we replace the wrapping message with the CCed one | |
138 # and continue the normal behaviour | |
139 message_elt['from'] = cc_message_elt['from'] | |
140 del message_elt.children[:] | |
141 for c in cc_message_elt.children: | |
142 message_elt.addChild(c) | |
143 return True | |
144 elif carbons_elt.name == 'sent': | |
145 # on send we parse the message and just add it to history | |
146 # and send it to frontends (without normal sending treatments) | |
147 mess_data = SatMessageProtocol.parseMessage(cc_message_elt, client) | |
148 if not mess_data['message'] and not mess_data['subject']: | |
149 return False | |
150 client.messageAddToHistory(mess_data) | |
151 client.messageSendToBridge(mess_data) | |
152 else: | |
153 log.warning(u"invalid message carbons received:\n{xml}".format( | |
154 xml = message_elt.toXml())) | |
155 return False | |
156 | |
157 | |
158 class XEP_0280_handler(XMPPHandler): | |
159 implements(iwokkel.IDisco) | |
160 | |
161 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
162 return [disco.DiscoFeature(NS_CARBONS)] | |
163 | |
164 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
165 return [] |