/usr/share/doc/python-mailmanclient/html/_sources/src/mailmanclient/docs/using.rst.txt is in python-mailmanclient-doc 3.1.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 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 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 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 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 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 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 | =============
Example Usage
=============
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).
>>> from mailmanclient import Client
>>> client = Client('http://localhost:9001/3.1', 'restadmin', 'restpass')
.. 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*.
We can retrieve basic information about the server.
>>> dump(client.system)
api_version: 3.1
http_etag: "..."
mailman_version: GNU Mailman ... (...)
python_version: ...
self_link: http://localhost:9001/3.1/system/versions
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
[]
It's easy to create a new domain; when you do, a proxy object for that domain
is returned.
>>> example_dot_com = client.create_domain('example.com')
>>> example_dot_com
<Domain "example.com">
>>> print(example_dot_com.description)
None
>>> print(example_dot_com.mail_host)
example.com
You can also get an existing domain independently using its mail host.
>>> example = client.get_domain('example.com')
>>> example
<Domain "example.com">
After creating a few more domains, we can print the list of all domains.
>>> client.create_domain('example.net')
<Domain "example.net">
>>> example_org = client.create_domain('example.org')
>>> print(example_org)
<Domain "example.org">
>>> for mail_host in client.domains:
... print(mail_host)
<Domain "example.com">
<Domain "example.net">
<Domain "example.org">
Also, domain can be deleted.
>>> example_org.delete()
>>> for mail_host in client.domains:
... print(mail_host)
<Domain "example.com">
<Domain "example.net">
Mailing lists
=============
Once you have a domain, you can create mailing lists in that domain.
>>> test_one = example.create_list('test-1')
>>> test_one
<List "test-1@example.com">
>>> print(test_one.fqdn_listname)
test-1@example.com
>>> print(test_one.mail_host)
example.com
>>> print(test_one.list_name)
test-1
>>> print(test_one.display_name)
Test-1
You can also retrieve the mailing list after the fact.
>>> my_list = client.get_list('test-1@example.com')
>>> my_list
<List "test-1@example.com">
And you can print all the known mailing lists.
::
>>> example.create_list('test-2')
<List "test-2@example.com">
>>> domain = client.get_domain('example.net')
>>> domain.create_list('test-3')
<List "test-3@example.net">
>>> example.create_list('test-3')
<List "test-3@example.com">
>>> for mlist in client.lists:
... print(mlist)
<List "test-1@example.com">
<List "test-2@example.com">
<List "test-3@example.com">
<List "test-3@example.net">
You can also select advertised lists only.
::
>>> my_list.settings['advertised'] = False
>>> my_list.settings.save()
>>> for mlist in client.get_lists(advertised=True):
... print(mlist)
<List "test-2@example.com">
<List "test-3@example.com">
<List "test-3@example.net">
List results can be retrieved as pages:
>>> page = client.get_list_page(count=2, page=1)
>>> page.nr
1
>>> len(page)
2
>>> page.total_size
4
>>> for m_list in page:
... print(m_list)
<List "test-1@example.com">
<List "test-2@example.com">
>>> page = page.next
>>> page.nr
2
>>> for m_list in page:
... print(m_list)
<List "test-3@example.com">
<List "test-3@example.net">
Pages can also use the advertised filter:
>>> page = client.get_list_page(count=2, page=1, advertised=True)
>>> for m_list in page:
... print(m_list)
<List "test-2@example.com">
<List "test-3@example.com">
If you only want to know all lists for a specific domain, use the domain
object.
>>> for mlist in example.lists:
... print(mlist)
<List "test-1@example.com">
<List "test-2@example.com">
<List "test-3@example.com">
It is also possible to display only advertised lists when using the domain.
>>> for mlist in example.get_lists(advertised=True):
... print(mlist)
<List "test-2@example.com">
<List "test-3@example.com">
>>> for mlist in example.get_list_page(count=2, page=1, advertised=True):
... print(mlist)
<List "test-2@example.com">
<List "test-3@example.com">
You can use a list instance to delete the list.
>>> test_three = client.get_list('test-3@example.net')
>>> test_three.delete()
You can also delete a list using the client instance's delete_list method.
>>> client.delete_list('test-3@example.com')
>>> for mlist in client.lists:
... print(mlist)
<List "test-1@example.com">
<List "test-2@example.com">
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.
::
>>> test_two = client.get_list('test-2@example.com')
>>> print(test_two.settings['subscription_policy'])
confirm
Email addresses need to be verified first, so if we try to subscribe a
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
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:
>>> data = test_one.subscribe('unconfirmed@example.com',
... 'Unconfirmed',
... pre_verified=True)
>>> data['token'] is not None
True
>>> print(data['token_owner'])
subscriber
If we know the user originated the subscription (for example if she or
he has been authenticated elsewhere), we can set the ``pre_confirmed``
flag.
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.
>>> test_one.subscribe('anna@example.com', 'Anna',
... pre_verified=True,
... pre_confirmed=True)
<Member "anna@example.com" on "test-1.example.com">
>>> test_one.subscribe('bill@example.com', 'Bill',
... pre_verified=True,
... pre_confirmed=True)
<Member "bill@example.com" on "test-1.example.com">
>>> test_two.subscribe('anna@example.com',
... pre_verified=True,
... pre_confirmed=True)
<Member "anna@example.com" on "test-2.example.com">
>>> test_two.subscribe('cris@example.com', 'Cris',
... pre_verified=True,
... pre_confirmed=True)
<Member "cris@example.com" on "test-2.example.com">
We can retrieve all known memberships. These are sorted first by mailing list
name, then by email address.
>>> for member in client.members:
... print(member)
<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">
We can also view the memberships for a single mailing list.
>>> for member in test_one.members:
... print(member)
<Member "anna@example.com" on "test-1.example.com">
<Member "bill@example.com" on "test-1.example.com">
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
>>> page.total_size
4
>>> for member in page:
... print(member)
<Member "anna@example.com" on "test-1.example.com">
<Member "bill@example.com" on "test-1.example.com">
>>> page = page.next
>>> page.nr
2
>>> for member in page:
... print(member)
<Member "anna@example.com" on "test-2.example.com">
<Member "cris@example.com" on "test-2.example.com">
>>> page = test_one.get_member_page(count=1, page=1)
>>> page.nr
1
>>> page.total_size
2
>>> for member in page:
... print(member)
<Member "anna@example.com" on "test-1.example.com">
>>> page = page.next
>>> page.nr
2
>>> page.total_size
2
>>> for member in page:
... print(member)
<Member "bill@example.com" on "test-1.example.com">
We can get a single membership too.
>>> cris_test_two = test_two.get_member('cris@example.com')
>>> cris_test_two
<Member "cris@example.com" on "test-2.example.com">
>>> print(cris_test_two.role)
member
A membership can also be retrieved without instantiating the list object first:
>>> client.get_member('test-2@example.com', 'cris@example.com')
<Member "cris@example.com" on "test-2.example.com">
A membership has preferences.
>>> prefs = cris_test_two.preferences
>>> print(prefs['delivery_mode'])
None
>>> print(prefs['acknowledge_posts'])
None
>>> print(prefs['delivery_status'])
None
>>> print(prefs['hide_address'])
None
>>> print(prefs['preferred_language'])
None
>>> print(prefs['receive_list_copy'])
None
>>> print(prefs['receive_own_postings'])
None
The membership object's ``user`` attribute will return a User object:
>>> cris_test_two.user
<User "Cris" (...)>
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):
...
ValueError: nomember@example.com is not a member address of test-2@example.com
After a while, Anna decides to unsubscribe from the Test One mailing list,
though she keeps her Test Two membership active.
>>> import time
>>> time.sleep(2)
>>> test_one.unsubscribe('anna@example.com')
>>> for member in client.members:
... print(member)
<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">
A little later, Cris decides to unsubscribe from the Test Two mailing list.
>>> cris_test_two.unsubscribe()
>>> for member in client.members:
... print(member)
<Member "bill@example.com" on "test-1.example.com">
<Member "anna@example.com" on "test-2.example.com">
If you try to unsubscribe an address which is not a member address
`ValueError` is raised:
>>> test_one.unsubscribe('nomember@example.com')
Traceback (most recent call last):
...
ValueError: nomember@example.com is not a member address of test-1@example.com
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
handle their messages going forward. In some cases, one might wish to
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.
Users
=====
Users are people with one or more list memberships. To get a list of all users,
access the clients user property.
>>> for user in client.users:
... print(user)
<User "..." (...)>
<User "..." (...)>
<User "..." (...)>
The list of users can also be paginated:
>>> page = client.get_user_page(count=4, page=1)
>>> page.nr
1
>>> page.total_size
5
>>> for user in page:
... print(user)
<User "Unverified" (...)>
<User "Unconfirmed" (...)>
<User "Anna" (...)>
<User "Bill" (...)>
You can get the next or previous pages without calling ``get_userpage`` again.
>>> page = page.next
>>> page.nr
2
>>> for user in page:
... print(user)
<User "Cris" (...)>
>>> page = page.previous
>>> page.nr
1
>>> for user in page:
... print(user)
<User "Unverified" (...)>
<User "Unconfirmed" (...)>
<User "Anna" (...)>
<User "Bill" (...)>
A single user can be retrieved using their email address.
>>> cris = client.get_user('cris@example.com')
>>> print(cris.display_name)
Cris
Every user has a list of one or more addresses.
>>> for address in cris.addresses:
... print(address)
... print(address.display_name)
... print(address.registered_on)
cris@example.com
Cris
...
Multiple addresses can be assigned to a user record:
>>> cris.add_address('cris.person@example.org')
cris.person@example.org
>>> print(client.get_address('cris.person@example.org'))
cris.person@example.org
>>> for address in cris.addresses:
... print(address)
cris.person@example.org
cris@example.com
Trying to add an existing address will raise an error:
>>> client.create_user(email='dana@example.org',
... password='somepass',
... display_name='Dana')
<User "Dana" (...)>
>>> 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:
>>> cris.add_address('dana@example.org', absorb_existing=True)
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
Addresses
=========
Addresses can be accessed directly:
>>> address = client.get_address('dana@example.org')
>>> print(address)
dana@example.org
>>> print(address.display_name)
Dana
The address has not been verified:
>>> print(address.verified)
False
But that can be done via the address object:
>>> address.verify()
>>> print(address.verified)
True
It can also be unverified:
>>> address.unverify()
>>> 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
Users can be added using ``create_user``. The display_name is optional:
>>> client.create_user(email='ler@primus.org',
... password='somepass',
... display_name='Ler')
<User "Ler" (...)>
>>> ler = client.get_user('ler@primus.org')
>>> print(ler.password)
$...
>>> print(ler.display_name)
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')
>>> print(ler.display_name)
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
User Subscriptions
------------------
A User's subscriptions can be access through their ``subscriptions`` property.
>>> bill = client.get_user('bill@example.com')
>>> for subscription in bill.subscriptions:
... print(subscription)
<Member "bill@example.com" on "test-1.example.com">
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.
>>> for list_id in bill.subscription_list_ids:
... print(list_id)
test-1.example.com
List Settings
=============
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.
>>> settings = test_one.settings
>>> len(settings)
56
>>> for attr in sorted(settings):
... print(attr + ': ' + str(settings[attr]))
acceptable_aliases: []
...
volume: 1
>>> print(settings['display_name'])
Test-1
We can access all valid list settings as attributes.
>>> print(settings['fqdn_listname'])
test-1@example.com
>>> print(settings['description'])
<BLANKLINE>
>>> settings['description'] = 'A very meaningful description.'
>>> settings['display_name'] = 'Test Numero Uno'
>>> settings.save()
>>> settings_new = test_one.settings
>>> print(settings_new['description'])
A very meaningful description.
>>> print(settings_new['display_name'])
Test Numero Uno
The settings object also supports the `get` method of usual Python
dictionaries:
>>> print(settings_new.get('OhNoIForgotTheKey',
... 'HowGoodIPlacedOneUnderTheDoormat'))
HowGoodIPlacedOneUnderTheDoormat
Preferences
===========
Preferences can be accessed and set for users, members and addresses.
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.
>>> global_prefs = client.preferences
>>> print(global_prefs['acknowledge_posts'])
False
>>> print(global_prefs['delivery_mode'])
regular
>>> print(global_prefs['delivery_status'])
enabled
>>> print(global_prefs['hide_address'])
True
>>> print(global_prefs['preferred_language'])
en
>>> print(global_prefs['receive_list_copy'])
True
>>> print(global_prefs['receive_own_postings'])
True
Preferences can be set, but you have to call ``save`` to make your changes
permanent.
>>> 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
>>> print(prefs['delivery_status'])
by_user
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
header-match
hold
moderation
reject
Owners and Moderators
=====================
Owners and moderators are properties of the list object.
>>> test_one.owners
[]
>>> test_one.moderators
[]
Owners can be added via the ``add_owner`` method:
>>> test_one.add_owner('foo@example.com')
>>> for owner in test_one.owners:
... print(owner)
foo@example.com
The owner of the list not automatically added as a member:
>>> test_one.members
[<Member "bill@example.com" on "test-1.example.com">]
Moderators can be added similarly:
>>> test_one.add_moderator('bar@example.com')
>>> for moderator in test_one.moderators:
... print(moderator)
bar@example.com
Moderators are also not automatically added as members:
>>> test_one.members
[<Member "bill@example.com" on "test-1.example.com">]
Members and owners/moderators are separate entries in in the general members
list:
>>> test_one.subscribe('bar@example.com', 'Bar',
... pre_verified=True,
... pre_confirmed=True)
<Member "bar@example.com" on "test-1.example.com">
>>> for member in client.members:
... 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
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
[]
Moderation
==========
Subscription Moderation
-----------------------
Subscription requests can be accessed through the list object's
`request` property. So let's create a non-open list first.
>>> confirm_first = example_dot_com.create_list('confirm-first')
>>> settings = confirm_first.settings
>>> settings['subscription_policy'] = 'moderate'
>>> settings.save()
>>> confirm_first = client.get_list('confirm-first.example.com')
>>> print(confirm_first.settings['subscription_policy'])
moderate
Initially there are no requests, so let's to subscribe someone to the
list. We'll get a token back.
>>> confirm_first.requests
[]
>>> 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.
>>> 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:
>>> response = confirm_first.moderate_request(request_1['token'], 'accept')
>>> 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:
>>> response = confirm_first.moderate_request(request_2['token'], 'reject')
>>> len(confirm_first.requests)
1
Let's discard Zeppo's request:
>>> response = confirm_first.moderate_request(request_3['token'], 'discard')
>>> len(confirm_first.requests)
0
Message Moderation
------------------
By injecting a message by a non-member into the incoming queue, we can
simulate a message being held for moderator approval.
>>> msg = """From: nomember@example.com
... To: test-1@example.com
... Subject: Something
... Message-ID: <moderated_01>
...
... Some text.
...
... """
>>> inq = client.queues['in']
>>> inq.inject('test-1.example.com', msg)
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:
... all_held = test_one.held
... if len(all_held) > 0:
... break
... time.sleep(0.1)
Messages held for moderation can be listed on a per list basis.
>>> print(all_held[0].request_id)
1
A held message can be retrieved by ID, and have attributes:
>>> heldmsg = test_one.get_held_message(1)
>>> print(heldmsg.subject)
Something
>>> print(heldmsg.reason)
The message is not from a list member
>>> 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.
>>> print(test_one.defer_message(heldmsg.request_id)['status'])
204
>>> len(test_one.held)
1
>>> print(heldmsg.discard()['status'])
204
>>> len(test_one.held)
0
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)
None
>>> bill_member.moderation_action = 'hold'
>>> bill_member.save()
>>> print(test_one.get_member('bill@example.com').moderation_action)
hold
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
>>> client.bans.add('bill@example.com')
bill@example.com
>>> print(list(client.bans))
[anna@example.com, bill@example.com]
The list of banned addresses can be paginated using the ``get_bans_page()``
method::
>>> print(list(client.get_bans_page(count=1, page=1)))
[anna@example.com]
>>> print(list(client.get_bans_page(count=1, page=2)))
[bill@example.com]
You can use the ``delete()`` method on a banned address to unban it, or the
``remove()`` method on the ban list::
>>> banned_anna.delete()
>>> 'anna@example.com' in client.bans
False
>>> print(list(client.bans))
[bill@example.com]
>>> client.bans.remove('bill@example.com')
>>> 'bill@example.com' in client.bans
False
>>> print(list(client.bans))
[]
The mailing-list-specific ban lists work in the same way::
>>> print(list(test_one.bans))
[]
>>> banned_anna = test_one.bans.add('anna@example.com')
>>> 'anna@example.com' in test_one.bans
True
>>> test_one.bans.add('bill@example.com')
bill@example.com
>>> print(list(test_one.bans))
[anna@example.com, bill@example.com]
>>> print(list(test_one.get_bans_page(count=1, page=1)))
[anna@example.com]
>>> print(list(test_one.get_bans_page(count=1, page=2)))
[bill@example.com]
>>> banned_anna.delete()
>>> 'anna@example.com' in test_one.bans
False
>>> test_one.bans.remove('bill@example.com')
>>> print(list(test_one.bans))
[]
Archivers
=========
Each list object has an ``archivers`` attribute.
>>> archivers = test_one.archivers
>>> print(archivers)
<Archivers on "test-1.example.com">
The activation status of each available archiver can be accessed like a
key in a dictionary.
>>> archivers = test_one.archivers
>>> for archiver in sorted(archivers.keys()):
... print('{0}: {1}'.format(archiver, archivers[archiver]))
mail-archive: True
mhonarc: True
prototype: True
>>> archivers['mail-archive']
True
>>> archivers['mhonarc']
True
They can also be set like items in dictionary.
>>> archivers['mail-archive'] = False
>>> archivers['mhonarc'] = False
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
have now been set.
>>> archivers = test_one.archivers
>>> archivers['mail-archive']
False
>>> archivers['mhonarc']
False
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)
<HeaderMatches for "test-1.example.com">
>>> 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'``.
>>> header_matches.add('Subject', '^test: ', 'discard')
<HeaderMatch on "subject">
>>> print(header_matches)
<HeaderMatches for "test-1.example.com">
>>> len(header_matches)
1
>>> print(list(header_matches))
[<HeaderMatch on "subject">]
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.
>>> header_matches.add('Subject', '^test: ', 'discard')
<HeaderMatch on "subject">
>>> header_matches[0].delete()
>>> len(header_matches) # not automatically updated
1
>>> len(test_one.header_matches)
0
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):
... print(cfg[key])
<Configuration: "antispam">
<Configuration: "archiver.mail_archive">
<Configuration: "archiver.master">
<Configuration: "archiver.mhonarc">
<Configuration: "archiver.prototype">
<Configuration: "bounces">
<Configuration: "database">
<Configuration: "devmode">
<Configuration: "digests">
<Configuration: "dmarc">
<Configuration: "language.ar">
<Configuration: "language.ast">
<Configuration: "language.ca">
<Configuration: "language.cs">
<Configuration: "language.da">
<Configuration: "language.de">
<Configuration: "language.el">
<Configuration: "language.en">
<Configuration: "language.es">
<Configuration: "language.et">
<Configuration: "language.eu">
<Configuration: "language.fi">
<Configuration: "language.fr">
<Configuration: "language.gl">
<Configuration: "language.he">
<Configuration: "language.hr">
<Configuration: "language.hu">
<Configuration: "language.ia">
<Configuration: "language.it">
<Configuration: "language.ja">
<Configuration: "language.ko">
<Configuration: "language.lt">
<Configuration: "language.nl">
<Configuration: "language.no">
<Configuration: "language.pl">
<Configuration: "language.pt">
<Configuration: "language.pt_BR">
<Configuration: "language.ro">
<Configuration: "language.ru">
<Configuration: "language.sk">
<Configuration: "language.sl">
<Configuration: "language.sr">
<Configuration: "language.sv">
<Configuration: "language.tr">
<Configuration: "language.uk">
<Configuration: "language.vi">
<Configuration: "language.zh_CN">
<Configuration: "language.zh_TW">
<Configuration: "logging.archiver">
<Configuration: "logging.bounce">
<Configuration: "logging.config">
<Configuration: "logging.database">
<Configuration: "logging.debug">
<Configuration: "logging.error">
<Configuration: "logging.fromusenet">
<Configuration: "logging.http">
<Configuration: "logging.locks">
<Configuration: "logging.mischief">
<Configuration: "logging.root">
<Configuration: "logging.runner">
<Configuration: "logging.smtp">
<Configuration: "logging.subscribe">
<Configuration: "logging.vette">
<Configuration: "mailman">
<Configuration: "mta">
<Configuration: "nntp">
<Configuration: "passwords">
<Configuration: "paths.dev">
<Configuration: "paths.fhs">
<Configuration: "paths.here">
<Configuration: "paths.local">
<Configuration: "runner.archive">
<Configuration: "runner.bad">
<Configuration: "runner.bounces">
<Configuration: "runner.command">
<Configuration: "runner.digest">
<Configuration: "runner.in">
<Configuration: "runner.lmtp">
<Configuration: "runner.nntp">
<Configuration: "runner.out">
<Configuration: "runner.pipeline">
<Configuration: "runner.rest">
<Configuration: "runner.retry">
<Configuration: "runner.shunt">
<Configuration: "runner.virgin">
<Configuration: "shell">
<Configuration: "styles">
<Configuration: "webservice">
Each configuration object is a dictionary and you can iterate over them:
>>> 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
post_hook :
pre_hook :
self_link : http://localhost:9001/3.1/system/configuration/mailman
sender_headers : from from_ reply-to sender
site_owner : changeme@example.com
.. >>> for domain in client.domains:
... domain.delete()
>>> for user in client.users:
... user.delete()
|