diff src/plugins/plugin_xep_0050.py @ 758:86224a13cc1d

plugin xep-0050: using new Sessions class
author Goffi <goffi@goffi.org>
date Tue, 24 Dec 2013 15:19:08 +0100
parents 7f98f53f6997
children ab851b46009c
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0050.py	Tue Dec 24 15:19:08 2013 +0100
+++ b/src/plugins/plugin_xep_0050.py	Tue Dec 24 15:19:08 2013 +0100
@@ -24,6 +24,7 @@
 from twisted.internet import defer, reactor
 from wokkel import disco, iwokkel, data_form
 from sat.core import exceptions
+from sat.memory.memory import Sessions
 from uuid import uuid4
 
 from zope.interface import implements
@@ -82,10 +83,9 @@
         self.label = label
         self.node = node
         self.features = [disco.DiscoFeature(feature) for feature in features]
-        self.timeout = timeout
         self.allowed_jids, self.allowed_groups, self.allowed_magics, self.forbidden_jids, self.forbidden_groups = allowed_jids, allowed_groups, allowed_magics, forbidden_jids, forbidden_groups
         self.client = client
-        self.sessions = {}
+        self.sessions = Sessions(timeout=timeout)
 
     def getName(self, xml_lang=None):
         return self.label
@@ -155,9 +155,7 @@
 
         self.client.xmlstream.send(result)
         if status in (XEP_0050.STATUS.COMPLETED, XEP_0050.STATUS.CANCELED):
-            timer = self.sessions[session_id][0]
-            timer.cancel()
-            self._purgeSession(session_id)
+            del self.sessions[session_id]
 
     def _sendError(self, error_constant, session_id, request):
         """ Send error stanza
@@ -170,14 +168,6 @@
             error_elt = iq_elt.elements(None, "error").next()
             error_elt.addElement(cmd_condition, NS_COMMANDS)
         self.client.xmlstream.send(iq_elt)
-        try:
-            timer = self.sessions[session_id][0]
-            timer.cancel()
-            self._purgeSession(session_id)
-        except KeyError:
-            pass
-
-    def _purgeSession(self, session_id):
         del self.sessions[session_id]
 
     def onRequest(self, command_elt, requestor, action, session_id):
@@ -185,18 +175,14 @@
             return self._sendError(XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent)
         if session_id:
             try:
-                timer, session_data = self.sessions[session_id]
+                session_data = self.sessions[session_id]
             except KeyError:
                 return self._sendError(XEP_0050.ERROR.SESSION_EXPIRED, session_id, command_elt.parent)
             if session_data['requestor'] != requestor:
                 return self._sendError(XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent)
-            timer.reset(self.timeout)
         else:
-            # we are starting a new session
-            session_id = str(uuid4())
-            session_data  = {'requestor': requestor}
-            timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
-            self.sessions[session_id] = (timer, session_data)
+            session_id, session_data = self.sessions.newSession()
+            session_data['requestor'] = requestor
         if action == XEP_0050.ACTION.CANCEL:
             d = defer.succeed((None, XEP_0050.STATUS.CANCELED, None, None))
         else: