comparison sat/plugins/plugin_xep_0166.py @ 3527:bbf92ef05f38

plugin XEP-0166, XEP-0234: better management of `terminate`: - new `text` argument to specify human readable reason of termination - handling of `FirstError` (used when a error happens in a DeferredList) - handling of `StanzaError` - (XEP-0234): show human readable text of the reason if present
author Goffi <goffi@goffi.org>
date Wed, 05 May 2021 15:37:33 +0200
parents be6d91572633
children 8e7d5796fb23
comparison
equal deleted inserted replaced
3526:e84ffb48acd4 3527:bbf92ef05f38
153 return client.send(iq_elt) 153 return client.send(iq_elt)
154 154
155 def _terminateEb(self, failure_): 155 def _terminateEb(self, failure_):
156 log.warning(_("Error while terminating session: {msg}").format(msg=failure_)) 156 log.warning(_("Error while terminating session: {msg}").format(msg=failure_))
157 157
158 def terminate(self, client, reason, session): 158 def terminate(self, client, reason, session, text=None):
159 """Terminate the session 159 """Terminate the session
160 160
161 send the session-terminate action, and delete the session data 161 send the session-terminate action, and delete the session data
162 @param reason(unicode, list[domish.Element]): if unicode, will be transformed to an element 162 @param reason(unicode, list[domish.Element]): if unicode, will be transformed to an element
163 if a list of element, add them as children of the <reason/> element 163 if a list of element, add them as children of the <reason/> element
170 if isinstance(reason, str): 170 if isinstance(reason, str):
171 reason_elt.addElement(reason) 171 reason_elt.addElement(reason)
172 else: 172 else:
173 for elt in reason: 173 for elt in reason:
174 reason_elt.addChild(elt) 174 reason_elt.addChild(elt)
175 if text is not None:
176 reason_elt.addElement("text", content=text)
175 self._delSession(client, session["id"]) 177 self._delSession(client, session["id"])
176 d = iq_elt.send() 178 d = iq_elt.send()
177 d.addErrback(self._terminateEb) 179 d.addErrback(self._terminateEb)
178 return d 180 return d
179 181
201 @param session(dict): data of the session 203 @param session(dict): data of the session
202 @param request(domsih.Element): jingle request 204 @param request(domsih.Element): jingle request
203 @param client: %(doc_client)s 205 @param client: %(doc_client)s
204 """ 206 """
205 log.warning(f"Error while processing jingle request [{client.profile}]") 207 log.warning(f"Error while processing jingle request [{client.profile}]")
208 if isinstance(failure_.value, defer.FirstError):
209 failure_ = failure_.value.subFailure.value
206 if isinstance(failure_, exceptions.DataError): 210 if isinstance(failure_, exceptions.DataError):
207 return self.sendError(client, "bad-request", session['id'], request) 211 return self.sendError(client, "bad-request", session["id"], request)
212 elif isinstance(failure_, error.StanzaError):
213 return self.terminate(client, self.REASON_FAILED_APPLICATION, session,
214 text=str(failure_))
208 else: 215 else:
209 log.error(f"Unmanaged jingle exception: {failure_}") 216 log.error(f"Unmanaged jingle exception: {failure_}")
210 return self.terminate(client, self.REASON_FAILED_APPLICATION, session) 217 return self.terminate(client, self.REASON_FAILED_APPLICATION, session,
218 text=str(failure_))
211 219
212 ## methods used by other plugins ## 220 ## methods used by other plugins ##
213 221
214 def registerApplication(self, namespace, handler): 222 def registerApplication(self, namespace, handler):
215 """Register an application plugin 223 """Register an application plugin