comparison sat/plugins/plugin_xep_0280.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 5060cbeec01e
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
17 # You should have received a copy of the GNU Affero General Public License 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/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _, D_ 20 from sat.core.i18n import _, D_
21 from sat.core.log import getLogger 21 from sat.core.log import getLogger
22
22 log = getLogger(__name__) 23 log = getLogger(__name__)
23 from sat.core import exceptions 24 from sat.core import exceptions
24 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
25 from sat.core.xmpp import SatMessageProtocol 26 from sat.core.xmpp import SatMessageProtocol
26 from twisted.words.protocols.jabber.error import StanzaError 27 from twisted.words.protocols.jabber.error import StanzaError
27 from twisted.internet import defer 28 from twisted.internet import defer
28 from wokkel import disco, iwokkel 29 from wokkel import disco, iwokkel
29 from zope.interface import implements 30 from zope.interface import implements
31
30 try: 32 try:
31 from twisted.words.protocols.xmlstream import XMPPHandler 33 from twisted.words.protocols.xmlstream import XMPPHandler
32 except ImportError: 34 except ImportError:
33 from wokkel.subprotocols import XMPPHandler 35 from wokkel.subprotocols import XMPPHandler
34 36
35 37
36 PARAM_CATEGORY = "Misc" 38 PARAM_CATEGORY = "Misc"
37 PARAM_NAME = "carbon" 39 PARAM_NAME = "carbon"
38 PARAM_LABEL = D_(u"Message carbons") 40 PARAM_LABEL = D_(u"Message carbons")
39 NS_CARBONS = 'urn:xmpp:carbons:2' 41 NS_CARBONS = "urn:xmpp:carbons:2"
40 42
41 PLUGIN_INFO = { 43 PLUGIN_INFO = {
42 C.PI_NAME: u"XEP-0280 Plugin", 44 C.PI_NAME: u"XEP-0280 Plugin",
43 C.PI_IMPORT_NAME: u"XEP-0280", 45 C.PI_IMPORT_NAME: u"XEP-0280",
44 C.PI_TYPE: u"XEP", 46 C.PI_TYPE: u"XEP",
45 C.PI_PROTOCOLS: [u"XEP-0280"], 47 C.PI_PROTOCOLS: [u"XEP-0280"],
46 C.PI_DEPENDENCIES: [], 48 C.PI_DEPENDENCIES: [],
47 C.PI_MAIN: u"XEP_0280", 49 C.PI_MAIN: u"XEP_0280",
48 C.PI_HANDLER: u"yes", 50 C.PI_HANDLER: u"yes",
49 C.PI_DESCRIPTION: D_(u"""Implementation of Message Carbons""") 51 C.PI_DESCRIPTION: D_(u"""Implementation of Message Carbons"""),
50 } 52 }
51 53
52 54
53 class XEP_0280(object): 55 class XEP_0280(object):
54 # TODO: param is only checked at profile connection 56 #  TODO: param is only checked at profile connection
55 # activate carbons on param change even after profile connection 57 # activate carbons on param change even after profile connection
56 # TODO: chat state notifications are not handled yet (and potentially other XEPs?) 58 # TODO: chat state notifications are not handled yet (and potentially other XEPs?)
57 59
58 params = """ 60 params = """
59 <params> 61 <params>
62 <param name="{param_name}" label="{param_label}" value="true" type="bool" security="0" /> 64 <param name="{param_name}" label="{param_label}" value="true" type="bool" security="0" />
63 </category> 65 </category>
64 </individual> 66 </individual>
65 </params> 67 </params>
66 """.format( 68 """.format(
67 category_name = PARAM_CATEGORY, 69 category_name=PARAM_CATEGORY,
68 category_label = D_(PARAM_CATEGORY), 70 category_label=D_(PARAM_CATEGORY),
69 param_name = PARAM_NAME, 71 param_name=PARAM_NAME,
70 param_label = PARAM_LABEL, 72 param_label=PARAM_LABEL,
71 ) 73 )
72 74
73 def __init__(self, host): 75 def __init__(self, host):
74 log.info(_("Plugin XEP_0280 initialization")) 76 log.info(_("Plugin XEP_0280 initialization"))
75 self.host = host 77 self.host = host
76 host.memory.updateParams(self.params) 78 host.memory.updateParams(self.params)
84 86
85 this method is intented to be called on final domish.Element by other plugins 87 this method is intented to be called on final domish.Element by other plugins
86 (in particular end 2 end encryption plugins) 88 (in particular end 2 end encryption plugins)
87 @param message_elt(domish.Element): <message> stanza 89 @param message_elt(domish.Element): <message> stanza
88 """ 90 """
89 if message_elt.name != u'message': 91 if message_elt.name != u"message":
90 log.error(u"addPrivateElt must be used with <message> stanzas") 92 log.error(u"addPrivateElt must be used with <message> stanzas")
91 return 93 return
92 message_elt.addElement((NS_CARBONS, u'private')) 94 message_elt.addElement((NS_CARBONS, u"private"))
93 95
94 @defer.inlineCallbacks 96 @defer.inlineCallbacks
95 def profileConnected(self, client): 97 def profileConnected(self, client):
96 """activate message carbons on connection if possible and activated in config""" 98 """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) 99 activate = self.host.memory.getParamA(
100 PARAM_NAME, PARAM_CATEGORY, profile_key=client.profile
101 )
98 if not activate: 102 if not activate:
99 log.info(_(u"Not activating message carbons as requested in params")) 103 log.info(_(u"Not activating message carbons as requested in params"))
100 return 104 return
101 try: 105 try:
102 yield self.host.checkFeatures(client, (NS_CARBONS,)) 106 yield self.host.checkFeatures(client, (NS_CARBONS,))
103 except exceptions.FeatureNotFound: 107 except exceptions.FeatureNotFound:
104 log.warning(_(u"server doesn't handle message carbons")) 108 log.warning(_(u"server doesn't handle message carbons"))
105 else: 109 else:
106 log.info(_(u"message carbons available, enabling it")) 110 log.info(_(u"message carbons available, enabling it"))
107 iq_elt = client.IQ() 111 iq_elt = client.IQ()
108 iq_elt.addElement((NS_CARBONS, 'enable')) 112 iq_elt.addElement((NS_CARBONS, "enable"))
109 try: 113 try:
110 yield iq_elt.send() 114 yield iq_elt.send()
111 except StanzaError as e: 115 except StanzaError as e:
112 log.warning(u"Can't activate message carbons: {}".format(e)) 116 log.warning(u"Can't activate message carbons: {}".format(e))
113 else: 117 else:
124 if carbons_elt is None: 128 if carbons_elt is None:
125 # this is not a message carbons, 129 # this is not a message carbons,
126 # we continue normal behaviour 130 # we continue normal behaviour
127 return True 131 return True
128 132
129 if message_elt['from'] != client.jid.userhost(): 133 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( 134 log.warning(
131 xml = message_elt.toXml(), 135 u"The message carbon received is not from our server, hack attempt?\n{xml}".format(
132 )) 136 xml=message_elt.toXml()
137 )
138 )
133 return 139 return
134 forwarded_elt = next(carbons_elt.elements(C.NS_FORWARD, 'forwarded')) 140 forwarded_elt = next(carbons_elt.elements(C.NS_FORWARD, "forwarded"))
135 cc_message_elt = next(forwarded_elt.elements(C.NS_CLIENT, 'message')) 141 cc_message_elt = next(forwarded_elt.elements(C.NS_CLIENT, "message"))
136 if carbons_elt.name == 'received': 142 if carbons_elt.name == "received":
137 # on receive we replace the wrapping message with the CCed one 143 # on receive we replace the wrapping message with the CCed one
138 # and continue the normal behaviour 144 # and continue the normal behaviour
139 message_elt['from'] = cc_message_elt['from'] 145 message_elt["from"] = cc_message_elt["from"]
140 del message_elt.children[:] 146 del message_elt.children[:]
141 for c in cc_message_elt.children: 147 for c in cc_message_elt.children:
142 message_elt.addChild(c) 148 message_elt.addChild(c)
143 return True 149 return True
144 elif carbons_elt.name == 'sent': 150 elif carbons_elt.name == "sent":
145 # on send we parse the message and just add it to history 151 # on send we parse the message and just add it to history
146 # and send it to frontends (without normal sending treatments) 152 # and send it to frontends (without normal sending treatments)
147 mess_data = SatMessageProtocol.parseMessage(cc_message_elt, client) 153 mess_data = SatMessageProtocol.parseMessage(cc_message_elt, client)
148 if not mess_data['message'] and not mess_data['subject']: 154 if not mess_data["message"] and not mess_data["subject"]:
149 return False 155 return False
150 client.messageAddToHistory(mess_data) 156 client.messageAddToHistory(mess_data)
151 client.messageSendToBridge(mess_data) 157 client.messageSendToBridge(mess_data)
152 else: 158 else:
153 log.warning(u"invalid message carbons received:\n{xml}".format( 159 log.warning(
154 xml = message_elt.toXml())) 160 u"invalid message carbons received:\n{xml}".format(
161 xml=message_elt.toXml()
162 )
163 )
155 return False 164 return False
156 165
157 166
158 class XEP_0280_handler(XMPPHandler): 167 class XEP_0280_handler(XMPPHandler):
159 implements(iwokkel.IDisco) 168 implements(iwokkel.IDisco)
160 169
161 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): 170 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
162 return [disco.DiscoFeature(NS_CARBONS)] 171 return [disco.DiscoFeature(NS_CARBONS)]
163 172
164 def getDiscoItems(self, requestor, target, nodeIdentifier=''): 173 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
165 return [] 174 return []