coding standards

This commit is contained in:
Yann Leboulanger 2010-08-24 23:12:34 +02:00
parent 092bde6389
commit b8914c6219
1 changed files with 70 additions and 59 deletions

View File

@ -27,7 +27,6 @@ from common import helpers
from common.socks5 import Socks5Receiver from common.socks5 import Socks5Receiver
import logging import logging
log = logging.getLogger('gajim.c.jingle_ft') log = logging.getLogger('gajim.c.jingle_ft')
STATE_NOT_STARTED = 0 STATE_NOT_STARTED = 0
@ -37,17 +36,19 @@ STATE_TRANSPORT_INFO = 3
STATE_PROXY_ACTIVATED = 4 STATE_PROXY_ACTIVATED = 4
class JingleFileTransfer(JingleContent): class JingleFileTransfer(JingleContent):
def __init__(self, session, transport=None, file_props=None, use_security=False): def __init__(self, session, transport=None, file_props=None,
use_security=False):
JingleContent.__init__(self, session, transport) JingleContent.__init__(self, session, transport)
log.info("transport value: %s" % transport) log.info("transport value: %s" % transport)
#events we might be interested in # events we might be interested in
self.callbacks['session-initiate'] += [self.__on_session_initiate] self.callbacks['session-initiate'] += [self.__on_session_initiate]
self.callbacks['session-accept'] += [self.__on_session_accept] self.callbacks['session-accept'] += [self.__on_session_accept]
self.callbacks['session-terminate'] += [self.__on_session_terminate] self.callbacks['session-terminate'] += [self.__on_session_terminate]
self.callbacks['transport-accept'] += [self.__on_transport_accept] self.callbacks['transport-accept'] += [self.__on_transport_accept]
self.callbacks['transport-replace'] += [self.__on_transport_replace] #fallback transport method self.callbacks['transport-replace'] += [self.__on_transport_replace]
# fallback transport method
self.callbacks['transport-reject'] += [self.__on_transport_reject] self.callbacks['transport-reject'] += [self.__on_transport_reject]
self.callbacks['transport-info'] += [self.__on_transport_info] self.callbacks['transport-info'] += [self.__on_transport_info]
self.callbacks['iq-result'] += [self.__on_iq_result] self.callbacks['iq-result'] += [self.__on_iq_result]
@ -71,7 +72,6 @@ class JingleFileTransfer(JingleContent):
log.info("FT request: %s" % file_props) log.info("FT request: %s" % file_props)
if transport is None: if transport is None:
self.transport = JingleTransportSocks5() self.transport = JingleTransportSocks5()
self.transport.set_file_props(self.file_props) self.transport.set_file_props(self.file_props)
@ -93,7 +93,8 @@ class JingleFileTransfer(JingleContent):
file_props['session-type'] = 'jingle' file_props['session-type'] = 'jingle'
self.use_security = bool(content.getTag('security')) self.use_security = bool(content.getTag('security'))
# TODO: extract fingerprint element, encryption method element for later use # TODO: extract fingerprint element, encryption method element for later
# use
file_tag = content.getTag('description').getTag('offer').getTag('file') file_tag = content.getTag('description').getTag('offer').getTag('file')
for attribute in file_tag.getAttrs(): for attribute in file_tag.getAttrs():
@ -119,7 +120,8 @@ class JingleFileTransfer(JingleContent):
self.transport.set_connection(self.session.connection) self.transport.set_connection(self.session.connection)
self.transport.set_file_props(self.file_props) self.transport.set_file_props(self.file_props)
if self.file_props.has_key("streamhosts"): if self.file_props.has_key("streamhosts"):
self.file_props['streamhosts'].extend(self.transport.remote_candidates) self.file_props['streamhosts'].extend(
self.transport.remote_candidates)
else: else:
self.file_props['streamhosts'] = self.transport.remote_candidates self.file_props['streamhosts'] = self.transport.remote_candidates
for host in self.file_props['streamhosts']: for host in self.file_props['streamhosts']:
@ -136,7 +138,6 @@ class JingleFileTransfer(JingleContent):
if not security: # responder can not verify our fingerprint if not security: # responder can not verify our fingerprint
self.use_security = False self.use_security = False
def __on_session_terminate(self, stanza, content, error, action): def __on_session_terminate(self, stanza, content, error, action):
log.info("__on_session_terminate") log.info("__on_session_terminate")
@ -154,7 +155,8 @@ class JingleFileTransfer(JingleContent):
if not self.weinitiate: # proxy activated from initiator if not self.weinitiate: # proxy activated from initiator
return return
streamhost_cid = content.getTag('transport').getTag('candidate-used').getAttr('cid') streamhost_cid = content.getTag('transport').getTag('candidate-used').\
getAttr('cid')
streamhost_used = None streamhost_used = None
for cand in self.transport.candidates: for cand in self.transport.candidates:
if cand['candidate_id'] == streamhost_cid: if cand['candidate_id'] == streamhost_cid:
@ -177,50 +179,60 @@ class JingleFileTransfer(JingleContent):
self.file_props['is_a_proxy'] = True self.file_props['is_a_proxy'] = True
receiver = Socks5Receiver(gajim.idlequeue, streamhost_used, receiver = Socks5Receiver(gajim.idlequeue, streamhost_used,
self.file_props['sid'], self.file_props) self.file_props['sid'], self.file_props)
gajim.socks5queue.add_receiver(self.session.connection.name, receiver) gajim.socks5queue.add_receiver(self.session.connection.name,
receiver)
streamhost_used['idx'] = receiver.queue_idx streamhost_used['idx'] = receiver.queue_idx
gajim.socks5queue.on_success = self.transport._on_proxy_auth_ok gajim.socks5queue.on_success = self.transport._on_proxy_auth_ok
else: else:
jid = gajim.get_jid_without_resource(self.session.ourjid) jid = gajim.get_jid_without_resource(self.session.ourjid)
gajim.socks5queue.send_file(self.file_props, self.session.connection.name) gajim.socks5queue.send_file(self.file_props,
self.session.connection.name)
def __on_iq_result(self, stanza, content, error, action): def __on_iq_result(self, stanza, content, error, action):
log.info("__on_iq_result") log.info("__on_iq_result")
if self.weinitiate and self.state == STATE_NOT_STARTED: if self.weinitiate and self.state == STATE_NOT_STARTED:
self.state = STATE_INITIALIZED self.state = STATE_INITIALIZED
self.session.connection.files_props[self.file_props['sid']] = self.file_props self.session.connection.files_props[self.file_props['sid']] = \
self.file_props
receiver = self.file_props['receiver'] receiver = self.file_props['receiver']
sender = self.file_props['sender'] sender = self.file_props['sender']
sha_str = helpers.get_auth_sha(self.file_props['sid'], sender, receiver) sha_str = helpers.get_auth_sha(self.file_props['sid'], sender,
receiver)
self.file_props['sha_str'] = sha_str self.file_props['sha_str'] = sha_str
port = gajim.config.get('file_transfers_port') port = gajim.config.get('file_transfers_port')
listener = gajim.socks5queue.start_listener(port, sha_str, listener = gajim.socks5queue.start_listener(port, sha_str,
self._store_socks5_sid, self.file_props['sid'], fingerprint = 'server') self._store_socks5_sid, self.file_props['sid'],
fingerprint='server')
if not listener: if not listener:
return return
# send error message, notify the user # send error message, notify the user
elif not self.weinitiate and self.state == STATE_NOT_STARTED: # session-accept iq-result elif not self.weinitiate and self.state == STATE_NOT_STARTED:
# session-accept iq-result
self.state = STATE_ACCEPTED self.state = STATE_ACCEPTED
if not gajim.socks5queue.get_file_props(self.session.connection.name, self.file_props['sid']): if not gajim.socks5queue.get_file_props(
gajim.socks5queue.add_file_props(self.session.connection.name, self.file_props) self.session.connection.name, self.file_props['sid']):
gajim.socks5queue.connect_to_hosts(self.session.connection.name, self.file_props['sid'], gajim.socks5queue.add_file_props(self.session.connection.name,
self.send_candidate_used, self._on_connect_error, self.file_props)
fingerprint = 'client') gajim.socks5queue.connect_to_hosts(self.session.connection.name,
elif not self.weinitiate and self.state == STATE_ACCEPTED: # transport-info iq-result self.file_props['sid'], self.send_candidate_used,
self._on_connect_error, fingerprint='client')
elif not self.weinitiate and self.state == STATE_ACCEPTED:
# transport-info iq-result
self.state = STATE_TRANSPORT_INFO self.state = STATE_TRANSPORT_INFO
elif self.weinitiate and self.state == STATE_INITIALIZED: # proxy activated elif self.weinitiate and self.state == STATE_INITIALIZED:
# proxy activated
self.state = STATE_PROXY_ACTIVATED self.state = STATE_PROXY_ACTIVATED
def send_candidate_used(self, streamhost): def send_candidate_used(self, streamhost):
""" """
send candidate-used stanza send candidate-used stanza
""" """
log.info("send_candidate_used") log.info('send_candidate_used')
if streamhost is None: if streamhost is None:
return return
@ -240,11 +252,11 @@ class JingleFileTransfer(JingleContent):
self.session.send_transport_info(content) self.session.send_transport_info(content)
def _on_connect_error(self, to, _id, sid, code=404): def _on_connect_error(self, to, _id, sid, code=404):
log.info("connect error, sid=" + sid) log.info('connect error, sid=' + sid)
return
def _fill_content(self, content): def _fill_content(self, content):
description_node = xmpp.simplexml.Node(tag=xmpp.NS_JINGLE_FILE_TRANSFER + ' description') description_node = xmpp.simplexml.Node(
tag=xmpp.NS_JINGLE_FILE_TRANSFER + ' description')
sioffer = xmpp.simplexml.Node(tag='offer') sioffer = xmpp.simplexml.Node(tag='offer')
file_tag = sioffer.setTag('file', namespace=xmpp.NS_FILE) file_tag = sioffer.setTag('file', namespace=xmpp.NS_FILE)
@ -257,7 +269,8 @@ class JingleFileTransfer(JingleContent):
description_node.addChild(node=sioffer) description_node.addChild(node=sioffer)
if self.use_security: if self.use_security:
security = xmpp.simplexml.Node(tag=xmpp.NS_JINGLE_XTLS + ' security') security = xmpp.simplexml.Node(
tag=xmpp.NS_JINGLE_XTLS + ' security')
# TODO: add fingerprint element # TODO: add fingerprint element
for m in ('x509', ): # supported authentication methods for m in ('x509', ): # supported authentication methods
method = xmpp.simplexml.Node(tag='method') method = xmpp.simplexml.Node(tag='method')
@ -270,10 +283,8 @@ class JingleFileTransfer(JingleContent):
def _store_socks5_sid(self, sid, hash_id): def _store_socks5_sid(self, sid, hash_id):
# callback from socsk5queue.start_listener # callback from socsk5queue.start_listener
self.file_props['hash'] = hash_id self.file_props['hash'] = hash_id
return
def get_content(desc): def get_content(desc):
return JingleFileTransfer return JingleFileTransfer
contents[xmpp.NS_JINGLE_FILE_TRANSFER] = get_content contents[xmpp.NS_JINGLE_FILE_TRANSFER] = get_content