Merge remote branch 'origin/xorg/maint' into xorg/master
[platal.git] / bin / lists.rpc.py
CommitLineData
0337d704 1#!/usr/bin/env python
2#***************************************************************************
5e1513f6 3#* Copyright (C) 2003-2011 Polytechnique.org *
0337d704 4#* http://opensource.polytechnique.org/ *
5#* *
6#* This program is free software; you can redistribute it and/or modify *
7#* it under the terms of the GNU General Public License as published by *
8#* the Free Software Foundation; either version 2 of the License, or *
9#* (at your option) any later version. *
10#* *
11#* This program is distributed in the hope that it will be useful, *
12#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14#* GNU General Public License for more details. *
15#* *
16#* You should have received a copy of the GNU General Public License *
17#* along with this program; if not, write to the Free Software *
18#* Foundation, Inc., *
19#* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20#***************************************************************************
21
22import base64, MySQLdb, os, getopt, sys, sha, signal, re, shutil, ConfigParser
23import MySQLdb.converters
24import SocketServer
25
26sys.path.append('/usr/lib/mailman/bin')
27
28from pwd import getpwnam
29from grp import getgrnam
30
31from SimpleXMLRPCServer import SimpleXMLRPCServer
32from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
33
34import paths
35from Mailman import MailList
36from Mailman import Utils
37from Mailman import Message
38from Mailman import Errors
39from Mailman import mm_cfg
40from Mailman import i18n
41from Mailman.UserDesc import UserDesc
42from Mailman.ListAdmin import readMessage
43from email.Iterators import typed_subpart_iterator
44from threading import Lock
45
46class AuthFailed(Exception): pass
47
48################################################################################
49#
50# CONFIG
51#
52#------------------------------------------------
53
54config = ConfigParser.ConfigParser()
78dd3eb2 55config.read(os.path.dirname(__file__)+'/../configs/platal.ini')
0337d704 56config.read(os.path.dirname(__file__)+'/../configs/platal.conf')
57
ae6c293b 58def get_config(sec, val, default=None):
0337d704 59 try:
60 return config.get(sec, val)[1:-1]
61 except ConfigParser.NoOptionError, e:
62 if default is None:
849baea6 63 sys.stderr.write('%s\n' % str(e))
0337d704 64 sys.exit(1)
65 else:
66 return default
67
0337d704 68MYSQL_USER = get_config('Core', 'dbuser')
69MYSQL_PASS = get_config('Core', 'dbpwd')
7660a7c7 70MYSQL_DB = get_config('Core', 'dbdb')
0337d704 71
72PLATAL_DOMAIN = get_config('Mail', 'domain')
73PLATAL_DOMAIN2 = get_config('Mail', 'domain2', '')
849baea6 74sys.stderr.write('PLATAL_DOMAIN = %s\n' % PLATAL_DOMAIN )
3e3d853e 75sys.stderr.write("MYSQL_DB = %s\n" % MYSQL_DB)
0337d704 76
0337d704 77VHOST_SEP = get_config('Lists', 'vhost_sep', '_')
78ON_CREATE_CMD = get_config('Lists', 'on_create', '')
79
1fec3393
FB
80SRV_HOST = get_config('Lists', 'rpchost', 'localhost')
81SRV_PORT = int(get_config('Lists', 'rpcport', '4949'))
82
0337d704 83################################################################################
84#
85# CLASSES
86#
87#------------------------------------------------
88# Manage Basic authentication
89#
90
91class BasicAuthXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
92
93 """XMLRPC Request Handler
94 This request handler is used to provide BASIC HTTP user authentication.
95 It first overloads the do_POST() function, authenticates the user, then
96 calls the super.do_POST().
97
98 Moreover, we override _dispatch, so that we call functions with as first
99 argument a UserDesc taken from the database, containing name, email and perms
100 """
101
c1bae0aa
FB
102 def _get_function(self, method):
103 try:
104 # check to see if a matching function has been registered
105 return self.server.funcs[method]
106 except:
107 raise Exception('method "%s" is not supported' % method)
108
c6aeb88a
FB
109 def is_rpc_path_valid(self):
110 return True
c1bae0aa 111
ae6c293b 112 def _dispatch(self, method, params):
c1bae0aa 113 return list_call_dispatcher(self._get_function(method), self.data[0], self.data[1], self.data[2], *params)
0337d704 114
115 def do_POST(self):
116 try:
117 _, auth = self.headers["authorization"].split()
118 uid, md5 = base64.decodestring(auth).strip().split(':')
119 vhost = self.path.split('/')[1].lower()
ae6c293b 120 self.data = self.getUser(uid, md5, vhost)
0337d704 121 if self.data is None:
122 raise AuthFailed
123 # Call super.do_POST() to do the actual work
124 SimpleXMLRPCRequestHandler.do_POST(self)
125 except:
126 self.send_response(401)
127 self.end_headers()
128
129 def getUser(self, uid, md5, vhost):
2ca5f031
FB
130 res = mysql_fetchone ("""SELECT a.full_name, IF(aa.alias IS NULL, a.email, CONCAT(aa.alias, '@%s')),
131 IF (a.is_admin, 'admin',
132 IF(FIND_IN_SET('lists', at.perms) OR FIND_IN_SET('lists', a.user_perms), 'lists', NULL))
7660a7c7 133 FROM accounts AS a
2ca5f031
FB
134 INNER JOIN account_types AS at ON (at.type = a.type)
135 LEFT JOIN aliases AS aa ON (a.uid = aa.uid AND aa.type = 'a_vie')
7660a7c7
FB
136 WHERE a.uid = '%s' AND a.password = '%s' AND a.state = 'active'
137 LIMIT 1""" \
2ca5f031 138 % (PLATAL_DOMAIN, uid, md5))
0337d704 139 if res:
ae6c293b 140 name, forlife, perms = res
9b12102b 141 if vhost != PLATAL_DOMAIN and perms != 'admin':
2ca5f031 142 res = mysql_fetchone ("""SELECT m.uid, IF(m.perms = 'admin', 'admin', 'lists')
7660a7c7
FB
143 FROM group_members AS m
144 INNER JOIN groups AS g ON (m.asso_id = g.id)
2ca5f031 145 WHERE uid = '%s' AND mail_domain = '%s'""" \
7660a7c7
FB
146 % (uid, vhost))
147 if res:
2ca5f031
FB
148 _, perms = res
149 userdesc = UserDesc(forlife, name, None, 0)
ae6c293b 150 return (userdesc, perms, vhost)
0337d704 151 else:
168ca63b 152 print >> sys.stderr, "no user found for uid: %s, passwd: %s" % (uid, md5)
0337d704 153 return None
ae6c293b 154
0337d704 155################################################################################
156#
157# XML RPC STUFF
158#
159#-------------------------------------------------------------------------------
160# helpers
161#
162
163def connectDB():
164 db = MySQLdb.connect(
7660a7c7 165 db=MYSQL_DB,
0337d704 166 user=MYSQL_USER,
167 passwd=MYSQL_PASS,
6bd94db9 168 unix_socket='/var/run/mysqld/mysqld.sock')
0337d704 169 db.ping()
31ddf875 170 db.autocommit(True)
0337d704 171 return db.cursor()
172
173def mysql_fetchone(query):
174 ret = None
175 try:
176 lock.acquire()
177 mysql.execute(query)
178 if int(mysql.rowcount) > 0:
179 ret = mysql.fetchone()
180 finally:
181 lock.release()
182 return ret
183
ae6c293b 184def is_admin_on(userdesc, perms, mlist):
0337d704 185 return ( perms == 'admin' ) or ( userdesc.address in mlist.owner )
186
187
ae6c293b 188def quote(s, is_header=False):
0337d704 189 if is_header:
ae6c293b 190 h = Utils.oneline(s, 'iso-8859-1')
0337d704 191 else:
192 h = s
ea626742 193 h = str('').join(re.split('[\x00-\x08\x0B-\x1f]+', h))
25112e7a 194 return Utils.uquote(h.replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;'))
0337d704 195
196def to_forlife(email):
197 try:
ae6c293b 198 mbox, fqdn = email.split('@')
0337d704 199 except:
200 mbox = email
201 fqdn = PLATAL_DOMAIN
202 if ( fqdn == PLATAL_DOMAIN ) or ( fqdn == PLATAL_DOMAIN2 ):
7660a7c7
FB
203 res = mysql_fetchone("""SELECT CONCAT(f.alias, '@%s'), a.full_name
204 FROM accounts AS a
50e2ba89 205 INNER JOIN aliases AS f ON (f.uid = a.uid AND f.type = 'a_vie')
7660a7c7
FB
206 INNER JOIN aliases AS aa ON (aa.uid = a.uid AND aa.alias = '%s'
207 AND a.type != 'homonyme')
208 WHERE a.state = 'active'
209 LIMIT 1""" \
210 % (PLATAL_DOMAIN, mbox))
0337d704 211 if res:
212 return res
213 else:
ae6c293b 214 return (None, None)
cf5e8ef1 215 return (email.lower(), mbox)
0337d704 216
217##
218# see /usr/lib/mailman/bin/rmlist
219##
220def remove_it(listname, filename):
221 if os.path.islink(filename) or os.path.isfile(filename):
222 os.unlink(filename)
223 elif os.path.isdir(filename):
224 shutil.rmtree(filename)
ae6c293b 225
c1bae0aa
FB
226##
227# Call dispatcher
228##
229
230def has_annotation(method, name):
231 """ Check if the method contains the given annoation.
232 """
233 return method.__doc__ and method.__doc__.find("@%s" % name) > -1
234
235def list_call_dispatcher(method, userdesc, perms, vhost, *arg):
236 """Dispatch the call to the right handler.
237 This function checks the options of the called method the set the environment of the call.
238 The dispatcher uses method annotation (special tokens in the documentation of the method) to
239 guess the requested environment:
240 @mlist: the handler requires a mlist object instead of the vhost/listname couple
241 @lock: the handler requires the mlist to be locked (@mlist MUST be specified)
242 @edit: the handler edit the mlist (@mlist MUST be specified)
243 @admin: the handler requires admin rights on the list (@mlist MUST be specified)
244 @root: the handler requires site admin rights
245 """
246 try:
168ca63b 247 print >> sys.stderr, "calling method: %s" % method
c1bae0aa
FB
248 if has_annotation(method, "root") and perms != "admin":
249 return 0
250 if has_annotation(method, "mlist"):
079b7d91 251 listname = str(arg[0])
c1bae0aa
FB
252 arg = arg[1:]
253 mlist = MailList.MailList(vhost + VHOST_SEP + listname.lower(), lock=0)
254 if has_annotation(method, "admin") and not is_admin_on(userdesc, perms, mlist):
255 return 0
256 if has_annotation(method, "edit") or has_annotation(method, "lock"):
257 return list_call_locked(method, userdesc, perms, mlist, has_annotation(method, "edit"), *arg)
258 else:
259 return method(userdesc, perms, mlist, *arg)
260 else:
261 return method(userdesc, perms, vhost, *arg)
262 except Exception, e:
fc240413 263 sys.stderr.write('Exception in dispatcher %s\n' % str(e))
c1bae0aa
FB
264 raise e
265 return 0
266
267def list_call_locked(method, userdesc, perms, mlist, edit, *arg):
268 """Call the given method after locking the mlist.
269 """
270 try:
271 mlist.Lock()
272 ret = method(userdesc, perms, mlist, *arg)
273 if edit:
274 mlist.Save()
275 mlist.Unlock()
276 return ret
fc240413
FB
277 except Exception, e:
278 sys.stderr.write('Exception in locked call %s: %s\n' % (method.__name__, str(e)))
c1bae0aa
FB
279 mlist.Unlock()
280 return 0
281 # TODO: use finally when switching to python 2.5
282
0337d704 283#-------------------------------------------------------------------------------
284# helpers on lists
285#
286
849baea6 287def is_subscription_pending(userdesc, perms, mlist):
c1bae0aa
FB
288 for id in mlist.GetSubscriptionIds():
289 if userdesc.address == mlist.GetRecord(id)[1]:
290 return True
291 return False
292
ae6c293b 293def get_list_info(userdesc, perms, mlist, front_page=0):
0337d704 294 members = mlist.getRegularMemberKeys()
295 is_member = userdesc.address in members
ae6c293b 296 is_owner = userdesc.address in mlist.owner
2ca5f031 297 if (mlist.advertised and perms in ('lists', 'admin')) or is_member or is_owner or (not front_page and perms == 'admin'):
0337d704 298 is_pending = False
299 if not is_member and (mlist.subscribe_policy > 1):
849baea6 300 is_pending = list_call_locked(is_subscription_pending, userdesc, perms, mlist, False)
c1bae0aa 301 if is_pending is 0:
5f0ac25e 302 return None
0337d704 303
304 host = mlist.internal_name().split(VHOST_SEP)[0].lower()
305 details = {
306 'list' : mlist.real_name,
307 'addr' : mlist.real_name.lower() + '@' + host,
308 'host' : host,
309 'desc' : quote(mlist.description),
310 'info' : quote(mlist.info),
311 'diff' : (mlist.default_member_moderation>0) + (mlist.generic_nonmember_action>0),
312 'ins' : mlist.subscribe_policy > 1,
ae6c293b 313 'priv' : 1-mlist.advertised,
0337d704 314 'sub' : 2*is_member + is_pending,
315 'own' : is_owner,
316 'nbsub': len(members)
317 }
ae6c293b 318 return (details, members)
ae525b0e 319 return None
0337d704 320
c1bae0aa
FB
321def get_options(userdesc, perms, mlist, opts):
322 """ Get the options of a list.
323 @mlist
324 @admin
325 """
326 options = { }
327 for (k, v) in mlist.__dict__.iteritems():
328 if k in opts:
329 if type(v) is str:
330 options[k] = quote(v)
331 else: options[k] = v
332 details = get_list_info(userdesc, perms, mlist)[0]
333 return (details, options)
334
d36b2def 335def set_options(userdesc, perms, mlist, opts, vals):
c1bae0aa
FB
336 for (k, v) in vals.iteritems():
337 if k not in opts:
338 continue
339 if k == 'default_member_moderation':
340 for member in mlist.getMembers():
341 mlist.setMemberOption(member, mm_cfg.Moderate, int(v))
342 t = type(mlist.__dict__[k])
343 if t is bool: mlist.__dict__[k] = bool(v)
344 elif t is int: mlist.__dict__[k] = int(v)
345 elif t is str: mlist.__dict__[k] = Utils.uncanonstr(v, 'fr')
346 else: mlist.__dict__[k] = v
347 return 1
0337d704 348
349#-------------------------------------------------------------------------------
350# users procedures for [ index.php ]
351#
352
ae6c293b 353def get_lists(userdesc, perms, vhost, email=None):
c1bae0aa
FB
354 """ List available lists for the given vhost
355 """
0337d704 356 if email is None:
357 udesc = userdesc
358 else:
cf5e8ef1 359 udesc = UserDesc(email.lower(), email.lower(), None, 0)
0337d704 360 prefix = vhost.lower()+VHOST_SEP
361 names = Utils.list_names()
362 names.sort()
363 result = []
364 for name in names:
365 if not name.startswith(prefix):
366 continue
367 try:
ae6c293b 368 mlist = MailList.MailList(name, lock=0)
0337d704 369 except:
370 continue
371 try:
ae525b0e
FB
372 details = get_list_info(udesc, perms, mlist, (email is None and vhost == PLATAL_DOMAIN))
373 if details is not None:
374 result.append(details[0])
849baea6
FB
375 except Exception, e:
376 sys.stderr.write('Can\'t get list %s: %s\n' % (name, str(e)))
0337d704 377 continue
378 return result
379
c1bae0aa
FB
380def subscribe(userdesc, perms, mlist):
381 """ Subscribe to a list.
382 @mlist
383 @edit
384 """
385 if ( mlist.subscribe_policy in (0, 1) ) or userdesc.address in mlist.owner:
386 mlist.ApprovedAddMember(userdesc)
387 result = 2
388 else:
389 result = 1
390 try:
391 mlist.AddMember(userdesc)
392 except Errors.MMNeedApproval:
393 pass
0337d704 394 return result
395
c1bae0aa 396def unsubscribe(userdesc, perms, mlist):
61d4544b 397 """ Unsubscribe from a list
c1bae0aa
FB
398 @mlist
399 @edit
400 """
401 mlist.ApprovedDeleteMember(userdesc.address)
402 return 1
0337d704 403
404#-------------------------------------------------------------------------------
405# users procedures for [ index.php ]
406#
407
79a9ca23 408def get_name(member):
409 try:
410 return quote(mlist.getMemberName(member))
411 except:
79a9ca23 412 return ''
413
c1bae0aa
FB
414def get_members(userdesc, perms, mlist):
415 """ List the members of a list.
416 @mlist
417 """
5f0ac25e
FB
418 infos = get_list_info(userdesc, perms, mlist)
419 if infos is None:
89cf6882
FB
420 # Do not return None, this is not serializable
421 return 0
5f0ac25e 422 details, members = infos
c1bae0aa
FB
423 members.sort()
424 members = map(lambda member: (get_name(member), member), members)
425 return (details, members, mlist.owner)
426
0337d704 427
428#-------------------------------------------------------------------------------
429# users procedures for [ trombi.php ]
430#
431
c1bae0aa
FB
432def get_members_limit(userdesc, perms, mlist, page, nb_per_page):
433 """ Get a range of members of the list.
434 @mlist
435 """
436 members = get_members(userdesc, perms, mlist)[1]
0337d704 437 i = int(page) * int(nb_per_page)
438 return (len(members), members[i:i+int(nb_per_page)])
439
c1bae0aa
FB
440def get_owners(userdesc, perms, mlist):
441 """ Get the owners of the list.
442 @mlist
443 """
444 details, members, owners = get_members(userdesc, perms, mlist)
ae6c293b 445 return (details, owners)
0337d704 446
c1bae0aa 447
0337d704 448#-------------------------------------------------------------------------------
449# owners procedures [ admin.php ]
450#
451
c1bae0aa
FB
452def replace_email(userdesc, perms, mlist, from_email, to_email):
453 """ Replace the address of a member by another one.
454 @mlist
455 @edit
456 @admin
457 """
458 mlist.ApprovedChangeMemberAddress(from_email.lower(), to_email.lower(), 0)
459 return 1
ae6c293b 460
c1bae0aa
FB
461def mass_subscribe(userdesc, perms, mlist, users):
462 """ Add a list of users to the list.
463 @mlist
464 @edit
465 @admin
466 """
467 members = mlist.getRegularMemberKeys()
468 added = []
c1bae0aa
FB
469 for user in users:
470 email, name = to_forlife(user)
471 if ( email is None ) or ( email in members ):
472 continue
473 userd = UserDesc(email, name, None, 0)
474 mlist.ApprovedAddMember(userd)
475 added.append( (quote(userd.fullname), userd.address) )
0337d704 476 return added
477
c1bae0aa
FB
478def mass_unsubscribe(userdesc, perms, mlist, users):
479 """ Remove a list of users from the list.
480 @mlist
481 @edit
482 @admin
483 """
484 map(lambda user: mlist.ApprovedDeleteMember(user), users)
0337d704 485 return users
486
c1bae0aa
FB
487def add_owner(userdesc, perms, mlist, user):
488 """ Add a owner to the list.
489 @mlist
490 @edit
491 @admin
492 """
493 email = to_forlife(user)[0]
494 if email is None:
0337d704 495 return 0
c1bae0aa
FB
496 if email not in mlist.owner:
497 mlist.owner.append(email)
0337d704 498 return True
499
c1bae0aa
FB
500def del_owner(userdesc, perms, mlist, user):
501 """ Remove a owner of the list.
502 @mlist
503 @edit
504 @admin
505 """
506 if len(mlist.owner) < 2:
0337d704 507 return 0
c1bae0aa 508 mlist.owner.remove(user)
0337d704 509 return True
510
511#-------------------------------------------------------------------------------
512# owners procedures [ admin.php ]
513#
514
c1bae0aa
FB
515def get_pending_ops(userdesc, perms, mlist):
516 """ Get the list of operation waiting for an action from the owners.
517 @mlist
518 @lock
519 @admin
520 """
521 subs = []
522 seen = []
523 dosave = False
524 for id in mlist.GetSubscriptionIds():
525 time, addr, fullname, passwd, digest, lang = mlist.GetRecord(id)
526 if addr in seen:
527 mlist.HandleRequest(id, mm_cfg.DISCARD)
528 dosave = True
529 continue
530 seen.append(addr)
531 try:
532 login = re.match("^[^.]*\.[^.]*\.\d\d\d\d$", addr.split('@')[0]).group()
533 subs.append({'id': id, 'name': quote(fullname), 'addr': addr, 'login': login })
534 except:
535 subs.append({'id': id, 'name': quote(fullname), 'addr': addr })
0337d704 536
c1bae0aa
FB
537 helds = []
538 for id in mlist.GetHeldMessageIds():
539 ptime, sender, subject, reason, filename, msgdata = mlist.GetRecord(id)
0337d704 540 fpath = os.path.join(mm_cfg.DATA_DIR, filename)
c1bae0aa
FB
541 try:
542 size = os.path.getsize(fpath)
543 except OSError, e:
544 if e.errno <> errno.ENOENT: raise
545 continue
546 try:
547 msg = readMessage(fpath)
548 fromX = msg.has_key("X-Org-Mail")
549 except:
550 pass
551 helds.append({
552 'id' : id,
0337d704 553 'sender': quote(sender, True),
554 'size' : size,
555 'subj' : quote(subject, True),
556 'stamp' : ptime,
c1bae0aa
FB
557 'fromx' : fromX
558 })
559 if dosave:
560 mlist.Save()
561 return (subs, helds)
562
563def handle_request(userdesc, perms, mlist, id, value, comment):
564 """ Handle a moderation request.
565 @mlist
566 @edit
567 @admin
568 """
569 mlist.HandleRequest(int(id), int(value), comment)
570 return 1
571
572def get_pending_sub(userdesc, perms, mlist, id):
573 """ Get informations about a given subscription moderation.
574 @mlist
575 @lock
576 @admin
577 """
578 sub = 0
579 id = int(id)
580 if id in mlist.GetSubscriptionIds():
581 time, addr, fullname, passwd, digest, lang = mlist.GetRecord(id)
582 try:
583 login = re.match("^[^.]*\.[^.]*\.\d\d\d\d$", addr.split('@')[0]).group()
584 sub = {'id': id, 'name': quote(fullname), 'addr': addr, 'login': login }
585 except:
586 sub = {'id': id, 'name': quote(fullname), 'addr': addr }
587 return sub
588
589def get_pending_mail(userdesc, perms, mlist, id, raw=0):
590 """ Get informations about a given mail moderation.
591 @mlist
592 @lock
593 @admin
594 """
595 ptime, sender, subject, reason, filename, msgdata = mlist.GetRecord(int(id))
596 fpath = os.path.join(mm_cfg.DATA_DIR, filename)
597 size = os.path.getsize(fpath)
598 msg = readMessage(fpath)
599
600 if raw:
601 return quote(str(msg))
602 results_plain = []
603 results_html = []
604 for part in typed_subpart_iterator(msg, 'text', 'plain'):
605 c = part.get_payload()
606 if c is not None: results_plain.append (c)
607 results_plain = map(lambda x: quote(x), results_plain)
608 for part in typed_subpart_iterator(msg, 'text', 'html'):
609 c = part.get_payload()
610 if c is not None: results_html.append (c)
611 results_html = map(lambda x: quote(x), results_html)
612 return {'id' : id,
613 'sender': quote(sender, True),
614 'size' : size,
615 'subj' : quote(subject, True),
616 'stamp' : ptime,
617 'parts_plain' : results_plain,
618 'parts_html': results_html }
0337d704 619
620#-------------------------------------------------------------------------------
621# owner options [ options.php ]
622#
623
624owner_opts = ['accept_these_nonmembers', 'admin_notify_mchanges', 'description', \
625 'default_member_moderation', 'generic_nonmember_action', 'info', \
626 'subject_prefix', 'goodbye_msg', 'send_goodbye_msg', 'subscribe_policy', \
627 'welcome_msg']
628
c1bae0aa
FB
629def get_owner_options(userdesc, perms, mlist):
630 """ Get the owner options of a list.
631 @mlist
632 @admin
633 """
634 return get_options(userdesc, perms, mlist, owner_opts)
0337d704 635
c1bae0aa
FB
636def set_owner_options(userdesc, perms, mlist, values):
637 """ Set the owner options of a list.
638 @mlist
639 @edit
640 @admin
641 """
642 return set_options(userdesc, perms, mlist, owner_opts, values)
0337d704 643
c1bae0aa
FB
644def add_to_wl(userdesc, perms, mlist, addr):
645 """ Add addr to the whitelist
646 @mlist
647 @edit
648 @admin
649 """
650 mlist.accept_these_nonmembers.append(addr)
651 return 1
0337d704 652
c1bae0aa
FB
653def del_from_wl(userdesc, perms, mlist, addr):
654 """ Remove an address from the whitelist
655 @mlist
656 @edit
657 @admin
658 """
659 mlist.accept_these_nonmembers.remove(addr)
660 return 1
0337d704 661
c1bae0aa
FB
662def get_bogo_level(userdesc, perms, mlist):
663 """ Compute bogo level from the filtering rules set up on the list.
664 @mlist
665 @admin
666 """
667 if len(mlist.header_filter_rules) == 0:
0337d704 668 return 0
c638d8c8 669
c1bae0aa
FB
670 unsurelevel = 0
671 filterlevel = 0
672 filterbase = 0
c638d8c8 673
c1bae0aa
FB
674 # The first rule filters Unsure mails
675 if mlist.header_filter_rules[0][0] == 'X-Spam-Flag: Unsure, tests=bogofilter':
676 unsurelevel = 1
677 filterbase = 1
c638d8c8 678
c1bae0aa
FB
679 # Check the other rules:
680 # - we have 2 rules: this is level 2 (drop > 0.999999, moderate Yes)
681 # - we have only one rule with HOLD directive : this is level 1 (moderate spams)
682 # - we have only one rule with DISCARD directive : this is level 3 (drop spams)
683 try:
684 action = mlist.header_filter_rules[filterbase + 1][1]
685 filterlevel = 2
0337d704 686 except:
c1bae0aa
FB
687 action = mlist.header_filter_rules[filterbase][1]
688 if action == mm_cfg.HOLD:
689 filterlevel = 1
690 elif action == mm_cfg.DISCARD:
691 filterlevel = 3
692 return (filterlevel << 1) + unsurelevel
0337d704 693
d36b2def 694def set_bogo_level(userdesc, perms, mlist, level):
c1bae0aa
FB
695 """ Set filter to the specified level.
696 @mlist
697 @edit
698 @admin
699 """
700 hfr = []
701
702 # The level is a combination of a spam filtering level and unsure filtering level
703 # - the unsure filtering level is only 1 bit (1 = HOLD unsures, 0 = Accept unsures)
704 # - the spam filtering level is a number growing with filtering strength
705 # (0 = no filtering, 1 = moderate spam, 2 = drop 0.999999 and moderate others, 3 = drop spams)
706 bogolevel = int(level)
707 filterlevel = bogolevel >> 1
708 unsurelevel = bogolevel & 1
709
710 # Set up unusre filtering
711 if unsurelevel == 1:
712 hfr.append(('X-Spam-Flag: Unsure, tests=bogofilter', mm_cfg.HOLD, False))
713
714 # Set up spam filtering
715 if filterlevel is 1:
716 hfr.append(('X-Spam-Flag: Yes, tests=bogofilter', mm_cfg.HOLD, False))
717 elif filterlevel is 2:
718 hfr.append(('X-Spam-Flag: Yes, tests=bogofilter, spamicity=(0\.999999|1\.000000)', mm_cfg.DISCARD, False))
719 hfr.append(('X-Spam-Flag: Yes, tests=bogofilter', mm_cfg.HOLD, False))
720 elif filterlevel is 3:
721 hfr.append(('X-Spam-Flag: Yes, tests=bogofilter', mm_cfg.DISCARD, False))
722
723 # save configuration
724 if mlist.header_filter_rules != hfr:
725 mlist.header_filter_rules = hfr
726 return 1
0337d704 727
728#-------------------------------------------------------------------------------
729# admin procedures [ soptions.php ]
730#
731
732admin_opts = [ 'advertised', 'archive', \
733 'max_message_size', 'msg_footer', 'msg_header']
734
c1bae0aa
FB
735def get_admin_options(userdesc, perms, mlist):
736 """ Get administrator options.
737 @mlist
738 @root
739 """
740 return get_options(userdesc, perms, mlist, admin_opts)
0337d704 741
c1bae0aa
FB
742def set_admin_options(userdesc, perms, mlist, values):
743 """ Set administrator options.
744 @mlist
745 @edit
746 @root
747 """
748 return set_options(userdesc, perms, mlist, admin_opts, values)
0337d704 749
750#-------------------------------------------------------------------------------
751# admin procedures [ check.php ]
752#
753
754check_opts = {
755 'acceptable_aliases' : '',
756 'admin_immed_notify' : True,
757 'administrivia' : True,
758 'anonymous_list' : False,
759 'autorespond_admin' : False,
760 'autorespond_postings' : False,
761 'autorespond_requests' : False,
762 'available_languages' : ['fr'],
763 'ban_list' : [],
764 'bounce_matching_headers' : '',
765 'bounce_processing' : False,
766 'convert_html_to_plaintext' : False,
767 'digestable' : False,
768 'digest_is_default' : False,
769 'discard_these_nonmembers' : [],
770 'emergency' : False,
771 'encode_ascii_prefixes' : 2,
772 'filter_content' : False,
773 'first_strip_reply_to' : False,
774 'forward_auto_discards' : True,
775 'hold_these_nonmembers' : [],
776 'host_name' : 'listes.polytechnique.org',
777 'include_list_post_header' : False,
778 'include_rfc2369_headers' : False,
779 'max_num_recipients' : 0,
780 'new_member_options' : 256,
781 'nondigestable' : True,
782 'obscure_addresses' : True,
783 'preferred_language' : 'fr',
784 'reject_these_nonmembers' : [],
785 'reply_goes_to_list' : 0,
786 'reply_to_address' : '',
787 'require_explicit_destination' : False,
788 'send_reminders' : 0,
789 'send_welcome_msg' : True,
790 'topics_enabled' : False,
791 'umbrella_list' : False,
792 'unsubscribe_policy' : 0,
793}
794
c1bae0aa
FB
795def check_options_runner(userdesc, perms, mlist, listname, correct):
796 options = { }
797 for (k, v) in check_opts.iteritems():
798 if mlist.__dict__[k] != v:
799 options[k] = v, mlist.__dict__[k]
800 if correct: mlist.__dict__[k] = v
801 if mlist.real_name.lower() != listname:
802 options['real_name'] = listname, mlist.real_name
803 if correct: mlist.real_name = listname
95bb095d 804 return 1
c1bae0aa
FB
805
806
ae6c293b 807def check_options(userdesc, perms, vhost, listname, correct=False):
c1bae0aa
FB
808 """ Check the list.
809 @root
810 """
88f7a3f1 811 listname = listname.lower()
c1bae0aa
FB
812 mlist = MailList.MailList(vhost + VHOST_SEP + listname, lock=0)
813 if correct:
814 return list_call_locked(check_options_runner, userdesc, perms, mlist, True, listname, True)
815 else:
816 return check_options_runner(userdesc, perms, mlist, listname, False)
0337d704 817
818#-------------------------------------------------------------------------------
819# super-admin procedures
820#
821
ae6c293b 822def get_all_lists(userdesc, perms, vhost):
c1bae0aa 823 """ Get all the list for the given vhost
d1c13dbf 824 @root
c1bae0aa 825 """
0337d704 826 prefix = vhost.lower()+VHOST_SEP
827 names = Utils.list_names()
828 names.sort()
829 result = []
830 for name in names:
831 if not name.startswith(prefix):
832 continue
ae6c293b 833 result.append(name.replace(prefix, ''))
0337d704 834 return result
835
d1c13dbf
FB
836def get_all_user_lists(userdesc, perms, vhost, email):
837 """ Get all the lists for the given user
838 @root
839 """
840 names = Utils.list_names()
841 names.sort()
842 result = []
843 for name in names:
844 try:
845 mlist = MailList.MailList(name, lock=0)
846 ismember = email in mlist.getRegularMemberKeys()
847 isowner = email in mlist.owner
848 if not ismember and not isowner:
849 continue
850 host = mlist.internal_name().split(VHOST_SEP)[0].lower()
851 result.append({ 'list': mlist.real_name,
852 'addr': mlist.real_name.lower() + '@' + host,
853 'host': host,
854 'own' : isowner,
855 'sub' : ismember
856 })
857 except Exception, e:
858 continue
859 return result
860
a8a11a70
FB
861def change_user_email(userdesc, perms, vhost, from_email, to_email):
862 """ Change the email of a user
863 @root
864 """
865 from_email = from_email.lower()
866 to_email = to_email.lower()
867 for list in Utils.list_names():
868 try:
869 mlist = MailList.MailList(list, lock=0)
870 except:
871 continue
872 try:
873 mlist.Lock()
874 mlist.ApprovedChangeMemberAddress(from_email, to_email, 0)
875 mlist.Save()
876 mlist.Unlock()
877 except:
878 mlist.Unlock()
879 return 1
880
881
ae6c293b 882def create_list(userdesc, perms, vhost, listname, desc, advertise, modlevel, inslevel, owners, members):
c1bae0aa
FB
883 """ Create a new list.
884 @root
885 """
886 name = vhost.lower() + VHOST_SEP + listname.lower();
0337d704 887 if Utils.list_exists(name):
42567b67 888 print >> sys.stderr, "List ", name, " already exists"
0337d704 889 return 0
ae6c293b 890
0337d704 891 owner = []
892 for o in owners:
42567b67
FB
893 email = to_forlife(o)
894 print >> sys.stderr, "owner in list", o, email
895 email = email[0]
0337d704 896 if email is not None:
897 owner.append(email)
898 if len(owner) is 0:
42567b67 899 print >> sys.stderr, "No owner found in ", owners
0337d704 900 return 0
901
902 mlist = MailList.MailList()
903 try:
904 oldmask = os.umask(002)
905 pw = sha.new('foobar').hexdigest()
ae6c293b 906
0337d704 907 try:
908 mlist.Create(name, owner[0], pw)
909 finally:
910 os.umask(oldmask)
911
912 mlist.real_name = listname
913 mlist.host_name = 'listes.polytechnique.org'
914 mlist.description = desc
915
916 mlist.advertised = int(advertise) is 0
917 mlist.default_member_moderation = int(modlevel) is 2
918 mlist.generic_nonmember_action = int(modlevel) > 0
919 mlist.subscribe_policy = 2 * (int(inslevel) is 1)
920 mlist.admin_notify_mchanges = (mlist.subscribe_policy or mlist.generic_nonmember_action or mlist.default_member_moderation or not mlist.advertised)
ae6c293b 921
0337d704 922 mlist.owner = owner
ae6c293b 923
0337d704 924 mlist.subject_prefix = '['+listname+'] '
925 mlist.max_message_size = 0
926
e480db84 927 inverted_listname = listname.lower() + '_' + vhost.lower()
0337d704 928 mlist.msg_footer = "_______________________________________________\n" \
31f2df6a 929 + "Liste de diffusion %(real_name)s\n" \
930 + "http://listes.polytechnique.org/members/" + inverted_listname
ae6c293b 931
0337d704 932 mlist.header_filter_rules = []
c638d8c8 933 mlist.header_filter_rules.append(('X-Spam-Flag: Unsure, tests=bogofilter', mm_cfg.HOLD, False))
0337d704 934 mlist.header_filter_rules.append(('X-Spam-Flag: Yes, tests=bogofilter', mm_cfg.HOLD, False))
0337d704 935
98c79ede 936 if ON_CREATE_CMD != '':
937 try: os.system(ON_CREATE_CMD + ' ' + name)
938 except: pass
939
95bb095d 940 check_options_runner(userdesc, perms, mlist, listname.lower(), True)
c1bae0aa 941 mass_subscribe(userdesc, perms, mlist, members)
0337d704 942 mlist.Save()
c1bae0aa
FB
943 finally:
944 mlist.Unlock()
95bb095d
FB
945
946 # avoid the "-1 mail to moderate" bug
947 mlist = MailList.MailList(name)
57b04c90
FB
948 try:
949 mlist._UpdateRecords()
950 mlist.Save()
951 finally:
952 mlist.Unlock()
95bb095d 953 return 1
c1bae0aa
FB
954
955def delete_list(userdesc, perms, mlist, del_archives=0):
956 """ Delete the list.
957 @mlist
958 @admin
959 """
960 lname = mlist.internal_name()
961 # remove the list
962 REMOVABLES = [ os.path.join('lists', lname), ]
963 # remove stalled locks
964 for filename in os.listdir(mm_cfg.LOCK_DIR):
965 fn_lname = filename.split('.')[0]
966 if fn_lname == lname:
967 REMOVABLES.append(os.path.join(mm_cfg.LOCK_DIR, filename))
968 # remove archives ?
969 if del_archives:
970 REMOVABLES.extend([
971 os.path.join('archives', 'private', lname),
972 os.path.join('archives', 'private', lname+'.mbox'),
973 os.path.join('archives', 'public', lname),
974 os.path.join('archives', 'public', lname+'.mbox')
975 ])
976 map(lambda dir: remove_it(lname, os.path.join(mm_cfg.VAR_PREFIX, dir)), REMOVABLES)
977 return 1
0337d704 978
ae6c293b 979def kill(userdesc, perms, vhost, alias, del_from_promo):
c1bae0aa
FB
980 """ Remove a user from all the lists.
981 """
0337d704 982 exclude = []
983 if not del_from_promo:
c1bae0aa 984 exclude.append(PLATAL_DOMAIN + VHOST_SEP + 'promo' + alias[-4:])
0337d704 985 for list in Utils.list_names():
c1bae0aa
FB
986 if list in exclude:
987 continue
0337d704 988 try:
ae6c293b 989 mlist = MailList.MailList(list, lock=0)
0337d704 990 except:
991 continue
992 try:
993 mlist.Lock()
ae6c293b 994 mlist.ApprovedDeleteMember(alias+'@'+PLATAL_DOMAIN, None, 0, 0)
0337d704 995 mlist.Save()
996 mlist.Unlock()
997 except:
998 mlist.Unlock()
999 return 1
1000
1001
1002#-------------------------------------------------------------------------------
1003# server
1004#
1005class FastXMLRPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer):
ae6c293b 1006 allow_reuse_address = True
0337d704 1007
1008################################################################################
1009#
ae6c293b 1010# INIT
0337d704 1011#
1012#-------------------------------------------------------------------------------
1013# use Mailman user and group (not root)
1014# fork in background if asked to
1015#
1016
1017uid = getpwnam(mm_cfg.MAILMAN_USER)[2]
1018gid = getgrnam(mm_cfg.MAILMAN_GROUP)[2]
1019
1020if not os.getuid():
ae6c293b 1021 os.setregid(gid, gid)
1022 os.setreuid(uid, uid)
0337d704 1023
1024signal.signal(signal.SIGHUP, signal.SIG_IGN)
1025
1026if ( os.getuid() is not uid ) or ( os.getgid() is not gid):
1027 sys.exit(0)
1028
1029opts, args = getopt.getopt(sys.argv[1:], 'f')
1030for o, a in opts:
1031 if o == '-f' and os.fork():
1032 sys.exit(0)
1033
1034i18n.set_language('fr')
1035mysql = connectDB()
1036lock = Lock()
1037
1038#-------------------------------------------------------------------------------
1039# server
1040#
1fec3393 1041server = FastXMLRPCServer((SRV_HOST, SRV_PORT), BasicAuthXMLRPCRequestHandler)
0337d704 1042
1043# index.php
1044server.register_function(get_lists)
1045server.register_function(subscribe)
1046server.register_function(unsubscribe)
1047# members.php
1048server.register_function(get_members)
1049# trombi.php
1050server.register_function(get_members_limit)
1051server.register_function(get_owners)
1052# admin.php
c4d57bd8 1053server.register_function(replace_email)
0337d704 1054server.register_function(mass_subscribe)
1055server.register_function(mass_unsubscribe)
1056server.register_function(add_owner)
1057server.register_function(del_owner)
1058# moderate.php
1059server.register_function(get_pending_ops)
1060server.register_function(handle_request)
4b0d9ef3 1061server.register_function(get_pending_sub)
0337d704 1062server.register_function(get_pending_mail)
1063# options.php
1064server.register_function(get_owner_options)
1065server.register_function(set_owner_options)
1066server.register_function(add_to_wl)
1067server.register_function(del_from_wl)
1068server.register_function(get_bogo_level)
1069server.register_function(set_bogo_level)
1070# soptions.php
1071server.register_function(get_admin_options)
1072server.register_function(set_admin_options)
1073# check.php
1074server.register_function(check_options)
1075# create + del
1076server.register_function(get_all_lists)
d1c13dbf 1077server.register_function(get_all_user_lists)
a8a11a70 1078server.register_function(change_user_email)
0337d704 1079server.register_function(create_list)
1080server.register_function(delete_list)
1081# utilisateurs.php
1082server.register_function(kill)
1083
1084server.serve_forever()
1085
c638d8c8 1086# vim:set et sw=4 sts=4 sws=4: