Message handler can now handle multiple message types
* each message type to be handle must add a filter function and a handler function tuple to the msg_handlers attribut of JCLComponent darcs-hash:20070522060000-86b55-0e8fa4afa2d57743d5cabe1a46cc3d24cc62b294.gz
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
##
|
##
|
||||||
## component.py
|
## component.py
|
||||||
## Login : David Rousselie <dax@happycoders.org>
|
## Login : David Rousselie <dax@happycoders.org>
|
||||||
@@ -93,7 +93,8 @@ class JCLComponent(Component, object):
|
|||||||
self.time_unit = 60
|
self.time_unit = 60
|
||||||
self.queue = Queue(100)
|
self.queue = Queue(100)
|
||||||
self.account_manager = AccountManager(self)
|
self.account_manager = AccountManager(self)
|
||||||
|
self.msg_handlers = []
|
||||||
|
|
||||||
self.__logger = logging.getLogger("jcl.jabber.JCLComponent")
|
self.__logger = logging.getLogger("jcl.jabber.JCLComponent")
|
||||||
self.lang = lang
|
self.lang = lang
|
||||||
self.running = False
|
self.running = False
|
||||||
@@ -204,6 +205,24 @@ class JCLComponent(Component, object):
|
|||||||
self.handle_message)
|
self.handle_message)
|
||||||
self.send_stanzas(self.account_manager.probe_all_accounts_presence())
|
self.send_stanzas(self.account_manager.probe_all_accounts_presence())
|
||||||
|
|
||||||
|
def password_msg_handler_filter(message, _account):
|
||||||
|
return hasattr(_account, 'password') \
|
||||||
|
and hasattr(_account, 'waiting_password_reply') \
|
||||||
|
and re.compile("\[PASSWORD\]").search(message.get_subject()) \
|
||||||
|
is not None
|
||||||
|
|
||||||
|
def password_msg_handler(message, _account):
|
||||||
|
lang_class = self.lang.get_lang_class_from_node(message.get_node())
|
||||||
|
_account.password = message.get_body()
|
||||||
|
_account.waiting_password_reply = False
|
||||||
|
msg = Message(from_jid = _account.jid, \
|
||||||
|
to_jid = message.get_from(), \
|
||||||
|
stanza_type = "normal", \
|
||||||
|
subject = lang_class.password_saved_for_session, \
|
||||||
|
body = lang_class.password_saved_for_session)
|
||||||
|
self.stream.send(msg)
|
||||||
|
self.msg_handlers += [(password_msg_handler, password_msg_handler_filter)]
|
||||||
|
|
||||||
def signal_handler(self, signum, frame):
|
def signal_handler(self, signum, frame):
|
||||||
"""Stop method handler
|
"""Stop method handler
|
||||||
"""
|
"""
|
||||||
@@ -441,27 +460,17 @@ class JCLComponent(Component, object):
|
|||||||
Handle password response message
|
Handle password response message
|
||||||
"""
|
"""
|
||||||
self.__logger.debug("MESSAGE: " + message.get_body())
|
self.__logger.debug("MESSAGE: " + message.get_body())
|
||||||
lang_class = self.lang.get_lang_class_from_node(message.get_node())
|
|
||||||
name = message.get_to().node
|
name = message.get_to().node
|
||||||
base_from_jid = unicode(message.get_from().bare())
|
bare_from_jid = unicode(message.get_from().bare())
|
||||||
self.db_connect()
|
self.db_connect()
|
||||||
accounts = Account.select(\
|
accounts = Account.select(\
|
||||||
AND(Account.q.name == name, \
|
AND(Account.q.name == name, \
|
||||||
Account.q.user_jid == base_from_jid))
|
Account.q.user_jid == bare_from_jid))
|
||||||
if accounts.count() == 1:
|
if accounts.count() != 1:
|
||||||
_account = list(accounts)[0]
|
print >>sys.stderr, "Account " + name + " for user " + bare_from_jid + " must be uniq"
|
||||||
if hasattr(_account, 'password') \
|
for (msg_handler, filter_func) in self.msg_handlers:
|
||||||
and hasattr(_account, 'waiting_password_reply') \
|
if filter_func(message, accounts[0]):
|
||||||
and re.compile("\[PASSWORD\]").search(message.get_subject()) \
|
msg_handler(message, accounts[0])
|
||||||
is not None:
|
|
||||||
_account.password = message.get_body()
|
|
||||||
_account.waiting_password_reply = False
|
|
||||||
msg = Message(from_jid = _account.jid, \
|
|
||||||
to_jid = message.get_from(), \
|
|
||||||
stanza_type = "normal", \
|
|
||||||
subject = lang_class.password_saved_for_session, \
|
|
||||||
body = lang_class.password_saved_for_session)
|
|
||||||
self.stream.send(msg)
|
|
||||||
self.db_disconnect()
|
self.db_disconnect()
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|||||||
@@ -1880,6 +1880,7 @@ class JCLComponent_TestCase(unittest.TestCase):
|
|||||||
def test_handle_message_password(self):
|
def test_handle_message_password(self):
|
||||||
self.comp.stream = MockStream()
|
self.comp.stream = MockStream()
|
||||||
self.comp.stream_class = MockStream
|
self.comp.stream_class = MockStream
|
||||||
|
self.comp.authenticated()
|
||||||
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
||||||
account11 = Account(user_jid = "user1@test.com", \
|
account11 = Account(user_jid = "user1@test.com", \
|
||||||
name = "account11", \
|
name = "account11", \
|
||||||
@@ -1901,9 +1902,10 @@ class JCLComponent_TestCase(unittest.TestCase):
|
|||||||
self.assertEqual(len(messages_sent), 0)
|
self.assertEqual(len(messages_sent), 0)
|
||||||
|
|
||||||
def test_handle_message_password_complex(self):
|
def test_handle_message_password_complex(self):
|
||||||
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
|
||||||
self.comp.stream = MockStream()
|
self.comp.stream = MockStream()
|
||||||
self.comp.stream_class = MockStream
|
self.comp.stream_class = MockStream
|
||||||
|
self.comp.authenticated()
|
||||||
|
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
||||||
account11 = ExampleAccount(user_jid = "user1@test.com", \
|
account11 = ExampleAccount(user_jid = "user1@test.com", \
|
||||||
name = "account11", \
|
name = "account11", \
|
||||||
jid = "account11@jcl.test.com")
|
jid = "account11@jcl.test.com")
|
||||||
|
|||||||
Reference in New Issue
Block a user