changeset 907a907d8c70 in /home/hg/repos/gajim
details:http://hg.gajim.org/gajim?cmd=changeset;node=907a907d8c70
description: coding standards
diffstat:
src/common/socks5.py | 218 +++++++++++++++++++++++++-------------------------
1 files changed, 107 insertions(+), 111 deletions(-)
diffs (truncated from 615 to 300 lines):
diff -r 83a02c001360 -r 907a907d8c70 src/common/socks5.py
--- a/src/common/socks5.py Mon Aug 22 23:57:29 2011 -0400
+++ b/src/common/socks5.py Wed Aug 24 10:36:00 2011 +0200
@@ -67,7 +67,7 @@
"""
def __init__(self, idlequeue, complete_transfer_cb=None,
- progress_transfer_cb=None, error_cb=None):
+ progress_transfer_cb=None, error_cb=None):
self.connected = 0
self.readers = {}
self.files_props = {}
@@ -85,7 +85,7 @@
self.on_failure = {} # {id: cb}
def start_listener(self, port, sha_str, sha_handler, fp, fingerprint=None,
- type='sender'):
+ type='sender'):
"""
Start waiting for incomming connections on (host, port) and do a socks5
authentication using sid for generated SHA
@@ -95,15 +95,15 @@
self.sha_handlers[sha_str] = (sha_handler, sid)
if self.listener is None:
self.listener = Socks5Listener(self.idlequeue, port, fp,
- fingerprint=fingerprint)
+ fingerprint=fingerprint)
self.listener.queue = self
self.listener.bind()
if self.listener.started is False:
self.listener = None
# We cannot bind port, call error callback and fail
self.error_cb(_('Unable to bind to port %s.') % port,
- _('Maybe you have another running instance of Gajim.
File '
- 'Transfer will be cancelled.'))
+ _('Maybe you have another running instance of Gajim. File '
+ 'Transfer will be cancelled.'))
return None
self.connected += 1
return self.listener
@@ -132,7 +132,7 @@
self.on_failure[sid] = on_failure
file_props = self.files_props[account][sid]
file_props['failure_cb'] = on_failure
-
+
# add streamhosts to the queue
for streamhost in file_props['streamhosts']:
if 'type' in streamhost and streamhost['type'] == 'proxy':
@@ -142,19 +142,17 @@
if receiving:
self.type = 'receiver'
socks5obj = Socks5Receiver(self.idlequeue, streamhost, sid,
- 'client', file_props,
- fingerprint=fp)
+ 'client', file_props, fingerprint=fp)
self.add_sockobj(account, socks5obj)
- else:
+ else:
self.type = 'sender'
- socks5obj = Socks5Sender(self.idlequeue,
- file_props['sha_str'], self, mode='client' ,
- _sock=None, host=str(streamhost['host']),
- port=int(streamhost['port']), fingerprint=fp,
- connected=False, file_props=file_props)
+ socks5obj = Socks5Sender(self.idlequeue, file_props['sha_str'],
+ self, mode='client' , _sock=None,
+ host=str(streamhost['host']), port=int(streamhost['port']),
+ fingerprint=fp, connected=False, file_props=file_props)
socks5obj.streamhost = streamhost
self.add_sockobj(account, socks5obj, type='sender')
-
+
streamhost['idx'] = socks5obj.queue_idx
def _socket_connected(self, streamhost, file_props):
@@ -206,7 +204,7 @@
# FIXME: make the sender reconnect also
print 'reconnecting using socks receiver'
client = Socks5Receiver(self.idlequeue, host, host['sid'],
- 'client',file_props)
+ 'client',file_props)
self.add_sockobj(client.account, client)
host['idx'] = client.queue_idx
# we still have chances to connect
@@ -235,7 +233,7 @@
# failure_cb exists - this means that it has never been called
if 'failure_cb' in file_props and file_props['failure_cb']:
file_props['failure_cb'](streamhost['initiator'], None,
- file_props['sid'], code = 404)
+ file_props['sid'], code = 404)
del(file_props['failure_cb'])
def add_sockobj(self, account, sockobj, type='receiver'):
@@ -264,9 +262,8 @@
Adds the sockobj to the current list of sockobjects
'''
keys = (fp['sid'], fp['name'], hash)
- sockobjects[keys] = sockobj
-
-
+ sockobjects[keys] = sockobj
+
def result_sha(self, sha_str, idx):
if sha_str in self.sha_handlers:
props = self.sha_handlers[sha_str]
@@ -303,8 +300,8 @@
def send_file(self, file_props, account, type):
for key in self.senders.keys():
if file_props['name'] in key and file_props['sid'] in key \
- and self.senders[key].mode == type:
-
+ and self.senders[key].mode == type:
+
log.info("socks5: sending file")
sender = self.senders[key]
file_props['streamhost-used'] = True
@@ -318,7 +315,6 @@
file_props['last-time'] = self.idlequeue.current_time()
file_props['received-len'] = 0
sender.file_props = file_props
-
def add_file_props(self, account, file_props):
"""
@@ -354,7 +350,7 @@
if sid in fl_props:
return fl_props[sid]
return None
-
+
def isHashInSockObjs(self, sockobjs, hash):
'''
It tells wether there is a particular hash in sockobjs or not
@@ -367,16 +363,16 @@
def on_connection_accepted(self, sock, listener):
sock_hash = sock.__hash__()
if self.type == 'sender' and \
- not self.isHashInSockObjs(self.senders, sock_hash):
-
- sockobj = Socks5Sender(self.idlequeue, sock_hash, self,
- 'server', sock[0], sock[1][0], sock[1][1],
- fingerprint='server', file_props=listener.file_props)
+ not self.isHashInSockObjs(self.senders, sock_hash):
+
+ sockobj = Socks5Sender(self.idlequeue, sock_hash, self, 'server',
+ sock[0], sock[1][0], sock[1][1], fingerprint='server',
+ file_props=listener.file_props)
self._add(sockobj, self.senders, listener.file_props, sock_hash)
# Start waiting for data
self.idlequeue.plug_idle(sockobj, False, True)
self.connected += 1
-
+
if self.type == 'receiver' and \
not self.isHashInSockObjs(self.readers, sock_hash):
sh = {}
@@ -384,19 +380,18 @@
sh['port'] = sock[1][1]
sh['initiator'] = None
sh['target'] = None
-
- sockobj = Socks5Receiver(idlequeue=self.idlequeue,
- streamhost=sh,sid=None,
- file_props=listener.file_props,
- mode='server',fingerprint='server')
-
+
+ sockobj = Socks5Receiver(idlequeue=self.idlequeue,
+ streamhost=sh,sid=None, file_props=listener.file_props,
+ mode='server',fingerprint='server')
+
self._add(sockobj, self.readers, listener.file_props, sock_hash)
-
+
sockobj.set_sock(sock[0])
sockobj.queue = self
self.connected += 1
-
-
+
+
def process_result(self, result, actor):
"""
Take appropriate actions upon the result:
@@ -461,7 +456,7 @@
try:
self.host = host
self.ais = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
- socket.SOCK_STREAM)
+ socket.SOCK_STREAM)
except socket.gaierror:
self.ais = None
self.idlequeue = idlequeue
@@ -485,12 +480,12 @@
Must be implemented by subclass.
"""
pass
-
+
def _is_connected(self):
if self.state < 5:
return False
return True
-
+
def connect(self):
"""
Create the socket and plug it to the idlequeue
@@ -532,8 +527,8 @@
errnum = ee[0]
self.connect_timeout += 1
if errnum == 111 or self.connect_timeout > 1000:
- self.queue._connection_refused(self.streamhost,
- self.file_props, self.queue_idx)
+ self.queue._connection_refused(self.streamhost,
self.file_props,
+ self.queue_idx)
self.connected = False
return None
# win32 needs this
@@ -554,7 +549,7 @@
self.queue._socket_connected(self.streamhost, self.file_props)
self.idlequeue.plug_idle(self, True, False)
return 1 # we are connected
-
+
def svr_main(self):
"""
Initial requests for verifying the connection
@@ -575,7 +570,7 @@
# unplug & plug for writing
self.idlequeue.plug_idle(self, True, False)
return None
-
+
def clnt_main(self, timeout=0):
"""
Begin negotiation. on success 'address' != 0
@@ -615,7 +610,7 @@
self.state = 5 # for senders: init file_props and send '\n'
if self.queue.on_success:
result = self.queue.send_success_reply(self.file_props,
- self.streamhost)
+ self.streamhost)
if result == 0:
self.state = 8
@@ -641,7 +636,7 @@
else:
# receiving file contents from socket
self.idlequeue.plug_idle(self, False, True)
-
+
self.file_props['continue_cb'] = self.continue_paused_transfer
# we have set up the connection, next - retrieve file
self.state = 6
@@ -649,13 +644,13 @@
self.idlequeue.plug_idle(self, True, False)
self.state += 1
return None
-
+
def pollout(self):
if self.mode == 'client':
self.clnt_pollout()
elif self.mode == 'server':
self.svr_pollout()
-
+
def svr_pollout(self):
if not self.connected:
self.disconnect()
@@ -685,7 +680,7 @@
self.state += 1
# unplug and plug this time for reading
self.idlequeue.plug_idle(self, False, True)
-
+
def clnt_pollout(self):
self.idlequeue.remove_timeout(self.fd)
try:
@@ -700,26 +695,25 @@
if self.file_props['paused']:
self.idlequeue.plug_idle(self, False, False)
return
- result = self.start_transfer() # send
+ result = self.start_transfer() # send
self.queue.process_result(result, self)
return
except (OpenSSL.SSL.WantReadError, OpenSSL.SSL.WantWriteError,
- OpenSSL.SSL.WantX509LookupError), e:
+ OpenSSL.SSL.WantX509LookupError), e:
log.info('caught SSL exception, ignored')
return
self.state += 1
# unplug and plug for reading
self.idlequeue.plug_idle(self, False, True)
self.idlequeue.set_read_timeout(self.fd, CONNECT_TIMEOUT)
-
-
_______________________________________________
Commits mailing list
[email protected]
http://lists.gajim.org/cgi-bin/listinfo/commits