Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0095.py @ 428:a4a9efadabfc
core: fixed memory initialisation sequence
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 11 Nov 2011 19:49:00 +0100 |
parents | cb0285372818 |
children | cf005701624b |
rev | line source |
---|---|
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 SAT plugin for managing xep-0095 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU General Public License for more details. |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from logging import debug, info, error |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.xish import domish |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.internet import protocol |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.words.protocols.jabber import client, jid |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.protocols.jabber import error as jab_error |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 import os.path |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from twisted.internet import reactor |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 import uuid |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from zope.interface import implements |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 try: |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 from twisted.words.protocols.xmlstream import XMPPHandler |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 except ImportError: |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 from wokkel.subprotocols import XMPPHandler |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 from wokkel import disco, iwokkel |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 IQ_SET = '/iq[@type="set"]' |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 NS_SI = 'http://jabber.org/protocol/si' |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 SI_REQUEST = IQ_SET + '/si[@xmlns="' + NS_SI + '"]' |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 SI_PROFILE_HEADER = "http://jabber.org/protocol/si/profile/" |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 PLUGIN_INFO = { |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 "name": "XEP 0095 Plugin", |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 "import_name": "XEP-0095", |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 "type": "XEP", |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 "protocols": ["XEP-0095"], |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 "main": "XEP_0095", |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 "handler": "yes", |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 "description": _("""Implementation of Stream Initiation""") |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 } |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 class XEP_0095(): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def __init__(self, host): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 info(_("Plugin XEP_0095 initialization")) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 self.host = host |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 self.si_profiles = {} #key: SI profile, value: callback |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 def getHandler(self, profile): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 return XEP_0095_handler(self) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 def registerSIProfile(self, si_profile, callback): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 """Add a callback for a SI Profile |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 param si_profile: SI profile name (e.g. file-transfer) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 param callback: method to call when the profile name is asked""" |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 self.si_profiles[si_profile] = callback |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 def streamInit(self, iq_el, profile): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 """This method is called on stream initiation (XEP-0095 #3.2) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 @param iq_el: IQ element |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 @param profile: %(doc_profile)s""" |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 info (_("XEP-0095 Stream initiation")) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 iq_el.handled=True |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 si_el = iq_el.firstChildElement() |
398 | 78 si_id = si_el.getAttribute('id') |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 si_mime_type = iq_el.getAttribute('mime-type', 'application/octet-stream') |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 si_profile = si_el.getAttribute('profile') |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 si_profile_key = si_profile[len(SI_PROFILE_HEADER):] if si_profile.startswith(SI_PROFILE_HEADER) else si_profile |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 if self.si_profiles.has_key(si_profile_key): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 #We know this SI profile, we call the callback |
398 | 84 self.si_profiles[si_profile_key](iq_el['id'], iq_el['from'], si_id, si_mime_type, si_el, profile) |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 else: |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 #We don't know this profile, we send an error |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 self.sendBadProfileError(iq_el['id'], iq_el['from'], profile) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
89 def sendRejectedError(self, iq_id, to_jid, reason = 'Offer Declined', profile='@NONE@'): |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 """Helper method to send when the stream is rejected |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
91 @param iq_id: IQ id |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 @param to_jid: recipient |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 @param reason: human readable reason (string) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 @param profile: %(doc_profile)s""" |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
95 self.sendError(iq_id, to_jid, 403, 'cancel', {'text':reason}, profile=profile) |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
97 def sendBadProfileError(self, iq_id, to_jid, profile): |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 """Helper method to send when we don't know the SI profile |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
99 @param iq_id: IQ id |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 @param to_jid: recipient |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 @param profile: %(doc_profile)s""" |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
102 self.sendError(iq_id, to_jid, 400, 'modify', profile=profile) |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
104 def sendBadRequestError(self, iq_id, to_jid, profile): |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 """Helper method to send when we don't know the SI profile |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
106 @param iq_id: IQ id |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 @param to_jid: recipient |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 @param profile: %(doc_profile)s""" |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
109 self.sendError(iq_id, to_jid, 400, 'cancel', profile=profile) |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
111 def sendFailedError(self, iq_id, to_jid, profile): |
391 | 112 """Helper method to send when we transfer failed |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
113 @param iq_id: IQ id |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 @param to_jid: recipient |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 @param profile: %(doc_profile)s""" |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
116 self.sendError(iq_id, to_jid, 500, 'cancel', {'custom':'failed'}, profile=profile) #as there is no error code for failed transfer, we use 500 (undefined-condition) |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
118 def sendError(self, iq_id, to_jid, err_code, err_type='cancel', data={}, profile='@NONE@'): |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 """Send IQ error as a result |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
120 @param iq_id: IQ id |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 @param to_jid: recipient |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 @param err_code: error err_code (see XEP-0095 #4.2) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 @param err_type: one of cancel, modify |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 @param data: error specific data (dictionary) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 @param profile: %(doc_profile)s |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 """ |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 client = self.host.getClient(profile) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 assert(client) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 result = domish.Element(('', 'iq')) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 result['type'] = 'result' |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
131 result['id'] = iq_id |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 result['to'] = to_jid |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 error_el = result.addElement('error') |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 error_el['err_code'] = str(err_code) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 error_el['type'] = err_type |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 if err_code==400 and err_type=='cancel': |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','bad-request')) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 error_el.addElement((NS_SI,'no-valid-streams')) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 elif err_code==400 and err_type=='modify': |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','bad-request')) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 error_el.addElement((NS_SI,'bad-profile')) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 elif err_code==403 and err_type=='cancel': |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','forbidden')) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 if data.has_key('text'): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','text'), content=data['text']) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 elif err_code==500 and err_type=='cancel': |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 condition_el = error_el.addElement((NS_SI,'undefined-condition')) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 if data.has_key('custom') and data['custom']=='failed': |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 condition_el.addContent('Stream failed') |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 client.xmlstream.send(result) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
153 def acceptStream(self, iq_id, to_jid, feature_elt, misc_elts=[], profile='@NONE@'): |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 """Send the accept stream initiation answer |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
155 @param iq_id: IQ id |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 @param feature_elt: domish element 'feature' containing stream method to use |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 @param misc_elts: list of domish element to add |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 @param profile: %(doc_profile)s""" |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 client = self.host.getClient(profile) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 assert(client) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 info (_("sending stream initiation accept answer")) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 result = domish.Element(('', 'iq')) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 result['type'] = 'result' |
394
8f3551ceee17
plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents:
391
diff
changeset
|
164 result['id'] = iq_id |
385
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 result['to'] = to_jid |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 si = result.addElement('si', NS_SI) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 si.addChild(feature_elt) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 for elt in misc_elts: |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 si.addChild(elt) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 client.xmlstream.send(result) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 def proposeStream(self, to_jid, si_profile, feature_elt, misc_elts, mime_type='application/octet-stream', profile_key='@NONE@'): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 """Propose a stream initiation |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 @param to_jid: recipient (JID) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 @param si_profile: Stream initiation profile (XEP-0095) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 @param feature_elt: feature domish element, according to XEP-0020 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 @param misc_elts: list of domish element to add for this profile |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 @param mime_type: stream mime type |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 @param profile: %(doc_profile)s |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 @return: session id, offer""" |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 current_jid, xmlstream = self.host.getJidNStream(profile_key) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 if not xmlstream: |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 error (_('Asking for an non-existant or not connected profile')) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 return "" |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 offer = client.IQ(xmlstream,'set') |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 sid = str(uuid.uuid4()) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 debug (_("Stream Session ID: %s") % offer["id"]) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 offer["from"] = current_jid.full() |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 offer["to"] = to_jid.full() |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 si=offer.addElement('si',NS_SI) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 si['id'] = sid |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 si["mime-type"] = mime_type |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 si["profile"] = si_profile |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 for elt in misc_elts: |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 si.addChild(elt) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 si.addChild(feature_elt) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 offer.send() |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 return sid, offer |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 class XEP_0095_handler(XMPPHandler): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 implements(iwokkel.IDisco) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 def __init__(self, plugin_parent): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 self.plugin_parent = plugin_parent |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 self.host = plugin_parent.host |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 def connectionInitialized(self): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 self.xmlstream.addObserver(SI_REQUEST, self.plugin_parent.streamInit, profile = self.parent.profile) |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 return [disco.DiscoFeature(NS_SI)] + [disco.DiscoFeature("http://jabber.org/protocol/si/profile/%s" % profile_name) for profile_name in self.plugin_parent.si_profiles] |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 def getDiscoItems(self, requestor, target, nodeIdentifier=''): |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 return [] |
41fdaeb005bc
plugins: Stream initiation (xep-0095) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 |