comment out most things, add some connection/disconnection and status updating

This commit is contained in:
Stefan Bethge 2006-05-27 23:57:43 +00:00
parent 0d195c1516
commit e324eb4f32

View file

@ -1,4 +1,4 @@
## common/connection.py ## common/connection_zeroconf.py
## ##
## Contributors for this file: ## Contributors for this file:
## - Yann Le Boulanger <asterix@lagaule.org> ## - Yann Le Boulanger <asterix@lagaule.org>
@ -34,10 +34,11 @@ import signal
if os.name != 'nt': if os.name != 'nt':
signal.signal(signal.SIGPIPE, signal.SIG_DFL) signal.signal(signal.SIGPIPE, signal.SIG_DFL)
import common.xmpp # import common.xmpp
from common import helpers from common import helpers
from common import gajim from common import gajim
from common import GnuPG from common import GnuPG
from common import zeroconf
from connection_handlers_zeroconf import * from connection_handlers_zeroconf import *
USE_GPG = GnuPG.USE_GPG USE_GPG = GnuPG.USE_GPG
@ -50,17 +51,18 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
def __init__(self, name): def __init__(self, name):
ConnectionHandlersZeroconf.__init__(self) ConnectionHandlersZeroconf.__init__(self)
self.name = name self.name = name
self.zeroconf = Zeroconf()
self.connected = 0 # offline self.connected = 0 # offline
self.connection = None # xmpppy ClientCommon instance # self.connection = None # xmpppy ClientCommon instance
# this property is used to prevent double connections # this property is used to prevent double connections
self.last_connection = None # last ClientCommon instance # self.last_connection = None # last ClientCommon instance
self.gpg = None self.gpg = None
self.status = '' self.status = ''
self.old_show = '' self.old_show = ''
# increase/decrease default timeout for server responses # increase/decrease default timeout for server responses
self.try_connecting_for_foo_secs = 45 self.try_connecting_for_foo_secs = 45
# holds the actual hostname to which we are connected # holds the actual hostname to which we are connected
self.connected_hostname = None # self.connected_hostname = None
self.time_to_reconnect = None self.time_to_reconnect = None
self.new_account_info = None self.new_account_info = None
self.bookmarks = [] self.bookmarks = []
@ -68,12 +70,12 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.last_io = gajim.idlequeue.current_time() self.last_io = gajim.idlequeue.current_time()
self.last_sent = [] self.last_sent = []
self.last_history_line = {} self.last_history_line = {}
self.password = gajim.config.get_per('accounts', name, 'password') # self.password = gajim.config.get_per('accounts', name, 'password')
self.server_resource = gajim.config.get_per('accounts', name, 'resource') # self.server_resource = gajim.config.get_per('accounts', name, 'resource')
if gajim.config.get_per('accounts', self.name, 'keep_alives_enabled'): # if gajim.config.get_per('accounts', self.name, 'keep_alives_enabled'):
self.keepalives = gajim.config.get_per('accounts', self.name,'keep_alive_every_foo_secs') # self.keepalives = gajim.config.get_per('accounts', self.name,'keep_alive_every_foo_secs')
else: # else:
self.keepalives = 0 # self.keepalives = 0
self.privacy_rules_supported = False self.privacy_rules_supported = False
# Do we continue connection when we get roster (send presence,get vcard...) # Do we continue connection when we get roster (send presence,get vcard...)
self.continue_connect_info = None self.continue_connect_info = None
@ -99,8 +101,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
def _reconnect(self): def _reconnect(self):
# Do not try to reco while we are already trying '''# Do not try to reco while we are already trying
''' self.time_to_reconnect = None self.time_to_reconnect = None
if self.connected < 2: #connection failed if self.connected < 2: #connection failed
gajim.log.debug('reconnect') gajim.log.debug('reconnect')
self.retrycount += 1 self.retrycount += 1
@ -109,13 +111,14 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.connect_and_init(self.old_show, self.status, signed) self.connect_and_init(self.old_show, self.status, signed)
else: else:
# reconnect succeeded # reconnect succeeded
self.time_to_reconnect = None
self.retrycount = 0 '''
self.time_to_reconnect = None self.time_to_reconnect = None
self.retrycount = 0 self.retrycount = 0
'''
gajim.log.debug('reconnect') gajim.log.debug('reconnect')
signed = self.get_signed_msg(self.status)
# TODO: no gpg for now, add some day
# signed = self.get_signed_msg(self.status)
# We are doing disconnect at so many places, better use one function in all # We are doing disconnect at so many places, better use one function in all
@ -125,13 +128,13 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.time_to_reconnect = None self.time_to_reconnect = None
if self.connection: if self.connection:
# make sure previous connection is completely closed # make sure previous connection is completely closed
gajim.proxy65_manager.disconnect(self.connection)
self.connection.disconnect()
self.last_connection = None self.last_connection = None
self.connection = None self.connection = None
self.zeroconf.disconnect()
'''
def _disconnectedReconnCB(self): def _disconnectedReconnCB(self):
'''Called when we are disconnected''' # Called when we are disconnected
gajim.log.debug('disconnectedReconnCB') gajim.log.debug('disconnectedReconnCB')
if self.connected > 1: if self.connected > 1:
# we cannot change our status to offline or connectiong # we cannot change our status to offline or connectiong
@ -163,14 +166,17 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.disconnect() self.disconnect()
self.on_purpose = False self.on_purpose = False
# END disconenctedReconnCB # END disconenctedReconnCB
'''
'''
def _connection_lost(self): def _connection_lost(self):
self.disconnect(on_purpose = False) self.disconnect(on_purpose = False)
self.dispatch('STATUS', 'offline') self.dispatch('STATUS', 'offline')
self.dispatch('ERROR', self.dispatch('ERROR',
(_('Connection with account "%s" has been lost') % self.name, (_('Connection with account "%s" has been lost') % self.name,
_('To continue sending and receiving messages, you will need to reconnect.'))) _('To continue sending and receiving messages, you will need to reconnect.')))
'''
'''
def _event_dispatcher(self, realm, event, data): def _event_dispatcher(self, realm, event, data):
if realm == common.xmpp.NS_REGISTER: if realm == common.xmpp.NS_REGISTER:
if event == common.xmpp.features_nb.REGISTER_DATA_RECEIVED: if event == common.xmpp.features_nb.REGISTER_DATA_RECEIVED:
@ -221,6 +227,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.dispatch('STANZA_ARRIVED', unicode(data, errors = 'ignore')) self.dispatch('STANZA_ARRIVED', unicode(data, errors = 'ignore'))
elif event == common.xmpp.transports.DATA_SENT: elif event == common.xmpp.transports.DATA_SENT:
self.dispatch('STANZA_SENT', unicode(data)) self.dispatch('STANZA_SENT', unicode(data))
'''
def select_next_host(self, hosts): def select_next_host(self, hosts):
hosts_best_prio = [] hosts_best_prio = []
@ -245,15 +252,19 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
min_w = h['weight'] min_w = h['weight']
return h return h
def connect(self, data = None): def connect(self, data = None):
# TODO: remove any server connection and get zeroconf instance instead
# get presence information from there zeroconf.connect()
''' Start a connection to the Jabber server. ''' Start a connection to the Jabber server.
Returns connection, and connection type ('tls', 'ssl', 'tcp', '') Returns connection, and connection type ('tls', 'ssl', 'tcp', '')
data MUST contain name, hostname, resource, usessl, proxy, data MUST contain name, hostname, resource, usessl, proxy,
use_custom_host, custom_host (if use_custom_host), custom_port (if use_custom_host, custom_host (if use_custom_host), custom_port (if
use_custom_host), ''' use_custom_host), '''
'''
if self.connection: if self.connection:
return self.connection, '' return self.connection, ''
@ -317,14 +328,17 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
gajim.resolver.resolve('_xmpp-client._tcp.' + h.encode('utf-8'), self._on_resolve) gajim.resolver.resolve('_xmpp-client._tcp.' + h.encode('utf-8'), self._on_resolve)
else: else:
self._on_resolve('', []) self._on_resolve('', [])
'''
'''
def _on_resolve(self, host, result_array): def _on_resolve(self, host, result_array):
# SRV query returned at least one valid result, we put it in hosts dict # SRV query returned at least one valid result, we put it in hosts dict
if len(result_array) != 0: if len(result_array) != 0:
self._hosts = [i for i in result_array] self._hosts = [i for i in result_array]
self.connect_to_next_host() self.connect_to_next_host()
'''
'''
def connect_to_next_host(self, retry = False): def connect_to_next_host(self, retry = False):
if len(self._hosts): if len(self._hosts):
if self.last_connection: if self.last_connection:
self.last_connection.socket.disconnect() self.last_connection.socket.disconnect()
@ -361,7 +375,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
else: else:
# try reconnect if connection has failed before auth to server # try reconnect if connection has failed before auth to server
self._disconnectedReconnCB() self._disconnectedReconnCB()
'''
'''
def _connect_failure(self, con_type = None): def _connect_failure(self, con_type = None):
if not con_type: if not con_type:
# we are not retrying, and not conecting # we are not retrying, and not conecting
@ -370,7 +385,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.dispatch('STATUS', 'offline') self.dispatch('STATUS', 'offline')
self.dispatch('ERROR', (_('Could not connect to "%s"') % self._hostname, self.dispatch('ERROR', (_('Could not connect to "%s"') % self._hostname,
_('Check your connection or try again later.'))) _('Check your connection or try again later.')))
'''
'''
def _connect_success(self, con, con_type): def _connect_success(self, con, con_type):
if not self.connected: # We went offline during connecting process if not self.connected: # We went offline during connecting process
# FIXME - not possible, maybe it was when we used threads # FIXME - not possible, maybe it was when we used threads
@ -388,7 +404,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.get_metacontacts() self.get_metacontacts()
self._register_handlers(con, con_type) self._register_handlers(con, con_type)
return True return True
'''
'''
def _register_handlers(self, con, con_type): def _register_handlers(self, con, con_type):
self.peerhost = con.get_peerhost() self.peerhost = con.get_peerhost()
# notify the gui about con_type # notify the gui about con_type
@ -399,7 +416,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
resource = gajim.config.get_per('accounts', self.name, 'resource') resource = gajim.config.get_per('accounts', self.name, 'resource')
self.connection = con self.connection = con
con.auth(name, self.password, resource, 1, self.__on_auth) con.auth(name, self.password, resource, 1, self.__on_auth)
'''
'''
def __on_auth(self, con, auth): def __on_auth(self, con, auth):
if not con: if not con:
self.disconnect(on_purpose = True) self.disconnect(on_purpose = True)
@ -436,31 +454,40 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.on_connect_auth(None) self.on_connect_auth(None)
self.on_connect_auth = None self.on_connect_auth = None
# END connect # END connect
'''
def quit(self, kill_core): def quit(self, kill_core):
if kill_core and self.connected > 1: if kill_core and self.connected > 1:
self.disconnect(on_purpose = True) self.disconnect(on_purpose = True)
'''
#invisible == no service announced( privacy rule? )
def build_privacy_rule(self, name, action): def build_privacy_rule(self, name, action):
'''Build a Privacy rule stanza for invisibility''' #Build a Privacy rule stanza for invisibility
iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '') iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '')
l = iq.getTag('query').setTag('list', {'name': name}) l = iq.getTag('query').setTag('list', {'name': name})
i = l.setTag('item', {'action': action, 'order': '1'}) i = l.setTag('item', {'action': action, 'order': '1'})
i.setTag('presence-out') i.setTag('presence-out')
return iq return iq
'''
'''
def activate_privacy_rule(self, name): def activate_privacy_rule(self, name):
'''activate a privacy rule''' activate a privacy rule
iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '') iq = common.xmpp.Iq('set', common.xmpp.NS_PRIVACY, xmlns = '')
iq.getTag('query').setTag('active', {'name': name}) iq.getTag('query').setTag('active', {'name': name})
self.connection.send(iq) self.connection.send(iq)
'''
'''
def send_invisible_presence(self, msg, signed, initial = False): def send_invisible_presence(self, msg, signed, initial = False):
# try to set the privacy rule # try to set the privacy rule
iq = self.build_privacy_rule('invisible', 'deny') iq = self.build_privacy_rule('invisible', 'deny')
self.connection.SendAndCallForResponse(iq, self._continue_invisible, self.connection.SendAndCallForResponse(iq, self._continue_invisible,
{'msg': msg, 'signed': signed, 'initial': initial}) {'msg': msg, 'signed': signed, 'initial': initial})
'''
'''
def _continue_invisible(self, con, iq_obj, msg, signed, initial): def _continue_invisible(self, con, iq_obj, msg, signed, initial):
ptype = '' ptype = ''
show = '' show = ''
@ -491,6 +518,8 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
#Inform GUI we just signed in #Inform GUI we just signed in
self.dispatch('SIGNED_IN', ()) self.dispatch('SIGNED_IN', ())
'''
def test_gpg_passphrase(self, password): def test_gpg_passphrase(self, password):
self.gpg.passphrase = password self.gpg.passphrase = password
@ -518,90 +547,77 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.dispatch('BAD_PASSPHRASE', ()) self.dispatch('BAD_PASSPHRASE', ())
return signed return signed
def connect_and_auth(self): def connect_and_auth(self):
'''
self.on_connect_success = self._connect_success self.on_connect_success = self._connect_success
self.on_connect_failure = self._connect_failure self.on_connect_failure = self._connect_failure
self.connect() self.connect()
'''
pass
def connect_and_init(self, show, msg, signed): def connect_and_init(self, show, msg, signed):
'''
self.continue_connect_info = [show, msg, signed] self.continue_connect_info = [show, msg, signed]
self.on_connect_auth = self._init_roster self.on_connect_auth = self._init_roster
self.connect_and_auth() self.connect_and_auth()
'''
if show == 'online':
show = 'avail'
self.zeroconf.txt['status'] = show
self.zeroconf.txt['msg'] = msg
self.connect()
def _init_roster(self, con): def _init_roster(self, con):
'''
self.connection = con self.connection = con
if self.connection: if self.connection:
con.set_send_timeout(self.keepalives, self.send_keepalive) con.set_send_timeout(self.keepalives, self.send_keepalive)
self.connection.onreceive(None) self.connection.onreceive(None)
# Ask metacontacts before roster # Ask metacontacts before roster
self.get_metacontacts() self.get_metacontacts()
'''
pass
def change_status(self, show, msg, sync = False, auto = False): def change_status(self, show, msg, sync = False, auto = False):
if not show in STATUS_LIST: if not show in STATUS_LIST:
return -1 return -1
sshow = helpers.get_xmpp_show(show)
if not msg:
msg = ''
keyID = gajim.config.get_per('accounts', self.name, 'keyid')
if keyID and USE_GPG and not msg:
lowered_uf_status_msg = helpers.get_uf_show(show).lower()
# do not show I'm invisible!
if lowered_uf_status_msg == _('invisible'):
lowered_uf_status_msg = _('offline')
msg = _("I'm %s") % lowered_uf_status_msg
signed = ''
if not auto and not show == 'offline':
signed = self.get_signed_msg(msg)
self.status = msg
if show != 'offline' and not self.connected:
# set old_show to requested 'show' in case we need to
# recconect before we auth to server
self.old_show = show
self.on_purpose = False
self.connect_and_init(show, msg, signed)
if show == 'chat':
show = 'online'
elif show == 'xa':
show = 'away'
# connect
if show != 'offline' and not self.connected:
self.on_purpose = False
self.connect_and_init(show, msg, '')
# disconnect
elif show == 'offline' and self.connected: elif show == 'offline' and self.connected:
self.connected = 0 self.connected = 0
if self.connection:
self.on_purpose = True
p = common.xmpp.Presence(typ = 'unavailable')
p = self.add_sha(p, False)
if msg:
p.setStatus(msg)
self.remove_all_transfers()
self.time_to_reconnect = None
self.connection.start_disconnect(p, self._on_disconnected)
else:
self.time_to_reconnect = None
self._on_disconnected() self._on_disconnected()
# update status
elif show != 'offline' and self.connected: elif show != 'offline' and self.connected:
# dont'try to connect, when we are in state 'connecting'
if self.connected == 1:
return
was_invisible = self.connected == STATUS_LIST.index('invisible') was_invisible = self.connected == STATUS_LIST.index('invisible')
self.connected = STATUS_LIST.index(show) self.connected = STATUS_LIST.index(show)
if show == 'invisible': if show == 'invisible':
self.send_invisible_presence(msg, signed) self.zeroconf.remove_announce()
return return
if was_invisible and self.privacy_rules_supported: if was_invisible:
iq = self.build_privacy_rule('visible', 'allow') self.zeroconf.announce()
self.connection.send(iq)
self.activate_privacy_rule('visible')
prio = unicode(gajim.config.get_per('accounts', self.name,
'priority'))
p = common.xmpp.Presence(typ = None, priority = prio, show = sshow)
p = self.add_sha(p)
if msg:
p.setStatus(msg)
if signed:
p.setTag(common.xmpp.NS_SIGNED + ' x').setData(signed)
if self.connection: if self.connection:
self.connection.send(p) txt = {}
txt['status'] = show
self.zeroconf.update_txt(txt)
self.dispatch('STATUS', show) self.dispatch('STATUS', show)
def _on_disconnected(self): def _on_disconnected(self):
''' called when a disconnect request has completed successfully'''
self.dispatch('STATUS', 'offline') self.dispatch('STATUS', 'offline')
self.disconnect() self.disconnect()
@ -609,13 +625,18 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
return STATUS_LIST[self.connected] return STATUS_LIST[self.connected]
def send_motd(self, jid, subject = '', msg = ''): def send_motd(self, jid, subject = '', msg = ''):
'''
if not self.connection: if not self.connection:
return return
msg_iq = common.xmpp.Message(to = jid, body = msg, subject = subject) msg_iq = common.xmpp.Message(to = jid, body = msg, subject = subject)
self.connection.send(msg_iq) self.connection.send(msg_iq)
'''
pass
def send_message(self, jid, msg, keyID, type = 'chat', subject='', def send_message(self, jid, msg, keyID, type = 'chat', subject='',
chatstate = None, msg_id = None, composing_jep = None, resource = None): chatstate = None, msg_id = None, composing_jep = None, resource = None):
'''
if not self.connection: if not self.connection:
return return
if not msg and chatstate is None: if not msg and chatstate is None:
@ -678,31 +699,47 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
kind = 'single_msg_sent' kind = 'single_msg_sent'
gajim.logger.write(kind, jid, log_msg) gajim.logger.write(kind, jid, log_msg)
self.dispatch('MSGSENT', (jid, msg, keyID)) self.dispatch('MSGSENT', (jid, msg, keyID))
'''
def send_stanza(self, stanza): def send_stanza(self, stanza):
''' send a stanza untouched ''' # send a stanza untouched
'''
if not self.connection: if not self.connection:
return return
self.connection.send(stanza) self.connection.send(stanza)
'''
pass
def ack_subscribed(self, jid): def ack_subscribed(self, jid):
if not self.connection: if not self.connection:
return return
pass
'''
gajim.log.debug('ack\'ing subscription complete for %s' % jid) gajim.log.debug('ack\'ing subscription complete for %s' % jid)
p = common.xmpp.Presence(jid, 'subscribe') p = common.xmpp.Presence(jid, 'subscribe')
self.connection.send(p) self.connection.send(p)
'''
def ack_unsubscribed(self, jid): def ack_unsubscribed(self, jid):
if not self.connection: if not self.connection:
return return
pass
'''
gajim.log.debug('ack\'ing unsubscription complete for %s' % jid) gajim.log.debug('ack\'ing unsubscription complete for %s' % jid)
p = common.xmpp.Presence(jid, 'unsubscribe') p = common.xmpp.Presence(jid, 'unsubscribe')
self.connection.send(p) self.connection.send(p)
'''
def request_subscription(self, jid, msg = '', name = '', groups = [], def request_subscription(self, jid, msg = '', name = '', groups = [],
auto_auth = False): auto_auth = False):
if not self.connection: if not self.connection:
return return
pass
'''
gajim.log.debug('subscription request for %s' % jid) gajim.log.debug('subscription request for %s' % jid)
if auto_auth: if auto_auth:
self.jids_for_auto_auth.append(jid) self.jids_for_auto_auth.append(jid)
@ -723,24 +760,36 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
msg = _('I would like to add you to my roster.') msg = _('I would like to add you to my roster.')
p.setStatus(msg) p.setStatus(msg)
self.connection.send(p) self.connection.send(p)
'''
def send_authorization(self, jid): def send_authorization(self, jid):
if not self.connection: if not self.connection:
return return
pass
'''
p = common.xmpp.Presence(jid, 'subscribed') p = common.xmpp.Presence(jid, 'subscribed')
p = self.add_sha(p) p = self.add_sha(p)
self.connection.send(p) self.connection.send(p)
'''
def refuse_authorization(self, jid): def refuse_authorization(self, jid):
if not self.connection: if not self.connection:
return return
pass
'''
p = common.xmpp.Presence(jid, 'unsubscribed') p = common.xmpp.Presence(jid, 'unsubscribed')
p = self.add_sha(p) p = self.add_sha(p)
self.connection.send(p) self.connection.send(p)
'''
def unsubscribe(self, jid, remove_auth = True): def unsubscribe(self, jid, remove_auth = True):
if not self.connection: if not self.connection:
return return
pass
'''
if remove_auth: if remove_auth:
self.connection.getRoster().delItem(jid) self.connection.getRoster().delItem(jid)
jid_list = gajim.config.get_per('contacts') jid_list = gajim.config.get_per('contacts')
@ -750,10 +799,14 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
else: else:
self.connection.getRoster().Unsubscribe(jid) self.connection.getRoster().Unsubscribe(jid)
self.update_contact(jid, '', []) self.update_contact(jid, '', [])
'''
def unsubscribe_agent(self, agent): def unsubscribe_agent(self, agent):
if not self.connection: if not self.connection:
return return
pass
'''
iq = common.xmpp.Iq('set', common.xmpp.NS_REGISTER, to = agent) iq = common.xmpp.Iq('set', common.xmpp.NS_REGISTER, to = agent)
iq.getTag('query').setTag('remove') iq.getTag('query').setTag('remove')
id = self.connection.getAnID() id = self.connection.getAnID()
@ -761,14 +814,18 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.awaiting_answers[id] = (AGENT_REMOVED, agent) self.awaiting_answers[id] = (AGENT_REMOVED, agent)
self.connection.send(iq) self.connection.send(iq)
self.connection.getRoster().delItem(agent) self.connection.getRoster().delItem(agent)
'''
def update_contact(self, jid, name, groups): def update_contact(self, jid, name, groups):
'''update roster item on jabber server''' '''
# update roster item on jabber server
if self.connection: if self.connection:
self.connection.getRoster().setItem(jid = jid, name = name, self.connection.getRoster().setItem(jid = jid, name = name,
groups = groups) groups = groups)
'''
def new_account(self, name, config, sync = False): def new_account(self, name, config, sync = False):
'''
# If a connection already exist we cannot create a new account # If a connection already exist we cannot create a new account
if self.connection : if self.connection :
return return
@ -778,8 +835,10 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.on_connect_success = self._on_new_account self.on_connect_success = self._on_new_account
self.on_connect_failure = self._on_new_account self.on_connect_failure = self._on_new_account
self.connect(config) self.connect(config)
'''
def _on_new_account(self, con = None, con_type = None): def _on_new_account(self, con = None, con_type = None):
'''
if not con_type: if not con_type:
self.dispatch('ACC_NOT_OK', self.dispatch('ACC_NOT_OK',
(_('Could not connect to "%s"') % self._hostname)) (_('Could not connect to "%s"') % self._hostname))
@ -787,11 +846,13 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.on_connect_failure = None self.on_connect_failure = None
self.connection = con self.connection = con
common.xmpp.features_nb.getRegInfo(con, self._hostname) common.xmpp.features_nb.getRegInfo(con, self._hostname)
'''
def account_changed(self, new_name): def account_changed(self, new_name):
self.name = new_name self.name = new_name
def request_last_status_time(self, jid, resource): def request_last_status_time(self, jid, resource):
'''
if not self.connection: if not self.connection:
return return
to_whom_jid = jid to_whom_jid = jid
@ -800,8 +861,11 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
iq = common.xmpp.Iq(to = to_whom_jid, typ = 'get', queryNS =\ iq = common.xmpp.Iq(to = to_whom_jid, typ = 'get', queryNS =\
common.xmpp.NS_LAST) common.xmpp.NS_LAST)
self.connection.send(iq) self.connection.send(iq)
'''
pass
def request_os_info(self, jid, resource): def request_os_info(self, jid, resource):
'''
if not self.connection: if not self.connection:
return return
to_whom_jid = jid to_whom_jid = jid
@ -810,18 +874,24 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
iq = common.xmpp.Iq(to = to_whom_jid, typ = 'get', queryNS =\ iq = common.xmpp.Iq(to = to_whom_jid, typ = 'get', queryNS =\
common.xmpp.NS_VERSION) common.xmpp.NS_VERSION)
self.connection.send(iq) self.connection.send(iq)
'''
pass
def get_settings(self): def get_settings(self):
''' Get Gajim settings as described in JEP 0049 ''' '''
# Get Gajim settings as described in JEP 0049
if not self.connection: if not self.connection:
return return
iq = common.xmpp.Iq(typ='get') iq = common.xmpp.Iq(typ='get')
iq2 = iq.addChild(name='query', namespace='jabber:iq:private') iq2 = iq.addChild(name='query', namespace='jabber:iq:private')
iq3 = iq2.addChild(name='gajim', namespace='gajim:prefs') iq3 = iq2.addChild(name='gajim', namespace='gajim:prefs')
self.connection.send(iq) self.connection.send(iq)
'''
pass
def get_bookmarks(self): def get_bookmarks(self):
'''Get Bookmarks from storage as described in JEP 0048''' '''
# Get Bookmarks from storage as described in JEP 0048
self.bookmarks = [] #avoid multiple bookmarks when re-connecting self.bookmarks = [] #avoid multiple bookmarks when re-connecting
if not self.connection: if not self.connection:
return return
@ -829,9 +899,12 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
iq2 = iq.addChild(name='query', namespace='jabber:iq:private') iq2 = iq.addChild(name='query', namespace='jabber:iq:private')
iq2.addChild(name='storage', namespace='storage:bookmarks') iq2.addChild(name='storage', namespace='storage:bookmarks')
self.connection.send(iq) self.connection.send(iq)
'''
pass
def store_bookmarks(self): def store_bookmarks(self):
''' Send bookmarks to the storage namespace ''' '''
# Send bookmarks to the storage namespace
if not self.connection: if not self.connection:
return return
iq = common.xmpp.Iq(typ='set') iq = common.xmpp.Iq(typ='set')
@ -852,18 +925,23 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
if bm['print_status']: if bm['print_status']:
iq5 = iq4.setTagData('print_status', bm['print_status']) iq5 = iq4.setTagData('print_status', bm['print_status'])
self.connection.send(iq) self.connection.send(iq)
'''
pass
'''
def get_metacontacts(self): def get_metacontacts(self):
'''Get metacontacts list from storage as described in JEP 0049'''
# Get metacontacts list from storage as described in JEP 0049
if not self.connection: if not self.connection:
return return
iq = common.xmpp.Iq(typ='get') iq = common.xmpp.Iq(typ='get')
iq2 = iq.addChild(name='query', namespace='jabber:iq:private') iq2 = iq.addChild(name='query', namespace='jabber:iq:private')
iq2.addChild(name='storage', namespace='storage:metacontacts') iq2.addChild(name='storage', namespace='storage:metacontacts')
self.connection.send(iq) self.connection.send(iq)
'''
'''
def store_metacontacts(self, tags_list): def store_metacontacts(self, tags_list):
''' Send meta contacts to the storage namespace ''' # Send meta contacts to the storage namespace
if not self.connection: if not self.connection:
return return
iq = common.xmpp.Iq(typ='set') iq = common.xmpp.Iq(typ='set')
@ -877,15 +955,21 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
dict_['order'] = data['order'] dict_['order'] = data['order']
iq3.addChild(name = 'meta', attrs = dict_) iq3.addChild(name = 'meta', attrs = dict_)
self.connection.send(iq) self.connection.send(iq)
'''
def send_agent_status(self, agent, ptype): def send_agent_status(self, agent, ptype):
'''
if not self.connection: if not self.connection:
return return
p = common.xmpp.Presence(to = agent, typ = ptype) p = common.xmpp.Presence(to = agent, typ = ptype)
p = self.add_sha(p, ptype != 'unavailable') p = self.add_sha(p, ptype != 'unavailable')
self.connection.send(p) self.connection.send(p)
'''
pass
def join_gc(self, nick, room, server, password): def join_gc(self, nick, room, server, password):
'''
if not self.connection: if not self.connection:
return return
show = helpers.get_xmpp_show(STATUS_LIST[self.connected]) show = helpers.get_xmpp_show(STATUS_LIST[self.connected])
@ -907,33 +991,48 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
if last_log is None: if last_log is None:
last_log = 0 last_log = 0
self.last_history_line[jid]= last_log self.last_history_line[jid]= last_log
'''
pass
def send_gc_message(self, jid, msg): def send_gc_message(self, jid, msg):
'''
if not self.connection: if not self.connection:
return return
msg_iq = common.xmpp.Message(jid, msg, typ = 'groupchat') msg_iq = common.xmpp.Message(jid, msg, typ = 'groupchat')
self.connection.send(msg_iq) self.connection.send(msg_iq)
self.dispatch('MSGSENT', (jid, msg)) self.dispatch('MSGSENT', (jid, msg))
'''
pass
def send_gc_subject(self, jid, subject): def send_gc_subject(self, jid, subject):
'''
if not self.connection: if not self.connection:
return return
msg_iq = common.xmpp.Message(jid,typ = 'groupchat', subject = subject) msg_iq = common.xmpp.Message(jid,typ = 'groupchat', subject = subject)
self.connection.send(msg_iq) self.connection.send(msg_iq)
'''
pass
def request_gc_config(self, room_jid): def request_gc_config(self, room_jid):
'''
iq = common.xmpp.Iq(typ = 'get', queryNS = common.xmpp.NS_MUC_OWNER, iq = common.xmpp.Iq(typ = 'get', queryNS = common.xmpp.NS_MUC_OWNER,
to = room_jid) to = room_jid)
self.connection.send(iq) self.connection.send(iq)
'''
pass
def change_gc_nick(self, room_jid, nick): def change_gc_nick(self, room_jid, nick):
'''
if not self.connection: if not self.connection:
return return
p = common.xmpp.Presence(to = '%s/%s' % (room_jid, nick)) p = common.xmpp.Presence(to = '%s/%s' % (room_jid, nick))
p = self.add_sha(p) p = self.add_sha(p)
self.connection.send(p) self.connection.send(p)
'''
pass
def send_gc_status(self, nick, jid, show, status): def send_gc_status(self, nick, jid, show, status):
'''
if not self.connection: if not self.connection:
return return
if show == 'invisible': if show == 'invisible':
@ -949,9 +1048,12 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
# send instantly so when we go offline, status is sent to gc before we # send instantly so when we go offline, status is sent to gc before we
# disconnect from jabber server # disconnect from jabber server
self.connection.send(p) self.connection.send(p)
'''
pass
def gc_set_role(self, room_jid, nick, role, reason = ''): def gc_set_role(self, room_jid, nick, role, reason = ''):
'''role is for all the life of the room so it's based on nick''' '''
# role is for all the life of the room so it's based on nick
if not self.connection: if not self.connection:
return return
iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\ iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\
@ -962,9 +1064,12 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
if reason: if reason:
item.addChild(name = 'reason', payload = reason) item.addChild(name = 'reason', payload = reason)
self.connection.send(iq) self.connection.send(iq)
'''
pass
def gc_set_affiliation(self, room_jid, jid, affiliation, reason = ''): def gc_set_affiliation(self, room_jid, jid, affiliation, reason = ''):
'''affiliation is for all the life of the room so it's based on jid''' '''
# affiliation is for all the life of the room so it's based on jid
if not self.connection: if not self.connection:
return return
iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\ iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\
@ -975,8 +1080,11 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
if reason: if reason:
item.addChild(name = 'reason', payload = reason) item.addChild(name = 'reason', payload = reason)
self.connection.send(iq) self.connection.send(iq)
'''
pass
def send_gc_affiliation_list(self, room_jid, list): def send_gc_affiliation_list(self, room_jid, list):
'''
if not self.connection: if not self.connection:
return return
iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS = \ iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS = \
@ -988,8 +1096,11 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
if list[jid].has_key('reason') and list[jid]['reason']: if list[jid].has_key('reason') and list[jid]['reason']:
item_tag.setTagData('reason', list[jid]['reason']) item_tag.setTagData('reason', list[jid]['reason'])
self.connection.send(iq) self.connection.send(iq)
'''
pass
def get_affiliation_list(self, room_jid, affiliation): def get_affiliation_list(self, room_jid, affiliation):
'''
if not self.connection: if not self.connection:
return return
iq = common.xmpp.Iq(typ = 'get', to = room_jid, queryNS = \ iq = common.xmpp.Iq(typ = 'get', to = room_jid, queryNS = \
@ -997,13 +1108,18 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
item = iq.getTag('query').setTag('item') item = iq.getTag('query').setTag('item')
item.setAttr('affiliation', affiliation) item.setAttr('affiliation', affiliation)
self.connection.send(iq) self.connection.send(iq)
'''
pass
def send_gc_config(self, room_jid, config): def send_gc_config(self, room_jid, config):
'''
iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\ iq = common.xmpp.Iq(typ = 'set', to = room_jid, queryNS =\
common.xmpp.NS_MUC_OWNER) common.xmpp.NS_MUC_OWNER)
query = iq.getTag('query') query = iq.getTag('query')
self.build_data_from_dict(query, config) self.build_data_from_dict(query, config)
self.connection.send(iq) self.connection.send(iq)
'''
pass
def gpg_passphrase(self, passphrase): def gpg_passphrase(self, passphrase):
if USE_GPG: if USE_GPG:
@ -1028,6 +1144,7 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
def change_password(self, password): def change_password(self, password):
if not self.connection: if not self.connection:
return return
'''
hostname = gajim.config.get_per('accounts', self.name, 'hostname') hostname = gajim.config.get_per('accounts', self.name, 'hostname')
username = gajim.config.get_per('accounts', self.name, 'name') username = gajim.config.get_per('accounts', self.name, 'name')
iq = common.xmpp.Iq(typ = 'set', to = hostname) iq = common.xmpp.Iq(typ = 'set', to = hostname)
@ -1035,8 +1152,11 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
q.setTagData('username',username) q.setTagData('username',username)
q.setTagData('password',password) q.setTagData('password',password)
self.connection.send(iq) self.connection.send(iq)
'''
pass
def unregister_account(self, on_remove_success): def unregister_account(self, on_remove_success):
'''
# no need to write this as a class method and keep the value of on_remove_success # no need to write this as a class method and keep the value of on_remove_success
# as a class property as pass it as an argument # as a class property as pass it as an argument
def _on_unregister_account_connect(con): def _on_unregister_account_connect(con):
@ -1054,26 +1174,37 @@ class ConnectionZeroconf(ConnectionHandlersZeroconf):
self.connect_and_auth() self.connect_and_auth()
else: else:
_on_unregister_account_connect(self.connection) _on_unregister_account_connect(self.connection)
'''
pass
def send_invite(self, room, to, reason=''): def send_invite(self, room, to, reason=''):
'''sends invitation''' '''
# sends invitation
message=common.xmpp.Message(to = room) message=common.xmpp.Message(to = room)
c = message.addChild(name = 'x', namespace = common.xmpp.NS_MUC_USER) c = message.addChild(name = 'x', namespace = common.xmpp.NS_MUC_USER)
c = c.addChild(name = 'invite', attrs={'to' : to}) c = c.addChild(name = 'invite', attrs={'to' : to})
if reason != '': if reason != '':
c.setTagData('reason', reason) c.setTagData('reason', reason)
self.connection.send(message) self.connection.send(message)
'''
pass
def send_keepalive(self): def send_keepalive(self):
'''
# nothing received for the last foo seconds (60 secs by default) # nothing received for the last foo seconds (60 secs by default)
if self.connection: if self.connection:
self.connection.send(' ') self.connection.send(' ')
'''
pass
def _reconnect_alarm(self): def _reconnect_alarm(self):
'''
if self.time_to_reconnect: if self.time_to_reconnect:
if self.connected < 2: if self.connected < 2:
self._reconnect() self._reconnect()
else: else:
self.time_to_reconnect = None self.time_to_reconnect = None
'''
pass
# END Connection # END Connection