changeset 3401:5c97ad3d1653

plugin XEP-0065: some cleaning: - reordered imports and removed useless try/except - log warning before sending error - avoid crash if connection is lost before session has has been received in server mode. - fixed missing string formatting keywords - renamed incorrect attribute and show explicitly that it's not used
author Goffi <goffi@goffi.org>
date Thu, 12 Nov 2020 14:53:15 +0100
parents 77ad87763f32
children 08a3e34aead1
files sat/plugins/plugin_xep_0065.py
diffstat 1 files changed, 22 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0065.py	Thu Nov 12 14:53:15 2020 +0100
+++ b/sat/plugins/plugin_xep_0065.py	Thu Nov 12 14:53:15 2020 +0100
@@ -54,13 +54,11 @@
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 
-from sat.core.i18n import _
-from sat.core.log import getLogger
-
-log = getLogger(__name__)
-from sat.core.constants import Const as C
-from sat.core import exceptions
-from sat.tools import sat_defer
+import struct
+import hashlib
+import uuid
+from collections import namedtuple
+from zope.interface import implementer
 from twisted.internet import protocol
 from twisted.internet import reactor
 from twisted.internet import error as internet_error
@@ -68,19 +66,15 @@
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber import xmlstream
 from twisted.internet import defer
-from collections import namedtuple
-import struct
-import hashlib
-import uuid
-
-from zope.interface import implementer
+from wokkel import disco, iwokkel
+from sat.core.i18n import _
+from sat.core.log import getLogger
+from sat.core.constants import Const as C
+from sat.core import exceptions
+from sat.tools import sat_defer
 
-try:
-    from twisted.words.protocols.xmlstream import XMPPHandler
-except ImportError:
-    from wokkel.subprotocols import XMPPHandler
 
-from wokkel import disco, iwokkel
+log = getLogger(__name__)
 
 
 PLUGIN_INFO = {
@@ -508,12 +502,12 @@
     def connectRequested(self, addr, port):
         # Check that this session is expected
         if not self.factory.addToSession(addr.decode('utf-8'), self):
-            self.sendErrorReply(REPLY_CONN_REFUSED)
             log.warning(
                 "Unexpected connection request received from {host}".format(
                     host=self.transport.getPeer().host
                 )
             )
+            self.sendErrorReply(REPLY_CONN_REFUSED)
             return
         self._session_hash = addr.decode('utf-8')
         self.connectCompleted(addr, 0)
@@ -592,7 +586,12 @@
         if self.state != STATE_READY:
             self.connection.errback(reason)
         if self.server_mode:
-            self.factory.removeFromSession(self._session_hash, self, reason)
+            try:
+                session_hash = self._session_hash
+            except AttributeError:
+                log.debug("no session has been received yet")
+            else:
+                self.factory.removeFromSession(session_hash, self, reason)
 
 
 class Socks5ServerFactory(protocol.ServerFactory):
@@ -822,7 +821,7 @@
         except jabber_error.StanzaError as failure:
             log.warning(
                 "Error while requesting proxy info on {jid}: {error}".format(
-                    proxy.full(), failure
+                    jid=proxy.full(), error=failure
                 )
             )
             notFound(server)
@@ -1057,7 +1056,7 @@
                 )
             candidates[candidates.index(candidate)] = None
 
-        def allTested(self):
+        def allTested(__):
             log.debug("All candidates have been tested")
             good_candidates = [c for c in candidates if c]
             return good_candidates[0] if good_candidates else None
@@ -1379,7 +1378,7 @@
 
 
 @implementer(iwokkel.IDisco)
-class XEP_0065_handler(XMPPHandler):
+class XEP_0065_handler(xmlstream.XMPPHandler):
 
     def __init__(self, plugin_parent):
         self.plugin_parent = plugin_parent