diff --git a/src/jcl/jabber/component.py b/src/jcl/jabber/component.py index 3cd40ad..ade01d4 100644 --- a/src/jcl/jabber/component.py +++ b/src/jcl/jabber/component.py @@ -49,7 +49,7 @@ from jcl.error import FieldError from jcl.jabber.disco import AccountDiscoGetInfoHandler, \ AccountTypeDiscoGetInfoHandler, RootDiscoGetItemsHandler, \ AccountTypeDiscoGetItemsHandler -from jcl.jabber.message import PasswordMessageHandler +from jcl.jabber.message import PasswordMessageHandler, HelpMessageHandler import jcl.jabber.command as command from jcl.jabber.command import CommandDiscoGetItemsHandler, \ CommandDiscoGetInfoHandler, JCLCommandManager, \ @@ -753,7 +753,8 @@ class JCLComponent(Component, object): self.handle_message) self.send_stanzas(self.account_manager.probe_all_accounts_presence()) - self.msg_handlers += [[PasswordMessageHandler(self)]] + self.msg_handlers += [[PasswordMessageHandler(self), + HelpMessageHandler(self)]] def signal_handler(self, signum, frame): """Stop method handler diff --git a/src/jcl/jabber/message.py b/src/jcl/jabber/message.py index 068f61d..01fc475 100644 --- a/src/jcl/jabber/message.py +++ b/src/jcl/jabber/message.py @@ -3,18 +3,18 @@ ## Login : David Rousselie ## Started on Wed Jun 27 21:43:57 2007 David Rousselie ## $Id$ -## +## ## Copyright (C) 2007 David Rousselie ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA @@ -22,6 +22,8 @@ import re +from pyxmpp.message import Message + from jcl.jabber import Handler import jcl.model.account as account from jcl.model.account import Account @@ -57,3 +59,27 @@ class PasswordMessageHandler(Handler): return self.component.account_manager.set_password(data, stanza.get_from(), stanza.get_body(), lang_class) + +class HelpMessageHandler(Handler): + """Handle 'help' sent in a message""" + + def __init__(self, component): + """Handler constructor""" + Handler.__init__(self, component) + self.help_regexp = re.compile("^help.*") + + def filter(self, stanza, lang_class): + """ + Test if stanza body match the help regexp. + """ + return self.help_regexp.search(stanza.get_body()) \ + or self.help_regexp.search(stanza.get_subject()) + + def handle(self, stanza, lang_class, data): + """ + Return a help message. + """ + return [Message(to_jid=stanza.get_from(), + from_jid=stanza.get_to(), + subject=lang_class.help_message_subject, + body=lang_class.help_message_body)] diff --git a/src/jcl/jabber/tests/message.py b/src/jcl/jabber/tests/message.py index adba08c..c8fd8c8 100644 --- a/src/jcl/jabber/tests/message.py +++ b/src/jcl/jabber/tests/message.py @@ -3,18 +3,18 @@ ## Login : David Rousselie ## Started on Fri Jul 6 21:40:55 2007 David Rousselie ## $Id$ -## +## ## Copyright (C) 2007 David Rousselie ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. -## +## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. -## +## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA @@ -26,7 +26,7 @@ from pyxmpp.message import Message from jcl.lang import Lang from jcl.jabber.component import JCLComponent -from jcl.jabber.message import PasswordMessageHandler +from jcl.jabber.message import PasswordMessageHandler, HelpMessageHandler import jcl.model.account as account import jcl.model as model from jcl.model.account import Account, User @@ -133,9 +133,99 @@ class PasswordMessageHandler_TestCase(JCLTestCase): self.assertEquals(account11.password, "secret") model.db_disconnect() +class HelpMessageHandler_TestCase(JCLTestCase): + def setUp(self): + JCLTestCase.setUp(self, tables=[User, Account, ExampleAccount]) + self.comp = JCLComponent("jcl.test.com", + "password", + "localhost", + "5347", + self.db_url) + self.handler = HelpMessageHandler(self.comp) + + def test_filter(self): + user1 = User(jid="user1@test.com") + account11 = ExampleAccount(user=user1, + name="account11", + jid="account11@jcl.test.com") + account12 = ExampleAccount(user=user1, + name="account12", + jid="account12@jcl.test.com") + message = Message(from_jid="user1@test.com", + to_jid="account11@jcl.test.com", + subject="", + body="help") + result = self.handler.filter(message, None) + self.assertNotEquals(None, result) + + def test_filter_long_help_message(self): + user1 = User(jid="user1@test.com") + account11 = ExampleAccount(user=user1, + name="account11", + jid="account11@jcl.test.com") + account12 = ExampleAccount(user=user1, + name="account12", + jid="account12@jcl.test.com") + message = Message(from_jid="user1@test.com", + to_jid="account11@jcl.test.com", + subject="", + body="help dalkjdjhbd") + result = self.handler.filter(message, None) + self.assertNotEquals(None, result) + + def test_filter_in_subject(self): + user1 = User(jid="user1@test.com") + account11 = ExampleAccount(user=user1, + name="account11", + jid="account11@jcl.test.com") + account12 = ExampleAccount(user=user1, + name="account12", + jid="account12@jcl.test.com") + message = Message(from_jid="user1@test.com", + to_jid="account11@jcl.test.com", + subject="help dalkjdjhbd", + body="") + result = self.handler.filter(message, None) + self.assertNotEquals(None, result) + + def test_filter_wrong_message(self): + user1 = User(jid="user1@test.com") + account11 = ExampleAccount(user=user1, + name="account11", + jid="account11@jcl.test.com") + account12 = ExampleAccount(user=user1, + name="account12", + jid="account12@jcl.test.com") + message = Message(from_jid="user1@test.com", + to_jid="account11@jcl.test.com", + subject="", + body="hepl") + result = self.handler.filter(message, None) + self.assertEquals(None, result) + + def test_handle(self): + user1 = User(jid="user1@test.com") + account11 = ExampleAccount(user=user1, + name="account11", + jid="account11@jcl.test.com") + account12 = ExampleAccount(user=user1, + name="account12", + jid="account12@jcl.test.com") + message = Message(from_jid="user1@test.com", + to_jid="account11@jcl.test.com", + subject="", + body="help") + messages = self.handler.handle(message, Lang.en, account11) + self.assertEquals(len(messages), 1) + self.assertEquals(messages[0].get_from(), "account11@jcl.test.com") + self.assertEquals(messages[0].get_to(), "user1@test.com") + self.assertEquals(messages[0].get_subject(), Lang.en.help_message_subject) + self.assertEquals(messages[0].get_body(), Lang.en.help_message_body) + def suite(): test_suite = unittest.TestSuite() test_suite.addTest(unittest.makeSuite(PasswordMessageHandler_TestCase, 'test')) + test_suite.addTest(unittest.makeSuite(HelpMessageHandler_TestCase, 'test')) return test_suite if __name__ == '__main__': diff --git a/src/jcl/lang.py b/src/jcl/lang.py index 60e11c1..cd1ce2d 100644 --- a/src/jcl/lang.py +++ b/src/jcl/lang.py @@ -237,6 +237,9 @@ class Lang: account_disabled = u"This account is disabled" account_error = u"This account has an error" + help_message_subject = u"Help" + help_message_body = u"No help" + class fr: component_name = u"composant générique Jabber Component Library" register_title = u"Enregistrement d'un nouveau compte" @@ -419,6 +422,9 @@ class Lang: account_disabled = u"Ce compte est désactivé" account_error = u"Ce compte est en erreur" + help_message_subject = u"Aide" + help_message_body = u"Pas d'aide !" + class nl: # TODO: when finish, delete this line and uncomment in tests/lang.py the makeSuite(Language_nl_TestCase, 'test') line register_title = u"Registratie van verbindingen voor Jabber Mail" diff --git a/src/jcl/tests/lang.py b/src/jcl/tests/lang.py index 931246a..3c04a33 100644 --- a/src/jcl/tests/lang.py +++ b/src/jcl/tests/lang.py @@ -239,6 +239,9 @@ class Language_TestCase(unittest.TestCase): self.assertNotEquals(self.lang_class.account_disabled, None) self.assertNotEquals(self.lang_class.account_error, None) + self.assertNotEquals(self.lang_class.help_message_subject, None) + self.assertNotEquals(self.lang_class.help_message_body, None) + class Language_fr_TestCase(Language_TestCase): def setUp(self): self.lang_class = Lang.fr