comparison 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
comparison
equal deleted inserted replaced
757:bbe55c7bee43 758:86224a13cc1d
22 from twisted.words.protocols.jabber import error as xmpp_error 22 from twisted.words.protocols.jabber import error as xmpp_error
23 from twisted.words.xish import domish 23 from twisted.words.xish import domish
24 from twisted.internet import defer, reactor 24 from twisted.internet import defer, reactor
25 from wokkel import disco, iwokkel, data_form 25 from wokkel import disco, iwokkel, data_form
26 from sat.core import exceptions 26 from sat.core import exceptions
27 from sat.memory.memory import Sessions
27 from uuid import uuid4 28 from uuid import uuid4
28 29
29 from zope.interface import implements 30 from zope.interface import implements
30 31
31 try: 32 try:
80 self.parent = parent 81 self.parent = parent
81 self.callback = callback 82 self.callback = callback
82 self.label = label 83 self.label = label
83 self.node = node 84 self.node = node
84 self.features = [disco.DiscoFeature(feature) for feature in features] 85 self.features = [disco.DiscoFeature(feature) for feature in features]
85 self.timeout = timeout
86 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 86 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
87 self.client = client 87 self.client = client
88 self.sessions = {} 88 self.sessions = Sessions(timeout=timeout)
89 89
90 def getName(self, xml_lang=None): 90 def getName(self, xml_lang=None):
91 return self.label 91 return self.label
92 92
93 def isAuthorised(self, requestor): 93 def isAuthorised(self, requestor):
153 if payload is not None: 153 if payload is not None:
154 command_elt.addChild(payload) 154 command_elt.addChild(payload)
155 155
156 self.client.xmlstream.send(result) 156 self.client.xmlstream.send(result)
157 if status in (XEP_0050.STATUS.COMPLETED, XEP_0050.STATUS.CANCELED): 157 if status in (XEP_0050.STATUS.COMPLETED, XEP_0050.STATUS.CANCELED):
158 timer = self.sessions[session_id][0] 158 del self.sessions[session_id]
159 timer.cancel()
160 self._purgeSession(session_id)
161 159
162 def _sendError(self, error_constant, session_id, request): 160 def _sendError(self, error_constant, session_id, request):
163 """ Send error stanza 161 """ Send error stanza
164 @param error_constant: one of XEP_OO50.ERROR 162 @param error_constant: one of XEP_OO50.ERROR
165 @param request: original request (domish.Element) 163 @param request: original request (domish.Element)
168 iq_elt = xmpp_error.StanzaError(xmpp_condition).toResponse(request) 166 iq_elt = xmpp_error.StanzaError(xmpp_condition).toResponse(request)
169 if cmd_condition: 167 if cmd_condition:
170 error_elt = iq_elt.elements(None, "error").next() 168 error_elt = iq_elt.elements(None, "error").next()
171 error_elt.addElement(cmd_condition, NS_COMMANDS) 169 error_elt.addElement(cmd_condition, NS_COMMANDS)
172 self.client.xmlstream.send(iq_elt) 170 self.client.xmlstream.send(iq_elt)
173 try:
174 timer = self.sessions[session_id][0]
175 timer.cancel()
176 self._purgeSession(session_id)
177 except KeyError:
178 pass
179
180 def _purgeSession(self, session_id):
181 del self.sessions[session_id] 171 del self.sessions[session_id]
182 172
183 def onRequest(self, command_elt, requestor, action, session_id): 173 def onRequest(self, command_elt, requestor, action, session_id):
184 if not self.isAuthorised(requestor): 174 if not self.isAuthorised(requestor):
185 return self._sendError(XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent) 175 return self._sendError(XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent)
186 if session_id: 176 if session_id:
187 try: 177 try:
188 timer, session_data = self.sessions[session_id] 178 session_data = self.sessions[session_id]
189 except KeyError: 179 except KeyError:
190 return self._sendError(XEP_0050.ERROR.SESSION_EXPIRED, session_id, command_elt.parent) 180 return self._sendError(XEP_0050.ERROR.SESSION_EXPIRED, session_id, command_elt.parent)
191 if session_data['requestor'] != requestor: 181 if session_data['requestor'] != requestor:
192 return self._sendError(XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent) 182 return self._sendError(XEP_0050.ERROR.FORBIDDEN, session_id, command_elt.parent)
193 timer.reset(self.timeout)
194 else: 183 else:
195 # we are starting a new session 184 session_id, session_data = self.sessions.newSession()
196 session_id = str(uuid4()) 185 session_data['requestor'] = requestor
197 session_data = {'requestor': requestor}
198 timer = reactor.callLater(self.timeout, self._purgeSession, session_id)
199 self.sessions[session_id] = (timer, session_data)
200 if action == XEP_0050.ACTION.CANCEL: 186 if action == XEP_0050.ACTION.CANCEL:
201 d = defer.succeed((None, XEP_0050.STATUS.CANCELED, None, None)) 187 d = defer.succeed((None, XEP_0050.STATUS.CANCELED, None, None))
202 else: 188 else:
203 d = defer.maybeDeferred(self.callback, command_elt, session_data, action, self.node, self.client.profile) 189 d = defer.maybeDeferred(self.callback, command_elt, session_data, action, self.node, self.client.profile)
204 d.addCallback(self._sendAnswer, session_id, command_elt.parent) 190 d.addCallback(self._sendAnswer, session_id, command_elt.parent)