using.rst 37.7 KB
Newer Older
Abhilash Raj's avatar
Abhilash Raj committed
1 2 3
=============
Example Usage
=============
Barry Warsaw's avatar
Barry Warsaw committed
4 5 6 7 8 9

This is the official Python bindings for the GNU Mailman REST API.  In order
to talk to Mailman, the engine's REST server must be running.  You begin by
instantiating a client object to access the root of the REST hierarchy,
providing it the base URL, user name and password (for Basic Auth).

10
    >>> from mailmanclient import Client
11
    >>> client = Client('http://localhost:9001/3.1', 'restadmin', 'restpass')
Barry Warsaw's avatar
Barry Warsaw committed
12

13 14 15 16 17
.. note::
    Please note that port '9001' is used above, since mailman's test server
    runs on port *9001*. In production Mailman's REST API usually listens on
    port *8001*.

Barry Warsaw's avatar
Barry Warsaw committed
18 19 20
We can retrieve basic information about the server.

    >>> dump(client.system)
21
    api_version: 3.1
Barry Warsaw's avatar
Barry Warsaw committed
22
    http_etag: "..."
Abhilash Raj's avatar
Abhilash Raj committed
23
    mailman_version: GNU Mailman ... (...)
Barry Warsaw's avatar
Barry Warsaw committed
24
    python_version: ...
25
    self_link: http://localhost:9001/3.1/system/versions
Barry Warsaw's avatar
Barry Warsaw committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

To start with, there are no known mailing lists.

    >>> client.lists
    []


Domains
=======

Before new mailing lists can be added, the domain that the list will live in
must be added.  By default, there are no known domains.

    >>> client.domains
    []

Barry Warsaw's avatar
Barry Warsaw committed
42 43
It's easy to create a new domain; when you do, a proxy object for that domain
is returned.
Barry Warsaw's avatar
Barry Warsaw committed
44

45
    >>> example_dot_com = client.create_domain('example.com')
46
    >>> print(example_dot_com.description)
Barry Warsaw's avatar
Barry Warsaw committed
47
    None
48
    >>> print(example_dot_com.mail_host)
Barry Warsaw's avatar
Barry Warsaw committed
49
    example.com
50 51 52 53 54 55 56 57
    >>> print(example_dot_com.alias_domain)
    None

A domain can have an alias_domain attribute to help with some unusual Postfix
configurations.

    >>> example_dot_edu = client.create_domain('example.edu',
    ...                                        alias_domain='x.example.edu')
58 59
    >>> print(example_dot_edu.mail_host)
    example.edu
60 61
    >>> print(example_dot_edu.alias_domain)
    x.example.edu
Barry Warsaw's avatar
Barry Warsaw committed
62

63
You can also get an existing domain independently using its mail host.
Barry Warsaw's avatar
Barry Warsaw committed
64 65

    >>> example = client.get_domain('example.com')
66 67
    >>> print(example.mail_host)
    example.com
Barry Warsaw's avatar
Barry Warsaw committed
68

69 70
After creating a few more domains, we can print the list of all domains.

71
    >>> example_net = client.create_domain('example.net')
72
    >>> example_org = client.create_domain('example.org')
73 74 75 76 77 78 79 80
    >>> print(example_org.mail_host)
    example.org
    >>> for domain in client.domains:
    ...     print(domain.mail_host)
    example.com
    example.edu
    example.net
    example.org
Florian Fuchs's avatar
Florian Fuchs committed
81

82 83
Also, domain can be deleted.

84
    >>> example_org.delete()
85 86 87 88 89
    >>> for domain in client.domains:
    ...     print(domain.mail_host)
    example.com
    example.edu
    example.net
90 91 92 93 94 95 96


Mailing lists
=============

Once you have a domain, you can create mailing lists in that domain.

97
    >>> test_one = example.create_list('test-1')
98
    >>> print(test_one.fqdn_listname)
99
    test-1@example.com
100
    >>> print(test_one.mail_host)
101
    example.com
102
    >>> print(test_one.list_name)
103
    test-1
104
    >>> print(test_one.display_name)
105
    Test-1
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
You can create a mailing list with a specific list style.

    >>> test_two = example.create_list('test-announce', style_name='legacy-announce')
    >>> print(test_two.fqdn_listname)
    test-announce@example.com

You can retrieve a list of known mailing list styles along with the default
one.

    >>> styles = client.styles
    >>> from operator import itemgetter
    >>> for style in sorted(styles['styles'], key=itemgetter('name')):
    ...     print('{0}: {1}'.format(style['name'], style['description']))
    legacy-announce: Announce only mailing list style.
    legacy-default: Ordinary discussion mailing list style.
122
    private-default: Discussion mailing list style with private archives.
123 124 125
    >>> print(styles['default'])
    legacy-default

126 127
You can also retrieve the mailing list after the fact.

128
    >>> my_list = client.get_list('test-1@example.com')
129 130
    >>> print(my_list.fqdn_listname)
    test-1@example.com
131 132 133 134

And you can print all the known mailing lists.
::

135 136
    >>> print(example.create_list('test-2').fqdn_listname)
    test-2@example.com
137
    >>> domain = client.get_domain('example.net')
138 139 140 141
    >>> print(domain.create_list('test-3').fqdn_listname)
    test-3@example.net
    >>> print(example.create_list('test-3').fqdn_listname)
    test-3@example.com
Barry Warsaw's avatar
Barry Warsaw committed
142

143
    >>> for mlist in client.lists:
144 145 146 147 148
    ...     print(mlist.fqdn_listname)
    test-1@example.com
    test-2@example.com
    test-3@example.com
    test-3@example.net
149
    test-announce@example.com
Florian Fuchs's avatar
Florian Fuchs committed
150

151 152 153 154 155 156
You can also select advertised lists only.
::

    >>> my_list.settings['advertised'] = False
    >>> my_list.settings.save()
    >>> for mlist in client.get_lists(advertised=True):
157 158 159 160
    ...     print(mlist.fqdn_listname)
    test-2@example.com
    test-3@example.com
    test-3@example.net
161
    test-announce@example.com
162

Florian Fuchs's avatar
Florian Fuchs committed
163 164 165 166 167
List results can be retrieved as pages:

    >>> page = client.get_list_page(count=2, page=1)
    >>> page.nr
    1
Florian Fuchs's avatar
Florian Fuchs committed
168 169
    >>> len(page)
    2
170
    >>> page.total_size
171
    5
Florian Fuchs's avatar
Florian Fuchs committed
172
    >>> for m_list in page:
173 174 175
    ...     print(m_list.fqdn_listname)
    test-1@example.com
    test-2@example.com
Florian Fuchs's avatar
Florian Fuchs committed
176 177 178 179
    >>> page = page.next
    >>> page.nr
    2
    >>> for m_list in page:
180 181 182
    ...     print(m_list.fqdn_listname)
    test-3@example.com
    test-3@example.net
Florian Fuchs's avatar
Florian Fuchs committed
183

184 185 186 187
Pages can also use the advertised filter:

    >>> page = client.get_list_page(count=2, page=1, advertised=True)
    >>> for m_list in page:
188 189 190
    ...     print(m_list.fqdn_listname)
    test-2@example.com
    test-3@example.com
191

192 193 194 195 196 197 198 199 200
Pages can also limit the results by domain:

    >>> page = client.get_list_page(mail_host='example.net')
    >>> for m_list in page:
    ...     print(m_list.fqdn_listname)
    test-3@example.net

You can also use the domain object if you only want to know all lists for a
specific domain without pagination.
201 202

    >>> for mlist in example.lists:
203 204 205 206
    ...     print(mlist.fqdn_listname)
    test-1@example.com
    test-2@example.com
    test-3@example.com
207
    test-announce@example.com
Barry Warsaw's avatar
Barry Warsaw committed
208

209 210 211
It is also possible to display only advertised lists when using the domain.

    >>> for mlist in example.get_lists(advertised=True):
212 213 214
    ...     print(mlist.fqdn_listname)
    test-2@example.com
    test-3@example.com
215
    test-announce@example.com
216
    >>> for mlist in example.get_list_page(count=2, page=1, advertised=True):
217 218 219
    ...     print(mlist.fqdn_listname)
    test-2@example.com
    test-3@example.com
220

221 222
You can use a list instance to delete the list.

223
    >>> test_three = client.get_list('test-3@example.net')
224 225 226 227
    >>> test_three.delete()

You can also delete a list using the client instance's delete_list method.

228
    >>> client.delete_list('test-3@example.com')
229 230

    >>> for mlist in client.lists:
231 232 233
    ...     print(mlist.fqdn_listname)
    test-1@example.com
    test-2@example.com
234
    test-announce@example.com
Barry Warsaw's avatar
Barry Warsaw committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

Membership
==========

Email addresses can subscribe to existing mailing lists, becoming members of
that list.  The address is a unique id for a specific user in the system, and
a member is a user that is subscribed to a mailing list.  Email addresses need
not be pre-registered, though the auto-registered user will be unique for each
email address.

The system starts out with no members.

    >>> client.members
    []

New members can be easily added; users are automatically registered.
::

253
    >>> test_two = client.get_list('test-2@example.com')
254 255
    >>> print(test_two.settings['subscription_policy'])
    confirm
Barry Warsaw's avatar
Barry Warsaw committed
256

257
Email addresses need to be verified first, so if we try to subscribe a
258 259 260 261 262 263 264 265
user, we get a response with a token:

    >>> data = test_one.subscribe('unverified@example.com', 'Unverified')
    >>> data['token'] is not None
    True
    >>> print(data['token_owner'])
    subscriber

266 267 268 269
If we know the email address to be valid, we can set the
``pre_verified`` flag. However, the list's subscription policy is
"confirm", so if we try to subscribe a user, we will also get a token
back:
270 271 272 273 274 275 276 277 278

    >>> data = test_one.subscribe('unconfirmed@example.com',
    ...                           'Unconfirmed',
    ...                            pre_verified=True)
    >>> data['token'] is not None
    True
    >>> print(data['token_owner'])
    subscriber

279 280
If we know the user originated the subscription (for example if she or
he has been authenticated elsewhere), we can set the ``pre_confirmed``
281 282
flag.

283 284 285
The ``pre_approved`` flag is used for lists that require moderator
approval and should only be used if the subscription is initiated by a
moderator or admin.
286

287 288 289 290
    >>> print(test_one.subscribe('anna@example.com', 'Anna',
    ...                          pre_verified=True,
    ...                          pre_confirmed=True))
    Member "anna@example.com" on "test-1.example.com"
291

292 293 294 295
    >>> print(test_one.subscribe('bill@example.com', 'Bill',
    ...                          pre_verified=True,
    ...                          pre_confirmed=True))
    Member "bill@example.com" on "test-1.example.com"
296

297 298 299 300
    >>> print(test_two.subscribe('anna@example.com',
    ...                          pre_verified=True,
    ...                          pre_confirmed=True))
    Member "anna@example.com" on "test-2.example.com"
301

302 303 304 305
    >>> print(test_two.subscribe('cris@example.com', 'Cris',
    ...                          pre_verified=True,
    ...                          pre_confirmed=True))
    Member "cris@example.com" on "test-2.example.com"
Barry Warsaw's avatar
Barry Warsaw committed
306 307 308 309 310

We can retrieve all known memberships.  These are sorted first by mailing list
name, then by email address.

    >>> for member in client.members:
311
    ...     print(member)
312 313 314 315
    Member "anna@example.com" on "test-1.example.com"
    Member "bill@example.com" on "test-1.example.com"
    Member "anna@example.com" on "test-2.example.com"
    Member "cris@example.com" on "test-2.example.com"
Barry Warsaw's avatar
Barry Warsaw committed
316 317 318 319

We can also view the memberships for a single mailing list.

    >>> for member in test_one.members:
320
    ...     print(member)
321 322
    Member "anna@example.com" on "test-1.example.com"
    Member "bill@example.com" on "test-1.example.com"
323

324 325 326 327 328 329 330 331
Membership may have a name associated, this depends on whether the member ``Address``
or ``User`` has a ``display_name`` attribute.

    >>> for member in test_one.members:
    ...     print(member.display_name)
    Anna
    Bill

Florian Fuchs's avatar
Florian Fuchs committed
332 333 334 335 336
Membership lists can be paginated, to recieve only a part of the result.

    >>> page = client.get_member_page(count=2, page=1)
    >>> page.nr
    1
337 338
    >>> page.total_size
    4
Florian Fuchs's avatar
Florian Fuchs committed
339
    >>> for member in page:
340
    ...     print(member)
341 342
    Member "anna@example.com" on "test-1.example.com"
    Member "bill@example.com" on "test-1.example.com"
Florian Fuchs's avatar
Florian Fuchs committed
343 344 345 346 347

    >>> page = page.next
    >>> page.nr
    2
    >>> for member in page:
348
    ...     print(member)
349 350
    Member "anna@example.com" on "test-2.example.com"
    Member "cris@example.com" on "test-2.example.com"
Florian Fuchs's avatar
Florian Fuchs committed
351 352 353 354

    >>> page = test_one.get_member_page(count=1, page=1)
    >>> page.nr
    1
355 356
    >>> page.total_size
    2
Florian Fuchs's avatar
Florian Fuchs committed
357
    >>> for member in page:
358
    ...     print(member)
359
    Member "anna@example.com" on "test-1.example.com"
Florian Fuchs's avatar
Florian Fuchs committed
360 361 362
    >>> page = page.next
    >>> page.nr
    2
363 364
    >>> page.total_size
    2
Florian Fuchs's avatar
Florian Fuchs committed
365
    >>> for member in page:
366
    ...     print(member)
367
    Member "bill@example.com" on "test-1.example.com"
Florian Fuchs's avatar
Florian Fuchs committed
368

369 370
We can get a single membership too.

Florian Fuchs's avatar
Florian Fuchs committed
371
    >>> cris_test_two = test_two.get_member('cris@example.com')
372 373
    >>> print(cris_test_two)
    Member "cris@example.com" on "test-2.example.com"
374
    >>> print(cris_test_two.role)
375
    member
376 377
    >>> print(cris_test_two.display_name)
    Cris
Florian Fuchs's avatar
Florian Fuchs committed
378

379 380
A membership can also be retrieved without instantiating the list object first:

381 382
    >>> print(client.get_member('test-2@example.com', 'cris@example.com'))
    Member "cris@example.com" on "test-2.example.com"
383

Florian Fuchs's avatar
Florian Fuchs committed
384 385 386
A membership has preferences.

    >>> prefs = cris_test_two.preferences
387
    >>> print(prefs['delivery_mode'])
388
    None
389
    >>> print(prefs['acknowledge_posts'])
Florian Fuchs's avatar
Florian Fuchs committed
390
    None
391
    >>> print(prefs['delivery_status'])
Florian Fuchs's avatar
Florian Fuchs committed
392
    None
393
    >>> print(prefs['hide_address'])
Florian Fuchs's avatar
Florian Fuchs committed
394
    None
395
    >>> print(prefs['preferred_language'])
396
    None
397
    >>> print(prefs['receive_list_copy'])
Florian Fuchs's avatar
Florian Fuchs committed
398
    None
399
    >>> print(prefs['receive_own_postings'])
Florian Fuchs's avatar
Florian Fuchs committed
400
    None
Florian Fuchs's avatar
Florian Fuchs committed
401 402 403

The membership object's ``user`` attribute will return a User object:

404 405 406
    >>> cris_u = cris_test_two.user
    >>> print(cris_u.display_name, cris_u.user_id)
    Cris ...
407 408 409 410 411 412

If you use an address which is not a member of test_two `ValueError` is raised:

    >>> test_two.unsubscribe('nomember@example.com')
    Traceback (most recent call last):
    ...
Abhilash Raj's avatar
Abhilash Raj committed
413
    ValueError: nomember@example.com is not a member address of test-2@example.com
414 415 416 417

After a while, Anna decides to unsubscribe from the Test One mailing list,
though she keeps her Test Two membership active.

Barry Warsaw's avatar
Barry Warsaw committed
418
    >>> import time
419
    >>> time.sleep(2)
420 421
    >>> test_one.unsubscribe('anna@example.com')
    >>> for member in client.members:
422
    ...     print(member)
423 424 425
    Member "bill@example.com" on "test-1.example.com"
    Member "anna@example.com" on "test-2.example.com"
    Member "cris@example.com" on "test-2.example.com"
426 427 428

A little later, Cris decides to unsubscribe from the Test Two mailing list.

Florian Fuchs's avatar
Florian Fuchs committed
429
    >>> cris_test_two.unsubscribe()
430
    >>> for member in client.members:
431
    ...     print(member)
432 433
    Member "bill@example.com" on "test-1.example.com"
    Member "anna@example.com" on "test-2.example.com"
434

435 436
If you try to unsubscribe an address which is not a member address
`ValueError` is raised:
437 438 439 440

    >>> test_one.unsubscribe('nomember@example.com')
    Traceback (most recent call last):
    ...
Abhilash Raj's avatar
Abhilash Raj committed
441
    ValueError: nomember@example.com is not a member address of test-1@example.com
442

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
If we want to mass unsubscribe users.

    >>> print(test_one.subscribe('jack@example.com', 'Jack',
    ...                          pre_verified=True,
    ...                          pre_confirmed=True))
    Member "jack@example.com" on "test-1.example.com"

    >>> print(test_one.subscribe('jill@example.com', 'Jill',
    ...                          pre_verified=True,
    ...                          pre_confirmed=True))
    Member "jill@example.com" on "test-1.example.com"

    >>> print(test_one.subscribe('hans@example.com', 'Hans',
    ...                          pre_verified=True,
    ...                          pre_confirmed=True))
    Member "hans@example.com" on "test-1.example.com"

    >>> email_list = ['jack@example.com','hans@example.com','jill@example.com','bully@example.com']
    >>> ();test_one.mass_unsubscribe(email_list);() # doctest: +ELLIPSIS
    (...)
    >>> for member in test_one.members:
    ...     print(member)
    Member "bill@example.com" on "test-1.example.com"

467

468 469 470 471 472
Non-Members
===========

When someone attempts to post to a list but is not a member, then they are
listed as a "non-member" of that list so that a moderator can choose how to
Terri Oda's avatar
Terri Oda committed
473
handle their messages going forward.  In some cases, one might wish to
474 475 476 477 478 479 480 481 482 483
accept or reject their future messages automatically.  Just like with regular
members, they are given a unique id.

The list starts out with no nonmembers.

    >>> test_one.nonmembers
    []

When someone tries to send a message to the list and they are not a
subscriber, they get added to the nonmember list.
484

485

486 487 488
Users
=====

489 490
Users are people with one or more list memberships. To get a list of all users,
access the clients user property.
491 492

    >>> for user in client.users:
493 494 495 496 497 498
    ...     print(user.display_name)
    Unverified
    Unconfirmed
    Anna
    Bill
    Cris
499 500 501
    Jack
    Jill
    Hans
502

503
The list of users can also be paginated:
Florian Fuchs's avatar
Florian Fuchs committed
504

505
    >>> page = client.get_user_page(count=4, page=1)
Florian Fuchs's avatar
Florian Fuchs committed
506 507
    >>> page.nr
    1
508
    >>> page.total_size
509
    8
Florian Fuchs's avatar
Florian Fuchs committed
510
    >>> for user in page:
511 512 513 514 515
    ...     print(user.display_name)
    Unverified
    Unconfirmed
    Anna
    Bill
Florian Fuchs's avatar
Florian Fuchs committed
516 517 518 519 520 521 522 523

You can get the next or previous pages without calling ``get_userpage`` again.

    >>> page = page.next
    >>> page.nr
    2

    >>> for user in page:
524 525
    ...     print(user.display_name)
    Cris
526 527 528
    Jack
    Jill
    Hans
Florian Fuchs's avatar
Florian Fuchs committed
529 530 531 532 533 534

    >>> page = page.previous
    >>> page.nr
    1

    >>> for user in page:
535 536 537 538 539
    ...     print(user.display_name)
    Unverified
    Unconfirmed
    Anna
    Bill
Florian Fuchs's avatar
Florian Fuchs committed
540

541 542 543
A single user can be retrieved using their email address.

    >>> cris = client.get_user('cris@example.com')
544
    >>> print(cris.display_name)
545 546
    Cris

Florian Fuchs's avatar
Florian Fuchs committed
547
Every user has a list of one or more addresses.
548 549

    >>> for address in cris.addresses:
550 551 552
    ...     print(address)
    ...     print(address.display_name)
    ...     print(address.registered_on)
553
    cris@example.com
554 555
    Cris
    ...
556

557 558
Multiple addresses can be assigned to a user record:

559
    >>> print(cris.add_address('cris.person@example.org'))
560
    cris.person@example.org
561
    >>> print(client.get_address('cris.person@example.org'))
562 563 564
    cris.person@example.org

    >>> for address in cris.addresses:
565
    ...     print(address)
566 567 568
    cris.person@example.org
    cris@example.com

569 570
Trying to add an existing address will raise an error:

571 572 573 574 575
    >>> dana = client.create_user(email='dana@example.org',
    ...                           password='somepass',
    ...                           display_name='Dana')
    >>> print(dana.display_name)
    Dana
576 577 578 579 580 581 582
    >>> cris.add_address('dana@example.org')  # doctest: +IGNORE_EXCEPTION_DETAIL
    Traceback (most recent call last):
    ...
    HTTPError: HTTP Error 400: Address already exists

This can be overridden by using the ``absorb_existing`` flag:

583
    >>> print(cris.add_address('dana@example.org', absorb_existing=True))
584 585 586 587 588 589 590 591 592 593 594
    dana@example.org

The user Chris will then be merged with Dana, acquiring all its subscriptions
and preferences. In case of conflict, Chris' original preferences will prevail.

    >>> for address in cris.addresses:
    ...     print(address)
    cris.person@example.org
    cris@example.com
    dana@example.org

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
Users can have one preferred address, which they can use for subscriptions. By
default, a User has no preferred address.

    >>> print(cris.preferred_address)
    None

A User can have a preferred address, but before that, the address needs to be
verified::

    >>> address = client.get_address('cris.person@example.org')
    >>> address.verify()
    >>> print(address.verified)
    True
    >>> cris.preferred_address = 'cris.person@example.org'
    >>> print(cris.preferred_address)
    cris.person@example.org

A User can change their preferred address.

    >>> cris.preferred_address = 'cris@example.com'
    >>> print(cris.preferred_address)
    cris@example.com

A User can also unset their preferred address by setting it to ``None``.

    >>> cris.preferred_address = None
    >>> print(cris.preferred_address)
    None
623 624 625

Addresses
=========
626

627 628
Addresses can be accessed directly:

629
    >>> address = client.get_address('dana@example.org')
630
    >>> print(address)
631
    dana@example.org
632
    >>> print(address.display_name)
633
    Dana
634 635 636

The address has not been verified:

637 638
    >>> print(address.verified)
    False
639 640 641 642

But that can be done via the address object:

    >>> address.verify()
643 644
    >>> print(address.verified)
    True
645 646 647 648

It can also be unverified:

    >>> address.unverify()
649 650 651 652 653 654 655 656 657 658 659
    >>> print(address.verified)
    False

Addresses can be deleted by calling their ``delete()`` method or by removing
them from their user's ``addresses`` list:

    >>> cris.addresses.remove('dana@example.org')
    >>> for address in cris.addresses:
    ...     print(address)
    cris.person@example.org
    cris@example.com
660 661


662
Users can be added using ``create_user``. The display_name is optional:
663 664 665 666 667
    >>> ler = client.create_user(email='ler@primus.org',
    ...                          password='somepass',
    ...                          display_name='Ler')
    >>> print(ler.display_name)
    Ler
668
    >>> ler = client.get_user('ler@primus.org')
669
    >>> print(ler.password)
670
    $...
671
    >>> print(ler.display_name)
672 673 674 675 676 677 678 679
    Ler

User attributes can be changed through assignment, but you need to call the
object's ``save`` method to store the changes in the mailman core database.

    >>> ler.display_name = 'Sir Ler'
    >>> ler.save()
    >>> ler = client.get_user('ler@primus.org')
680
    >>> print(ler.display_name)
681 682 683 684 685 686 687 688 689 690 691 692 693
    Sir Ler

Passwords can be changed as well:

    >>> old_pwd = ler.password
    >>> ler.password = 'easy'
    >>> old_pwd == ler.password
    True
    >>> ler.save()
    >>> old_pwd == ler.password
    False


Florian Fuchs's avatar
Florian Fuchs committed
694 695 696
User Subscriptions
------------------

Florian Fuchs's avatar
Florian Fuchs committed
697
A User's subscriptions can be access through their ``subscriptions`` property.
Florian Fuchs's avatar
Florian Fuchs committed
698 699 700

    >>> bill = client.get_user('bill@example.com')
    >>> for subscription in bill.subscriptions:
701
    ...     print(subscription)
702
    Member "bill@example.com" on "test-1.example.com"
Florian Fuchs's avatar
Florian Fuchs committed
703

704 705
If all you need are the list ids of all mailing lists a user is subscribed to,
you can use the ``subscription_list_ids`` property.
Florian Fuchs's avatar
Florian Fuchs committed
706 707

    >>> for list_id in bill.subscription_list_ids:
708
    ...     print(list_id)
709
    test-1.example.com
Florian Fuchs's avatar
Florian Fuchs committed
710 711


712 713 714
List Settings
=============

715 716
We can get all list settings via a lists settings attribute. A proxy object
for the settings is returned which behaves much like a dictionary.
717 718 719

    >>> settings = test_one.settings
    >>> for attr in sorted(settings):
720
    ...     print(attr + ': ' + str(settings[attr]))
721 722
    acceptable_aliases: []
    ...
723
    volume: 1
724

725
    >>> print(settings['display_name'])
726
    Test-1
727 728 729

We can access all valid list settings as attributes.

730
    >>> print(settings['fqdn_listname'])
731
    test-1@example.com
732
    >>> print(settings['description'])
Abhilash Raj's avatar
Abhilash Raj committed
733
    <BLANKLINE>
734
    >>> settings['description'] = 'A very meaningful description.'
Florian Fuchs's avatar
Florian Fuchs committed
735
    >>> settings['display_name'] = 'Test Numero Uno'
736 737 738 739

    >>> settings.save()

    >>> settings_new = test_one.settings
740
    >>> print(settings_new['description'])
741
    A very meaningful description.
742
    >>> print(settings_new['display_name'])
743
    Test Numero Uno
744

745 746
The settings object also supports the `get` method of usual Python
dictionaries:
747

748 749
    >>> print(settings_new.get('OhNoIForgotTheKey',
    ...                        'HowGoodIPlacedOneUnderTheDoormat'))
750 751
    HowGoodIPlacedOneUnderTheDoormat

752

753 754 755 756 757
Preferences
===========

Preferences can be accessed and set for users, members and addresses.

758 759
By default, preferences are not set and fall back to the global system
preferences. They're read-only and can be accessed through the client object.
760 761

    >>> global_prefs = client.preferences
762
    >>> print(global_prefs['acknowledge_posts'])
763
    False
764
    >>> print(global_prefs['delivery_mode'])
765
    regular
766
    >>> print(global_prefs['delivery_status'])
767
    enabled
768
    >>> print(global_prefs['hide_address'])
769
    True
770
    >>> print(global_prefs['preferred_language'])
771
    en
772
    >>> print(global_prefs['receive_list_copy'])
773
    True
774
    >>> print(global_prefs['receive_own_postings'])
775 776
    True

777 778
Preferences can be set, but you have to call ``save`` to make your changes
permanent.
Florian Fuchs's avatar
Florian Fuchs committed
779

780 781 782 783
    >>> prefs = test_two.get_member('anna@example.com').preferences
    >>> prefs['delivery_status'] = 'by_user'
    >>> prefs.save()
    >>> prefs = test_two.get_member('anna@example.com').preferences
784
    >>> print(prefs['delivery_status'])
785 786
    by_user

787

Simon Hanna's avatar
Simon Hanna committed
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
Pipelines and Chains
====================

The available pipelines and chains can also be retrieved:

    >>> pipelines = client.pipelines['pipelines']
    >>> for pipeline in pipelines:
    ...     print(pipeline)
    default-owner-pipeline
    default-posting-pipeline
    virgin
    >>> chains = client.chains['chains']
    >>> for chain in chains:
    ...     print(chain)
    accept
    default-owner-chain
    default-posting-chain
    discard
806
    dmarc
Simon Hanna's avatar
Simon Hanna committed
807 808 809 810 811 812
    header-match
    hold
    moderation
    reject


813 814 815 816 817 818 819 820 821 822
Owners and Moderators
=====================

Owners and moderators are properties of the list object.

    >>> test_one.owners
    []
    >>> test_one.moderators
    []

823 824
Owners can be added via the ``add_owner`` method and they can have an optional
``display_name`` associated like other ``members``:
825

826
    >>> test_one.add_owner('foo@example.com', display_name='Foo')
827
    >>> for owner in test_one.owners:
828
    ...     print(owner.email)
829 830 831 832
    foo@example.com

The owner of the list not automatically added as a member:

833 834 835
    >>> for m in test_one.members:
    ...     print(m)
    Member "bill@example.com" on "test-1.example.com"
836 837 838

Moderators can be added similarly:

839
    >>> test_one.add_moderator('bar@example.com', display_name='Bar')
840
    >>> for moderator in test_one.moderators:
841
    ...     print(moderator.email)
842 843 844 845
    bar@example.com

Moderators are also not automatically added as members:

846 847 848
    >>> for m in test_one.members:
    ...     print(m)
    Member "bill@example.com" on "test-1.example.com"
849 850 851 852

Members and owners/moderators are separate entries in in the general members
list:

853 854 855 856
    >>> print(test_one.subscribe('bar@example.com', 'Bar',
    ...                          pre_verified=True,
    ...                          pre_confirmed=True))
    Member "bar@example.com" on "test-1.example.com"
857

858 859
    >>> test_four_net = example_net.create_list('test-4')
    >>> test_four_net.add_owner('foo@example.com', display_name='Foo')
860
    >>> for member in client.members:
861 862 863 864 865 866
    ...     print('%s: %s' % (member, member.role))
    Member "foo@example.com" on "test-1.example.com": owner
    Member "bar@example.com" on "test-1.example.com": moderator
    Member "bar@example.com" on "test-1.example.com": member
    Member "bill@example.com" on "test-1.example.com": member
    Member "anna@example.com" on "test-2.example.com": member
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
    Member "foo@example.com" on "test-4.example.net": owner

You can find the lists that a user is a member, moderator, or owner of:

    >>> lists = client.find_lists('bill@example.com', 'member')
    >>> for m_list in lists:
    ...     print(m_list.fqdn_listname)
    test-1@example.com
    >>> lists = client.find_lists('bar@example.com', 'moderator')
    >>> for m_list in lists:
    ...     print(m_list.fqdn_listname)
    test-1@example.com
    >>> lists = client.find_lists('foo@example.com', 'owner')
    >>> for m_list in lists:
    ...     print(m_list.fqdn_listname)
    test-1@example.com
    test-4@example.net

You can also filter those results by domain:

    >>> lists = client.find_lists('foo@example.com', 'owner',
    ...                           mail_host='example.net')
    >>> for m_list in lists:
    ...     print(m_list.fqdn_listname)
    test-4@example.net
892 893 894 895 896 897 898 899 900 901 902 903

Both owners and moderators can be removed:

    >>> test_one.remove_owner('foo@example.com')
    >>> test_one.owners
    []

    test_one.remove_moderator('bar@example.com')
    test_one.moderators
    []


904 905 906
Moderation
==========

907 908 909 910

Subscription Moderation
-----------------------

911 912
Subscription requests can be accessed through the list object's
`request` property. So let's create a non-open list first.
913 914 915 916 917

    >>> confirm_first = example_dot_com.create_list('confirm-first')
    >>> settings = confirm_first.settings
    >>> settings['subscription_policy'] = 'moderate'
    >>> settings.save()
918

919 920 921
    >>> confirm_first = client.get_list('confirm-first.example.com')
    >>> print(confirm_first.settings['subscription_policy'])
    moderate
922

923 924
Initially there are no requests, so let's to subscribe someone to the
list. We'll get a token back.
925

926
    >>> confirm_first.requests
927
    []
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
    >>> data = confirm_first.subscribe('groucho@example.com',
    ...                                pre_verified=True,
    ...                                pre_confirmed=True)
    >>> print(data['token_owner'])
    moderator

Now the request shows up in the list of requests:

    >>> import time; time.sleep(5)
    >>> len(confirm_first.requests)
    1

    >>> request_1 = confirm_first.requests[0]
    >>> print(request_1['email'])
    groucho@example.com
    >>> print (request_1['token'] is not None)
    True
    >>> print(request_1['token_owner'])
    moderator
    >>> print(request_1['request_date'] is not None)
    True
    >>> print(request_1['list_id'])
    confirm-first.example.com

Subscription requests can be accepted, deferred, rejected or
discarded using the request token.
954

955 956 957 958 959 960 961 962 963 964 965 966
    >>> data = confirm_first.subscribe('harpo@example.com',
    ...                                pre_verified=True,
    ...                                pre_confirmed=True)
    >>> data = confirm_first.subscribe('zeppo@example.com',
    ...                                pre_verified=True,
    ...                                pre_confirmed=True)

    >>> len(confirm_first.requests)
    3

Let's accept Groucho:

967
    >>> response = confirm_first.moderate_request(request_1['token'], 'accept')
968 969 970 971 972 973 974 975 976 977 978 979 980
    >>> len(confirm_first.requests)
    2

    >>> request_2 = confirm_first.requests[0]
    >>> print(request_2['email'])
    harpo@example.com

    >>> request_3 = confirm_first.requests[1]
    >>> print(request_3['email'])
    zeppo@example.com

Let's reject Harpo:

981
    >>> response = confirm_first.moderate_request(request_2['token'], 'reject')
982 983 984 985 986
    >>> len(confirm_first.requests)
    1

Let's discard Zeppo's request:

987
    >>> response = confirm_first.moderate_request(request_3['token'], 'discard')
988 989
    >>> len(confirm_first.requests)
    0
990 991


Florian Fuchs's avatar
Florian Fuchs committed
992 993 994
Message Moderation
------------------

Barry Warsaw's avatar
Barry Warsaw committed
995 996 997
By injecting a message by a non-member into the incoming queue, we can
simulate a message being held for moderator approval.

Florian Fuchs's avatar
Florian Fuchs committed
998
    >>> msg = """From: nomember@example.com
999
    ... To: test-1@example.com
Florian Fuchs's avatar
Florian Fuchs committed
1000 1001
    ... Subject: Something
    ... Message-ID: <moderated_01>
1002
    ...
Florian Fuchs's avatar
Florian Fuchs committed
1003
    ... Some text.
1004
    ...
Florian Fuchs's avatar
Florian Fuchs committed
1005
    ... """
Barry Warsaw's avatar
Barry Warsaw committed
1006
    >>> inq = client.queues['in']
1007
    >>> inq.inject('test-1.example.com', msg)
Barry Warsaw's avatar
Barry Warsaw committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

Now wait until the message has been processed.

    >>> while True:
    ...     if len(inq.files) == 0:
    ...         break
    ...     time.sleep(0.1)

It might take a few moments for the message to show up in the moderation
queue.

    >>> while True:
1020 1021
    ...     all_held = test_one.held
    ...     if len(all_held) > 0:
Barry Warsaw's avatar
Barry Warsaw committed
1022 1023
    ...         break
    ...     time.sleep(0.1)
Florian Fuchs's avatar
Florian Fuchs committed
1024

1025 1026
Messages held for moderation can be listed on a per list basis.

1027
    >>> print(all_held[0].request_id)
1028 1029 1030 1031 1032 1033
    1

A held message can be retrieved by ID, and have attributes:

    >>> heldmsg = test_one.get_held_message(1)
    >>> print(heldmsg.subject)
Florian Fuchs's avatar
Florian Fuchs committed
1034
    Something
1035
    >>> print(heldmsg.reason)
1036
    The message is not from a list member
1037 1038 1039 1040 1041 1042 1043
    >>> print(heldmsg.sender)
    nomember@example.com
    >>> 'Message-ID: <moderated_01>' in heldmsg.msg
    True

A moderation action can be taken on them using the list methods or the held
message's methods.
Florian Fuchs's avatar
Florian Fuchs committed
1044

1045
    >>> print(test_one.defer_message(heldmsg.request_id).status_code)
Florian Fuchs's avatar
Florian Fuchs committed
1046 1047
    204

Florian Fuchs's avatar
Florian Fuchs committed
1048 1049 1050
    >>> len(test_one.held)
    1

1051
    >>> print(heldmsg.discard().status_code)
Florian Fuchs's avatar
Florian Fuchs committed
1052 1053 1054 1055
    204

    >>> len(test_one.held)
    0
1056

1057 1058 1059 1060 1061 1062 1063 1064
Member moderation
-----------------

Each member or non-member can have a specific moderation action. It is set
using the 'moderation_action' property:

    >>> bill_member = test_one.get_member('bill@example.com')
    >>> print(bill_member.moderation_action)
1065
    None
1066 1067 1068 1069 1070
    >>> bill_member.moderation_action = 'hold'
    >>> bill_member.save()
    >>> print(test_one.get_member('bill@example.com').moderation_action)
    hold

Aurélien Bompard's avatar
Aurélien Bompard committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
Banning addresses
-----------------

A ban list is a list of email addresses that are not allowed to subscribe to a
mailing-list. There are two types of ban lists: each mailing-list has its ban
list, and there is a site-wide list. Addresses on the site-wide list are
prevented from subscribing to every mailing-list on the server.

To view the site-wide ban list, use the `bans` property::

    >>> list(client.bans)
    []

You can use the `add` method on the ban list to ban an email address::

    >>> banned_anna = client.bans.add('anna@example.com')
    >>> print(banned_anna)
    anna@example.com
    >>> 'anna@example.com' in client.bans
    True
1091 1092 1093 1094 1095
    >>> print(client.bans.add('bill@example.com'))
    bill@example.com
    >>> for addr in list(client.bans):
    ...     print(addr)
    anna@example.com
1096 1097 1098 1099 1100
    bill@example.com

The list of banned addresses can be paginated using the ``get_bans_page()``
method::

1101 1102 1103 1104 1105 1106
    >>> for addr in list(client.get_bans_page(count=1, page=1)):
    ...     print(addr)
    anna@example.com
    >>> for addr in list(client.get_bans_page(count=1, page=2)):
    ...     print(addr)
    bill@example.com
Aurélien Bompard's avatar
Aurélien Bompard committed
1107

1108 1109
You can use the ``delete()`` method on a banned address to unban it, or the
``remove()`` method on the ban list::
Aurélien Bompard's avatar
Aurélien Bompard committed
1110 1111 1112 1113

    >>> banned_anna.delete()
    >>> 'anna@example.com' in client.bans
    False
1114 1115 1116
    >>> for addr in list(client.bans):
    ...     print(addr)
    bill@example.com
1117 1118 1119 1120
    >>> client.bans.remove('bill@example.com')
    >>> 'bill@example.com' in client.bans
    False
    >>> print(list(client.bans))
Aurélien Bompard's avatar
Aurélien Bompard committed
1121 1122
    []

1123

Aurélien Bompard's avatar
Aurélien Bompard committed
1124 1125
The mailing-list-specific ban lists work in the same way::

1126
    >>> print(list(test_one.bans))
Aurélien Bompard's avatar
Aurélien Bompard committed
1127 1128 1129 1130
    []
    >>> banned_anna = test_one.bans.add('anna@example.com')
    >>> 'anna@example.com' in test_one.bans
    True
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    >>> print(test_one.bans.add('bill@example.com'))
    bill@example.com
    >>> for addr in list(test_one.bans):
    ...     print(addr)
    anna@example.com
    bill@example.com
    >>> for addr in list(test_one.get_bans_page(count=1, page=1)):
    ...     print(addr)
    anna@example.com
    >>> for addr in list(test_one.get_bans_page(count=1, page=2)):
    ...     print(addr)
1142
    bill@example.com
Aurélien Bompard's avatar
Aurélien Bompard committed
1143 1144 1145
    >>> banned_anna.delete()
    >>> 'anna@example.com' in test_one.bans
    False
1146 1147
    >>> test_one.bans.remove('bill@example.com')
    >>> print(list(test_one.bans))
Aurélien Bompard's avatar
Aurélien Bompard committed
1148 1149
    []

1150 1151 1152 1153 1154

Archivers
=========


1155 1156 1157 1158
Each list object has an ``archivers`` attribute.

    >>> archivers = test_one.archivers
    >>> print(archivers)
1159
    Archivers on test-1.example.com
1160

1161
The activation status of each available archiver can be accessed like a
1162
key in a dictionary.
1163

1164
    >>> archivers = test_one.archivers
1165 1166
    >>> for archiver in sorted(archivers.keys()):
    ...     print('{0}: {1}'.format(archiver, archivers[archiver]))
1167 1168
    mail-archive: True
    mhonarc: True
1169
    prototype: True
1170

1171 1172 1173 1174
    >>> archivers['mail-archive']
    True
    >>> archivers['mhonarc']
    True
1175

1176 1177 1178 1179 1180
They can also be set like items in dictionary.

    >>> archivers['mail-archive'] = False
    >>> archivers['mhonarc'] = False

1181 1182
So if we get a new ``archivers`` object from the API (by accessing the
list's archiver attribute again), we can see that the archiver stati
1183 1184 1185 1186 1187 1188
have now been set.

    >>> archivers = test_one.archivers
    >>> archivers['mail-archive']
    False
    >>> archivers['mhonarc']
1189
    False
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201


Header matches
==============

Header matches are filtering rules that apply to messages sent to a mailing
list. They match a header to a pattern using a regular expression, and matching
patterns can trigger specific moderation actions. They are accessible via the
mailing list's ``header_matches`` attribute, which behaves like a list.

    >>> header_matches = test_one.header_matches
    >>> print(header_matches)
1202
    Header matches for "test-1.example.com"
1203 1204 1205 1206 1207 1208 1209 1210 1211
    >>> len(header_matches)
    0

Header matches can be added using the ``add()`` method. The arguments are:

- the header to consider (``str``). Il will be lower-cased.
- the regular expression to use for filtering (``str``)
- the action to take when the header matches the pattern. This can be
  ``'accept'``, ``'discard'``, ``'reject'``, or ``'hold'``.
1212
- the tag (``str``) to group a set of header matches.
1213

1214
    >>> print(header_matches.add('Subject', '^test: ', 'discard', 'sometag'))
1215
    Header match on "subject"
1216
    >>> print(header_matches)
1217
    Header matches for "test-1.example.com"
1218 1219
    >>> len(header_matches)
    1
1220 1221 1222
    >>> for hm in list(header_matches):
    ...     print(hm)
    Header match on "subject"
1223

1224 1225 1226 1227 1228 1229
Header matches can be filtered using ``.find()`` method to query a set
of HeaderMatches::

  >>> header_matches.find(tag='sometag')
  [<HeaderMatch on 'subject'>]

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
You can delete a header match by deleting it from the ``header_matches``
collection.

    >>> del header_matches[0]
    >>> len(header_matches)
    0

You can also delete a header match using its ``delete()`` method, but be aware
that the collection will not automatically be updated. Get a new collection
from the list's ``header_matches`` attribute to see the change.

1241 1242
    >>> print(header_matches.add('Subject', '^test: ', 'discard'))
    Header match on "subject"
1243 1244 1245 1246 1247
    >>> header_matches[0].delete()
    >>> len(header_matches) # not automatically updated
    1
    >>> len(test_one.header_matches)
    0
1248 1249


1250 1251 1252 1253 1254 1255 1256 1257
Configuration
=============

Mailman Core exposes all its configuration through REST API. All these
configuration options are read-only.

    >>> cfg = client.configuration
    >>> for key in sorted(cfg):
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
    ...     print(cfg[key].name)
    antispam
    archiver.mail_archive
    archiver.master
    archiver.mhonarc
    archiver.prototype
    bounces
    database
    devmode
    digests
    dmarc
    language.ar
    language.ast
    language.ca
    language.cs
    language.da
    language.de
    language.el
    language.en
    language.es
    language.et
    language.eu
    language.fi
    language.fr
    language.gl
    language.he
    language.hr
    language.hu
    language.ia
    language.it
    language.ja
    language.ko
    language.lt
    language.nl
    language.no
    language.pl
    language.pt
    language.pt_BR
    language.ro
    language.ru
    language.sk
    language.sl
    language.sr
    language.sv
    language.tr
    language.uk
    language.vi
    language.zh_CN
    language.zh_TW
    logging.archiver
    logging.bounce
    logging.config
    logging.database
    logging.debug
    logging.error
    logging.fromusenet
    logging.http
    logging.locks
    logging.mischief
    logging.plugins
    logging.root
    logging.runner
    logging.smtp
    logging.subscribe
    logging.vette
    mailman
    mta
    nntp
    passwords
    paths.dev
    paths.fhs
    paths.here
    paths.local
    plugin.master
    runner.archive
    runner.bad
    runner.bounces
    runner.command
    runner.digest
    runner.in
    runner.lmtp
    runner.nntp
    runner.out
    runner.pipeline
    runner.rest
    runner.retry
    runner.shunt
    runner.virgin
    shell
    styles
    webservice
1349 1350


1351 1352
Each configuration object is a dictionary and you can iterate over them
::
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364

     >>> for key in sorted(cfg['mailman']):
     ...     print('{} : {}'.format(key, cfg['mailman'][key]))
     cache_life : 7d
     default_language : en
     email_commands_max_lines : 10
     filtered_messages_are_preservable : no
     html_to_plain_text_command : /usr/bin/lynx -dump $filename
     layout : here
     listname_chars : [-_.0-9a-z]
     noreply_address : noreply
     pending_request_life : 3d
1365 1366
     post_hook :
     pre_hook :
Abhilash Raj's avatar
Abhilash Raj committed
1367
     self_link : http://localhost:9001/3.1/system/configuration/mailman
1368 1369 1370
     sender_headers : from from_ reply-to sender
     site_owner : changeme@example.com

Abhilash Raj's avatar
Abhilash Raj committed
1371 1372 1373 1374
..   >>> for domain in client.domains:
     ...    domain.delete()
     >>> for user in client.users:
     ...    user.delete()