From 7de87c19a0ac50b037bf16bddc5089edc03fe771 Mon Sep 17 00:00:00 2001 From: Yann Leboulanger Date: Sat, 6 Apr 2013 10:14:28 +0200 Subject: [PATCH] move ConnectionPEP class to connection_handlers to prevent import loop --- src/common/connection_handlers.py | 167 ++++++++++++++++- src/common/pep.py | 169 +----------------- .../zeroconf/connection_handlers_zeroconf.py | 6 +- 3 files changed, 170 insertions(+), 172 deletions(-) diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py index d428a9e79..3b813489e 100644 --- a/src/common/connection_handlers.py +++ b/src/common/connection_handlers.py @@ -48,7 +48,6 @@ from common import dataforms from common import jingle_xtls from common.commands import ConnectionCommands from common.pubsub import ConnectionPubSub -from common.pep import ConnectionPEP from common.protocol.caps import ConnectionCaps from common.protocol.bytestream import ConnectionSocks5Bytestream from common.protocol.bytestream import ConnectionIBBytestream @@ -767,6 +766,172 @@ class ConnectionVcard: p = self.add_sha(p) self.connection.send(p) + +class ConnectionPEP(object): + + def __init__(self, account, dispatcher, pubsub_connection): + self._account = account + self._dispatcher = dispatcher + self._pubsub_connection = pubsub_connection + self.reset_awaiting_pep() + + def pep_change_account_name(self, new_name): + self._account = new_name + + def reset_awaiting_pep(self): + self.to_be_sent_activity = None + self.to_be_sent_mood = None + self.to_be_sent_tune = None + self.to_be_sent_nick = None + self.to_be_sent_location = None + + def send_awaiting_pep(self): + """ + Send pep info that were waiting for connection + """ + if self.to_be_sent_activity: + self.send_activity(*self.to_be_sent_activity) + if self.to_be_sent_mood: + self.send_mood(*self.to_be_sent_mood) + if self.to_be_sent_tune: + self.send_tune(*self.to_be_sent_tune) + if self.to_be_sent_nick: + self.send_nick(self.to_be_sent_nick) + if self.to_be_sent_location: + self.send_location(self.to_be_sent_location) + self.reset_awaiting_pep() + + def _pubsubEventCB(self, xmpp_dispatcher, msg): + ''' Called when we receive with pubsub event. ''' + gajim.nec.push_incoming_event(PEPReceivedEvent(None, conn=self, + stanza=msg)) + + def send_activity(self, activity, subactivity=None, message=None): + if self.connected == 1: + # We are connecting, keep activity in mem and send it when we'll be + # connected + self.to_be_sent_activity = (activity, subactivity, message) + return + if not self.pep_supported: + return + item = nbxmpp.Node('activity', {'xmlns': nbxmpp.NS_ACTIVITY}) + if activity: + i = item.addChild(activity) + if subactivity: + i.addChild(subactivity) + if message: + i = item.addChild('text') + i.addData(message) + self._pubsub_connection.send_pb_publish('', nbxmpp.NS_ACTIVITY, item, + '0') + + def retract_activity(self): + if not self.pep_supported: + return + self.send_activity(None) + # not all client support new XEP, so we still retract + self._pubsub_connection.send_pb_retract('', nbxmpp.NS_ACTIVITY, '0') + + def send_mood(self, mood, message=None): + if self.connected == 1: + # We are connecting, keep mood in mem and send it when we'll be + # connected + self.to_be_sent_mood = (mood, message) + return + if not self.pep_supported: + return + item = nbxmpp.Node('mood', {'xmlns': nbxmpp.NS_MOOD}) + if mood: + item.addChild(mood) + if message: + i = item.addChild('text') + i.addData(message) + self._pubsub_connection.send_pb_publish('', nbxmpp.NS_MOOD, item, '0') + + def retract_mood(self): + if not self.pep_supported: + return + self.send_mood(None) + # not all client support new XEP, so we still retract + self._pubsub_connection.send_pb_retract('', nbxmpp.NS_MOOD, '0') + + def send_tune(self, artist='', title='', source='', track=0, length=0, + items=None): + if self.connected == 1: + # We are connecting, keep tune in mem and send it when we'll be + # connected + self.to_be_sent_tune = (artist, title, source, track, length, items) + return + if not self.pep_supported: + return + item = nbxmpp.Node('tune', {'xmlns': nbxmpp.NS_TUNE}) + if artist: + i = item.addChild('artist') + i.addData(artist) + if title: + i = item.addChild('title') + i.addData(title) + if source: + i = item.addChild('source') + i.addData(source) + if track: + i = item.addChild('track') + i.addData(track) + if length: + i = item.addChild('length') + i.addData(length) + if items: + item.addChild(payload=items) + self._pubsub_connection.send_pb_publish('', nbxmpp.NS_TUNE, item, '0') + + def retract_tune(self): + if not self.pep_supported: + return + self.send_tune(None) + # not all client support new XEP, so we still retract + self._pubsub_connection.send_pb_retract('', nbxmpp.NS_TUNE, '0') + + def send_nickname(self, nick): + if self.connected == 1: + # We are connecting, keep nick in mem and send it when we'll be + # connected + self.to_be_sent_nick = nick + return + if not self.pep_supported: + return + item = nbxmpp.Node('nick', {'xmlns': nbxmpp.NS_NICK}) + item.addData(nick) + self._pubsub_connection.send_pb_publish('', nbxmpp.NS_NICK, item, '0') + + def retract_nickname(self): + if not self.pep_supported: + return + self.send_nickname(None) + # not all client support new XEP, so we still retract + self._pubsub_connection.send_pb_retract('', nbxmpp.NS_NICK, '0') + + def send_location(self, info): + if self.connected == 1: + # We are connecting, keep location in mem and send it when we'll be + # connected + self.to_be_sent_location = info + return + if not self.pep_supported: + return + item = nbxmpp.Node('geoloc', {'xmlns': nbxmpp.NS_LOCATION}) + for field in LOCATION_DATA: + if info.get(field, None): + i = item.addChild(field) + i.addData(info[field]) + self._pubsub_connection.send_pb_publish('', nbxmpp.NS_LOCATION, item, '0') + + def retract_location(self): + if not self.pep_supported: + return + self.send_location({}) + # not all client support new XEP, so we still retract + self._pubsub_connection.send_pb_retract('', nbxmpp.NS_LOCATION, '0') + # basic connection handlers used here and in zeroconf class ConnectionHandlersBase: def __init__(self): diff --git a/src/common/pep.py b/src/common/pep.py index ae175932c..be8a596b9 100644 --- a/src/common/pep.py +++ b/src/common/pep.py @@ -473,171 +473,4 @@ class UserLocationPEP(AbstractPEP): SUPPORTED_PERSONAL_USER_EVENTS = [UserMoodPEP, UserTunePEP, UserActivityPEP, - UserNicknamePEP, UserLocationPEP] - -from common.connection_handlers_events import PEPReceivedEvent - -class ConnectionPEP(object): - - def __init__(self, account, dispatcher, pubsub_connection): - self._account = account - self._dispatcher = dispatcher - self._pubsub_connection = pubsub_connection - self.reset_awaiting_pep() - - def pep_change_account_name(self, new_name): - self._account = new_name - - def reset_awaiting_pep(self): - self.to_be_sent_activity = None - self.to_be_sent_mood = None - self.to_be_sent_tune = None - self.to_be_sent_nick = None - self.to_be_sent_location = None - - def send_awaiting_pep(self): - """ - Send pep info that were waiting for connection - """ - if self.to_be_sent_activity: - self.send_activity(*self.to_be_sent_activity) - if self.to_be_sent_mood: - self.send_mood(*self.to_be_sent_mood) - if self.to_be_sent_tune: - self.send_tune(*self.to_be_sent_tune) - if self.to_be_sent_nick: - self.send_nick(self.to_be_sent_nick) - if self.to_be_sent_location: - self.send_location(self.to_be_sent_location) - self.reset_awaiting_pep() - - def _pubsubEventCB(self, xmpp_dispatcher, msg): - ''' Called when we receive with pubsub event. ''' - gajim.nec.push_incoming_event(PEPReceivedEvent(None, conn=self, - stanza=msg)) - - def send_activity(self, activity, subactivity=None, message=None): - if self.connected == 1: - # We are connecting, keep activity in mem and send it when we'll be - # connected - self.to_be_sent_activity = (activity, subactivity, message) - return - if not self.pep_supported: - return - item = nbxmpp.Node('activity', {'xmlns': nbxmpp.NS_ACTIVITY}) - if activity: - i = item.addChild(activity) - if subactivity: - i.addChild(subactivity) - if message: - i = item.addChild('text') - i.addData(message) - self._pubsub_connection.send_pb_publish('', nbxmpp.NS_ACTIVITY, item, - '0') - - def retract_activity(self): - if not self.pep_supported: - return - self.send_activity(None) - # not all client support new XEP, so we still retract - self._pubsub_connection.send_pb_retract('', nbxmpp.NS_ACTIVITY, '0') - - def send_mood(self, mood, message=None): - if self.connected == 1: - # We are connecting, keep mood in mem and send it when we'll be - # connected - self.to_be_sent_mood = (mood, message) - return - if not self.pep_supported: - return - item = nbxmpp.Node('mood', {'xmlns': nbxmpp.NS_MOOD}) - if mood: - item.addChild(mood) - if message: - i = item.addChild('text') - i.addData(message) - self._pubsub_connection.send_pb_publish('', nbxmpp.NS_MOOD, item, '0') - - def retract_mood(self): - if not self.pep_supported: - return - self.send_mood(None) - # not all client support new XEP, so we still retract - self._pubsub_connection.send_pb_retract('', nbxmpp.NS_MOOD, '0') - - def send_tune(self, artist='', title='', source='', track=0, length=0, - items=None): - if self.connected == 1: - # We are connecting, keep tune in mem and send it when we'll be - # connected - self.to_be_sent_tune = (artist, title, source, track, length, items) - return - if not self.pep_supported: - return - item = nbxmpp.Node('tune', {'xmlns': nbxmpp.NS_TUNE}) - if artist: - i = item.addChild('artist') - i.addData(artist) - if title: - i = item.addChild('title') - i.addData(title) - if source: - i = item.addChild('source') - i.addData(source) - if track: - i = item.addChild('track') - i.addData(track) - if length: - i = item.addChild('length') - i.addData(length) - if items: - item.addChild(payload=items) - self._pubsub_connection.send_pb_publish('', nbxmpp.NS_TUNE, item, '0') - - def retract_tune(self): - if not self.pep_supported: - return - self.send_tune(None) - # not all client support new XEP, so we still retract - self._pubsub_connection.send_pb_retract('', nbxmpp.NS_TUNE, '0') - - def send_nickname(self, nick): - if self.connected == 1: - # We are connecting, keep nick in mem and send it when we'll be - # connected - self.to_be_sent_nick = nick - return - if not self.pep_supported: - return - item = nbxmpp.Node('nick', {'xmlns': nbxmpp.NS_NICK}) - item.addData(nick) - self._pubsub_connection.send_pb_publish('', nbxmpp.NS_NICK, item, '0') - - def retract_nickname(self): - if not self.pep_supported: - return - self.send_nickname(None) - # not all client support new XEP, so we still retract - self._pubsub_connection.send_pb_retract('', nbxmpp.NS_NICK, '0') - - def send_location(self, info): - if self.connected == 1: - # We are connecting, keep location in mem and send it when we'll be - # connected - self.to_be_sent_location = info - return - if not self.pep_supported: - return - item = nbxmpp.Node('geoloc', {'xmlns': nbxmpp.NS_LOCATION}) - for field in LOCATION_DATA: - if info.get(field, None): - i = item.addChild(field) - i.addData(info[field]) - self._pubsub_connection.send_pb_publish('', nbxmpp.NS_LOCATION, item, '0') - - def retract_location(self): - if not self.pep_supported: - return - self.send_location({}) - # not all client support new XEP, so we still retract - self._pubsub_connection.send_pb_retract('', nbxmpp.NS_LOCATION, '0') + UserNicknamePEP, UserLocationPEP] \ No newline at end of file diff --git a/src/common/zeroconf/connection_handlers_zeroconf.py b/src/common/zeroconf/connection_handlers_zeroconf.py index ce5273123..e7648a89b 100644 --- a/src/common/zeroconf/connection_handlers_zeroconf.py +++ b/src/common/zeroconf/connection_handlers_zeroconf.py @@ -34,7 +34,6 @@ from common import helpers from common import gajim from common.zeroconf import zeroconf from common.commands import ConnectionCommands -from common.pep import ConnectionPEP from common.protocol.bytestream import ConnectionSocks5BytestreamZeroconf from common.connection_handlers_events import ZeroconfMessageReceivedEvent @@ -72,8 +71,9 @@ class ConnectionVcard(connection_handlers.ConnectionVcard): class ConnectionHandlersZeroconf(ConnectionVcard, -ConnectionSocks5BytestreamZeroconf, ConnectionCommands, ConnectionPEP, -connection_handlers.ConnectionHandlersBase, connection_handlers.ConnectionJingle): +ConnectionSocks5BytestreamZeroconf, ConnectionCommands, +connection_handlers.ConnectionPEP, connection_handlers.ConnectionHandlersBase, +connection_handlers.ConnectionJingle): def __init__(self): ConnectionVcard.__init__(self) ConnectionSocks5BytestreamZeroconf.__init__(self)