Debian Codegraph @

django_mailman3/lib/mailman.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2016-2023 by the Free Software Foundation, Inc.
4 #
5 # This file is part of Django-Mailman.
6 #
7 # Django-Mailman is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation, either version 3 of the License, or (at your option)
10 # any later version.
11 #
12 # Django-Mailman is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 # more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Django-Mailman. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Author: Aurelien Bompard <[email protected]>
21 #
22
23 import logging
24 from urllib.error import HTTPError
25
26 from django.conf import settings
27 from django.core.cache import cache
28 from django.db import IntegrityError
29
30 from allauth.account.models import EmailAddress
31 from mailmanclient import Client as MailmanClient
32 from mailmanclient import MailmanConnectionError
33
34
35 logger = logging.getLogger(__name__)
36
37 _REQUEST_HOOKS = []
38 #: List of hooks that can into request parameter of Mailmanclient.
39
40
41 def mailmanclient_request_hook(fn):
42 """Decorator that converts a method into Mailmanclient request hook."""
43 if fn not in _REQUEST_HOOKS:
44 _REQUEST_HOOKS.append(fn)
45 return fn
46
47
48 def get_request_hooks():
49 """Get a list of default request hooks.
50
51 Returns default hooks, but only in DEBUG mode.
52 """
53 return _REQUEST_HOOKS
54
55
56 def get_mailman_client(api_version='3.1'):
57 """Return an instance of Mailman Client.
58
59 :param api_version: The API version for Mailman Core to use.
60 :type api_version: string
61 :returns: An instance of :class:`mailmanclient.Client`
62 """
63 # easier to patch during unit tests
64 client = MailmanClient(
65 '{}/{}'.format(settings.MAILMAN_REST_API_URL, api_version),
66 settings.MAILMAN_REST_API_USER,
67 settings.MAILMAN_REST_API_PASS,
68 get_request_hooks())
69 return client
70
71
72 def get_mailman_user(user):
73 """Given a Django user, return the Mailman's user object.
74
75 If the user does not exist, we will try to create one. If neither of the
76 get or create options work, perhaps because API is un-reachable, we return
77 a None value.
78
79 :param user: Instance of a Django user.
80 :returns: Mailman user or None if Mailman API isn't available.
81 :rtype: :class:`mailmanclient.User`
82 """
83 # Only cache the mailman user_id, not the whole user instance, because
84 # mailmanclient is not pickle-safe
85 cache_key = "User:%s:mailman_user_id" % user.id
86 mm_user_id = cache.get(cache_key)
87 try:
88 mm_client = get_mailman_client()
89 mm_user = None
90 if mm_user_id is not None:
91 # Due upgrade from Mailman API 3.0 to 3.1, integer user_id can
92 # return 404 if the API version used was 3.1 (which has user_ids as
93 # UUIDs). So, we are going to lookup with email if the cached
94 # user_id returns 404.
95 try:
96 mm_user = mm_client.get_user(mm_user_id)
97 except HTTPError as e:
98 if e.code != 404:
99 raise
100 # So, either the user_id wasn't cached or the cached user_id didn't
101 # return a valid user. Now lookup with user's email.
102 if mm_user is None:
103 try:
104 mm_user = mm_client.get_user(user.email)
105 except HTTPError as e:
106 if e.code != 404:
107 raise # will be caught down there
108 mm_user = mm_client.create_user(
109 user.email, user.get_full_name())
110 # XXX The email is not set as verified, because we don't
111 # know if the registration that was used verified it.
112 logger.info("Created Mailman user for %s (%s)",
113 user.username, user.email)
114 # Update the cache to avoid a lookup next time.
115 cache.set(cache_key, mm_user.user_id, None)
116 return mm_user
117 except (HTTPError, MailmanConnectionError) as e:
118 logger.warning(
119 "Error getting or creating the Mailman user of %s (%s): %s",
120 user.username, user.email, e)
121 return None
122
123
124 def get_mailman_user_id(user):
125 # TODO: Optimization: look in the cache first, if not found call
126 # get_mailman_user() as before
127 mm_user = get_mailman_user(user)
128 if mm_user is None:
129 return None
130 return str(mm_user.user_id)
131
132
133 def get_subscriptions(user):
134 # Get subscriptions for the provided Django user.
135 def _get_value():
136 mm_user = get_mailman_user(user)
137 if mm_user is None:
138 return {}
139 subscriptions = dict([
140 (member.list_id, member.address)
141 for member in mm_user.subscriptions
142 if member.role != "nonmember"
143 ])
144 return subscriptions
145 # TODO: how should this be invalidated? Subscribe to a signal in
146 # mailman when a new subscription occurs? Or store in the session?
147 return cache.get_or_set(
148 "User:%s:subscriptions" % user.id,
149 _get_value, 60, version=2) # 1 minute
150 # TODO: increase the cache duration when we have Mailman signals
151
152
153 def add_address_to_mailman_user(user, address):
154 """Associate a verified address with a Mailman user."""
155 logger.debug("Associating address %s with user %s in Mailman",
156 address, user.username)
157 mm_user = get_mailman_user(user)
158 if mm_user is None:
159 logger.info("Could not find or create a Mailman user for %s",
160 user.username)
161 return
162 existing_addresses = [str(addr) for addr in mm_user.addresses]
163 if address not in existing_addresses:
164 # Associate it with the user.
165 try:
166 mm_address = mm_user.add_address(address, absorb_existing=True)
167 mm_address.verify()
168 logger.debug("Associated address %s with %s",
169 address, user.username)
170 except HTTPError as e:
171 logger.warning("Can't add %s to %s: %s",
172 address, user.username, e)
173 else:
174 # Already associated, just mark it verified.
175 mm_address = get_mailman_client().get_address(address)
176 if mm_address.verified_on is None:
177 mm_address.verify()
178
179
180 def sync_email_addresses(user):
181 # Synchronize email addresses for the user in Mailman and in Django. When
182 # an address is missing, it is added, no deletion is performed here.
183 # For deletion, use the appropriate view/form. The 'verified' bit is also
184 # sychronized.
185 logger.debug("Synchronizing email addresses for user %s", user.username)
186 mm_user = get_mailman_user(user)
187 if mm_user is None:
188 logger.info("Could not find or create a Mailman user for %s",
189 user.username)
190 return
191
192 def check_verified(django_address, mm_address):
193 if mm_address.verified and not django_address.verified:
194 django_address.verified = True
195 django_address.save()
196 if django_address.verified and not mm_address.verified:
197 mm_address.verify()
198
199 django_addresses = EmailAddress.objects.filter(user=user).all()
200 mailman_addresses = mm_user.addresses
201 # Django
202 for django_address in django_addresses:
203 mm_address = mailman_addresses.find_by_email(django_address.email)
204 if mm_address is not None:
205 check_verified(django_address, mm_address)
206 else:
207 mm_address = mm_user.add_address(
208 django_address.email, absorb_existing=True)
209 if django_address.verified:
210 mm_address.verify()
211 # Mailman
212 for mm_address in mailman_addresses:
213 if not mm_address.verified:
214 continue
215 try:
216 django_address, _created = EmailAddress.objects.get_or_create(
217 user=user, email=mm_address.email)
218 except IntegrityError:
219 continue # Email exists and belongs to another user.
220 check_verified(django_address, mm_address)
221
222
223 def update_preferred_address(user, to_email_address):
224 """Update the preferred address of a user in Mailman Core.
225
226 This synchronizes the preferred address between Mailman Core and Django,
227 although, it is a one-way sync.
228 """
229 logger.debug("Synchronizing primary address for user %s", user.username)
230 mm_user = get_mailman_user(user)
231
232 if mm_user is None:
233 logger.info(
234 "Could not find or create a Mailman user for %s", user.username)
235 return
236 if not to_email_address.verified:
237 return
238
239 # Note: Although, setting preferred address will already add the address to
240 # the user, but if the address isn't verified then it will result in an
241 # exception. So, we trigger a verify + add workflow before setting primary
242 # address.
243 add_address_to_mailman_user(user, to_email_address.email)
244
245 # Finally, set the preferred address.
246
247 try:
248 mm_user.preferred_address = to_email_address.email
249 except HTTPError:
250 logger.error("Failed to update preferred address for %s in Core",
251 user.username, exc_info=True)