Remove obvious code duplication.

This commit is contained in:
Stephan Erb 2009-12-10 23:22:51 +01:00
parent bb50871bce
commit 24e2047fe3
1 changed files with 19 additions and 37 deletions

View File

@ -182,13 +182,7 @@ class ConnectionBytestream:
query = iq.setTag('query', namespace=xmpp.NS_BYTESTREAM) query = iq.setTag('query', namespace=xmpp.NS_BYTESTREAM)
query.setAttr('mode', 'plain') query.setAttr('mode', 'plain')
query.setAttr('sid', file_props['sid']) query.setAttr('sid', file_props['sid'])
for ft_host in ft_add_hosts: self._add_streamhosts_to_query(query, sender, port, fd_add_hosts)
# The streamhost, if set
ostreamhost = xmpp.Node(tag='streamhost')
query.addChild(node=ostreamhost)
ostreamhost.setAttr('port', unicode(port))
ostreamhost.setAttr('host', ft_host)
ostreamhost.setAttr('jid', sender)
try: try:
# The ip we're connected to server with # The ip we're connected to server with
my_ips = [self.peerhost[0]] my_ips = [self.peerhost[0]]
@ -196,12 +190,7 @@ class ConnectionBytestream:
for addr in socket.getaddrinfo(socket.gethostname(), None): for addr in socket.getaddrinfo(socket.gethostname(), None):
if not addr[4][0] in my_ips and not addr[4][0].startswith('127'): if not addr[4][0] in my_ips and not addr[4][0].startswith('127'):
my_ips.append(addr[4][0]) my_ips.append(addr[4][0])
for ip in my_ips: self._add_streamhosts_to_query(query, sender, port, my_ips)
streamhost = xmpp.Node(tag='streamhost')
query.addChild(node=streamhost)
streamhost.setAttr('port', unicode(port))
streamhost.setAttr('host', ip)
streamhost.setAttr('jid', sender)
except socket.gaierror: except socket.gaierror:
self.dispatch('ERROR', (_('Wrong host'), self.dispatch('ERROR', (_('Wrong host'),
_('Invalid local address? :-O'))) _('Invalid local address? :-O')))
@ -211,17 +200,20 @@ class ConnectionBytestream:
file_props['proxy_receiver'] = unicode(receiver) file_props['proxy_receiver'] = unicode(receiver)
file_props['proxy_sender'] = unicode(sender) file_props['proxy_sender'] = unicode(sender)
file_props['proxyhosts'] = proxyhosts file_props['proxyhosts'] = proxyhosts
for proxyhost in proxyhosts: for proxyhost in proxyhosts:
self._add_streamhosts_to_query(query, proxyhost['jid'],
proxyhost['port'], [proxyhost['host']])
self.connection.send(iq)
def _add_streamhosts_to_query(self, query, sender, port, hosts):
for host in hosts:
streamhost = xmpp.Node(tag='streamhost') streamhost = xmpp.Node(tag='streamhost')
query.addChild(node=streamhost) query.addChild(node=streamhost)
streamhost.setAttr('port', proxyhost['port']) streamhost.setAttr('port', unicode(port))
streamhost.setAttr('host', proxyhost['host']) streamhost.setAttr('host', host)
streamhost.setAttr('jid', proxyhost['jid']) streamhost.setAttr('jid', sender)
# don't add the proxy child tag for streamhosts, which are proxies
# proxy = streamhost.setTag('proxy')
# proxy.setNamespace(common.xmpp.NS_STREAM)
self.connection.send(iq)
def send_file_rejection(self, file_props, code='403', typ=None): def send_file_rejection(self, file_props, code='403', typ=None):
""" """
@ -517,13 +509,8 @@ class ConnectionBytestream:
raise xmpp.NodeProcessed raise xmpp.NodeProcessed
def _siResultCB(self, con, iq_obj): def _siResultCB(self, con, iq_obj):
id_ = iq_obj.getAttr('id') file_props = self.files_props.get(iq_obj.getAttr('id'))
if id_ not in self.files_props: if not file_props:
# no such jid
return
file_props = self.files_props[id_]
if file_props is None:
# file properties for jid is none
return return
if 'request-id' in file_props: if 'request-id' in file_props:
# we have already sent streamhosts info # we have already sent streamhosts info
@ -603,13 +590,8 @@ class ConnectionBytestream:
profile = si.getAttr('profile') profile = si.getAttr('profile')
if profile != xmpp.NS_FILE: if profile != xmpp.NS_FILE:
return return
id_ = iq_obj.getAttr('id') file_props = self.files_props.get(iq_obj.getAttr('id'))
if id_ not in self.files_props: if not file_props:
# no such jid
return
file_props = self.files_props[id_]
if file_props is None:
# file properties for jid is none
return return
jid = self._ft_get_from(iq_obj) jid = self._ft_get_from(iq_obj)
file_props['error'] = -3 file_props['error'] = -3