VirtualMailManager/emailaddress.py
author Pascal Volk <user@localhost.localdomain.org>
Sun, 23 Feb 2014 18:16:57 +0000
branchv0.7.x
changeset 737 81f4bfc34ed8
parent 711 2a75058fc064
permissions -rw-r--r--
VMM/cli/subcommands: userpassword: Added option --hash. vmm userpassword --hash {SCHEME}$HASH can be used to save a password hash, e.g. generated by doveadm pw, as new user password.
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 -*-
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 675
diff changeset
     2
# Copyright (c) 2008 - 2014, Pascal Volk
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
     4
"""
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     5
    VirtualMailManager.emailaddress
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
     7
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
     8
    Virtual Mail Manager's EmailAddress class to handle e-mail addresses.
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
     9
"""
253
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
    10
import re
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
367
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    12
from VirtualMailManager.domain import check_domainname, get_gid
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    13
from VirtualMailManager.constants import \
427
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    14
     DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG, \
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    15
     DOMAIN_INVALID
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    16
from VirtualMailManager.errors import DomainError, EmailAddressError as EAErr
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    18
673
de046a3b29a5 VMM: Post-2to3 fix. Use the re.ASCII flag for ASCII-only patterns.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    19
RE_LOCALPART = re.compile(r"[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]", re.ASCII)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    20
_ = lambda msg: msg
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    22
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
class EmailAddress(object):
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    24
    """Simple class for validated e-mail addresses."""
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    25
    __slots__ = ('_localpart', '_domainname')
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    26
427
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    27
    def __init__(self, address, _validate=True):
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    28
        """Creates a new instance from the string/unicode ``address``."""
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    29
        assert isinstance(address, str)
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    30
        self._localpart = None
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    31
        self._domainname = None
427
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    32
        if _validate:
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    33
            self._chk_address(address)
116
949c5db6447a Fixed a logical mistake in EmailAddress.__ne__() (not used),
Pascal Volk <neverseen@users.sourceforge.net>
parents: 108
diff changeset
    34
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    35
    @property
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    36
    def localpart(self):
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    37
        """The local-part of the address *local-part@domain*"""
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    38
        return self._localpart
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    39
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    40
    @property
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    41
    def domainname(self):
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    42
        """The domain part of the address *local-part@domain*"""
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    43
        return self._domainname
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    44
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
    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
    46
        if isinstance(other, self.__class__):
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    47
            return self._localpart == other._localpart and \
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    48
                    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
    49
        return NotImplemented
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
    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
    52
        if isinstance(other, self.__class__):
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    53
            return self._localpart != other._localpart or \
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    54
                    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
    55
        return NotImplemented
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
218
84094c7fa28b VMM/EmailAddress: implemented EmailAddress.__hash__().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    57
    def __hash__(self):
84094c7fa28b VMM/EmailAddress: implemented EmailAddress.__hash__().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    58
        return hash((self._localpart.lower(), self._domainname.lower()))
84094c7fa28b VMM/EmailAddress: implemented EmailAddress.__hash__().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    59
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
    def __repr__(self):
195
05dd49fc3ea1 VMM/EmailAddress: reworked class EmailAddress again.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 194
diff changeset
    61
        return "EmailAddress('%s@%s')" % (self._localpart, self._domainname)
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
    def __str__(self):
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    64
        return '%s@%s' % (self._localpart, self._domainname)
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    66
    def _chk_address(self, address):
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    67
        """Checks if the string ``address`` could be used for an e-mail
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    68
        address.  If so, it will assign the corresponding values to the
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    69
        attributes `_localpart` and `_domainname`."""
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    70
        parts = address.split('@')
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    71
        p_len = len(parts)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    72
        if p_len < 2:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    73
            raise EAErr(_("Missing the '@' sign in address: '%s'") % address,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    74
                        INVALID_ADDRESS)
193
a259bdeaab5c VMM/EmailAddress: rework EmailAddress class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    75
        elif p_len > 2:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    76
            raise EAErr(_("Too many '@' signs in address: '%s'") % address,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    77
                        INVALID_ADDRESS)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    78
        if not parts[0]:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    79
            raise EAErr(_("Missing local-part in address: '%s'") % address,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    80
                        LOCALPART_INVALID)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    81
        if not parts[1]:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    82
            raise EAErr(_("Missing domain name in address: '%s'") % address,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    83
                        DOMAIN_NO_NAME)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    84
        self._localpart = check_localpart(parts[0])
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 195
diff changeset
    85
        self._domainname = check_domainname(parts[1])
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    86
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
    87
367
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    88
class DestinationEmailAddress(EmailAddress):
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    89
    """Provides additionally the domains group ID - when the domain is known
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    90
    in the database."""
427
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    91
    __slots__ = ('_gid', '_localhost')
367
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    92
427
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    93
    def __init__(self, address, dbh, _validate=False):
367
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    94
        """Creates a new DestinationEmailAddress instance
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    95
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    96
        Arguments:
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    97
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    98
        `address`: string/unicode
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    99
          a e-mail address like user@example.com
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
   100
        `dbh`: psycopg2._psycopg.connection
367
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   101
          a database connection for the database access
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   102
        """
427
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   103
        super(DestinationEmailAddress, self).__init__(address, _validate)
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   104
        self._localhost = False
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   105
        if not _validate:
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   106
            try:
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   107
                self._chk_address(address)
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   108
            except DomainError as err:
427
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   109
                if err.code is DOMAIN_INVALID and \
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   110
                   address.split('@')[1] == 'localhost':
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   111
                    self._localhost = True
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   112
                    self._domainname = 'localhost'
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   113
                else:
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   114
                    raise
367
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   115
        self._gid = 0
427
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   116
        if not self._localhost:
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   117
            self._find_domain(dbh)
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   118
        else:
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   119
            self._localpart = self._localpart.lower()
367
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   120
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   121
    def _find_domain(self, dbh):
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   122
        """Checks if the domain is known"""
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   123
        self._gid = get_gid(dbh, self._domainname)
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   124
        if self._gid:
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   125
            self._localpart = self._localpart.lower()
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   126
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   127
    @property
427
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   128
    def at_localhost(self):
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   129
        """True when the address is something@localhost."""
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   130
        return self._localhost
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   131
8e41e77b84e6 VMM/emailaddress: Class DestinationEmailAddress accepts also
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
   132
    @property
367
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   133
    def gid(self):
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   134
        """The domains group ID. 0 if the domain is not known."""
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   135
        return self._gid
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   136
a6ad9895989d VMM/emailaddress: Added class DestinationEmailAddress.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   137
253
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   138
def check_localpart(localpart):
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   139
    """Returns the validated local-part `localpart`.
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   140
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   141
    Throws a `EmailAddressError` if the local-part is too long or contains
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   142
    invalid characters.
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   143
    """
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   144
    if len(localpart) > 64:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   145
        raise EAErr(_("The local-part '%s' is too long.") % localpart,
253
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   146
                    LOCALPART_TOO_LONG)
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   147
    invalid_chars = set(RE_LOCALPART.findall(localpart))
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   148
    if invalid_chars:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   149
        i_chars = ''.join(('"%s" ' % c for c in invalid_chars))
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   150
        raise EAErr(_("The local-part '%(l_part)s' contains invalid "
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   151
                      "characters: %(i_chars)s") % {'l_part': localpart,
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
   152
                    'i_chars': i_chars}, LOCALPART_INVALID)
253
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   153
    return localpart
58d1b6f41664 VMM: moved check_localpart() to the EmailAddress module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 218
diff changeset
   154
194
6c06edb5b2d2 VMM/EmailAddress: reworked once more.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 193
diff changeset
   155
del _