Mercurial > libervia-backend
annotate plugins/plugin_xep_0096.py @ 52:6455fb62ff83
Connection/disconnection signals
- wix, sortilege: management of this new signals
/!\ sortilege is bugged, the contact list window is not updated correctly
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 07 Jan 2010 01:55:30 +1100 |
parents | 4392f1fdb064 |
children | a5b5fb5fc9fd |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for managing xep-0096 | |
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from logging import debug, info, error | |
23 from twisted.words.xish import domish | |
24 from twisted.internet import protocol | |
39
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
25 from twisted.words.protocols.jabber import client, jid, xmlstream |
2e3411a6baad
Wix: external server management in gateways manager, SàT: bug fixes in gateway management
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
26 from twisted.words.protocols.jabber import error as jab_error |
0 | 27 import os.path |
28 from twisted.internet import reactor #FIXME best way ??? | |
1 | 29 import pdb |
0 | 30 |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
31 from zope.interface import implements |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
32 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
33 try: |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
34 from twisted.words.protocols.xmlstream import XMPPHandler |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
35 except ImportError: |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
36 from wokkel.subprotocols import XMPPHandler |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
37 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
38 from wokkel import disco, iwokkel |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
39 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
40 IQ_SET = '/iq[@type="set"]' |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
41 NS_SI = 'http://jabber.org/protocol/si' |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
42 SI_REQUEST = IQ_SET + '/si[@xmlns="' + NS_SI + '"]' |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
43 |
0 | 44 PLUGIN_INFO = { |
45 "name": "XEP 0096 Plugin", | |
46 "import_name": "XEP_0096", | |
47 "type": "XEP", | |
48 | 48 "protocols": ["XEP-0096"], |
0 | 49 "dependencies": ["XEP_0065"], |
50 "main": "XEP_0096", | |
51 "description": """Implementation of SI File Transfert""" | |
52 } | |
53 | |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
54 class XEP_0096(XMPPHandler): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
55 implements(iwokkel.IDisco) |
19
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
56 |
0 | 57 def __init__(self, host): |
58 info("Plugin XEP_0096 initialization") | |
59 self.host = host | |
60 self._waiting_for_approval = {} | |
7
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
61 host.bridge.addMethod("sendFile", ".communication", in_sign='ss', out_sign='s', method=self.sendFile) |
0 | 62 |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
63 def connectionInitialized(self): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
64 self.xmlstream.addObserver(SI_REQUEST, self.xep_96) |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
65 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
66 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
67 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
68 return [disco.DiscoFeature(NS_SI)] |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
69 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
70 def getDiscoItems(self, requestor, target, nodeIdentifier=''): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
71 return [] |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
72 |
0 | 73 def xep_96(self, IQ): |
74 info ("XEP-0096 management") | |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
75 IQ.handled=True |
0 | 76 SI_elem = IQ.firstChildElement() |
77 debug(SI_elem.toXml()) | |
78 filename = "" | |
79 file_size = "" | |
80 for element in SI_elem.elements(): | |
81 if element.name == "file": | |
82 info ("File proposed: name=[%s] size=%s", element['name'], element['size']) | |
83 filename = element["name"] | |
84 file_size = element["size"] | |
85 elif element.name == "feature": | |
86 from_jid = IQ["from"] | |
87 self._waiting_for_approval[IQ["id"]] = (element, from_jid, file_size) | |
88 data={ "filename":filename, "from":from_jid, "size":file_size } | |
89 self.host.askConfirmation(IQ["id"], "FILE_TRANSFERT", data, self.confirmationCB) | |
90 | |
91 def confirmationCB(self, id, accepted, data): | |
92 """Called on confirmation answer""" | |
93 if accepted: | |
94 data['size'] = self._waiting_for_approval[id][2] | |
95 self.host.plugins["XEP_0065"].setData(data, id) | |
96 self.approved(id) | |
97 else: | |
98 debug ("Transfert [%s] refused", id) | |
99 del(self._waiting_for_approval[id]) | |
100 | |
101 def approved(self, id): | |
102 """must be called when a file transfert has be accepted by client""" | |
103 debug ("Transfert [%s] accepted", id) | |
104 | |
105 if ( not self._waiting_for_approval.has_key(id) ): | |
106 error ("Approved unknow id !") | |
107 #TODO: manage this (maybe approved by several frontends) | |
108 else: | |
109 element, from_id, size = self._waiting_for_approval[id] | |
110 del(self._waiting_for_approval[id]) | |
111 self.negociate(element, id, from_id) | |
112 | |
113 def negociate(self, feat_elem, id, to_jid): | |
114 #TODO: put this in a plugin | |
115 #FIXME: over ultra mega ugly, need to be generic | |
116 info ("Feature negociation") | |
117 data = feat_elem.firstChildElement() | |
118 field = data.firstChildElement() | |
119 #FIXME: several options ! Q&D code for test only | |
120 option = field.firstChildElement() | |
121 value = option.firstChildElement() | |
122 if unicode(value) == "http://jabber.org/protocol/bytestreams": | |
123 #ugly, as usual, need to be entirely rewritten (just for test !) | |
124 result = domish.Element(('', 'iq')) | |
125 result['type'] = 'result' | |
126 result['id'] = id | |
127 result['to'] = to_jid | |
128 si = result.addElement('si', 'http://jabber.org/protocol/si') | |
129 file = si.addElement('file', 'http://jabber.org/protocol/si/profile/file-transfer') | |
130 feature = si.addElement('feature', 'http://jabber.org/protocol/feature-neg') | |
131 x = feature.addElement('x', 'jabber:x:data') | |
132 x['type'] = 'submit' | |
133 field = x.addElement('field') | |
134 field['var'] = 'stream-method' | |
135 value = field.addElement('value') | |
136 value.addContent('http://jabber.org/protocol/bytestreams') | |
137 self.host.xmlstream.send(result) | |
138 | |
139 def fileCB(self, answer): | |
140 if answer['type']=="result": #FIXME FIXME FIXME ugly ugly ugly ! and temp FIXME FIXME FIXME | |
141 info("SENDING UGLY ANSWER") | |
142 offer=client.IQ(self.host.xmlstream,'set') | |
143 offer["from"]=self.host.me.full() | |
144 offer["to"]=answer['from'] | |
145 query=offer.addElement('query', 'http://jabber.org/protocol/bytestreams') | |
146 query['mode']='tcp' | |
147 streamhost=query.addElement('streamhost') | |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
148 streamhost['host']=self.host.memory.getParamA("IP", "File Transfert") |
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
149 streamhost['port']=self.host.memory.getParamA("Port", "File Transfert") |
0 | 150 streamhost['jid']=self.host.me.full() |
151 offer.send() | |
152 | |
153 | |
154 | |
155 | |
156 def sendFile(self, to, filepath): | |
157 """send a file using XEP-0096 | |
158 Return an unique id to identify the transfert | |
159 """ | |
160 debug ("sendfile (%s) to %s", filepath, to ) | |
161 print type(filepath), type(to) | |
162 | |
163 statinfo = os.stat(filepath) | |
164 | |
165 offer=client.IQ(self.host.xmlstream,'set') | |
166 debug ("Transfert ID: %s", offer["id"]) | |
167 | |
168 self.host.plugins["XEP_0065"].sendFile(offer["id"], filepath, str(statinfo.st_size)) | |
169 | |
170 offer["from"]=self.host.me.full() | |
171 offer["to"]=jid.JID(to).full() | |
172 si=offer.addElement('si','http://jabber.org/protocol/si') | |
173 si["mime-type"]='text/plain' | |
174 si["profile"]='http://jabber.org/protocol/si/profile/file-transfer' | |
8 | 175 file = si.addElement('file', 'http://jabber.org/protocol/si/profile/file-transfer') |
0 | 176 file['name']=os.path.basename(filepath) |
177 file['size']=str(statinfo.st_size) | |
178 | |
179 ### | |
180 # FIXME: Ugly temporary hard coded implementation of XEP-0020 & XEP-0004, | |
181 # Need to be recoded elsewhere in a more generic way | |
182 ### | |
183 | |
184 feature=si.addElement('feature', "http://jabber.org/protocol/feature-neg") | |
185 x=feature.addElement('x', "jabber:x:data") | |
186 x['type']='form' | |
187 field=x.addElement('field') | |
188 field['type']='list-single' | |
189 field['var']='stream-method' | |
190 option = field.addElement('option') | |
191 value = option.addElement('value', content='http://jabber.org/protocol/bytestreams') | |
192 | |
193 offer.addCallback(self.fileCB) | |
194 offer.send() | |
195 return offer["id"] #XXX: using IQ id as file transfert id seems OK as IQ id are required | |
196 |