comparison src/plugins/plugin_xep_0096.py @ 223:86d249b6d9b7

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