diff -r af555e6967c8 -r 58d1b6f41664 VirtualMailManager/EmailAddress.py --- a/VirtualMailManager/EmailAddress.py Thu Apr 15 23:25:24 2010 +0000 +++ b/VirtualMailManager/EmailAddress.py Fri Apr 16 03:02:34 2010 +0000 @@ -7,13 +7,15 @@ Virtual Mail Manager's EmailAddress class to handle e-mail addresses. """ +import re -from VirtualMailManager import check_domainname, check_localpart +from VirtualMailManager import check_domainname from VirtualMailManager.constants.ERROR import \ - DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID + DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG from VirtualMailManager.errors import EmailAddressError as EAErr +RE_LOCALPART = re.compile(r"[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]") _ = lambda msg: msg @@ -81,4 +83,23 @@ self._domainname = check_domainname(parts[1]) +def check_localpart(localpart): + """Returns the validated local-part `localpart`. + + Throws a `EmailAddressError` if the local-part is too long or contains + invalid characters. + """ + if len(localpart) > 64: + raise EAErr(_(u"The local-part '%s' is too long") % localpart, + LOCALPART_TOO_LONG) + invalid_chars = set(RE_LOCALPART.findall(localpart)) + if invalid_chars: + i_chars = u''.join((u'"%s" ' % c for c in invalid_chars)) + raise EAErr(_(u"The local-part '%(l_part)s' contains invalid \ +characters: %(i_chars)s") % + {'l_part': localpart, 'i_chars': i_chars}, + LOCALPART_INVALID) + return localpart + + del _