Mercurial > libervia-backend
annotate plugins/plugin_xep_0096.py @ 40:6f0699ba0329
plugin XEP-0077: minor log fix
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 19 Dec 2009 18:13:04 +1100 |
parents | 2e3411a6baad |
children | 4392f1fdb064 |
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 "dependencies": ["XEP_0065"], | |
49 "main": "XEP_0096", | |
50 "description": """Implementation of SI File Transfert""" | |
51 } | |
52 | |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
53 class XEP_0096(XMPPHandler): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
54 implements(iwokkel.IDisco) |
19
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
55 |
0 | 56 def __init__(self, host): |
57 info("Plugin XEP_0096 initialization") | |
58 self.host = host | |
59 self._waiting_for_approval = {} | |
7
c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
60 host.bridge.addMethod("sendFile", ".communication", in_sign='ss', out_sign='s', method=self.sendFile) |
0 | 61 |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
62 def connectionInitialized(self): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
63 self.xmlstream.addObserver(SI_REQUEST, self.xep_96) |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
64 |
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 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
67 return [disco.DiscoFeature(NS_SI)] |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
68 |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
69 def getDiscoItems(self, requestor, target, nodeIdentifier=''): |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
70 return [] |
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
71 |
0 | 72 def xep_96(self, IQ): |
73 info ("XEP-0096 management") | |
15
218ec9984fa5
wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
74 IQ.handled=True |
0 | 75 SI_elem = IQ.firstChildElement() |
76 debug(SI_elem.toXml()) | |
77 filename = "" | |
78 file_size = "" | |
79 for element in SI_elem.elements(): | |
80 if element.name == "file": | |
81 info ("File proposed: name=[%s] size=%s", element['name'], element['size']) | |
82 filename = element["name"] | |
83 file_size = element["size"] | |
84 elif element.name == "feature": | |
85 from_jid = IQ["from"] | |
86 self._waiting_for_approval[IQ["id"]] = (element, from_jid, file_size) | |
87 data={ "filename":filename, "from":from_jid, "size":file_size } | |
88 self.host.askConfirmation(IQ["id"], "FILE_TRANSFERT", data, self.confirmationCB) | |
89 | |
90 def confirmationCB(self, id, accepted, data): | |
91 """Called on confirmation answer""" | |
92 if accepted: | |
93 data['size'] = self._waiting_for_approval[id][2] | |
94 self.host.plugins["XEP_0065"].setData(data, id) | |
95 self.approved(id) | |
96 else: | |
97 debug ("Transfert [%s] refused", id) | |
98 del(self._waiting_for_approval[id]) | |
99 | |
100 def approved(self, id): | |
101 """must be called when a file transfert has be accepted by client""" | |
102 debug ("Transfert [%s] accepted", id) | |
103 | |
104 if ( not self._waiting_for_approval.has_key(id) ): | |
105 error ("Approved unknow id !") | |
106 #TODO: manage this (maybe approved by several frontends) | |
107 else: | |
108 element, from_id, size = self._waiting_for_approval[id] | |
109 del(self._waiting_for_approval[id]) | |
110 self.negociate(element, id, from_id) | |
111 | |
112 def negociate(self, feat_elem, id, to_jid): | |
113 #TODO: put this in a plugin | |
114 #FIXME: over ultra mega ugly, need to be generic | |
115 info ("Feature negociation") | |
116 data = feat_elem.firstChildElement() | |
117 field = data.firstChildElement() | |
118 #FIXME: several options ! Q&D code for test only | |
119 option = field.firstChildElement() | |
120 value = option.firstChildElement() | |
121 if unicode(value) == "http://jabber.org/protocol/bytestreams": | |
122 #ugly, as usual, need to be entirely rewritten (just for test !) | |
123 result = domish.Element(('', 'iq')) | |
124 result['type'] = 'result' | |
125 result['id'] = id | |
126 result['to'] = to_jid | |
127 si = result.addElement('si', 'http://jabber.org/protocol/si') | |
128 file = si.addElement('file', 'http://jabber.org/protocol/si/profile/file-transfer') | |
129 feature = si.addElement('feature', 'http://jabber.org/protocol/feature-neg') | |
130 x = feature.addElement('x', 'jabber:x:data') | |
131 x['type'] = 'submit' | |
132 field = x.addElement('field') | |
133 field['var'] = 'stream-method' | |
134 value = field.addElement('value') | |
135 value.addContent('http://jabber.org/protocol/bytestreams') | |
136 self.host.xmlstream.send(result) | |
137 | |
138 def fileCB(self, answer): | |
139 if answer['type']=="result": #FIXME FIXME FIXME ugly ugly ugly ! and temp FIXME FIXME FIXME | |
140 info("SENDING UGLY ANSWER") | |
141 offer=client.IQ(self.host.xmlstream,'set') | |
142 offer["from"]=self.host.me.full() | |
143 offer["to"]=answer['from'] | |
144 query=offer.addElement('query', 'http://jabber.org/protocol/bytestreams') | |
145 query['mode']='tcp' | |
146 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
|
147 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
|
148 streamhost['port']=self.host.memory.getParamA("Port", "File Transfert") |
0 | 149 streamhost['jid']=self.host.me.full() |
150 offer.send() | |
151 | |
152 | |
153 | |
154 | |
155 def sendFile(self, to, filepath): | |
156 """send a file using XEP-0096 | |
157 Return an unique id to identify the transfert | |
158 """ | |
159 debug ("sendfile (%s) to %s", filepath, to ) | |
160 print type(filepath), type(to) | |
161 | |
162 statinfo = os.stat(filepath) | |
163 | |
164 offer=client.IQ(self.host.xmlstream,'set') | |
165 debug ("Transfert ID: %s", offer["id"]) | |
166 | |
167 self.host.plugins["XEP_0065"].sendFile(offer["id"], filepath, str(statinfo.st_size)) | |
168 | |
169 offer["from"]=self.host.me.full() | |
170 offer["to"]=jid.JID(to).full() | |
171 si=offer.addElement('si','http://jabber.org/protocol/si') | |
172 si["mime-type"]='text/plain' | |
173 si["profile"]='http://jabber.org/protocol/si/profile/file-transfer' | |
8 | 174 file = si.addElement('file', 'http://jabber.org/protocol/si/profile/file-transfer') |
0 | 175 file['name']=os.path.basename(filepath) |
176 file['size']=str(statinfo.st_size) | |
177 | |
178 ### | |
179 # FIXME: Ugly temporary hard coded implementation of XEP-0020 & XEP-0004, | |
180 # Need to be recoded elsewhere in a more generic way | |
181 ### | |
182 | |
183 feature=si.addElement('feature', "http://jabber.org/protocol/feature-neg") | |
184 x=feature.addElement('x', "jabber:x:data") | |
185 x['type']='form' | |
186 field=x.addElement('field') | |
187 field['type']='list-single' | |
188 field['var']='stream-method' | |
189 option = field.addElement('option') | |
190 value = option.addElement('value', content='http://jabber.org/protocol/bytestreams') | |
191 | |
192 offer.addCallback(self.fileCB) | |
193 offer.send() | |
194 return offer["id"] #XXX: using IQ id as file transfert id seems OK as IQ id are required | |
195 |