plugin xep-0050: using new Sessions class
This commit is contained in:
parent
3f236bbb32
commit
aa8ca0772c
1 changed files with 6 additions and 20 deletions
|
|
@ -24,6 +24,7 @@ from twisted.words.xish import domish
|
|||
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 @@ class AdHocCommand(XMPPHandler):
|
|||
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 @@ class AdHocCommand(XMPPHandler):
|
|||
|
||||
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 @@ class AdHocCommand(XMPPHandler):
|
|||
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 @@ class AdHocCommand(XMPPHandler):
|
|||
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:
|
||||
|
|
|
|||
Loading…
Reference in a new issue