diff options
author | David Rousselie <dax@happycoders.org> | 2008-09-02 00:42:03 +0400 |
---|---|---|
committer | David Rousselie <dax@happycoders.org> | 2008-09-02 00:42:03 +0400 |
commit | 37babbc31c01c81485fdd6ce6ef95e3c737a0a37 (patch) | |
tree | 5b4d3e0ea8a45c3a3d6cd0f631bf74802be6b702 /src | |
parent | d22e56891116e25b602f9e3c1a48586f61b5e469 (diff) |
Filters not well formed account name given in register form
darcs-hash:20080901204203-86b55-80775d24770b57c736d5a8dc7d2e50bb279e958a.gz
Diffstat (limited to 'src')
-rw-r--r-- | src/jcl/error.py | 34 | ||||
-rw-r--r-- | src/jcl/jabber/register.py | 43 | ||||
-rw-r--r-- | src/jcl/jabber/tests/__init__.py | 3 | ||||
-rw-r--r-- | src/jcl/jabber/tests/component.py | 4 | ||||
-rw-r--r-- | src/jcl/jabber/tests/disco.py | 1 | ||||
-rw-r--r-- | src/jcl/lang.py | 5 | ||||
-rw-r--r-- | src/jcl/model/account.py | 4 | ||||
-rw-r--r-- | src/jcl/tests/lang.py | 4 |
8 files changed, 76 insertions, 22 deletions
diff --git a/src/jcl/error.py b/src/jcl/error.py index f99a67a..5bf3ced 100644 --- a/src/jcl/error.py +++ b/src/jcl/error.py @@ -3,18 +3,18 @@ ## Login : David Rousselie <dax@happycoders.org> ## Started on Sun Nov 5 20:13:48 2006 David Rousselie ## $Id$ -## +## ## Copyright (C) 2006 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 @@ -24,13 +24,35 @@ __revision__ = "$Id: error.py,v 1.1 2006/11/05 20:13:48 dax Exp $" +from jcl.lang import Lang + class FieldError(Exception): """Error raised when error exists on Jabber Data Form fields""" - def __init__(self, field, error_msg): + def __init__(self, field, message="", message_property=None, lang_class=Lang.en): Exception.__init__(self) self.field = field - self.error_msg = error_msg + self.lang_class = lang_class + self.message_property = message_property + self.message = message def __str__(self): - return "Error with " + str(self.field) + " field: " + str(self.error_msg) + if self.message_property is None \ + or not hasattr(self.lang_class, self.message_property): + return self.lang_class.field_error % (str(self.field), self.message) + else: + return self.lang_class.field_error % \ + (str(self.field), + str(getattr(self.lang_class, self.message_property))) + +class MandatoryFieldError(FieldError): + """Error raised when a mandatory field in a Form is not supplied""" + + def __init__ (self, field): + FieldError.__init__(self, field, message_property="mandatory_field") + +class NotWellFormedFieldError(FieldError): + """Error raised when a supplied field in a Form is not well formed""" + + def __init__ (self, field): + FieldError.__init__(self, field, message_property="not_well_formed_field") diff --git a/src/jcl/jabber/register.py b/src/jcl/jabber/register.py index f719b8a..4c24bcc 100644 --- a/src/jcl/jabber/register.py +++ b/src/jcl/jabber/register.py @@ -1,20 +1,21 @@ +# -*- coding: utf-8 -*- ## ## register.py ## Login : David Rousselie <dax@happycoders.org> ## Started on Wed Jul 18 21:32:51 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 @@ -23,10 +24,11 @@ import logging import sys import traceback +import re import pyxmpp.error as error -from jcl.error import FieldError +from jcl.error import FieldError, MandatoryFieldError, NotWellFormedFieldError import jcl.jabber as jabber class SetRegisterHandler(object): @@ -36,6 +38,7 @@ class SetRegisterHandler(object): self.component = component self.account_manager = component.account_manager self.__logger = logging.getLogger("jcl.jabber.SetRegisterHandler") + self.field_name_regexp = re.compile("^[^@]+$") def filter(self, info_query, lang_class, x_data): """Filter requests to be handled""" @@ -43,7 +46,7 @@ class SetRegisterHandler(object): def handle(self, info_query, lang_class, data, x_data): """Handle disco get items request""" - return None + return self.validate_form(x_data, info_query, lang_class) def handle_error(self, field_error, info_query, lang_class): type, value, stack = sys.exc_info() @@ -51,13 +54,26 @@ class SetRegisterHandler(object): (field_error, "".join(traceback.format_exception (type, value, stack, 5)))) iq_error = info_query.make_error_response("not-acceptable") + field_error.lang_class = lang_class text = iq_error.get_error().xmlnode.newTextChild(\ None, "text", - lang_class.mandatory_field % (field_error.field)) + str(field_error)) text.setNs(text.newNs(error.STANZA_ERROR_NS, None)) return [iq_error] + def validate_form(self, form, info_query, lang_class): + """Test if given form is valid""" + if form is None or not "name" in form or form["name"].value == "": + return self.handle_error(\ + MandatoryFieldError("name"), + info_query, lang_class) + if not self.field_name_regexp.match(form["name"].value): + return self.handle_error(\ + NotWellFormedFieldError("name"), + info_query, lang_class) + return None + class RootSetRegisterHandler(SetRegisterHandler): def __init__(self, component): @@ -73,10 +89,11 @@ class RootSetRegisterHandler(SetRegisterHandler): """ """ self.__logger.debug("root_set_register") + stanzas = SetRegisterHandler.handle(self, info_query, lang_class, + data, x_data) + if stanzas is not None: + return stanzas _account = None - if not "name" in x_data or x_data["name"].value == "": - return self.handle_error(FieldError("name", ""), - info_query, lang_class) try: info_queries = self.account_manager.create_default_account(\ x_data["name"].value, @@ -104,6 +121,10 @@ class AccountSetRegisterHandler(SetRegisterHandler): """ """ self.__logger.debug("account_set_register") + stanzas = SetRegisterHandler.handle(self, info_query, lang_class, + data, x_data) + if stanzas is not None: + return stanzas _account = None resource = info_query.get_to().resource if resource is not None: @@ -138,6 +159,10 @@ class AccountTypeSetRegisterHandler(SetRegisterHandler): """ """ self.__logger.debug("account_type_set_register") + stanzas = SetRegisterHandler.handle(self, info_query, lang_class, + data, x_data) + if stanzas is not None: + return stanzas account_type = data _account = None try: diff --git a/src/jcl/jabber/tests/__init__.py b/src/jcl/jabber/tests/__init__.py index b2d0eee..9b84e29 100644 --- a/src/jcl/jabber/tests/__init__.py +++ b/src/jcl/jabber/tests/__init__.py @@ -6,7 +6,7 @@ import unittest import jcl.jabber as jabber from jcl.jabber.tests import component, feeder, command, message, presence, \ - disco, vcard + disco, vcard, register class HandlerType1: pass @@ -39,6 +39,7 @@ def suite(): test_suite.addTest(presence.suite()) test_suite.addTest(disco.suite()) test_suite.addTest(vcard.suite()) + test_suite.addTest(register.suite()) return test_suite if __name__ == '__main__': diff --git a/src/jcl/jabber/tests/component.py b/src/jcl/jabber/tests/component.py index 3f19cfa..5ece064 100644 --- a/src/jcl/jabber/tests/component.py +++ b/src/jcl/jabber/tests/component.py @@ -1327,7 +1327,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase): self.assertEquals(stanza_error.get_condition().name, "not-acceptable") self.assertEquals(stanza_error.get_text(), - Lang.en.mandatory_field % ("name")) + Lang.en.field_error % ("name", Lang.en.mandatory_field)) def test_handle_set_register_new_field_mandatory(self): self.comp.stream = MockStream() @@ -1358,7 +1358,7 @@ class JCLComponent_handle_set_register_TestCase(JCLComponent_TestCase): self.assertEquals(stanza_error.get_condition().name, "not-acceptable") self.assertEquals(stanza_error.get_text(), - Lang.en.mandatory_field % ("login")) + Lang.en.field_error % ("login", Lang.en.mandatory_field)) def test_handle_set_register_update_not_existing(self): self.comp.stream = MockStream() diff --git a/src/jcl/jabber/tests/disco.py b/src/jcl/jabber/tests/disco.py index f210960..56b150b 100644 --- a/src/jcl/jabber/tests/disco.py +++ b/src/jcl/jabber/tests/disco.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- ## ## disco.py ## Login : David Rousselie <dax@happycoders.org> diff --git a/src/jcl/lang.py b/src/jcl/lang.py index 0a95d40..9fb79cb 100644 --- a/src/jcl/lang.py +++ b/src/jcl/lang.py @@ -89,7 +89,9 @@ class Lang: update_account_message_subject = u"Updated account '%s'" update_account_message_body = u"Updated account" - mandatory_field = u"%s is required" + field_error = u"Error with '%s' field: %s" + mandatory_field = u"required field" + not_well_formed_field = u"not well formed field" field_chat_action = u"Action when state is 'Free For Chat'" field_online_action = u"Action when state is 'Online'" @@ -265,6 +267,7 @@ class Lang: update_account_message_body = u"Compte mis à jour" mandatory_field = u"%s est requis" + not_well_formed_field = u"Le champs %s n'est pas acceptable" field_chat_action = u"Action lorsque l'état est 'Free For Chat'" field_online_action = u"Action lorsque l'état est 'Online'" diff --git a/src/jcl/model/account.py b/src/jcl/model/account.py index 7bd0775..8f850d8 100644 --- a/src/jcl/model/account.py +++ b/src/jcl/model/account.py @@ -34,7 +34,7 @@ from sqlobject.joins import MultipleJoin from sqlobject.sqlbuilder import AND from jcl.lang import Lang -from jcl.error import FieldError +from jcl.error import MandatoryFieldError import jcl.model as model OFFLINE = "offline" @@ -58,7 +58,7 @@ def mandatory_field(field_name, field_value): """Used as default function for field that must be specified and cannot have an empty value""" if field_value is None or str(field_value) == "": - raise FieldError(field_name, "Field required") + raise MandatoryFieldError(field_name) return field_value class User(InheritableSQLObject): diff --git a/src/jcl/tests/lang.py b/src/jcl/tests/lang.py index ff5d5ce..9a92ad0 100644 --- a/src/jcl/tests/lang.py +++ b/src/jcl/tests/lang.py @@ -91,7 +91,9 @@ class Language_TestCase(unittest.TestCase): self.assertNotEquals(self.lang_class.update_account_message_subject % (""), None) self.assertNotEquals(self.lang_class.update_account_message_body, None) - self.assertNotEquals(self.lang_class.mandatory_field % (""), None) + self.assertNotEquals(self.lang_class.field_error % ("", ""), None) + self.assertNotEquals(self.lang_class.mandatory_field, None) + self.assertNotEquals(self.lang_class.not_well_formed_field, None) self.assertNotEquals(self.lang_class.field_chat_action, None) self.assertNotEquals(self.lang_class.field_online_action, None) |