annotate src/plugins/plugin_xep_0065.py @ 394:8f3551ceee17

plugin XEP-0065: refactored and misc stuff fixed. Still not finished plugins XEP-0096: XEP-0065 (Socks5 stream method) managed
author Goffi <goffi@goffi.org>
date Mon, 03 Oct 2011 18:05:15 +0200
parents 7c79d4a8c9e6
children cb0285372818
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 SAT plugin for managing xep-0065
goffi@necton2
parents:
diff changeset
5
goffi@necton2
parents:
diff changeset
6 Copyright (C)
251
dbe1803eca7c minor copyright update, date format harmonisation
Goffi <goffi@goffi.org>
parents: 228
diff changeset
7 2002, 2003, 2004 Dave Smith (dizzyd@jabber.org)
dbe1803eca7c minor copyright update, date format harmonisation
Goffi <goffi@goffi.org>
parents: 228
diff changeset
8 2007, 2008 Fabio Forno (xmpp:ff@jabber.bluendo.com)
dbe1803eca7c minor copyright update, date format harmonisation
Goffi <goffi@goffi.org>
parents: 228
diff changeset
9 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
10
goffi@necton2
parents:
diff changeset
11 This program is free software: you can redistribute it and/or modify
goffi@necton2
parents:
diff changeset
12 it under the terms of the GNU General Public License as published by
goffi@necton2
parents:
diff changeset
13 the Free Software Foundation, either version 3 of the License, or
goffi@necton2
parents:
diff changeset
14 (at your option) any later version.
goffi@necton2
parents:
diff changeset
15
goffi@necton2
parents:
diff changeset
16 This program is distributed in the hope that it will be useful,
goffi@necton2
parents:
diff changeset
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
goffi@necton2
parents:
diff changeset
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
goffi@necton2
parents:
diff changeset
19 GNU General Public License for more details.
goffi@necton2
parents:
diff changeset
20
goffi@necton2
parents:
diff changeset
21 You should have received a copy of the GNU General Public License
goffi@necton2
parents:
diff changeset
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
goffi@necton2
parents:
diff changeset
23
goffi@necton2
parents:
diff changeset
24 --
goffi@necton2
parents:
diff changeset
25
goffi@necton2
parents:
diff changeset
26 This program is based on proxy65 (http://code.google.com/p/proxy65),
goffi@necton2
parents:
diff changeset
27 originaly written by David Smith and modified by Fabio Forno.
goffi@necton2
parents:
diff changeset
28 It is sublicensed under GPL v3 (or any later version) as allowed by the original
goffi@necton2
parents:
diff changeset
29 license.
goffi@necton2
parents:
diff changeset
30
goffi@necton2
parents:
diff changeset
31 --
goffi@necton2
parents:
diff changeset
32
goffi@necton2
parents:
diff changeset
33 Here is a copy of the original license:
goffi@necton2
parents:
diff changeset
34
goffi@necton2
parents:
diff changeset
35 Copyright (C)
goffi@necton2
parents:
diff changeset
36 2002-2004 Dave Smith (dizzyd@jabber.org)
goffi@necton2
parents:
diff changeset
37 2007-2008 Fabio Forno (xmpp:ff@jabber.bluendo.com)
goffi@necton2
parents:
diff changeset
38
goffi@necton2
parents:
diff changeset
39 Permission is hereby granted, free of charge, to any person obtaining a copy
goffi@necton2
parents:
diff changeset
40 of this software and associated documentation files (the "Software"), to deal
goffi@necton2
parents:
diff changeset
41 in the Software without restriction, including without limitation the rights
goffi@necton2
parents:
diff changeset
42 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
goffi@necton2
parents:
diff changeset
43 copies of the Software, and to permit persons to whom the Software is
goffi@necton2
parents:
diff changeset
44 furnished to do so, subject to the following conditions:
goffi@necton2
parents:
diff changeset
45
goffi@necton2
parents:
diff changeset
46 The above copyright notice and this permission notice shall be included in
goffi@necton2
parents:
diff changeset
47 all copies or substantial portions of the Software.
goffi@necton2
parents:
diff changeset
48
goffi@necton2
parents:
diff changeset
49 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
goffi@necton2
parents:
diff changeset
50 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
goffi@necton2
parents:
diff changeset
51 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
goffi@necton2
parents:
diff changeset
52 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
goffi@necton2
parents:
diff changeset
53 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
goffi@necton2
parents:
diff changeset
54 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
goffi@necton2
parents:
diff changeset
55 THE SOFTWARE.
goffi@necton2
parents:
diff changeset
56 """
goffi@necton2
parents:
diff changeset
57
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
58 from logging import debug, info, warning, error
0
goffi@necton2
parents:
diff changeset
59 from twisted.internet import protocol, reactor
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
60 from twisted.internet import error as jab_error
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
61 from twisted.words.protocols.jabber import client, jid
0
goffi@necton2
parents:
diff changeset
62 from twisted.protocols.basic import FileSender
goffi@necton2
parents:
diff changeset
63 from twisted.words.xish import domish
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
64 from twisted.web.client import getPage
0
goffi@necton2
parents:
diff changeset
65 import struct
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
66 import urllib
0
goffi@necton2
parents:
diff changeset
67 import hashlib, pdb
goffi@necton2
parents:
diff changeset
68
15
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
69 from zope.interface import implements
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
70
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
71 try:
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
72 from twisted.words.protocols.xmlstream import XMPPHandler
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
73 except ImportError:
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
74 from wokkel.subprotocols import XMPPHandler
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
75
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
76 from wokkel import disco, iwokkel
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
77
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
78 IQ_SET = '/iq[@type="set"]'
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
79 NS_BS = 'http://jabber.org/protocol/bytestreams'
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
80 BS_REQUEST = IQ_SET + '/query[@xmlns="' + NS_BS + '"]'
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
81 TIMEOUT = 60 #timeout for workflow
15
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
82
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
83
0
goffi@necton2
parents:
diff changeset
84
goffi@necton2
parents:
diff changeset
85 PLUGIN_INFO = {
goffi@necton2
parents:
diff changeset
86 "name": "XEP 0065 Plugin",
291
7c79d4a8c9e6 plugins: fixed bad import names
Goffi <goffi@goffi.org>
parents: 251
diff changeset
87 "import_name": "XEP-0065",
0
goffi@necton2
parents:
diff changeset
88 "type": "XEP",
48
4392f1fdb064 plugins improvement
Goffi <goffi@goffi.org>
parents: 38
diff changeset
89 "protocols": ["XEP-0065"],
0
goffi@necton2
parents:
diff changeset
90 "main": "XEP_0065",
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
91 "handler": "yes",
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
92 "description": _("""Implementation of SOCKS5 Bytestreams""")
0
goffi@necton2
parents:
diff changeset
93 }
goffi@necton2
parents:
diff changeset
94
goffi@necton2
parents:
diff changeset
95 STATE_INITIAL = 0
goffi@necton2
parents:
diff changeset
96 STATE_AUTH = 1
goffi@necton2
parents:
diff changeset
97 STATE_REQUEST = 2
goffi@necton2
parents:
diff changeset
98 STATE_READY = 3
goffi@necton2
parents:
diff changeset
99 STATE_AUTH_USERPASS = 4
goffi@necton2
parents:
diff changeset
100 STATE_TARGET_INITIAL = 5
goffi@necton2
parents:
diff changeset
101 STATE_TARGET_AUTH = 6
goffi@necton2
parents:
diff changeset
102 STATE_TARGET_REQUEST = 7
goffi@necton2
parents:
diff changeset
103 STATE_TARGET_READY = 8
goffi@necton2
parents:
diff changeset
104 STATE_LAST = 9
goffi@necton2
parents:
diff changeset
105
goffi@necton2
parents:
diff changeset
106 STATE_CONNECT_PENDING = STATE_LAST + 1
goffi@necton2
parents:
diff changeset
107
goffi@necton2
parents:
diff changeset
108 SOCKS5_VER = 0x05
goffi@necton2
parents:
diff changeset
109
goffi@necton2
parents:
diff changeset
110 ADDR_IPV4 = 0x01
goffi@necton2
parents:
diff changeset
111 ADDR_DOMAINNAME = 0x03
goffi@necton2
parents:
diff changeset
112 ADDR_IPV6 = 0x04
goffi@necton2
parents:
diff changeset
113
goffi@necton2
parents:
diff changeset
114 CMD_CONNECT = 0x01
goffi@necton2
parents:
diff changeset
115 CMD_BIND = 0x02
goffi@necton2
parents:
diff changeset
116 CMD_UDPASSOC = 0x03
goffi@necton2
parents:
diff changeset
117
goffi@necton2
parents:
diff changeset
118 AUTHMECH_ANON = 0x00
goffi@necton2
parents:
diff changeset
119 AUTHMECH_USERPASS = 0x02
goffi@necton2
parents:
diff changeset
120 AUTHMECH_INVALID = 0xFF
goffi@necton2
parents:
diff changeset
121
goffi@necton2
parents:
diff changeset
122 REPLY_SUCCESS = 0x00
goffi@necton2
parents:
diff changeset
123 REPLY_GENERAL_FAILUR = 0x01
goffi@necton2
parents:
diff changeset
124 REPLY_CONN_NOT_ALLOWED = 0x02
goffi@necton2
parents:
diff changeset
125 REPLY_NETWORK_UNREACHABLE = 0x03
goffi@necton2
parents:
diff changeset
126 REPLY_HOST_UNREACHABLE = 0x04
goffi@necton2
parents:
diff changeset
127 REPLY_CONN_REFUSED = 0x05
goffi@necton2
parents:
diff changeset
128 REPLY_TTL_EXPIRED = 0x06
goffi@necton2
parents:
diff changeset
129 REPLY_CMD_NOT_SUPPORTED = 0x07
goffi@necton2
parents:
diff changeset
130 REPLY_ADDR_NOT_SUPPORTED = 0x08
goffi@necton2
parents:
diff changeset
131
goffi@necton2
parents:
diff changeset
132
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
133 def calculateHash(from_jid, to_jid, sid):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
134 """Calculate SHA1 Hash according to XEP-0065
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
135 @param from_jid: jid of the requester
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
136 @param to_jid: jid of the target
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
137 @param sid: session id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
138 @return: hash (string)"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
139 return hashlib.sha1((sid + from_jid.full() + to_jid.full()).encode('utf-8')).hexdigest()
0
goffi@necton2
parents:
diff changeset
140
goffi@necton2
parents:
diff changeset
141
goffi@necton2
parents:
diff changeset
142
goffi@necton2
parents:
diff changeset
143 class SOCKSv5(protocol.Protocol, FileSender):
goffi@necton2
parents:
diff changeset
144 def __init__(self):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
145 debug(_("Protocol init"))
0
goffi@necton2
parents:
diff changeset
146 self.state = STATE_INITIAL
goffi@necton2
parents:
diff changeset
147 self.buf = ""
goffi@necton2
parents:
diff changeset
148 self.supportedAuthMechs = [ AUTHMECH_ANON ]
goffi@necton2
parents:
diff changeset
149 self.supportedAddrs = [ ADDR_DOMAINNAME ]
goffi@necton2
parents:
diff changeset
150 self.enabledCommands = [ CMD_CONNECT ]
goffi@necton2
parents:
diff changeset
151 self.peersock = None
goffi@necton2
parents:
diff changeset
152 self.addressType = 0
goffi@necton2
parents:
diff changeset
153 self.requestType = 0
goffi@necton2
parents:
diff changeset
154
goffi@necton2
parents:
diff changeset
155 def _startNegotiation(self):
goffi@necton2
parents:
diff changeset
156 debug("_startNegotiation")
goffi@necton2
parents:
diff changeset
157 self.state = STATE_TARGET_AUTH
goffi@necton2
parents:
diff changeset
158 self.transport.write(struct.pack('!3B', SOCKS5_VER, 1, AUTHMECH_ANON))
goffi@necton2
parents:
diff changeset
159
goffi@necton2
parents:
diff changeset
160 def _parseNegotiation(self):
goffi@necton2
parents:
diff changeset
161 debug("_parseNegotiation")
goffi@necton2
parents:
diff changeset
162 try:
goffi@necton2
parents:
diff changeset
163 # Parse out data
goffi@necton2
parents:
diff changeset
164 ver, nmethod = struct.unpack('!BB', self.buf[:2])
goffi@necton2
parents:
diff changeset
165 methods = struct.unpack('%dB' % nmethod, self.buf[2:nmethod+2])
goffi@necton2
parents:
diff changeset
166
goffi@necton2
parents:
diff changeset
167 # Ensure version is correct
goffi@necton2
parents:
diff changeset
168 if ver != 5:
goffi@necton2
parents:
diff changeset
169 self.transport.write(struct.pack('!BB', SOCKS5_VER, AUTHMECH_INVALID))
goffi@necton2
parents:
diff changeset
170 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
171 return
goffi@necton2
parents:
diff changeset
172
goffi@necton2
parents:
diff changeset
173 # Trim off front of the buffer
goffi@necton2
parents:
diff changeset
174 self.buf = self.buf[nmethod+2:]
goffi@necton2
parents:
diff changeset
175
goffi@necton2
parents:
diff changeset
176 # Check for supported auth mechs
goffi@necton2
parents:
diff changeset
177 for m in self.supportedAuthMechs:
goffi@necton2
parents:
diff changeset
178 if m in methods:
goffi@necton2
parents:
diff changeset
179 # Update internal state, according to selected method
goffi@necton2
parents:
diff changeset
180 if m == AUTHMECH_ANON:
goffi@necton2
parents:
diff changeset
181 self.state = STATE_REQUEST
goffi@necton2
parents:
diff changeset
182 elif m == AUTHMECH_USERPASS:
goffi@necton2
parents:
diff changeset
183 self.state = STATE_AUTH_USERPASS
goffi@necton2
parents:
diff changeset
184 # Complete negotiation w/ this method
goffi@necton2
parents:
diff changeset
185 self.transport.write(struct.pack('!BB', SOCKS5_VER, m))
goffi@necton2
parents:
diff changeset
186 return
goffi@necton2
parents:
diff changeset
187
goffi@necton2
parents:
diff changeset
188 # No supported mechs found, notify client and close the connection
goffi@necton2
parents:
diff changeset
189 self.transport.write(struct.pack('!BB', SOCKS5_VER, AUTHMECH_INVALID))
goffi@necton2
parents:
diff changeset
190 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
191 except struct.error:
goffi@necton2
parents:
diff changeset
192 pass
goffi@necton2
parents:
diff changeset
193
goffi@necton2
parents:
diff changeset
194 def _parseUserPass(self):
goffi@necton2
parents:
diff changeset
195 debug("_parseUserPass")
goffi@necton2
parents:
diff changeset
196 try:
goffi@necton2
parents:
diff changeset
197 # Parse out data
goffi@necton2
parents:
diff changeset
198 ver, ulen = struct.unpack('BB', self.buf[:2])
goffi@necton2
parents:
diff changeset
199 uname, = struct.unpack('%ds' % ulen, self.buf[2:ulen + 2])
goffi@necton2
parents:
diff changeset
200 plen, = struct.unpack('B', self.buf[ulen + 2])
goffi@necton2
parents:
diff changeset
201 password, = struct.unpack('%ds' % plen, self.buf[ulen + 3:ulen + 3 + plen])
goffi@necton2
parents:
diff changeset
202 # Trim off fron of the buffer
goffi@necton2
parents:
diff changeset
203 self.buf = self.buf[3 + ulen + plen:]
goffi@necton2
parents:
diff changeset
204 # Fire event to authenticate user
goffi@necton2
parents:
diff changeset
205 if self.authenticateUserPass(uname, password):
goffi@necton2
parents:
diff changeset
206 # Signal success
goffi@necton2
parents:
diff changeset
207 self.state = STATE_REQUEST
goffi@necton2
parents:
diff changeset
208 self.transport.write(struct.pack('!BB', SOCKS5_VER, 0x00))
goffi@necton2
parents:
diff changeset
209 else:
goffi@necton2
parents:
diff changeset
210 # Signal failure
goffi@necton2
parents:
diff changeset
211 self.transport.write(struct.pack('!BB', SOCKS5_VER, 0x01))
goffi@necton2
parents:
diff changeset
212 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
213 except struct.error:
goffi@necton2
parents:
diff changeset
214 pass
goffi@necton2
parents:
diff changeset
215
goffi@necton2
parents:
diff changeset
216 def sendErrorReply(self, errorcode):
goffi@necton2
parents:
diff changeset
217 debug("sendErrorReply")
goffi@necton2
parents:
diff changeset
218 # Any other address types are not supported
goffi@necton2
parents:
diff changeset
219 result = struct.pack('!BBBBIH', SOCKS5_VER, errorcode, 0, 1, 0, 0)
goffi@necton2
parents:
diff changeset
220 self.transport.write(result)
goffi@necton2
parents:
diff changeset
221 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
222
goffi@necton2
parents:
diff changeset
223 def _parseRequest(self):
goffi@necton2
parents:
diff changeset
224 debug("_parseRequest")
goffi@necton2
parents:
diff changeset
225 try:
goffi@necton2
parents:
diff changeset
226 # Parse out data and trim buffer accordingly
goffi@necton2
parents:
diff changeset
227 ver, cmd, rsvd, self.addressType = struct.unpack('!BBBB', self.buf[:4])
goffi@necton2
parents:
diff changeset
228
goffi@necton2
parents:
diff changeset
229 # Ensure we actually support the requested address type
goffi@necton2
parents:
diff changeset
230 if self.addressType not in self.supportedAddrs:
goffi@necton2
parents:
diff changeset
231 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
232 return
goffi@necton2
parents:
diff changeset
233
goffi@necton2
parents:
diff changeset
234 # Deal with addresses
goffi@necton2
parents:
diff changeset
235 if self.addressType == ADDR_IPV4:
goffi@necton2
parents:
diff changeset
236 addr, port = struct.unpack('!IH', self.buf[4:10])
goffi@necton2
parents:
diff changeset
237 self.buf = self.buf[10:]
goffi@necton2
parents:
diff changeset
238 elif self.addressType == ADDR_DOMAINNAME:
goffi@necton2
parents:
diff changeset
239 nlen = ord(self.buf[4])
goffi@necton2
parents:
diff changeset
240 addr, port = struct.unpack('!%dsH' % nlen, self.buf[5:])
goffi@necton2
parents:
diff changeset
241 self.buf = self.buf[7 + len(addr):]
goffi@necton2
parents:
diff changeset
242 else:
goffi@necton2
parents:
diff changeset
243 # Any other address types are not supported
goffi@necton2
parents:
diff changeset
244 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
245 return
goffi@necton2
parents:
diff changeset
246
goffi@necton2
parents:
diff changeset
247 # Ensure command is supported
goffi@necton2
parents:
diff changeset
248 if cmd not in self.enabledCommands:
goffi@necton2
parents:
diff changeset
249 # Send a not supported error
goffi@necton2
parents:
diff changeset
250 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
251 return
goffi@necton2
parents:
diff changeset
252
goffi@necton2
parents:
diff changeset
253 # Process the command
goffi@necton2
parents:
diff changeset
254 if cmd == CMD_CONNECT:
goffi@necton2
parents:
diff changeset
255 self.connectRequested(addr, port)
goffi@necton2
parents:
diff changeset
256 elif cmd == CMD_BIND:
goffi@necton2
parents:
diff changeset
257 self.bindRequested(addr, port)
goffi@necton2
parents:
diff changeset
258 else:
goffi@necton2
parents:
diff changeset
259 # Any other command is not supported
goffi@necton2
parents:
diff changeset
260 self.sendErrorReply(REPLY_CMD_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
261
goffi@necton2
parents:
diff changeset
262 except struct.error, why:
goffi@necton2
parents:
diff changeset
263 return None
goffi@necton2
parents:
diff changeset
264
goffi@necton2
parents:
diff changeset
265 def _makeRequest(self):
goffi@necton2
parents:
diff changeset
266 debug("_makeRequest")
goffi@necton2
parents:
diff changeset
267 self.state = STATE_TARGET_REQUEST
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
268 sha1 = calculateHash(self.data["from"], self.data["to"], self.sid)
0
goffi@necton2
parents:
diff changeset
269 request = struct.pack('!5B%dsH' % len(sha1), SOCKS5_VER, CMD_CONNECT, 0, ADDR_DOMAINNAME, len(sha1), sha1, 0)
goffi@necton2
parents:
diff changeset
270 self.transport.write(request)
goffi@necton2
parents:
diff changeset
271
goffi@necton2
parents:
diff changeset
272 def _parseRequestReply(self):
goffi@necton2
parents:
diff changeset
273 debug("_parseRequestReply")
goffi@necton2
parents:
diff changeset
274 try:
goffi@necton2
parents:
diff changeset
275 ver, rep, rsvd, self.addressType = struct.unpack('!BBBB', self.buf[:4])
goffi@necton2
parents:
diff changeset
276 # Ensure we actually support the requested address type
goffi@necton2
parents:
diff changeset
277 if self.addressType not in self.supportedAddrs:
goffi@necton2
parents:
diff changeset
278 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
279 return
goffi@necton2
parents:
diff changeset
280
goffi@necton2
parents:
diff changeset
281 # Deal with addresses
goffi@necton2
parents:
diff changeset
282 if self.addressType == ADDR_IPV4:
goffi@necton2
parents:
diff changeset
283 addr, port = struct.unpack('!IH', self.buf[4:10])
goffi@necton2
parents:
diff changeset
284 self.buf = self.buf[10:]
goffi@necton2
parents:
diff changeset
285 elif self.addressType == ADDR_DOMAINNAME:
goffi@necton2
parents:
diff changeset
286 nlen = ord(self.buf[4])
goffi@necton2
parents:
diff changeset
287 addr, port = struct.unpack('!%dsH' % nlen, self.buf[5:])
goffi@necton2
parents:
diff changeset
288 self.buf = self.buf[7 + len(addr):]
goffi@necton2
parents:
diff changeset
289 else:
goffi@necton2
parents:
diff changeset
290 # Any other address types are not supported
goffi@necton2
parents:
diff changeset
291 self.sendErrorReply(REPLY_ADDR_NOT_SUPPORTED)
goffi@necton2
parents:
diff changeset
292 return
goffi@necton2
parents:
diff changeset
293
goffi@necton2
parents:
diff changeset
294 # Ensure reply is OK
goffi@necton2
parents:
diff changeset
295 if rep != REPLY_SUCCESS:
goffi@necton2
parents:
diff changeset
296 self.loseConnection()
goffi@necton2
parents:
diff changeset
297 return
goffi@necton2
parents:
diff changeset
298
goffi@necton2
parents:
diff changeset
299 self.state = STATE_TARGET_READY
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
300 self.factory.activateCb(self.sid, self.factory.iq_id)
0
goffi@necton2
parents:
diff changeset
301
goffi@necton2
parents:
diff changeset
302 except struct.error, why:
goffi@necton2
parents:
diff changeset
303 return None
goffi@necton2
parents:
diff changeset
304
goffi@necton2
parents:
diff changeset
305 def connectionMade(self):
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
306 debug("connectionMade (mode = %s)" % "requester" if isinstance(self.factory, Socks5ServerFactory) else "target")
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
307
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
308 if isinstance(self.factory, Socks5ClientFactory):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
309 self.sid = self.factory.sid
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
310 self.data = self.factory.data
0
goffi@necton2
parents:
diff changeset
311 self.state = STATE_TARGET_INITIAL
goffi@necton2
parents:
diff changeset
312 self._startNegotiation()
goffi@necton2
parents:
diff changeset
313
goffi@necton2
parents:
diff changeset
314 def connectRequested(self, addr, port):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
315 debug("connectRequested")
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
316
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
317 # Check that this session if expected
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
318 if not self.factory.hash_sid_map.has_key(addr):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
319 #no: we refuse it
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
320 self.sendErrorReply(socks5.REPLY_CONN_REFUSED)
0
goffi@necton2
parents:
diff changeset
321 return
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
322 self.sid = self.factory.hash_sid_map[addr]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
323 self.factory.current_stream[self.sid]["start_transfer_cb"] = self.startTransfer
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
324 self.connectCompleted(addr, 0)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
325 self.transport.stopReading()
0
goffi@necton2
parents:
diff changeset
326
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
327 def startTransfer(self, file_obj):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
328 """Callback called when the result iq is received"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
329 d = self.beginFileTransfer(file_obj, self.transport)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
330 d.addCallback(self.fileTransfered)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
331
0
goffi@necton2
parents:
diff changeset
332 def fileTransfered(self, d):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
333 info(_("File transfer completed, closing connection"))
0
goffi@necton2
parents:
diff changeset
334 self.transport.loseConnection()
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
335 self.factory.finishedCb(self.sid, True)
0
goffi@necton2
parents:
diff changeset
336
goffi@necton2
parents:
diff changeset
337 def connectCompleted(self, remotehost, remoteport):
goffi@necton2
parents:
diff changeset
338 debug("connectCompleted")
goffi@necton2
parents:
diff changeset
339 if self.addressType == ADDR_IPV4:
goffi@necton2
parents:
diff changeset
340 result = struct.pack('!BBBBIH', SOCKS5_VER, REPLY_SUCCESS, 0, 1, remotehost, remoteport)
goffi@necton2
parents:
diff changeset
341 elif self.addressType == ADDR_DOMAINNAME:
goffi@necton2
parents:
diff changeset
342 result = struct.pack('!BBBBB%dsH' % len(remotehost), SOCKS5_VER, REPLY_SUCCESS, 0,
goffi@necton2
parents:
diff changeset
343 ADDR_DOMAINNAME, len(remotehost), remotehost, remoteport)
goffi@necton2
parents:
diff changeset
344 self.transport.write(result)
goffi@necton2
parents:
diff changeset
345 self.state = STATE_READY
goffi@necton2
parents:
diff changeset
346
goffi@necton2
parents:
diff changeset
347 def bindRequested(self, addr, port):
goffi@necton2
parents:
diff changeset
348 pass
goffi@necton2
parents:
diff changeset
349
goffi@necton2
parents:
diff changeset
350 def authenticateUserPass(self, user, passwd):
goffi@necton2
parents:
diff changeset
351 debug("User/pass: %s/%s", user, passwd)
goffi@necton2
parents:
diff changeset
352 return True
goffi@necton2
parents:
diff changeset
353
goffi@necton2
parents:
diff changeset
354 def dataReceived(self, buf):
goffi@necton2
parents:
diff changeset
355 if self.state == STATE_TARGET_READY:
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
356 self.data["file_obj"].write(buf)
0
goffi@necton2
parents:
diff changeset
357 return
goffi@necton2
parents:
diff changeset
358
goffi@necton2
parents:
diff changeset
359 self.buf = self.buf + buf
goffi@necton2
parents:
diff changeset
360 if self.state == STATE_INITIAL:
goffi@necton2
parents:
diff changeset
361 self._parseNegotiation()
goffi@necton2
parents:
diff changeset
362 if self.state == STATE_AUTH_USERPASS:
goffi@necton2
parents:
diff changeset
363 self._parseUserPass()
goffi@necton2
parents:
diff changeset
364 if self.state == STATE_REQUEST:
goffi@necton2
parents:
diff changeset
365 self._parseRequest()
goffi@necton2
parents:
diff changeset
366 if self.state == STATE_TARGET_AUTH:
goffi@necton2
parents:
diff changeset
367 ver, method = struct.unpack('!BB', buf)
goffi@necton2
parents:
diff changeset
368 self.buf = self.buf[2:]
goffi@necton2
parents:
diff changeset
369 if ver!=SOCKS5_VER or method!=AUTHMECH_ANON:
goffi@necton2
parents:
diff changeset
370 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
371 else:
goffi@necton2
parents:
diff changeset
372 self._makeRequest()
goffi@necton2
parents:
diff changeset
373 if self.state == STATE_TARGET_REQUEST:
goffi@necton2
parents:
diff changeset
374 self._parseRequestReply()
goffi@necton2
parents:
diff changeset
375
goffi@necton2
parents:
diff changeset
376
goffi@necton2
parents:
diff changeset
377 def clientConnectionLost(self, reason):
goffi@necton2
parents:
diff changeset
378 debug("clientConnectionLost")
goffi@necton2
parents:
diff changeset
379 self.transport.loseConnection()
goffi@necton2
parents:
diff changeset
380
goffi@necton2
parents:
diff changeset
381 def connectionLost(self, reason):
goffi@necton2
parents:
diff changeset
382 debug("connectionLost")
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
383 if self.state != STATE_CONNECT_PENDING:
0
goffi@necton2
parents:
diff changeset
384 self.transport.unregisterProducer()
goffi@necton2
parents:
diff changeset
385 if self.peersock != None:
goffi@necton2
parents:
diff changeset
386 self.peersock.peersock = None
goffi@necton2
parents:
diff changeset
387 self.peersock.transport.unregisterProducer()
goffi@necton2
parents:
diff changeset
388 self.peersock = None
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
389
0
goffi@necton2
parents:
diff changeset
390
goffi@necton2
parents:
diff changeset
391 class Socks5ServerFactory(protocol.ServerFactory):
goffi@necton2
parents:
diff changeset
392 protocol = SOCKSv5
goffi@necton2
parents:
diff changeset
393
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
394 def __init__(self, current_stream, hash_sid_map, finishedCb):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
395 self.current_stream = current_stream
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
396 self.hash_sid_map = hash_sid_map
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
397 self.finishedCb = finishedCb
0
goffi@necton2
parents:
diff changeset
398
goffi@necton2
parents:
diff changeset
399 def startedConnecting(self, connector):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
400 debug (_("Socks 5 server connection started"))
0
goffi@necton2
parents:
diff changeset
401
goffi@necton2
parents:
diff changeset
402 def clientConnectionLost(self, connector, reason):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
403 debug (_("Socks 5 server connection lost (reason: %s)"), reason)
0
goffi@necton2
parents:
diff changeset
404
goffi@necton2
parents:
diff changeset
405 class Socks5ClientFactory(protocol.ClientFactory):
goffi@necton2
parents:
diff changeset
406 protocol = SOCKSv5
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
407
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
408 def __init__(self, current_stream, sid, iq_id, activateCb, finishedCb):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
409 self.data = current_stream[sid]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
410 self.sid = sid
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
411 self.iq_id = iq_id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
412 self.activateCb = activateCb
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
413 self.finishedCb = finishedCb
0
goffi@necton2
parents:
diff changeset
414
goffi@necton2
parents:
diff changeset
415 def startedConnecting(self, connector):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
416 debug (_("Socks 5 client connection started"))
0
goffi@necton2
parents:
diff changeset
417
goffi@necton2
parents:
diff changeset
418 def clientConnectionLost(self, connector, reason):
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
419 debug (_("Socks 5 client connection lost"))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
420 self.finishedCb(self.sid, reason.type == jab_error.ConnectionDone) #TODO: really check if the state is actually successful
0
goffi@necton2
parents:
diff changeset
421
goffi@necton2
parents:
diff changeset
422
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
423 class XEP_0065():
15
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
424
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
425 NAMESPACE = NS_BS
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
426
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 15
diff changeset
427 params = """
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 15
diff changeset
428 <params>
60
9764e027ecc0 SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents: 57
diff changeset
429 <general>
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
430 <category name="File Transfer">
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
431 <param name="IP" value='0.0.0.0' default_cb='yes' type="string" />
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 15
diff changeset
432 <param name="Port" value="28915" type="string" />
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 15
diff changeset
433 </category>
60
9764e027ecc0 SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents: 57
diff changeset
434 </general>
19
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 15
diff changeset
435 </params>
f2a745ca0fbc refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents: 15
diff changeset
436 """
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
437
0
goffi@necton2
parents:
diff changeset
438 def __init__(self, host):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
439 info(_("Plugin XEP_0065 initialization"))
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
440
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
441 #session data
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
442 self.current_stream = {} #key: stream_id, value: data(dict)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
443 self.hash_sid_map = {} #key: hash of the transfer session, value: session id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
444
0
goffi@necton2
parents:
diff changeset
445 self.host = host
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
446 debug(_("registering"))
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
447 self.server_factory = Socks5ServerFactory(self.current_stream, self.hash_sid_map, self._killId)
21
633c5ed65701 parameters: new button type (not finished)
Goffi <goffi@goffi.org>
parents: 20
diff changeset
448
633c5ed65701 parameters: new button type (not finished)
Goffi <goffi@goffi.org>
parents: 20
diff changeset
449 #parameters
38
3e24753b9e0b Fixed parameters loading/saving
Goffi <goffi@goffi.org>
parents: 22
diff changeset
450 host.memory.importParams(XEP_0065.params)
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
451 host.memory.setDefault("IP", "File Transfer", self.getExternalIP)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
452 port = int(self.host.memory.getParamA("Port", "File Transfer"))
21
633c5ed65701 parameters: new button type (not finished)
Goffi <goffi@goffi.org>
parents: 20
diff changeset
453
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
454 info(_("Launching Socks5 Stream server on port %d"), port)
0
goffi@necton2
parents:
diff changeset
455 reactor.listenTCP(port, self.server_factory)
15
218ec9984fa5 wokkel integration part III + memory saved again
Goffi <goffi@goffi.org>
parents: 9
diff changeset
456
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents: 69
diff changeset
457 def getHandler(self, profile):
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
458 return XEP_0065_handler(self)
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
459
20
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
460 def getExternalIP(self):
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
461 """Return IP visible from outside, by asking to a website"""
fc8c202cda87 refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents: 19
diff changeset
462 return getPage("http://www.goffi.org/sat_tools/get_ip.php")
0
goffi@necton2
parents:
diff changeset
463
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
464 def getProgress(self, sid, data):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
465 """Fill data with position of current transfer"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
466 try:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
467 file_obj = self.current_stream[sid]["file_obj"]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
468 data["position"] = str(file_obj.tell())
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
469 data["size"] = str(self.current_stream[sid]["size"])
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
470 except:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
471 pass
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
472
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
473 def _timeOut(self, sid):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
474 """Delecte current_stream id, called after timeout
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
475 @param id: id of self.current_stream"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
476 info(_("Socks5 Bytestream: TimeOut reached for id %s") % sid);
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
477 self._killId(sid, False, "TIMEOUT")
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
478
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
479 def _killId(self, sid, success=False, failure_reason="UNKNOWN"):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
480 """Delete an current_stream id, clean up associated observers
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
481 @param sid: id of self.current_stream"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
482 if not self.current_stream.has_key(sid):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
483 warning(_("kill id called on a non existant id"))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
484 return
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
485 if self.current_stream[sid].has_key("observer_cb"):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
486 xmlstream = self.current_stream[sid]["xmlstream"]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
487 xmlstream.removeObserver(self.current_stream[sid]["event_data"], self.current_stream[sid]["observer_cb"])
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
488 if self.current_stream[sid]['timer'].active():
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
489 self.current_stream[sid]['timer'].cancel()
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
490 if self.current_stream[sid].has_key("size"):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
491 self.host.removeProgressCB(sid)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
492
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
493 file_obj = self.current_stream[sid]['file_obj']
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
494 success_cb = self.current_stream[sid]['success_cb']
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
495 failure_cb = self.current_stream[sid]['failure_cb']
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
496
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
497 del self.current_stream[sid]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
498 if self.hash_sid_map.has_key(sid):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
499 del self.hash_sid_map[sid]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
500
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
501 if success:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
502 success_cb(sid, file_obj, NS_BS)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
503 else:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
504 failure_cb(sid, file_obj, NS_BS, failure_reason)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
505
0
goffi@necton2
parents:
diff changeset
506 def setData(self, data, id):
goffi@necton2
parents:
diff changeset
507 self.data = data
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
508 self.transfer_id = id
0
goffi@necton2
parents:
diff changeset
509
goffi@necton2
parents:
diff changeset
510 def sendFile(self, id, filepath, size):
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
511 #lauching socks5 requester
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
512 debug(_("Launching socks5 requester"))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
513 self.server_factory.protocol.mode = "requester"
0
goffi@necton2
parents:
diff changeset
514 self.server_factory.protocol.filepath = filepath
goffi@necton2
parents:
diff changeset
515 self.server_factory.protocol.filesize = size
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
516 self.server_factory.protocol.transfer_id = id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
517
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
518
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
519 def startStream(self, file_obj, to_jid, sid, length, successCb, failureCb, size = None, profile='@NONE@'):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
520 """Launch the stream workflow
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
521 @param file_obj: file_obj to send
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
522 @param to_jid: JID of the recipient
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
523 @param sid: Stream session id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
524 @param length: number of byte to send, or None to send until the end
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
525 @param successCb: method to call when stream successfuly finished
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
526 @param failureCb: method to call when something goes wrong
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
527 @param profile: %(doc_profile)s"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
528 if length != None:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
529 error(_('stream length not managed yet'))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
530 return;
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
531 profile_jid, xmlstream = self.host.getJidNStream(profile)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
532 data = self.current_stream[sid] = {}
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
533 data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
534 data["file_obj"] = file_obj
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
535 data["to"] = to_jid
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
536 data["success_cb"] = successCb
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
537 data["failure_cb"] = failureCb
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
538 data["xmlstream"] = xmlstream
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
539 data["hash"] = calculateHash(profile_jid, to_jid, sid)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
540 self.hash_sid_map[data["hash"]] = sid
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
541 if size:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
542 data["size"] = size
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
543 self.host.registerProgressCB(sid, self.getProgress)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
544 iq_elt = client.IQ(xmlstream,'set')
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
545 iq_elt["from"] = profile_jid.full()
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
546 iq_elt["to"] = to_jid.full()
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
547 query_elt = iq_elt.addElement('query', NS_BS)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
548 query_elt['mode'] = 'tcp'
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
549 query_elt['sid'] = sid
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
550 streamhost = query_elt.addElement('streamhost')
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
551 streamhost['host'] = "127.0.0.1" #self.host.memory.getParamA("IP", "File Transfer")
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
552 streamhost['port'] = self.host.memory.getParamA("Port", "File Transfer")
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
553 streamhost['jid'] = profile_jid.full()
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
554 iq_elt.addCallback(self.iqResult, sid)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
555 iq_elt.send()
0
goffi@necton2
parents:
diff changeset
556
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
557 def iqResult(self, sid, iq_elt):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
558 """Called when the result of open iq is received"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
559 if iq_elt["type"] == "error":
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
560 warning(_("Transfer failed"))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
561 return
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
562
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
563 try:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
564 data = self.current_stream[sid]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
565 callback = data["start_transfer_cb"]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
566 file_obj = data["file_obj"]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
567 timer = data["timer"]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
568 except KeyError:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
569 error(_("Internal error, can't do transfer"))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
570 return
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
571
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
572 if timer.active():
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
573 timer.cancel()
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
574
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
575 callback(file_obj)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
576
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
577
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
578 def prepareToReceive(self, from_jid, sid, file_obj, size, success_cb, failure_cb):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
579 """Called when a bytestream is imminent
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
580 @param from_jid: jid of the sender
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
581 @param sid: Stream id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
582 @param file_obj: File object where data will be written
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
583 @param size: full size of the data, or None if unknown
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
584 @param success_cb: method to call when successfuly finished
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
585 @param failure_cb: method to call when something goes wrong"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
586 data = self.current_stream[sid] = {}
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
587 data["from"] = from_jid
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
588 data["file_obj"] = file_obj
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
589 data["seq"] = -1
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
590 if size:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
591 data["size"] = size
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
592 self.host.registerProgressCB(sid, self.getProgress)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
593 data["timer"] = reactor.callLater(TIMEOUT, self._timeOut, sid)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
594 data["success_cb"] = success_cb
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
595 data["failure_cb"] = failure_cb
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
596
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
597
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
598 def streamQuery(self, iq_elt, profile):
0
goffi@necton2
parents:
diff changeset
599 """Get file using byte stream"""
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
600 debug(_("BS stream query"))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
601 profile_jid, xmlstream = self.host.getJidNStream(profile)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
602 iq_elt.handled = True
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
603 query_elt = iq_elt.firstChildElement()
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
604 sid = query_elt.getAttribute("sid")
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
605 streamhost_elts = filter(lambda elt: elt.name == 'streamhost', query_elt.elements())
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
606
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
607 if not sid in self.current_stream:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
608 warning(_("Ignoring unexpected BS transfer: %s" % sid))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
609 self.sendNotAcceptableError(iq_elt['id'], iq_elt['from'], xmlstream)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
610 return
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
611
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
612 self.current_stream[sid]["to"] = jid.JID(iq_elt["to"])
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
613 self.current_stream[sid]["xmlstream"] = xmlstream
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
614
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
615 if not streamhost_elts:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
616 warning(_("No streamhost found in stream query %s" % sid))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
617 self.sendBadRequestError(iq_elt['id'], iq_elt['from'], xmlstream)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
618 return
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
619
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
620 streamhost_elt = streamhost_elts[0] #TODO: manage several streamhost elements case
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
621 sh_host = streamhost_elt.getAttribute("host")
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
622 sh_port = streamhost_elt.getAttribute("port")
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
623 sh_jid = streamhost_elt.getAttribute("jid")
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
624 if not sh_host or not sh_port or not sh_jid:
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
625 warning(_("incomplete streamhost element"))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
626 self.sendBadRequestError(iq_elt['id'], iq_elt['from'], xmlstream)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
627 return
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
628
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
629 self.current_stream[sid]["streamhost"] = (sh_host, sh_port, sh_jid)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
630
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
631 info (_("Stream proposed: host=[%(host)s] port=[%(port)s]") % {'host':sh_host, 'port':sh_port})
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
632 factory = Socks5ClientFactory(self.current_stream, sid, iq_elt["id"], self.activateStream, self._killId)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
633 reactor.connectTCP(sh_host, int(sh_port), factory)
0
goffi@necton2
parents:
diff changeset
634
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
635 def activateStream(self, sid, iq_id):
69
86f1f7f6d332 i18n first draft
Goffi <goffi@goffi.org>
parents: 64
diff changeset
636 debug(_("activating stream"))
0
goffi@necton2
parents:
diff changeset
637 result = domish.Element(('', 'iq'))
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
638 data = self.current_stream[sid]
0
goffi@necton2
parents:
diff changeset
639 result['type'] = 'result'
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
640 result['id'] = iq_id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
641 result['from'] = data["to"].full()
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
642 result['to'] = data["from"].full()
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
643 query = result.addElement('query', NS_BS)
0
goffi@necton2
parents:
diff changeset
644 query['sid'] = sid
goffi@necton2
parents:
diff changeset
645 streamhost = query.addElement('streamhost-used')
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
646 streamhost['jid'] = data["streamhost"][2]
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
647 data["xmlstream"].send(result)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
648
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
649 def sendNotAcceptableError(self, iq_id, to_jid, xmlstream):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
650 """Not acceptable error used when the stream is not expected or something is going wrong
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
651 @param iq_id: IQ id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
652 @param to_jid: addressee
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
653 @param xmlstream: XML stream to use to send the error"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
654 result = domish.Element(('', 'iq'))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
655 result['type'] = 'result'
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
656 result['id'] = iq_id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
657 result['to'] = to_jid
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
658 error_el = result.addElement('error')
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
659 error_el['type'] = 'modify'
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
660 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','not-acceptable'))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
661 xmlstream.send(result)
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
662
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
663 def sendBadRequestError(self, iq_id, to_jid, xmlstream):
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
664 """Not acceptable error used when the stream is not expected or something is going wrong
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
665 @param iq_id: IQ id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
666 @param to_jid: addressee
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
667 @param xmlstream: XML stream to use to send the error"""
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
668 result = domish.Element(('', 'iq'))
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
669 result['type'] = 'result'
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
670 result['id'] = iq_id
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
671 result['to'] = to_jid
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
672 error_el = result.addElement('error')
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
673 error_el['type'] = 'cancel'
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
674 error_el.addElement(('urn:ietf:params:xml:ns:xmpp-stanzas','bad-request'))
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
675 xmlstream.send(result)
0
goffi@necton2
parents:
diff changeset
676
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
677 class XEP_0065_handler(XMPPHandler):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
678 implements(iwokkel.IDisco)
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
679
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
680 def __init__(self, plugin_parent):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
681 self.plugin_parent = plugin_parent
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
682 self.host = plugin_parent.host
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
683
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
684 def connectionInitialized(self):
394
8f3551ceee17 plugin XEP-0065: refactored and misc stuff fixed. Still not finished
Goffi <goffi@goffi.org>
parents: 291
diff changeset
685 self.xmlstream.addObserver(BS_REQUEST, self.plugin_parent.streamQuery, profile = self.parent.profile)
64
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
686
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
687
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
688 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
689 return [disco.DiscoFeature(NS_BS)]
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
690
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
691 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
d46f849664aa SàT: multi-profile, plugins updated
Goffi <goffi@goffi.org>
parents: 60
diff changeset
692 return []