VirtualMailManager/EmailAddress.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sat, 02 Jan 2010 15:45:12 +0000
changeset 162 0ac9ef587769
parent 155 eb866ebb9f2e
child 185 6e1ef32fbd82
permissions -rw-r--r--
Updated copyright notices to include the year 2010. Also corrected the name of the copyright holder and updated the e-mail address.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 155
diff changeset
     2
# Copyright (c) 2008 - 2010, Pascal Volk
76
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
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
"""Virtual Mail Manager's EmailAddress class to handle e-mail addresses."""
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
     7
from __main__ import re, ERR
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
from Exceptions import VMMEmailAddressException as VMMEAE
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
import VirtualMailManager as VMM
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
class EmailAddress(object):
108
fb88585f17fe VMM.EmailAddress: Added __slots__
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    14
    __slots__ = ('_localpart', '_domainname')
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
    def __init__(self, address):
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
        self._localpart = None
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
        self._domainname = None
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
        self.__chkAddress(address)
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    19
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
    def __eq__(self, other):
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    21
        if isinstance(other, self.__class__):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
            return self._localpart == other._localpart\
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
                    and self._domainname == other._domainname
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    24
        return NotImplemented
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
    def __ne__(self, other):
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    27
        if isinstance(other, self.__class__):
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    28
            return self._localpart != other._localpart\
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    29
                    or self._domainname != other._domainname
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    30
        return NotImplemented
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
    def __repr__(self):
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        return "EmailAddress('%s@%s')" % (self._localpart, self._domainname)
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
    def __str__(self):
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
        return "%s@%s" % (self._localpart, self._domainname)
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
    def __chkAddress(self, address):
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        try:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
            localpart, domain = address.split('@')
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
        except ValueError:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 116
diff changeset
    42
            raise VMMEAE(_(u"Missing '@' sign in e-mail address “%s”.") %
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
                address, ERR.INVALID_ADDRESS)
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
        except AttributeError:
155
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
    45
            raise VMMEAE(_(u"“%s” doesn't look like an e-mail address.") %
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
                address, ERR.INVALID_ADDRESS)
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
        if len(domain) > 0:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
            domain = VMM.VirtualMailManager.chkDomainname(domain)
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
        else:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 116
diff changeset
    50
            raise VMMEAE(_(u"Missing domain name after “%s@”.") %
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
                    localpart, ERR.DOMAIN_NO_NAME)
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
        localpart = self.__chkLocalpart(localpart)
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
        self._localpart, self._domainname = localpart, domain
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
    def __chkLocalpart(self, localpart):
155
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
    56
        """Validates the local-part of an e-mail address.
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    57
155
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
    58
        Arguments:
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
    59
        localpart -- local-part of the e-mail address that should be validated (str)
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        """
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        if len(localpart) < 1:
155
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
    62
            raise VMMEAE(_(u'No local-part specified.'),
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
                ERR.LOCALPART_INVALID)
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
        if len(localpart) > 64:
155
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
    65
            raise VMMEAE(_(u'The local-part “%s” is too long') %
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
                localpart, ERR.LOCALPART_TOO_LONG)
87
f9090d1a0730 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    67
        ic = set(re.findall(RE_LOCALPART, localpart))
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
        if len(ic):
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
            ichrs = ''
87
f9090d1a0730 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    70
            for c in ic:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 116
diff changeset
    71
                ichrs += u"“%s” " % c
155
eb866ebb9f2e Fixed some grammar errors and typos. (Reported by Clytie Siddall)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
    72
            raise VMMEAE(_(u"The local-part “%(lpart)s” contains invalid\
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
 characters: %(ichrs)s") % {'lpart': localpart, 'ichrs': ichrs},
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
        return localpart
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76