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