author | Pascal Volk <neverseen@users.sourceforge.net> |
Fri, 07 Aug 2009 01:03:46 +0200 | |
changeset 100 | b581e747e720 |
parent 87 | f9090d1a0730 |
child 102 | 485d3f7d6981 |
permissions | -rw-r--r-- |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
2 |
# Copyright 2008 VEB IT |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
4 |
# $Id$ |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
5 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
6 |
"""Virtual Mail Manager's EmailAddress class to handle e-mail addresses.""" |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
7 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
8 |
from constants.VERSION import VERSION |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
9 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
10 |
__author__ = 'Pascal Volk <p.volk@veb-it.de>' |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
11 |
__version__ = VERSION |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
12 |
__revision__ = 'rev '+'$Rev$'.split()[1] |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
13 |
__date__ = '$Date$'.split()[1] |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
14 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
15 |
import re |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
16 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
17 |
from Exceptions import VMMEmailAddressException as VMMEAE |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
18 |
import VirtualMailManager as VMM |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
19 |
import constants.ERROR as ERR |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
20 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
21 |
RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]""" |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
22 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
23 |
class EmailAddress(object): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
24 |
def __init__(self, address): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
self._localpart = None |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
26 |
self._domainname = None |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
27 |
self.__chkAddress(address) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
28 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
29 |
def __eq__(self, other): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
30 |
if hasattr(other, '_localpart') and hasattr(other, '_domainname'): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
31 |
return self._localpart == other._localpart\ |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
32 |
and self._domainname == other._domainname |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
33 |
else: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
return NotImplemented |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
36 |
def __ne__(self, other): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
37 |
if hasattr(other, '_localpart') and hasattr(other, '_domainname'): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
38 |
return not self._localpart == other._localpart\ |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
39 |
and self._domainname == other._domainname |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
40 |
else: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
41 |
return NotImplemented |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
42 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
43 |
def __repr__(self): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
44 |
return "EmailAddress('%s@%s')" % (self._localpart, self._domainname) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
45 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
46 |
def __str__(self): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
return "%s@%s" % (self._localpart, self._domainname) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
48 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
49 |
def __chkAddress(self, address): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
50 |
try: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
51 |
localpart, domain = address.split('@') |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
52 |
except ValueError: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
53 |
raise VMMEAE(_(u"Missing '@' sign in e-mail address »%s«.") % |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
54 |
address, ERR.INVALID_ADDRESS) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
55 |
except AttributeError: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
56 |
raise VMMEAE(_(u"»%s« looks not like an e-mail address.") % |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
57 |
address, ERR.INVALID_ADDRESS) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
58 |
if len(domain) > 0: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
59 |
domain = VMM.VirtualMailManager.chkDomainname(domain) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
60 |
else: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
61 |
raise VMMEAE(_(u"Missing domain name after »%s@«.") % |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
localpart, ERR.DOMAIN_NO_NAME) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
63 |
localpart = self.__chkLocalpart(localpart) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
self._localpart, self._domainname = localpart, domain |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
65 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
66 |
def __chkLocalpart(self, localpart): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
"""Validates the local part of an e-mail address. |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
68 |
|
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
Keyword arguments: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
70 |
localpart -- of the e-mail address that should be validated (str) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
71 |
""" |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
72 |
if len(localpart) < 1: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
73 |
raise VMMEAE(_(u'No localpart specified.'), |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
74 |
ERR.LOCALPART_INVALID) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
75 |
if len(localpart) > 64: |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
76 |
raise VMMEAE(_(u'The local part »%s« is too long') % |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
77 |
localpart, ERR.LOCALPART_TOO_LONG) |
87
f9090d1a0730
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
76
diff
changeset
|
78 |
ic = set(re.findall(RE_LOCALPART, localpart)) |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
79 |
if len(ic): |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
80 |
ichrs = '' |
87
f9090d1a0730
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
76
diff
changeset
|
81 |
for c in ic: |
76
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
82 |
ichrs += u"»%s« " % c |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
83 |
raise VMMEAE(_(u"The local part »%(lpart)s« contains invalid\ |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
84 |
characters: %(ichrs)s") % {'lpart': localpart, 'ichrs': ichrs}, |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
85 |
ERR.LOCALPART_INVALID) |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
86 |
return localpart |
14c0a092d7d2
* 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
87 |