annotate plugins/plugin_xep_0096.py @ 19:f2a745ca0fbc

refactoring: using xml params part III (parameters import)
author Goffi <goffi@goffi.org>
date Sat, 07 Nov 2009 21:23:28 +0100
parents 218ec9984fa5
children bb72c29f3432
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #!/usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
goffi@necton2
parents:
diff changeset
4 """
goffi@necton2
parents:
diff changeset
5 SAT plugin for managing xep-0096
goffi@necton2
parents:
diff changeset
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
goffi@necton2
parents:
diff changeset
7
goffi@necton2
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
goffi@necton2
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
goffi@necton2
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
goffi@necton2
parents:
diff changeset
11 (at your option) any later version.
goffi@necton2
parents:
diff changeset
12
goffi@necton2
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
goffi@necton2
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
goffi@necton2
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
goffi@necton2
parents:
diff changeset
16 GNU General Public License for more details.
goffi@necton2
parents:
diff changeset
17
goffi@necton2
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
goffi@necton2
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
goffi@necton2
parents:
diff changeset
20 """
goffi@necton2
parents:
diff changeset
21
goffi@necton2
parents:
diff changeset
22 from logging import debug, info, error
goffi@necton2
parents:
diff changeset
23 from twisted.words.xish import domish
goffi@necton2
parents:
diff changeset
24 from twisted.internet import protocol
goffi@necton2
parents:
diff changeset
25 from twisted.words.protocols.jabber import client, jid, xmlstream, error
goffi@necton2
parents:
diff changeset
26 import os.path
goffi@necton2
parents:
diff changeset
27 from twisted.internet import reactor #FIXME best way ???
1
a06a151fc31f Disconnect first draft
Goffi <goffi@goffi.org>
parents: 0
diff changeset
28 import pdb
0
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
43 PLUGIN_INFO = {
goffi@necton2
parents:
diff changeset
44 "name": "XEP 0096 Plugin",
goffi@necton2
parents:
diff changeset
45 "import_name": "XEP_0096",
goffi@necton2
parents:
diff changeset
46 "type": "XEP",
goffi@necton2
parents:
diff changeset
47 "dependencies": ["XEP_0065"],
goffi@necton2
parents:
diff changeset
48 "main": "XEP_0096",
goffi@necton2
parents:
diff changeset
49 "description": """Implementation of SI File Transfert"""
goffi@necton2
parents:
diff changeset
50 }
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
55 def __init__(self, host):
goffi@necton2
parents:
diff changeset
56 info("Plugin XEP_0096 initialization")
goffi@necton2
parents:
diff changeset
57 self.host = host
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
71 def xep_96(self, IQ):
goffi@necton2
parents:
diff changeset
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
goffi@necton2
parents:
diff changeset
74 SI_elem = IQ.firstChildElement()
goffi@necton2
parents:
diff changeset
75 debug(SI_elem.toXml())
goffi@necton2
parents:
diff changeset
76 filename = ""
goffi@necton2
parents:
diff changeset
77 file_size = ""
goffi@necton2
parents:
diff changeset
78 for element in SI_elem.elements():
goffi@necton2
parents:
diff changeset
79 if element.name == "file":
goffi@necton2
parents:
diff changeset
80 info ("File proposed: name=[%s] size=%s", element['name'], element['size'])
goffi@necton2
parents:
diff changeset
81 filename = element["name"]
goffi@necton2
parents:
diff changeset
82 file_size = element["size"]
goffi@necton2
parents:
diff changeset
83 elif element.name == "feature":
goffi@necton2
parents:
diff changeset
84 from_jid = IQ["from"]
goffi@necton2
parents:
diff changeset
85 self._waiting_for_approval[IQ["id"]] = (element, from_jid, file_size)
goffi@necton2
parents:
diff changeset
86 data={ "filename":filename, "from":from_jid, "size":file_size }
goffi@necton2
parents:
diff changeset
87 self.host.askConfirmation(IQ["id"], "FILE_TRANSFERT", data, self.confirmationCB)
goffi@necton2
parents:
diff changeset
88
goffi@necton2
parents:
diff changeset
89 def confirmationCB(self, id, accepted, data):
goffi@necton2
parents:
diff changeset
90 """Called on confirmation answer"""
goffi@necton2
parents:
diff changeset
91 if accepted:
goffi@necton2
parents:
diff changeset
92 data['size'] = self._waiting_for_approval[id][2]
goffi@necton2
parents:
diff changeset
93 self.host.plugins["XEP_0065"].setData(data, id)
goffi@necton2
parents:
diff changeset
94 self.approved(id)
goffi@necton2
parents:
diff changeset
95 else:
goffi@necton2
parents:
diff changeset
96 debug ("Transfert [%s] refused", id)
goffi@necton2
parents:
diff changeset
97 del(self._waiting_for_approval[id])
goffi@necton2
parents:
diff changeset
98
goffi@necton2
parents:
diff changeset
99 def approved(self, id):
goffi@necton2
parents:
diff changeset
100 """must be called when a file transfert has be accepted by client"""
goffi@necton2
parents:
diff changeset
101 debug ("Transfert [%s] accepted", id)
goffi@necton2
parents:
diff changeset
102
goffi@necton2
parents:
diff changeset
103 if ( not self._waiting_for_approval.has_key(id) ):
goffi@necton2
parents:
diff changeset
104 error ("Approved unknow id !")
goffi@necton2
parents:
diff changeset
105 #TODO: manage this (maybe approved by several frontends)
goffi@necton2
parents:
diff changeset
106 else:
goffi@necton2
parents:
diff changeset
107 element, from_id, size = self._waiting_for_approval[id]
goffi@necton2
parents:
diff changeset
108 del(self._waiting_for_approval[id])
goffi@necton2
parents:
diff changeset
109 self.negociate(element, id, from_id)
goffi@necton2
parents:
diff changeset
110
goffi@necton2
parents:
diff changeset
111 def negociate(self, feat_elem, id, to_jid):
goffi@necton2
parents:
diff changeset
112 #TODO: put this in a plugin
goffi@necton2
parents:
diff changeset
113 #FIXME: over ultra mega ugly, need to be generic
goffi@necton2
parents:
diff changeset
114 info ("Feature negociation")
goffi@necton2
parents:
diff changeset
115 data = feat_elem.firstChildElement()
goffi@necton2
parents:
diff changeset
116 field = data.firstChildElement()
goffi@necton2
parents:
diff changeset
117 #FIXME: several options ! Q&D code for test only
goffi@necton2
parents:
diff changeset
118 option = field.firstChildElement()
goffi@necton2
parents:
diff changeset
119 value = option.firstChildElement()
goffi@necton2
parents:
diff changeset
120 if unicode(value) == "http://jabber.org/protocol/bytestreams":
goffi@necton2
parents:
diff changeset
121 #ugly, as usual, need to be entirely rewritten (just for test !)
goffi@necton2
parents:
diff changeset
122 result = domish.Element(('', 'iq'))
goffi@necton2
parents:
diff changeset
123 result['type'] = 'result'
goffi@necton2
parents:
diff changeset
124 result['id'] = id
goffi@necton2
parents:
diff changeset
125 result['to'] = to_jid
goffi@necton2
parents:
diff changeset
126 si = result.addElement('si', 'http://jabber.org/protocol/si')
goffi@necton2
parents:
diff changeset
127 file = si.addElement('file', 'http://jabber.org/protocol/si/profile/file-transfer')
goffi@necton2
parents:
diff changeset
128 feature = si.addElement('feature', 'http://jabber.org/protocol/feature-neg')
goffi@necton2
parents:
diff changeset
129 x = feature.addElement('x', 'jabber:x:data')
goffi@necton2
parents:
diff changeset
130 x['type'] = 'submit'
goffi@necton2
parents:
diff changeset
131 field = x.addElement('field')
goffi@necton2
parents:
diff changeset
132 field['var'] = 'stream-method'
goffi@necton2
parents:
diff changeset
133 value = field.addElement('value')
goffi@necton2
parents:
diff changeset
134 value.addContent('http://jabber.org/protocol/bytestreams')
goffi@necton2
parents:
diff changeset
135 self.host.xmlstream.send(result)
goffi@necton2
parents:
diff changeset
136
goffi@necton2
parents:
diff changeset
137 def fileCB(self, answer):
goffi@necton2
parents:
diff changeset
138 if answer['type']=="result": #FIXME FIXME FIXME ugly ugly ugly ! and temp FIXME FIXME FIXME
goffi@necton2
parents:
diff changeset
139 info("SENDING UGLY ANSWER")
goffi@necton2
parents:
diff changeset
140 offer=client.IQ(self.host.xmlstream,'set')
goffi@necton2
parents:
diff changeset
141 offer["from"]=self.host.me.full()
goffi@necton2
parents:
diff changeset
142 offer["to"]=answer['from']
goffi@necton2
parents:
diff changeset
143 query=offer.addElement('query', 'http://jabber.org/protocol/bytestreams')
goffi@necton2
parents:
diff changeset
144 query['mode']='tcp'
goffi@necton2
parents:
diff changeset
145 streamhost=query.addElement('streamhost')
goffi@necton2
parents:
diff changeset
146 streamhost['host']=self.host.memory.getParamV("IP", "File Transfert")
goffi@necton2
parents:
diff changeset
147 streamhost['port']=self.host.memory.getParamV("Port", "File Transfert")
goffi@necton2
parents:
diff changeset
148 streamhost['jid']=self.host.me.full()
goffi@necton2
parents:
diff changeset
149 offer.send()
goffi@necton2
parents:
diff changeset
150
goffi@necton2
parents:
diff changeset
151
goffi@necton2
parents:
diff changeset
152
goffi@necton2
parents:
diff changeset
153
goffi@necton2
parents:
diff changeset
154 def sendFile(self, to, filepath):
goffi@necton2
parents:
diff changeset
155 """send a file using XEP-0096
goffi@necton2
parents:
diff changeset
156 Return an unique id to identify the transfert
goffi@necton2
parents:
diff changeset
157 """
goffi@necton2
parents:
diff changeset
158 debug ("sendfile (%s) to %s", filepath, to )
goffi@necton2
parents:
diff changeset
159 print type(filepath), type(to)
goffi@necton2
parents:
diff changeset
160
goffi@necton2
parents:
diff changeset
161 statinfo = os.stat(filepath)
goffi@necton2
parents:
diff changeset
162
goffi@necton2
parents:
diff changeset
163 offer=client.IQ(self.host.xmlstream,'set')
goffi@necton2
parents:
diff changeset
164 debug ("Transfert ID: %s", offer["id"])
goffi@necton2
parents:
diff changeset
165
goffi@necton2
parents:
diff changeset
166 self.host.plugins["XEP_0065"].sendFile(offer["id"], filepath, str(statinfo.st_size))
goffi@necton2
parents:
diff changeset
167
goffi@necton2
parents:
diff changeset
168 offer["from"]=self.host.me.full()
goffi@necton2
parents:
diff changeset
169 offer["to"]=jid.JID(to).full()
goffi@necton2
parents:
diff changeset
170 si=offer.addElement('si','http://jabber.org/protocol/si')
goffi@necton2
parents:
diff changeset
171 si["mime-type"]='text/plain'
goffi@necton2
parents:
diff changeset
172 si["profile"]='http://jabber.org/protocol/si/profile/file-transfer'
8
4b05308d45f9 fixed bad namespace
Goffi <goffi@goffi.org>
parents: 7
diff changeset
173 file = si.addElement('file', 'http://jabber.org/protocol/si/profile/file-transfer')
0
goffi@necton2
parents:
diff changeset
174 file['name']=os.path.basename(filepath)
goffi@necton2
parents:
diff changeset
175 file['size']=str(statinfo.st_size)
goffi@necton2
parents:
diff changeset
176
goffi@necton2
parents:
diff changeset
177 ###
goffi@necton2
parents:
diff changeset
178 # FIXME: Ugly temporary hard coded implementation of XEP-0020 & XEP-0004,
goffi@necton2
parents:
diff changeset
179 # Need to be recoded elsewhere in a more generic way
goffi@necton2
parents:
diff changeset
180 ###
goffi@necton2
parents:
diff changeset
181
goffi@necton2
parents:
diff changeset
182 feature=si.addElement('feature', "http://jabber.org/protocol/feature-neg")
goffi@necton2
parents:
diff changeset
183 x=feature.addElement('x', "jabber:x:data")
goffi@necton2
parents:
diff changeset
184 x['type']='form'
goffi@necton2
parents:
diff changeset
185 field=x.addElement('field')
goffi@necton2
parents:
diff changeset
186 field['type']='list-single'
goffi@necton2
parents:
diff changeset
187 field['var']='stream-method'
goffi@necton2
parents:
diff changeset
188 option = field.addElement('option')
goffi@necton2
parents:
diff changeset
189 value = option.addElement('value', content='http://jabber.org/protocol/bytestreams')
goffi@necton2
parents:
diff changeset
190
goffi@necton2
parents:
diff changeset
191 offer.addCallback(self.fileCB)
goffi@necton2
parents:
diff changeset
192 offer.send()
goffi@necton2
parents:
diff changeset
193 return offer["id"] #XXX: using IQ id as file transfert id seems OK as IQ id are required
goffi@necton2
parents:
diff changeset
194