summaryrefslogtreecommitdiff
path: root/src/mailman/model/docs/listmanager.rst
blob: 380fe7704c50fbe84c673d8b355fd6469fc5ad53 (plain) (blame)
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
========================
The mailing list manager
========================

The ``IListManager`` is how you create, delete, and retrieve mailing list
objects.

    >>> from mailman.interfaces.listmanager import IListManager
    >>> from zope.component import getUtility
    >>> list_manager = getUtility(IListManager)


Creating a mailing list
=======================

Creating the list returns the newly created IMailList object.

    >>> from mailman.interfaces.mailinglist import IMailingList
    >>> mlist = list_manager.create('test@example.com')
    >>> IMailingList.providedBy(mlist)
    True

All lists with identities have a short name, a host name, a fully qualified
listname, and an `RFC 2369`_ list id.  This latter will not change even if the
mailing list moves to a different host, so it is what uniquely distinguishes
the mailing list to the system.

    >>> print mlist.list_name
    test
    >>> print mlist.mail_host
    example.com
    >>> print mlist.fqdn_listname
    test@example.com
    >>> print mlist.list_id
    test.example.com

If you try to create a mailing list with the same name as an existing list,
you will get an exception.

    >>> list_manager.create('test@example.com')
    Traceback (most recent call last):
    ...
    ListAlreadyExistsError: test@example.com

It is an error to create a mailing list that isn't a fully qualified list name
(i.e. posting address).

    >>> list_manager.create('foo')
    Traceback (most recent call last):
    ...
    InvalidEmailAddressError: foo


Deleting a mailing list
=======================

Use the list manager to delete a mailing list.

    >>> list_manager.delete(mlist)
    >>> sorted(list_manager.names)
    []

After deleting the list, you can create it again.

    >>> mlist = list_manager.create('test@example.com')
    >>> print mlist.fqdn_listname
    test@example.com


Retrieving a mailing list
=========================

When a mailing list exists, you can ask the list manager for it and you will
always get the same object back.

    >>> mlist_2 = list_manager.get('test@example.com')
    >>> mlist_2 is mlist
    True

You can also get a mailing list by it's list id.

    >>> mlist_2 = list_manager.get_by_list_id('test.example.com')
    >>> mlist_2 is mlist
    True

If you try to get a list that doesn't existing yet, you get ``None``.

    >>> print list_manager.get('test_2@example.com')
    None
    >>> print list_manager.get_by_list_id('test_2.example.com')
    None

You also get ``None`` if the list name is invalid.

    >>> print list_manager.get('foo')
    None


Iterating over all mailing lists
================================

Once you've created a bunch of mailing lists, you can use the list manager to
iterate over the mailing list objects, the list posting addresses, or the list
address components.
::

    >>> mlist_3 = list_manager.create('test_3@example.com')
    >>> mlist_4 = list_manager.create('test_4@example.com')

    >>> for name in sorted(list_manager.names):
    ...     print name
    test@example.com
    test_3@example.com
    test_4@example.com

    >>> for list_id in sorted(list_manager.list_ids):
    ...     print list_id
    test.example.com
    test_3.example.com
    test_4.example.com

    >>> for fqdn_listname in sorted(m.fqdn_listname
    ...                             for m in list_manager.mailing_lists):
    ...     print fqdn_listname
    test@example.com
    test_3@example.com
    test_4@example.com

    >>> for list_name, mail_host in sorted(list_manager.name_components,
    ...                                    key=lambda (name, host): name):
    ...     print list_name, '@', mail_host
    test   @ example.com
    test_3 @ example.com
    test_4 @ example.com


.. _`RFC 2369`: http://www.faqs.org/rfcs/rfc2369.html