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