coding standards

This commit is contained in:
Yann Leboulanger 2012-08-22 12:21:45 +02:00
parent f801d33b53
commit 133593e1ca
3 changed files with 35 additions and 38 deletions

View File

@ -54,7 +54,8 @@ class JingleFileTransfer(JingleContent):
# 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-initiate-sent'] += [self.__on_session_initiate_sent] self.callbacks['session-initiate-sent'] += [
self.__on_session_initiate_sent]
self.callbacks['content-add'] += [self.__on_session_initiate] self.callbacks['content-add'] += [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]
@ -105,7 +106,7 @@ class JingleFileTransfer(JingleContent):
STATE_TRANSFERING : StateTransfering(self), STATE_TRANSFERING : StateTransfering(self),
STATE_TRANSPORT_REPLACE : StateTransportReplace(self), STATE_TRANSPORT_REPLACE : StateTransportReplace(self),
STATE_CAND_SENT_AND_RECEIVED : StateCandSentAndRecv(self) STATE_CAND_SENT_AND_RECEIVED : StateCandSentAndRecv(self)
} }
def __state_changed(self, nextstate, args=None): def __state_changed(self, nextstate, args=None):
# Executes the next state action and sets the next state # Executes the next state action and sets the next state
@ -130,9 +131,8 @@ class JingleFileTransfer(JingleContent):
def __send_hash(self): def __send_hash(self):
# Send hash in a session info # Send hash in a session info
checksum = xmpp.Node(tag='checksum', checksum = xmpp.Node(tag='checksum', payload=[xmpp.Node(tag='file',
payload=[xmpp.Node(tag='file', payload=[self._calcHash()])])
payload=[self._calcHash()])])
checksum.setNamespace(xmpp.NS_JINGLE_FILE_TRANSFER) checksum.setNamespace(xmpp.NS_JINGLE_FILE_TRANSFER)
self.session.__session_info(checksum ) self.session.__session_info(checksum )

View File

@ -17,11 +17,11 @@ from jingle_transport import *
from common.socks5 import Socks5ReceiverClient, Socks5SenderClient from common.socks5 import Socks5ReceiverClient, Socks5SenderClient
class JingleFileTransferStates: class JingleFileTransferStates:
'''
# This class implements the state machine design pattern This class implements the state machine design pattern
'''
def __init__(self, jingleft): def __init__(self, jingleft):
self.jft = jingleft self.jft = jingleft
def action(self, args=None): def action(self, args=None):
@ -32,7 +32,6 @@ class JingleFileTransferStates:
class StateInitialized(JingleFileTransferStates): class StateInitialized(JingleFileTransferStates):
''' '''
This state initializes the file transfer This state initializes the file transfer
''' '''
@ -47,14 +46,12 @@ class StateInitialized(JingleFileTransferStates):
if self.jft.use_security: if self.jft.use_security:
fingerprint = 'client' fingerprint = 'client'
# Connect to the candidate host, on success call on_connect method # Connect to the candidate host, on success call on_connect method
gajim.socks5queue.connect_to_hosts( gajim.socks5queue.connect_to_hosts(self.jft.session.connection.name,
self.jft.session.connection.name, self.jft.file_props.sid, self.jft.on_connect,
self.jft.file_props.sid, self.jft.on_connect, self.jft._on_connect_error, fingerprint=fingerprint)
self.jft._on_connect_error, fingerprint=fingerprint)
class StateCandSent(JingleFileTransferStates): class StateCandSent(JingleFileTransferStates):
''' '''
This state sends our nominated candidate This state sends our nominated candidate
''' '''
@ -89,7 +86,6 @@ class StateCandSent(JingleFileTransferStates):
self._sendCand(args) self._sendCand(args)
class StateCandReceived(JingleFileTransferStates): class StateCandReceived(JingleFileTransferStates):
''' '''
This state happens when we receive a candidate. This state happens when we receive a candidate.
It takes the arguments: canError if we receive a candidate-error It takes the arguments: canError if we receive a candidate-error
@ -112,13 +108,10 @@ class StateCandReceived(JingleFileTransferStates):
# We save the candidate nominated by peer # We save the candidate nominated by peer
self.jft.nominated_cand['peer-cand'] = streamhost_used self.jft.nominated_cand['peer-cand'] = streamhost_used
def action(self, args=None): def action(self, args=None):
self._recvCand(args) self._recvCand(args)
class StateCandSentAndRecv( StateCandSent, StateCandReceived): class StateCandSentAndRecv( StateCandSent, StateCandReceived):
''' '''
This state happens when we have received and sent the candidates. This state happens when we have received and sent the candidates.
It takes the boolean argument: sendCand in order to decide whether It takes the boolean argument: sendCand in order to decide whether
@ -126,14 +119,12 @@ class StateCandSentAndRecv( StateCandSent, StateCandReceived):
''' '''
def action(self, args=None): def action(self, args=None):
if args['sendCand']: if args['sendCand']:
self._sendCand(args) self._sendCand(args)
else: else:
self._recvCand(args) self._recvCand(args)
class StateTransportReplace(JingleFileTransferStates): class StateTransportReplace(JingleFileTransferStates):
''' '''
This state initiates transport replace This state initiates transport replace
''' '''
@ -142,7 +133,6 @@ class StateTransportReplace(JingleFileTransferStates):
self.jft.session.transport_replace() self.jft.session.transport_replace()
class StateTransfering(JingleFileTransferStates): class StateTransfering(JingleFileTransferStates):
''' '''
This state will start the transfer depeding on the type of transport This state will start the transfer depeding on the type of transport
we have. we have.
@ -150,8 +140,8 @@ class StateTransfering(JingleFileTransferStates):
def __start_IBB_transfer(self, con): def __start_IBB_transfer(self, con):
fp = open(self.jft.file_props.file_name, 'r') fp = open(self.jft.file_props.file_name, 'r')
con.OpenStream( self.jft.transport.sid, con.OpenStream( self.jft.transport.sid, self.jft.session.peerjid, fp,
self.jft.session.peerjid, fp, blocksize=4096) blocksize=4096)
def __start_SOCK5_transfer(self): def __start_SOCK5_transfer(self):
# It tells wether we start the transfer as client or server # It tells wether we start the transfer as client or server
@ -172,7 +162,8 @@ class StateTransfering(JingleFileTransferStates):
self.jft.file_props.proxy_receiver = streamhost_used['target'] self.jft.file_props.proxy_receiver = streamhost_used['target']
else: else:
self.jft.file_props.proxy_sender = streamhost_used['target'] self.jft.file_props.proxy_sender = streamhost_used['target']
self.jft.file_props.proxy_receiver = streamhost_used['initiator'] self.jft.file_props.proxy_receiver = streamhost_used[
'initiator']
# This needs to be changed when requesting # This needs to be changed when requesting
if not self.jft.weinitiate and streamhost_used['type'] == 'proxy': if not self.jft.weinitiate and streamhost_used['type'] == 'proxy':

View File

@ -63,8 +63,8 @@ def load_cert_file(cert_path, cert_store):
try: try:
f = open(cert_path) f = open(cert_path)
except IOError, e: except IOError, e:
log.warning('Unable to open certificate file %s: %s' % \ log.warning('Unable to open certificate file %s: %s' % (cert_path,
(cert_path, str(e))) str(e)))
return return
lines = f.readlines() lines = f.readlines()
i = 0 i = 0
@ -76,14 +76,14 @@ def load_cert_file(cert_path, cert_store):
cert = ''.join(lines[begin:i+2]) cert = ''.join(lines[begin:i+2])
try: try:
x509cert = OpenSSL.crypto.load_certificate( x509cert = OpenSSL.crypto.load_certificate(
OpenSSL.crypto.FILETYPE_PEM, cert) OpenSSL.crypto.FILETYPE_PEM, cert)
cert_store.add_cert(x509cert) cert_store.add_cert(x509cert)
except OpenSSL.crypto.Error, exception_obj: except OpenSSL.crypto.Error, exception_obj:
log.warning('Unable to load a certificate from file %s: %s' %\ log.warning('Unable to load a certificate from file %s: %s' %\
(cert_path, exception_obj.args[0][0][2])) (cert_path, exception_obj.args[0][0][2]))
except: except:
log.warning('Unknown error while loading certificate from file ' log.warning('Unknown error while loading certificate from file '
'%s' % cert_path) '%s' % cert_path)
begin = -1 begin = -1
i += 1 i += 1
@ -94,7 +94,8 @@ def get_context(fingerprint, verify_cb=None):
ctx = SSL.Context(SSL.TLSv1_METHOD) ctx = SSL.Context(SSL.TLSv1_METHOD)
if fingerprint == 'server': # for testing purposes only if fingerprint == 'server': # for testing purposes only
ctx.set_verify(SSL.VERIFY_NONE|SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb or default_callback) ctx.set_verify(SSL.VERIFY_NONE|SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
verify_cb or default_callback)
elif fingerprint == 'client': elif fingerprint == 'client':
ctx.set_verify(SSL.VERIFY_PEER, verify_cb or default_callback) ctx.set_verify(SSL.VERIFY_PEER, verify_cb or default_callback)
@ -103,13 +104,15 @@ def get_context(fingerprint, verify_cb=None):
ctx.use_certificate_file(cert_name + '.cert') ctx.use_certificate_file(cert_name + '.cert')
store = ctx.get_cert_store() store = ctx.get_cert_store()
for f in os.listdir(os.path.expanduser(gajim.MY_PEER_CERTS_PATH)): for f in os.listdir(os.path.expanduser(gajim.MY_PEER_CERTS_PATH)):
load_cert_file(os.path.join(os.path.expanduser(gajim.MY_PEER_CERTS_PATH), f), store) load_cert_file(os.path.join(os.path.expanduser(
gajim.MY_PEER_CERTS_PATH), f), store)
log.debug('certificate file ' + f + ' loaded fingerprint ' + \ log.debug('certificate file ' + f + ' loaded fingerprint ' + \
fingerprint) fingerprint)
return ctx return ctx
def send_cert(con, jid_from, sid): def send_cert(con, jid_from, sid):
certpath = os.path.join(gajim.MY_CERT_DIR, SELF_SIGNED_CERTIFICATE) + '.cert' certpath = os.path.join(gajim.MY_CERT_DIR, SELF_SIGNED_CERTIFICATE) + \
'.cert'
certfile = open(certpath, 'r') certfile = open(certpath, 'r')
certificate = '' certificate = ''
for line in certfile.readlines(): for line in certfile.readlines():
@ -225,14 +228,17 @@ def createCertificate(req, (issuerCert, issuerKey), serial, (notBefore, notAfter
def make_certs(filepath, CN): def make_certs(filepath, CN):
""" """
make self signed certificates make self signed certificates
filepath : absolute path of certificate file, will be appended the '.pkey' and '.cert' extensions filepath : absolute path of certificate file, will be appended the '.pkey'
and '.cert' extensions
CN : common name CN : common name
""" """
key = createKeyPair(TYPE_RSA, 1024) key = createKeyPair(TYPE_RSA, 1024)
req = createCertRequest(key, CN=CN) req = createCertRequest(key, CN=CN)
cert = createCertificate(req, (req, key), 0, (0, 60*60*24*365*5)) # five years cert = createCertificate(req, (req, key), 0, (0, 60*60*24*365*5)) # five years
open(filepath + '.pkey', 'w').write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key)) open(filepath + '.pkey', 'w').write(crypto.dump_privatekey(
open(filepath + '.cert', 'w').write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) crypto.FILETYPE_PEM, key))
open(filepath + '.cert', 'w').write(crypto.dump_certificate(
crypto.FILETYPE_PEM, cert))
if __name__ == '__main__': if __name__ == '__main__':