diff --git a/run_tests.py b/run_tests.py index 8d17c82..2bb443a 100644 --- a/run_tests.py +++ b/run_tests.py @@ -58,7 +58,7 @@ if __name__ == '__main__': jcl_suite = unittest.TestSuite() # jcl_suite.addTest(FeederComponent_TestCase('test_handle_tick')) -# jcl_suite.addTest(JCLComponent_TestCase('test_handle_get_register_exist_complex')) +# jcl_suite.addTest(JCLComponent_TestCase('test_send_error_second')) # jcl_suite = unittest.TestSuite((component_suite)) # jcl_suite = unittest.TestSuite((presence_account_suite)) jcl_suite = unittest.TestSuite((component_suite, \ diff --git a/src/jcl/jabber/component.py b/src/jcl/jabber/component.py index d34d724..7087b0c 100644 --- a/src/jcl/jabber/component.py +++ b/src/jcl/jabber/component.py @@ -33,6 +33,7 @@ import time import logging import signal import re +import traceback from Queue import Queue @@ -328,11 +329,12 @@ class JCLComponent(Component, object): def get_reg_form_for_account(_account_class, name, base_from_jid): self.db_connect() # TODO : do it only one time - for _account in _account_class.select(\ + _accounts = _account_class.select(\ AND(_account_class.q.name == name, \ - _account_class.q.user_jid == base_from_jid)): + _account_class.q.user_jid == base_from_jid)) + if _accounts is not None: self.get_reg_form_init(lang_class, \ - _account).as_xml(query) + _accounts[0]).as_xml(query) self.db_disconnect() self.__logger.debug("GET_REGISTER") @@ -719,6 +721,23 @@ class JCLComponent(Component, object): (_account.name)) self.stream.send(msg) + def _send_error(self, _account, exception): + """Send an error message only one time until _account.in_error + has been reset to False""" + if _account.in_error == False: + _account.in_error = True + msg = Message(from_jid = _account.jid, \ + to_jid = _account.user_jid, \ + stanza_type = "error", \ + subject = _account.default_lang_class.check_error_subject, \ + body = _account.default_lang_class.check_error_body \ + % (exception)) + self.stream.send(msg) + type, value, stack = sys.exc_info() + self.__logger.debug("Error while first checking mail : %s\n%s" \ + % (exception, "".join(traceback.format_exception + (type, value, stack, 5)))) + def get_jid(self, _account): """Return account jid based on account instance and component jid """ diff --git a/src/jcl/lang.py b/src/jcl/lang.py index eb4e86b..2f9f7bf 100644 --- a/src/jcl/lang.py +++ b/src/jcl/lang.py @@ -107,8 +107,8 @@ class Lang: # connection_label = u"%s connection '%s'" # update_account_message_subject = u"Updated account '%s'" # update_account_message_body = u"Updated account" -# check_error_subject = u"Error while checking emails." -# check_error_body = u"An error appears while checking emails:\n\t%s" + check_error_subject = u"Error while checking emails." + check_error_body = u"An error appears while checking emails:\n\t%s" # new_mail_subject = u"New email from %s" # new_digest_subject = u"%i new email(s)" diff --git a/src/jcl/model/account.py b/src/jcl/model/account.py index 241d659..9994187 100644 --- a/src/jcl/model/account.py +++ b/src/jcl/model/account.py @@ -27,7 +27,7 @@ __revision__ = "$Id: account.py,v 1.3 2005/09/18 20:24:07 dax Exp $" from sqlobject.inheritance import InheritableSQLObject -from sqlobject.col import StringCol, EnumCol, IntCol +from sqlobject.col import StringCol, EnumCol, IntCol, BoolCol from sqlobject.dbconnection import ConnectionHub from jcl.lang import Lang @@ -68,6 +68,7 @@ class Account(InheritableSQLObject): jid = StringCol() ## Not yet used first_check = BoolCol(default = True) __status = StringCol(default = OFFLINE, dbName = "status") + in_error = BoolCol(default = False) ## Use these attributs to support volatile password ## login = StringCol(default = "") @@ -211,3 +212,17 @@ class PresenceAccount(Account): is_action_possible, mandatory_field)] get_register_fields = classmethod(_get_register_fields) + + def get_action(self): + """Get apropriate action depending on current status""" + mapping = {"online": self.online_action, + "chat": self.chat_action, + "away": self.away_action, + "xa": self.xa_action, + "dnd": self.dnd_action, + "offline": self.offline_action} + if mapping.has_key(self.status): + return mapping[self.status] + return PresenceAccount.DO_NOTHING + + action = property(get_action) diff --git a/tests/jcl/jabber/test_component.py b/tests/jcl/jabber/test_component.py index fd54714..f177b3c 100644 --- a/tests/jcl/jabber/test_component.py +++ b/tests/jcl/jabber/test_component.py @@ -1836,3 +1836,34 @@ class JCLComponent_TestCase(unittest.TestCase): def test_handle_tick(self): self.assertRaises(NotImplementedError, self.comp.handle_tick) + + def test_send_error_first(self): + self.comp.stream = MockStream() + self.comp.stream_class = MockStream + account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) + _account = Account(user_jid = "user1@test.com", \ + name = "account11", \ + jid = "account11@jcl.test.com") + exception = Exception("test exception") + self.comp._send_error(_account, exception) + self.assertEqual(len(self.comp.stream.sent), 1) + error_sent = self.comp.stream.sent[0] + self.assertEqual(error_sent.get_to(), _account.user_jid) + self.assertEqual(error_sent.get_from(), _account.jid) + self.assertEqual(error_sent.get_type(), "error") + self.assertEqual(error_sent.get_subject(), _account.default_lang_class.check_error_subject) + self.assertEqual(error_sent.get_body(), _account.default_lang_class.check_error_body \ + % (exception)) + del account.hub.threadConnection + + def test_send_error_second(self): + self.comp.stream = MockStream() + self.comp.stream_class = MockStream + account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL) + _account = Account(user_jid = "user1@test.com", \ + name = "account11", \ + jid = "account11@jcl.test.com") + _account.in_error = True + exception = Exception("test exception") + self.comp._send_error(_account, exception) + self.assertEqual(len(self.comp.stream.sent), 0)