comparison sat/plugins/plugin_xep_0199.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT plugin for Delayed Delivery (XEP-0199) 4 # SAT plugin for Delayed Delivery (XEP-0199)
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) 6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
23 23
24 log = getLogger(__name__) 24 log = getLogger(__name__)
25 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
26 from wokkel import disco, iwokkel 26 from wokkel import disco, iwokkel
27 from twisted.words.protocols.jabber import xmlstream, jid 27 from twisted.words.protocols.jabber import xmlstream, jid
28 from zope.interface import implements 28 from zope.interface import implementer
29 import time 29 import time
30 30
31 31
32 PLUGIN_INFO = { 32 PLUGIN_INFO = {
33 C.PI_NAME: u"XMPP PING", 33 C.PI_NAME: "XMPP PING",
34 C.PI_IMPORT_NAME: u"XEP-0199", 34 C.PI_IMPORT_NAME: "XEP-0199",
35 C.PI_TYPE: u"XEP", 35 C.PI_TYPE: "XEP",
36 C.PI_PROTOCOLS: [u"XEP-199"], 36 C.PI_PROTOCOLS: ["XEP-199"],
37 C.PI_MAIN: "XEP_0199", 37 C.PI_MAIN: "XEP_0199",
38 C.PI_HANDLER: u"yes", 38 C.PI_HANDLER: "yes",
39 C.PI_DESCRIPTION: D_(u"""Implementation of XMPP Ping"""), 39 C.PI_DESCRIPTION: D_("""Implementation of XMPP Ping"""),
40 } 40 }
41 41
42 NS_PING = u"urn:xmpp:ping" 42 NS_PING = "urn:xmpp:ping"
43 PING_REQUEST = C.IQ_GET + '/ping[@xmlns="' + NS_PING + '"]' 43 PING_REQUEST = C.IQ_GET + '/ping[@xmlns="' + NS_PING + '"]'
44 44
45 45
46 class XEP_0199(object): 46 class XEP_0199(object):
47 47
48 def __init__(self, host): 48 def __init__(self, host):
49 log.info(_("XMPP Ping plugin initialization")) 49 log.info(_("XMPP Ping plugin initialization"))
50 self.host = host 50 self.host = host
51 host.bridge.addMethod( 51 host.bridge.addMethod(
52 "ping", ".plugin", in_sign='ss', out_sign='d', method=self._ping, async=True) 52 "ping", ".plugin", in_sign='ss', out_sign='d', method=self._ping, async_=True)
53 try: 53 try:
54 self.text_cmds = self.host.plugins[C.TEXT_CMDS] 54 self.text_cmds = self.host.plugins[C.TEXT_CMDS]
55 except KeyError: 55 except KeyError:
56 log.info(_(u"Text commands not available")) 56 log.info(_("Text commands not available"))
57 else: 57 else:
58 self.text_cmds.registerTextCommands(self) 58 self.text_cmds.registerTextCommands(self)
59 59
60 def getHandler(self, client): 60 def getHandler(self, client):
61 return XEP_0199_handler(self) 61 return XEP_0199_handler(self)
62 62
63 def _pingRaiseIfFailure(self, pong): 63 def _pingRaiseIfFailure(self, pong):
64 """If ping didn't succeed, raise the failure, else return pong delay""" 64 """If ping didn't succeed, raise the failure, else return pong delay"""
65 if pong[0] != u"PONG": 65 if pong[0] != "PONG":
66 raise pong[0] 66 raise pong[0]
67 return pong[1] 67 return pong[1]
68 68
69 def _ping(self, jid_s, profile): 69 def _ping(self, jid_s, profile):
70 client = self.host.getClient(profile) 70 client = self.host.getClient(profile)
73 d.addCallback(self._pingRaiseIfFailure) 73 d.addCallback(self._pingRaiseIfFailure)
74 return d 74 return d
75 75
76 def _pingCb(self, iq_result, send_time): 76 def _pingCb(self, iq_result, send_time):
77 receive_time = time.time() 77 receive_time = time.time()
78 return (u"PONG", receive_time - send_time) 78 return ("PONG", receive_time - send_time)
79 79
80 def _pingEb(self, failure_, send_time): 80 def _pingEb(self, failure_, send_time):
81 receive_time = time.time() 81 receive_time = time.time()
82 return (failure_.value, receive_time - send_time) 82 return (failure_.value, receive_time - send_time)
83 83
100 100
101 def _cmd_ping_fb(self, pong, client, mess_data): 101 def _cmd_ping_fb(self, pong, client, mess_data):
102 """Send feedback to client when pong data is received""" 102 """Send feedback to client when pong data is received"""
103 txt_cmd = self.host.plugins[C.TEXT_CMDS] 103 txt_cmd = self.host.plugins[C.TEXT_CMDS]
104 104
105 if pong[0] == u"PONG": 105 if pong[0] == "PONG":
106 txt_cmd.feedBack(client, u"PONG ({time} s)".format(time=pong[1]), mess_data) 106 txt_cmd.feedBack(client, "PONG ({time} s)".format(time=pong[1]), mess_data)
107 else: 107 else:
108 txt_cmd.feedBack( 108 txt_cmd.feedBack(
109 client, _(u"ping error ({err_msg}). Response time: {time} s") 109 client, _("ping error ({err_msg}). Response time: {time} s")
110 .format(err_msg=pong[0], time=pong[1]), mess_data) 110 .format(err_msg=pong[0], time=pong[1]), mess_data)
111 111
112 def cmd_ping(self, client, mess_data): 112 def cmd_ping(self, client, mess_data):
113 """ping an entity 113 """ping an entity
114 114
118 if mess_data["unparsed"].strip(): 118 if mess_data["unparsed"].strip():
119 try: 119 try:
120 entity_jid = jid.JID(mess_data["unparsed"].strip()) 120 entity_jid = jid.JID(mess_data["unparsed"].strip())
121 except RuntimeError: 121 except RuntimeError:
122 txt_cmd = self.host.plugins[C.TEXT_CMDS] 122 txt_cmd = self.host.plugins[C.TEXT_CMDS]
123 txt_cmd.feedBack(client, _(u'Invalid jid: "{entity_jid}"').format( 123 txt_cmd.feedBack(client, _('Invalid jid: "{entity_jid}"').format(
124 entity_jid=mess_data["unparsed"].strip()), mess_data) 124 entity_jid=mess_data["unparsed"].strip()), mess_data)
125 return False 125 return False
126 else: 126 else:
127 entity_jid = mess_data["to"] 127 entity_jid = mess_data["to"]
128 d = self.ping(client, entity_jid) 128 d = self.ping(client, entity_jid)
129 d.addCallback(self._cmd_ping_fb, client, mess_data) 129 d.addCallback(self._cmd_ping_fb, client, mess_data)
130 130
131 return False 131 return False
132 132
133 def onPingRequest(self, iq_elt, client): 133 def onPingRequest(self, iq_elt, client):
134 log.info(_(u"XMPP PING received from {from_jid} [{profile}]").format( 134 log.info(_("XMPP PING received from {from_jid} [{profile}]").format(
135 from_jid=iq_elt["from"], profile=client.profile)) 135 from_jid=iq_elt["from"], profile=client.profile))
136 iq_elt.handled = True 136 iq_elt.handled = True
137 iq_result_elt = xmlstream.toResponse(iq_elt, "result") 137 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
138 client.send(iq_result_elt) 138 client.send(iq_result_elt)
139 139
140 140
141 @implementer(iwokkel.IDisco)
141 class XEP_0199_handler(xmlstream.XMPPHandler): 142 class XEP_0199_handler(xmlstream.XMPPHandler):
142 implements(iwokkel.IDisco)
143 143
144 def __init__(self, plugin_parent): 144 def __init__(self, plugin_parent):
145 self.plugin_parent = plugin_parent 145 self.plugin_parent = plugin_parent
146 146
147 def connectionInitialized(self): 147 def connectionInitialized(self):