diff options
| author | Barry Warsaw | 2011-01-06 16:19:00 -0500 |
|---|---|---|
| committer | Barry Warsaw | 2011-01-06 16:19:00 -0500 |
| commit | d0112b5dc950b8d0180997b6e6dc71bd66af1ee8 (patch) | |
| tree | 2f1143c787c69dd224d763917bc3d1f0154dbc06 /src/mailman/email | |
| parent | c7e794caecb8b12d250be92f698fed8fa1f8a101 (diff) | |
| download | mailman-d0112b5dc950b8d0180997b6e6dc71bd66af1ee8.tar.gz mailman-d0112b5dc950b8d0180997b6e6dc71bd66af1ee8.tar.zst mailman-d0112b5dc950b8d0180997b6e6dc71bd66af1ee8.zip | |
Diffstat (limited to 'src/mailman/email')
| -rw-r--r-- | src/mailman/email/validate.py | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/src/mailman/email/validate.py b/src/mailman/email/validate.py index 4eb13eee8..0c4c6a5e0 100644 --- a/src/mailman/email/validate.py +++ b/src/mailman/email/validate.py @@ -21,15 +21,18 @@ from __future__ import absolute_import, unicode_literals __metaclass__ = type __all__ = [ - 'is_valid', - 'validate', + 'Validator', ] import re +from zope.interface import implements + + from mailman.email.utils import split_email -from mailman.interfaces.address import InvalidEmailAddressError +from mailman.interfaces.address import ( + IEmailValidator, InvalidEmailAddressError) # What other characters should be disallowed? @@ -37,34 +40,31 @@ _badchars = re.compile(r'[][()<>|;^,\000-\037\177-\377]') -def validate(address): - """Validate an email address. +class Validator: + """An email address validator.""" - :param address: An email address. - :type address: string - :raise InvalidEmailAddressError: when the address is deemed invalid. - """ - if not is_valid(address): - raise InvalidEmailAddressError(repr(address)) + implements(IEmailValidator) + def is_valid(self, email): + """See `IEmailValidator`.""" + if not email or ' ' in email: + return False + if _badchars.search(email) or email[0] == '-': + return False + user, domain_parts = split_email(email) + # Local, unqualified addresses are not allowed. + if not domain_parts: + return False + if len(domain_parts) < 2: + return False + return True - -def is_valid(address): - """Check if an email address if valid. + def validate(self, email): + """Validate an email address. - :param address: An email address. - :type address: string - :return: A flag indicating whether the email address is okay or not. - :rtype: bool - """ - if not address or ' ' in address: - return False - if _badchars.search(address) or address[0] == '-': - return False - user, domain_parts = split_email(address) - # Local, unqualified addresses are not allowed. - if not domain_parts: - return False - if len(domain_parts) < 2: - return False - return True + :param address: An email address. + :type address: string + :raise InvalidEmailAddressError: when the address is deemed invalid. + """ + if not self.is_valid(email): + raise InvalidEmailAddressError(repr(email)) |
