Move presence_available handlers to AccountManager

darcs-hash:20070329162807-86b55-2a353558d6a26e686454022c092d4d161a91d786.gz
This commit is contained in:
David Rousselie
2007-03-29 18:28:07 +02:00
parent 863da17c6f
commit 5fb853bdfe
2 changed files with 143 additions and 85 deletions

View File

@@ -58,7 +58,7 @@ if __name__ == '__main__':
jcl_suite = unittest.TestSuite() jcl_suite = unittest.TestSuite()
# jcl_suite.addTest(FeederComponent_TestCase('test_handle_tick')) # jcl_suite.addTest(FeederComponent_TestCase('test_handle_tick'))
# jcl_suite.addTest(JCLComponent_TestCase('test_handle_set_register_update_complex')) # jcl_suite.addTest(JCLComponent_TestCase('test_handle_presence_available_to_component'))
# jcl_suite.addTest(PresenceAccount_TestCase('test_possibles_actions')) # jcl_suite.addTest(PresenceAccount_TestCase('test_possibles_actions'))
# jcl_suite = unittest.TestSuite((component_suite)) # jcl_suite = unittest.TestSuite((component_suite))
# jcl_suite = unittest.TestSuite((presence_account_suite)) # jcl_suite = unittest.TestSuite((presence_account_suite))

View File

@@ -256,20 +256,20 @@ class JCLComponent(Component, object):
root_handler, \ root_handler, \
send_result = False): send_result = False):
"""Apply given handler depending on info_query receiver""" """Apply given handler depending on info_query receiver"""
bare_from_jid = info_query.get_from().bare() from_jid = info_query.get_from()
to_jid = info_query.get_to() to_jid = info_query.get_to()
name = to_jid.node name = to_jid.node
account_type = to_jid.resource account_type = to_jid.resource
lang_class = self.lang.get_lang_class_from_node(info_query.get_node()) lang_class = self.lang.get_lang_class_from_node(info_query.get_node())
if name is not None: # account if name is not None: # account
self.__logger.debug("Applying behavior on account " + name) self.__logger.debug("Applying behavior on account " + name)
result = account_handler(name, bare_from_jid, account_type or "", lang_class) result = account_handler(name, from_jid, account_type or "", lang_class)
elif account_type is None: # root elif account_type is None: # root
self.__logger.debug("Applying behavior on root node") self.__logger.debug("Applying behavior on root node")
result = root_handler(name, bare_from_jid, "", lang_class) # TODO : account_type not needed result = root_handler(name, from_jid, "", lang_class) # TODO : account_type not needed
else: # account type else: # account type
self.__logger.debug("Applying behavior on account type " + account_type) self.__logger.debug("Applying behavior on account type " + account_type)
result = account_type_handler(name, bare_from_jid, account_type, lang_class) result = account_type_handler(name, from_jid, account_type, lang_class)
if send_result: if send_result:
self.send_stanzas(result) self.send_stanzas(result)
return result return result
@@ -278,11 +278,11 @@ class JCLComponent(Component, object):
"""Discovery get info handler """Discovery get info handler
""" """
return self.apply_behavior(info_query, \ return self.apply_behavior(info_query, \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.account_disco_get_info(), \ self.account_manager.account_disco_get_info(), \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.account_type_disco_get_info(), \ self.account_manager.account_type_disco_get_info(), \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.root_disco_get_info(self.name, \ self.account_manager.root_disco_get_info(self.name, \
self.disco_identity.category, \ self.disco_identity.category, \
self.disco_identity.type)) self.disco_identity.type))
@@ -291,12 +291,12 @@ class JCLComponent(Component, object):
"""Discovery get nested nodes handler """Discovery get nested nodes handler
""" """
return self.apply_behavior(info_query, \ return self.apply_behavior(info_query, \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
DiscoItems(), \ DiscoItems(), \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.account_type_disco_get_items(bare_from_jid, account_type), \ self.account_manager.account_type_disco_get_items(from_jid, account_type), \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.root_disco_get_items(bare_from_jid)) self.account_manager.root_disco_get_items(from_jid))
def handle_get_version(self, info_query): def handle_get_version(self, info_query):
"""Get Version handler """Get Version handler
@@ -315,17 +315,17 @@ class JCLComponent(Component, object):
""" """
self.__logger.debug("GET_REGISTER") self.__logger.debug("GET_REGISTER")
self.apply_behavior(info_query, \ self.apply_behavior(info_query, \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.account_get_register(info_query, \ self.account_manager.account_get_register(info_query, \
name, \ name, \
bare_from_jid, \ from_jid, \
account_type, \ account_type, \
lang_class), \ lang_class), \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.account_type_get_register(info_query, \ self.account_manager.account_type_get_register(info_query, \
account_type, \ account_type, \
lang_class), \ lang_class), \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.root_get_register(info_query, \ self.account_manager.root_get_register(info_query, \
lang_class), \ lang_class), \
send_result = True) send_result = True)
@@ -359,28 +359,27 @@ class JCLComponent(Component, object):
self.stream.send(iq_error) self.stream.send(iq_error)
return return
account_name = x_data["name"].value account_name = x_data["name"].value
self.apply_behavior(info_query, \ return self.apply_behavior(info_query, \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.account_set_register(name, \ self.account_manager.account_set_register(name, \
bare_from_jid, \ from_jid, \
lang_class, \ lang_class, \
x_data, \ x_data, \
info_query), \ info_query), \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.account_type_set_register(account_name, \ self.account_manager.account_type_set_register(account_name, \
bare_from_jid, \ from_jid, \
account_type, \ account_type, \
lang_class, \ lang_class, \
x_data, \ x_data, \
info_query), \ info_query), \
lambda name, bare_from_jid, account_type, lang_class: \ lambda name, from_jid, account_type, lang_class: \
self.account_manager.root_set_register(account_name, \ self.account_manager.root_set_register(account_name, \
bare_from_jid, \ from_jid, \
lang_class, \ lang_class, \
x_data, \ x_data, \
info_query), \ info_query), \
send_result = True) send_result = True)
return
def handle_presence_available(self, stanza): def handle_presence_available(self, stanza):
"""Handle presence availability """Handle presence availability
@@ -388,40 +387,20 @@ class JCLComponent(Component, object):
all accounts for current user. Otherwise, send presence from current account. all accounts for current user. Otherwise, send presence from current account.
""" """
self.__logger.debug("PRESENCE_AVAILABLE") return self.apply_behavior(stanza, \
from_jid = stanza.get_from() lambda name, from_jid, account_type, lang_class: \
base_from_jid = unicode(from_jid.bare()) self.account_manager.account_handle_presence_available(name, \
name = stanza.get_to().node from_jid, \
lang_class = self.lang.get_lang_class_from_node(stanza.get_node()) lang_class, \
show = stanza.get_show() stanza.get_show()), \
self.__logger.debug("SHOW : " + str(show)) lambda name, from_jid, account_type, lang_class: \
if name: [], \
self.__logger.debug("TO : " + name + " " + base_from_jid) lambda name, from_jid, account_type, lang_class: \
self.db_connect() self.account_manager.root_handle_presence_available(from_jid, \
if not name: lang_class, \
accounts = self.account_classes[0].select(\ stanza.get_show()), \
self.account_classes[0].q.user_jid == base_from_jid) send_result = True)
accounts_length = 0
for _account in accounts:
accounts_length += 1
self._send_presence_available(_account, show, lang_class)
if (accounts_length > 0):
presence = Presence(from_jid = self.jid, \
to_jid = from_jid, \
status = \
str(accounts_length) \
+ lang_class.message_status, \
show = show, \
stanza_type = "available")
self.stream.send(presence)
else:
accounts = self.account_classes[0].select(\
AND(self.account_classes[0].q.name == name, \
self.account_classes[0].q.user_jid == base_from_jid))
if accounts.count() > 0:
self._send_presence_available(accounts[0], show, lang_class)
self.db_disconnect()
return 1
def handle_presence_unavailable(self, stanza): def handle_presence_unavailable(self, stanza):
"""Handle presence unavailability """Handle presence unavailability
@@ -570,6 +549,7 @@ class JCLComponent(Component, object):
self.__logger.debug(account_class_name + " not found") self.__logger.debug(account_class_name + " not found")
return None return None
# TODO : delete
def _send_presence_available(self, _account, show, lang_class): def _send_presence_available(self, _account, show, lang_class):
"""Send available presence to account's user and ask for password """Send available presence to account's user and ask for password
if necessary""" if necessary"""
@@ -686,7 +666,7 @@ class AccountManager(object):
return disco_info return disco_info
###### disco_get_items handlers ###### ###### disco_get_items handlers ######
def account_type_disco_get_items(self, bare_from_jid, account_type): def account_type_disco_get_items(self, from_jid, account_type):
"""Discovery get_items on an account type node""" """Discovery get_items on an account type node"""
self.__logger.debug("Listing account for " + account_type) self.__logger.debug("Listing account for " + account_type)
disco_items = DiscoItems() disco_items = DiscoItems()
@@ -694,14 +674,14 @@ class AccountManager(object):
if account_class is not None: if account_class is not None:
self._list_accounts(disco_items, \ self._list_accounts(disco_items, \
account_class, \ account_class, \
bare_from_jid, \ from_jid.bare(), \
account_type = account_type) account_type = account_type)
else: else:
self.__logger.error("Error: " + account_class.__name__ \ self.__logger.error("Error: " + account_class.__name__ \
+ " class not in account_classes") + " class not in account_classes")
return disco_items return disco_items
def root_disco_get_items(self, bare_from_jid): def root_disco_get_items(self, from_jid):
"""Discovery get_items on root node""" """Discovery get_items on root node"""
disco_items = DiscoItems() disco_items = DiscoItems()
regexp_type = re.compile("(.*)Account$") regexp_type = re.compile("(.*)Account$")
@@ -719,7 +699,7 @@ class AccountManager(object):
if match is not None: if match is not None:
account_type = match.group(1) account_type = match.group(1)
list_func(disco_items, account_class, \ list_func(disco_items, account_class, \
bare_from_jid, account_type) from_jid.bare(), account_type)
else: else:
self.__logger.error(account_class.__name__ + \ self.__logger.error(account_class.__name__ + \
" name not well formed") " name not well formed")
@@ -728,7 +708,7 @@ class AccountManager(object):
###### get_register handlers ###### ###### get_register handlers ######
def account_get_register(self, info_query, \ def account_get_register(self, info_query, \
name, \ name, \
bare_from_jid, \ from_jid, \
account_type, \ account_type, \
lang_class): lang_class):
"""Handle get_register on an account. """Handle get_register on an account.
@@ -739,7 +719,7 @@ class AccountManager(object):
# TODO : do it only one time # TODO : do it only one time
accounts = account_class.select(\ accounts = account_class.select(\
AND(account_class.q.name == name, \ AND(account_class.q.name == name, \
account_class.q.user_jid == unicode(bare_from_jid))) account_class.q.user_jid == unicode(from_jid.bare())))
if accounts is not None: if accounts is not None:
query = info_query.new_query("jabber:iq:register") query = info_query.new_query("jabber:iq:register")
self.get_reg_form_init(lang_class, \ self.get_reg_form_init(lang_class, \
@@ -847,9 +827,10 @@ class AccountManager(object):
self.db_disconnect() self.db_disconnect()
return result return result
def account_set_register(self, name, bare_from_jid, lang_class, \ def account_set_register(self, name, from_jid, lang_class, \
x_data, info_query): x_data, info_query):
"""Update account""" """Update account"""
bare_from_jid = from_jid.bare()
self.db_connect() self.db_connect()
accounts = Account.select(\ accounts = Account.select(\
AND(Account.q.name == name, \ AND(Account.q.name == name, \
@@ -870,13 +851,14 @@ class AccountManager(object):
return [] return []
def _account_type_set_register(self, name, \ def _account_type_set_register(self, name, \
bare_from_jid, \ from_jid, \
account_class, \ account_class, \
lang_class, \ lang_class, \
x_data, \ x_data, \
info_query): info_query):
"""Create new account from account_class""" """Create new account from account_class"""
self.db_connect() self.db_connect()
bare_from_jid = from_jid.bare()
_account = account_class(user_jid = unicode(bare_from_jid), \ _account = account_class(user_jid = unicode(bare_from_jid), \
name = name, \ name = name, \
jid = self.get_account_jid(name)) jid = self.get_account_jid(name))
@@ -888,27 +870,63 @@ class AccountManager(object):
info_query, True, first_account) info_query, True, first_account)
def account_type_set_register(self, name, \ def account_type_set_register(self, name, \
bare_from_jid, \ from_jid, \
account_type, \ account_type, \
lang_class, \ lang_class, \
x_data, \ x_data, \
info_query): info_query):
"""Create new typed account""" """Create new typed account"""
return self._account_type_set_register(name, bare_from_jid, \ return self._account_type_set_register(name, from_jid, \
self._get_account_class(account_type + "Account"), lang_class, \ self._get_account_class(account_type + "Account"), lang_class, \
x_data, info_query) x_data, info_query)
def root_set_register(self, name, bare_from_jid, lang_class, \ def root_set_register(self, name, from_jid, lang_class, \
x_data, info_query): x_data, info_query):
"""Create new account when managing only one account type""" """Create new account when managing only one account type"""
if not self.has_multiple_account_type: if not self.has_multiple_account_type:
return self._account_type_set_register(name, bare_from_jid, \ return self._account_type_set_register(name, from_jid, \
self.account_classes[0], \ self.account_classes[0], \
lang_class, x_data, \ lang_class, x_data, \
info_query) info_query)
else: else:
return [] return []
###### presence_available handlers ######
def account_handle_presence_available(self, name, from_jid, lang_class, show):
"""Handle presence \"available\" sent to an account JID"""
result = []
self.db_connect()
accounts = Account.select(\
AND(Account.q.name == name, \
Account.q.user_jid == unicode(from_jid.bare())))
if accounts.count() > 0:
result.extend(self._send_presence_available(accounts[0], show, lang_class))
self.db_disconnect()
return result
def root_handle_presence_available(self, from_jid, lang_class, show):
"""Handle presence \"available\" sent to component JID"""
result = []
self.db_connect()
accounts = Account.select(\
Account.q.user_jid == unicode(from_jid.bare()))
accounts_length = 0
for _account in accounts:
accounts_length += 1
result.extend(self._send_presence_available(_account, \
show, \
lang_class))
self.db_disconnect()
if (accounts_length > 0):
result.append(Presence(from_jid = self.component.jid, \
to_jid = from_jid, \
status = \
str(accounts_length) \
+ lang_class.message_status, \
show = show, \
stanza_type = "available"))
return result
###### Utils methods ###### ###### Utils methods ######
def _list_accounts(self, disco_items, _account_class, bare_from_jid, account_type = ""): def _list_accounts(self, disco_items, _account_class, bare_from_jid, account_type = ""):
"""List accounts in disco_items for given _account_class and user jid""" """List accounts in disco_items for given _account_class and user jid"""
@@ -1011,3 +1029,43 @@ class AccountManager(object):
def get_account_jid(self, name): def get_account_jid(self, name):
"""Compose account jid from account name""" """Compose account jid from account name"""
return name + u"@" + unicode(self.component.jid) return name + u"@" + unicode(self.component.jid)
def _send_presence_available(self, _account, show, lang_class):
"""Send available presence to account's user and ask for password
if necessary"""
result = []
_account.default_lang_class = lang_class
old_status = _account.status
if show is None:
_account.status = account.ONLINE
else:
_account.status = show
result.append(Presence(from_jid = _account.jid, \
to_jid = _account.user_jid, \
status = _account.status_msg, \
show = show, \
stanza_type = "available"))
if hasattr(_account, 'store_password') \
and hasattr(_account, 'password') \
and _account.store_password == False \
and old_status == account.OFFLINE \
and _account.password == None :
result.extend(self.ask_password(_account, lang_class))
return result
def ask_password(self, _account, lang_class):
"""Send a Jabber message to ask for account password
"""
result = []
if hasattr(_account, 'waiting_password_reply') \
and not _account.waiting_password_reply \
and _account.status != account.OFFLINE:
_account.waiting_password_reply = True
result.append(Message(from_jid = _account.jid, \
to_jid = _account.user_jid, \
stanza_type = "normal", \
subject = u"[PASSWORD] " + \
lang_class.ask_password_subject, \
body = lang_class.ask_password_body % \
(_account.name)))
return result