annotate src/plugins/plugin_xep_0047.py @ 391:c34fd9d6242e

spelling
author Goffi <goffi@goffi.org>
date Thu, 29 Sep 2011 14:59:14 +0200
parents 6ff50d609b16
children 8f3551ceee17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
3
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for managing gateways (xep-0047)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
7
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
12
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
17
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, warning, error
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.internet import protocol
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber import client, jid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import error as jab_error
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.xish import domish
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import twisted.internet.error
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.internet import reactor
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from wokkel import disco, iwokkel
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
31
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from zope.interface import implements
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
34 import base64
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
36 try:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from twisted.words.protocols.xmlstream import XMPPHandler
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
38 except ImportError:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from wokkel.subprotocols import XMPPHandler
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
40
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
41 MESSAGE = '/message'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
42 IQ = '/iq'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
43 IQ_SET = '/iq[@type="set"]'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
44 NS_IBB = 'http://jabber.org/protocol/ibb'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
45 IBB_OPEN = IQ_SET + '/open[@xmlns="' + NS_IBB + '"]'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
46 IBB_CLOSE = IQ_SET + '/close[@xmlns="' + NS_IBB + '" and @sid="%s"]'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
47 IBB_IQ_DATA = IQ + '/data[@xmlns="' + NS_IBB + '" and @sid="%s"]' #we use IQ instead of IQ_SET because of a bug in Gajim
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
48 IBB_MESSAGE_DATA = MESSAGE + '/data[@xmlns="' + NS_IBB + '" and @sid="%s"]'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49 TIMEOUT = 60 #timeout for workflow
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 BLOCK_SIZE = 4096
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
51
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
52 PLUGIN_INFO = {
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
53 "name": "In-Band Bytestream Plugin",
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "import_name": "XEP-0047",
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "type": "XEP",
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
56 "protocols": ["XEP-0047"],
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
57 "main": "XEP_0047",
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
58 "handler": "yes",
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
59 "description": _("""Implementation of In-Band Bytestreams""")
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
60 }
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
61
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
62 class XEP_0047():
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 NAMESPACE = NS_IBB
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def __init__(self, host):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
66 info(_("In-Band Bytestreams plugin initialization"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.host = host
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.current_stream = {} #key: stream_id, value: data(dict)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def getHandler(self, profile):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return XEP_0047_handler(self)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
72
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def _timeOut(self, sid):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74 """Delecte current_stream id, called after timeout
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @param id: id of self.current_stream"""
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76 info(_("In-Band Bytestream: TimeOut reached for id %s") % sid);
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self._killId(sid, False, "TIMEOUT")
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
79 def _killId(self, sid, success=False, failure_reason="UNKNOWN"):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80 """Delete an current_stream id, clean up associated observers
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 @param sid: id of self.current_stream"""
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
82 if not self.current_stream.has_key(sid):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
83 warning(_("kill id called on a non existant id"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if self.current_stream[sid].has_key("observer_cb"):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86 xmlstream = self.current_stream[sid]["xmlstream"]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 xmlstream.removeObserver(self.current_stream[sid]["event_data"], self.current_stream[sid]["observer_cb"])
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.current_stream[sid]['timer'].cancel()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 if self.current_stream[sid].has_key("size"):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.host.removeProgressCB(sid)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 file_obj = self.current_stream[sid]['file_obj']
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 success_cb = self.current_stream[sid]['success_cb']
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
94 failure_cb = self.current_stream[sid]['failure_cb']
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 del self.current_stream[sid]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
98 if success:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
99 success_cb(sid, file_obj, NS_IBB)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
100 else:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
101 failure_cb(sid, file_obj, NS_IBB, failure_reason)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
102
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
103 def getProgress(self, sid, data):
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 389
diff changeset
104 """Fill data with position of current transfer"""
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
105 try:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
106 file_obj = self.current_stream[sid]["file_obj"]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
107 data["position"] = str(file_obj.tell())
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
108 data["size"] = str(self.current_stream[sid]["size"])
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
109 except:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
110 pass
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
111
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
112 def prepareToReceive(self, from_jid, sid, file_obj, size, success_cb, failure_cb):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
113 """Called when a bytestream is imminent
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
114 @param from_jid: jid of the sender
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
115 @param id: Stream id
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
116 @param file_obj: File Object where the data will be written"""
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
117 data = self.current_stream[sid] = {}
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
118 data["from"] = from_jid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
119 data["file_obj"] = file_obj
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
120 data["seq"] = -1
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
121 if size:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
122 data["size"] = size
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
123 self.host.registerProgressCB(sid, self.getProgress)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
124 data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
125 data["success_cb"] = success_cb
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
126 data["failure_cb"] = failure_cb
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
127
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
128 def streamOpening(self, IQ, profile):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
129 debug(_("IBB stream opening"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
130 IQ.handled=True
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
131 profile_jid, xmlstream = self.host.getJidNStream(profile)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
132 open_elt = IQ.firstChildElement()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
133 block_size = open_elt.getAttribute('block-size')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
134 sid = open_elt.getAttribute('sid')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
135 stanza = open_elt.getAttribute('stanza', 'iq')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
136 if not sid or not block_size or int(block_size)>65535:
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 389
diff changeset
137 warning(_("malformed IBB transfer: %s" % IQ['id']))
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
138 self.sendNotAcceptableError(IQ['id'], IQ['from'], xmlstream)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
139 return
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
140 if not sid in self.current_stream:
391
c34fd9d6242e spelling
Goffi <goffi@goffi.org>
parents: 389
diff changeset
141 warning(_("Ignoring unexpected IBB transfer: %s" % sid))
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
142 self.sendNotAcceptableError(IQ['id'], IQ['from'], xmlstream)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
143 return
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
144 if self.current_stream[sid]["from"] != jid.JID(IQ['from']):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
145 warning(_("sended jid inconsistency (man in the middle attack attempt ?)"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
146 self.sendNotAcceptableError(IQ['id'], IQ['from'], xmlstream)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
147 self._killId(sid, False, "PROTOCOL_ERROR")
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
148 return
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
149
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
150 #at this stage, the session looks ok and will be accepted
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
151
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
152 #we reset the timeout:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
153 self.current_stream[sid]["timer"].reset(TIMEOUT)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
154
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
155 #we save the xmlstream, events and observer data to allow observer removal
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
156 self.current_stream[sid]["xmlstream"] = xmlstream
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
157 self.current_stream[sid]["event_data"] = event_data = (IBB_MESSAGE_DATA if stanza=='message' else IBB_IQ_DATA) % sid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
158 self.current_stream[sid]["observer_cb"] = observer_cb = self.messageData if stanza=='message' else self.iqData
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
159 event_close = IBB_CLOSE % sid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
160 #we now set the stream observer to look after data packet
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
161 xmlstream.addObserver(event_data, observer_cb, profile = profile)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
162 xmlstream.addOnetimeObserver(event_close, self.streamClosing, profile = profile)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
163 #finally, we send the accept stanza
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
164 result = domish.Element(('', 'iq'))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
165 result['type'] = 'result'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
166 result['id'] = IQ['id']
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
167 result['to'] = IQ['from']
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
168 xmlstream.send(result)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
169
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
170 def streamClosing(self, IQ, profile):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
171 IQ.handled=True
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
172 debug(_("IBB stream closing"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
173 data_elt = IQ.firstChildElement()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
174 sid = data_elt.getAttribute('sid')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
175 result = domish.Element(('', 'iq'))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
176 result['type'] = 'result'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
177 result['id'] = IQ['id']
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
178 result['to'] = IQ['from']
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
179 self.current_stream[sid]["xmlstream"].send(result)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
180 self._killId(sid, success=True)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
181
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
182 def iqData(self, IQ, profile):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
183 IQ.handled=True
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
184 data_elt = IQ.firstChildElement()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
185
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
186 if self._manageDataElt(data_elt, 'iq', IQ['id'], jid.JID(IQ['from'])):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
187 #and send a success answer
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
188 result = domish.Element(('', 'iq'))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
189 result['type'] = 'result'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
190 result['id'] = IQ['id']
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
191 result['to'] = IQ['from']
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
192 _jid, xmlstream = self.host.getJidNStream(profile)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
193 xmlstream.send(result)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
194
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
195 def messageData(self, message_elt, profile):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
196 data_elt = message_elt.firstChildElement()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
197 sid = message_elt.getAttribute('id','')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
198 self._manageDataElt(message_elt, 'message', sid, jid.JID(message_elt['from']))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
199
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
200 def _manageDataElt(self, data_elt, stanza, sid, stanza_from_jid):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
201 """Manage the data elelement (check validity and write to the file_obj)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
202 @param data_elt: "data" domish element
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
203 @return: True if success"""
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
204 sid = data_elt.getAttribute('sid')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
205 if sid not in self.current_stream:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
206 error(_("Received data for an unknown session id"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
207 return False
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
208 xmlstream = self.current_stream[sid]["xmlstream"]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
209
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
210 from_jid = self.current_stream[sid]["from"]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
211 file_obj = self.current_stream[sid]["file_obj"]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
212
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
213 if stanza_from_jid != from_jid:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
214 warning(_("sended jid inconsistency (man in the middle attack attempt ?)"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
215 if stanza=='iq':
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
216 self.sendNotAcceptableError(sid, from_jid, xmlstream)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
217 return False
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
218
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
219 self.current_stream[sid]["seq"]+=1
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
220 if int(data_elt.getAttribute("seq",-1)) != self.current_stream[sid]["seq"]:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
221 warning(_("Sequence error"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
222 if stanza=='iq':
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
223 self.sendNotAcceptableError(IQ["id"], from_jid, xmlstream)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
224 return False
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
225
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
226 #we reset the timeout:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
227 self.current_stream[sid]["timer"].reset(TIMEOUT)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
228
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
229 #we can now decode the data
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
230 try:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
231 file_obj.write(base64.b64decode(str(data_elt)))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
232 except TypeError:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
233 #The base64 data is invalid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
234 warning(_("Invalid base64 data"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
235 if stanza=='iq':
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
236 self.sendNotAcceptableError(IQ["id"], from_jid, xmlstream)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
237 return False
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
238 return True
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
239
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
240 def sendNotAcceptableError(self, iq_id, to_jid, xmlstream):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
241 """Not acceptable error used when the stream is not expected or something is going wrong
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
242 @param iq_id: IQ id
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
243 @param to_jid: addressee
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
244 @param xmlstream: XML stream to use to send the error"""
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
245 result = domish.Element(('', 'iq'))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
246 result['type'] = 'result'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
247 result['id'] = iq_id
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
248 result['to'] = to_jid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
249 error_el = result.addElement('error')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
250 error_el['type'] = 'cancel'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
251 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','not-acceptable'))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
252 xmlstream.send(result)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
253
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
254 def startStream(self, file_obj, to_jid, sid, length, successCb, failureCb, size = None, profile='@NONE@'):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
255 """Launch the stream workflow
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
256 @param file_obj: file_obj to send
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
257 @param to_jid: JID of the recipient
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
258 @param sid: Stream session id
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
259 @param length: number of byte to send, or None to send until the end
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
260 @param successCb: method to call when stream successfuly finished
389
6ff50d609b16 plugin XEP-0047: typo
Goffi <goffi@goffi.org>
parents: 384
diff changeset
261 @param failureCb: method to call when something goes wrong
384
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
262 @param profile: %(doc_profile)s"""
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
263 if length != None:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
264 error(_('stream length not managed yet'))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
265 return;
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
266 profile_jid, xmlstream = self.host.getJidNStream(profile)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
267 data = self.current_stream[sid] = {}
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
268 data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
269 data["file_obj"] = file_obj
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
270 data["to"] = to_jid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
271 data["success_cb"] = successCb
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
272 data["failure_cb"] = failureCb
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
273 data["xmlstream"] = xmlstream
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
274 data["block_size"] = BLOCK_SIZE
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
275 if size:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
276 data["size"] = size
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
277 self.host.registerProgressCB(sid, self.getProgress)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
278 iq_elt = client.IQ(xmlstream,'set')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
279 iq_elt['from'] = profile_jid.full()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
280 iq_elt['to'] = to_jid.full()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
281 open_elt = iq_elt.addElement('open',NS_IBB)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
282 open_elt['block-size'] = str(BLOCK_SIZE)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
283 open_elt['sid'] = sid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
284 open_elt['stanza'] = 'iq'
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
285 iq_elt.addCallback(self.iqResult, sid, 0, length)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
286 iq_elt.send()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
287
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
288 def iqResult(self, sid, seq, length, iq_elt):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
289 """Called when the result of open iq is received"""
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
290 data = self.current_stream[sid]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
291 if iq_elt.type == "error":
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
292 warning(_("Transfer failed"))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
293 self.terminateStream(sid, "IQ_ERROR")
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
294 return
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
295
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
296 buffer = data["file_obj"].read(data["block_size"])
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
297 if buffer:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
298 next_iq_elt = client.IQ(data["xmlstream"],'set')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
299 next_iq_elt['to'] = data["to"].full()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
300 data_elt = next_iq_elt.addElement('data', NS_IBB)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
301 data_elt['seq'] = str(seq)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
302 data_elt['sid'] = sid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
303 data_elt.addContent(base64.b64encode(buffer))
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
304 next_iq_elt.addCallback(self.iqResult, sid, seq+1, length)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
305 next_iq_elt.send()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
306 else:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
307 self.terminateStream(sid)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
308
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
309 def terminateStream(self, sid, failure_reason = None):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
310 """Terminate the stream session
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
311 @param to_jid: recipient
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
312 @param sid: Session id
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
313 @param file_obj: file object used
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
314 @param xmlstream: XML stream used with this session
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
315 @param progress_cb: True if we have to remove the progress callback
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
316 @param callback: method to call after finishing
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
317 @param failure_reason: reason of the failure, or None if steam was successful"""
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
318 data = self.current_stream[sid]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
319 iq_elt = client.IQ(data["xmlstream"],'set')
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
320 iq_elt['to'] = data["to"].full()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
321 close_elt = iq_elt.addElement('close',NS_IBB)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
322 close_elt['sid'] = sid
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
323 iq_elt.send()
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
324 self.host.removeProgressCB(sid)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
325 if failure_reason:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
326 self._killId(sid, False, failure_reason)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
327 else:
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
328 self._killId(sid, True)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
329
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
330 class XEP_0047_handler(XMPPHandler):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
331 implements(iwokkel.IDisco)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
332
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
333 def __init__(self,parent):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
334 self.plugin_parent = parent
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
335
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
336 def connectionInitialized(self):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
337 self.xmlstream.addObserver(IBB_OPEN, self.plugin_parent.streamOpening, profile = self.parent.profile)
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
338
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
339 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
340 return [disco.DiscoFeature(NS_IBB)]
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
341
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
342 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
785420cd63f7 plugins: In-Band Bytestreams (XEP-0047) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
343 return []