~barry/mailman/templatecache

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Copyright (C) 2007-2012 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Interface for email address related information."""

from __future__ import absolute_import, print_function, unicode_literals

__metaclass__ = type
__all__ = [
    'AddressAlreadyLinkedError',
    'AddressError',
    'AddressNotLinkedError',
    'AddressVerificationEvent',
    'EmailError',
    'ExistingAddressError',
    'IAddress',
    'IEmailValidator',
    'InvalidEmailAddressError',
    ]


from zope.interface import Interface, Attribute

from mailman.interfaces.errors import MailmanError



class EmailError(MailmanError):
    """A generic text email address-related error occurred."""

    def __init__(self, email):
        super(EmailError, self).__init__()
        self.email = email

    def __str__(self):
        return self.email


class AddressError(MailmanError):
    """A generic IAddress-related error occurred."""

    def __init__(self, address):
        super(AddressError, self).__init__()
        self.address = address

    def __str__(self):
        return str(self.address)


class ExistingAddressError(AddressError):
    """The given email address already exists."""


class AddressAlreadyLinkedError(AddressError):
    """The address is already linked to a user."""


class AddressNotLinkedError(AddressError):
    """The address is not linked to the user."""


class InvalidEmailAddressError(EmailError):
    """Email address is invalid."""



class AddressVerificationEvent:
    """Triggered when an address gets verified or unverified."""

    def __init__(self, address):
        self.address = address

    def __str__(self):
        return '<{0} {1} {2}>'.format(
            self.__class__.__name__,
            self.address.email,
            ('unverified' if self.address.verified_on is None
             else self.address.verified_on))



class IAddress(Interface):
    """Email address related information."""

    email = Attribute(
        """Read-only text email address.""")

    original_email = Attribute(
        """Read-only original case-preserved email address.

        For almost all intents and purposes, email addresses in Mailman are
        case insensitive, however because RFC 2821 allows for case sensitive
        local parts, Mailman preserves the case of the original email address
        when delivering a message to the user.

        `original_email` will be the same as `email` if the original email
        address was all lower case.  Otherwise `original_email` will be the
        case preserved email address; `email` will always be lower case.
        """)

    display_name = Attribute(
        """Optional display name associated with the email address.""")

    registered_on = Attribute(
        """The date and time at which this email address was registered.

        Registeration is really the date at which this address became known to
        us.  It may have been explicitly registered by a user, or it may have
        been implicitly registered, e.g. by showing up in a nonmember
        posting.""")

    verified_on = Attribute(
        """The date and time at which this email address was validated, or
        None if the email address has not yet been validated.  The specific
        method of validation is not defined here.""")

    preferences = Attribute(
        """This address's preferences.""")



class IEmailValidator(Interface):
    """An email validator."""

    def is_valid(email):
        """Check if an email address if valid.

        :param email: A text email address.
        :type email: str
        :return: A flag indicating whether the email address is okay or not.
        :rtype: bool
        """

    def validate(email):
        """Validate an email address.

        :param email: A text email address.
        :type email: str
        :raise InvalidEmailAddressError: when `email` is deemed invalid.
        """