Merge remote branch 'origin/platal-1.0.0'
[platal.git] / bin / lists.rpc.py
index e324f54..9ed42d8 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #***************************************************************************
-#*  Copyright (C) 2004-2008 polytechnique.org                              *
+#*  Copyright (C) 2003-2010 Polytechnique.org                              *
 #*  http://opensource.polytechnique.org/                                   *
 #*                                                                         *
 #*  This program is free software; you can redistribute it and/or modify   *
@@ -67,6 +67,7 @@ def get_config(sec, val, default=None):
 
 MYSQL_USER     = get_config('Core', 'dbuser')
 MYSQL_PASS     = get_config('Core', 'dbpwd')
+MYSQL_DB       = get_config('Core', 'dbdb')
 
 PLATAL_DOMAIN  = get_config('Mail', 'domain')
 PLATAL_DOMAIN2 = get_config('Mail', 'domain2', '')
@@ -104,9 +105,10 @@ class BasicAuthXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
         except:
             raise Exception('method "%s" is not supported' % method)
 
+    def is_rpc_path_valid(self):
+        return True
 
     def _dispatch(self, method, params):
-        new_params = list(params)
         return list_call_dispatcher(self._get_function(method), self.data[0], self.data[1], self.data[2], *params)
 
     def do_POST(self):
@@ -124,19 +126,22 @@ class BasicAuthXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
             self.end_headers()
 
     def getUser(self, uid, md5, vhost):
-        res = mysql_fetchone ("""SELECT  CONCAT(u.prenom, ' ', u.nom), a.alias, u.perms
-                                   FROM  auth_user_md5 AS u
-                             INNER JOIN  aliases       AS a ON ( a.id=u.user_id AND a.type='a_vie' )
-                                  WHERE  u.user_id = '%s' AND u.password = '%s' AND u.perms IN ('admin', 'user')
-                                  LIMIT  1""" %( uid, md5 ) )
+        res = mysql_fetchone ("""SELECT  a.full_name, aa.alias, IF (a.is_admin, 'admin', NULL)
+                                   FROM  accounts AS a
+                             INNER JOIN  aliases  AS aa ON (a.uid = aa.uid AND aa.type = 'a_vie')
+                                  WHERE  a.uid = '%s' AND a.password = '%s' AND a.state = 'active'
+                                  LIMIT  1""" \
+                              % (uid, md5))
         if res:
             name, forlife, perms = res
             if vhost != PLATAL_DOMAIN:
-                res = mysql_fetchone ("""SELECT  uid
-                                          FROM  groupex.membres AS m
-                                    INNER JOIN  groupex.asso    AS a ON (m.asso_id = a.id)
-                                         WHERE  perms='admin' AND uid='%s' AND mail_domain='%s'""" %( uid , vhost ) )
-                if res: perms= 'admin'
+                res = mysql_fetchone ("""SELECT  m.uid
+                                           FROM  group_members AS m
+                                     INNER JOIN  groups        AS g ON (m.asso_id = g.id)
+                                          WHERE  perms = 'admin' AND uid = '%s' AND mail_domain = '%s'""" \
+                                      % (uid, vhost))
+                if res:
+                    perms= 'admin'
             userdesc = UserDesc(forlife+'@'+PLATAL_DOMAIN, name, None, 0)
             return (userdesc, perms, vhost)
         else:
@@ -152,7 +157,7 @@ class BasicAuthXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
 
 def connectDB():
     db = MySQLdb.connect(
-            db='x4dat',
+            db=MYSQL_DB,
             user=MYSQL_USER,
             passwd=MYSQL_PASS,
             unix_socket='/var/run/mysqld/mysqld.sock')
@@ -189,12 +194,14 @@ def to_forlife(email):
         mbox = email
         fqdn = PLATAL_DOMAIN
     if ( fqdn == PLATAL_DOMAIN ) or ( fqdn == PLATAL_DOMAIN2 ):
-        res = mysql_fetchone("""SELECT  CONCAT(f.alias, '@%s'), CONCAT(u.prenom, ' ', u.nom)
-                                  FROM  auth_user_md5 AS u
-                            INNER JOIN  aliases       AS f ON (f.id=u.user_id AND f.type='a_vie')
-                            INNER JOIN  aliases       AS a ON (a.id=u.user_id AND a.alias='%s' AND a.type!='homonyme')
-                                 WHERE  u.perms IN ('admin', 'user')
-                                 LIMIT  1""" %( PLATAL_DOMAIN, mbox ) )
+        res = mysql_fetchone("""SELECT  CONCAT(f.alias, '@%s'), a.full_name
+                                  FROM  accounts AS a
+                            INNER JOIN  aliases  AS f  ON (f.uid = a.uid AND f.type = 'a_vie')
+                            INNER JOIN  aliases  AS aa ON (aa.uid = a.uid AND aa.alias = '%s'
+                                                           AND a.type != 'homonyme')
+                                 WHERE  a.state = 'active'
+                                 LIMIT  1""" \
+                              % (PLATAL_DOMAIN, mbox))
         if res:
             return res
         else:
@@ -246,6 +253,7 @@ def list_call_dispatcher(method, userdesc, perms, vhost, *arg):
         else:
             return method(userdesc, perms, vhost, *arg)
     except Exception, e:
+        sys.stderr.write('Exception in dispatcher %s\n' % str(e))
         raise e
         return 0
 
@@ -259,7 +267,8 @@ def list_call_locked(method, userdesc, perms, mlist, edit, *arg):
             mlist.Save()
         mlist.Unlock()
         return ret
-    except:
+    except Exception, e:
+        sys.stderr.write('Exception in locked call %s: %s\n' % (method.__name__, str(e)))
         mlist.Unlock()
         return 0
     # TODO: use finally when switching to python 2.5
@@ -316,12 +325,7 @@ def get_options(userdesc, perms, mlist, opts):
     details = get_list_info(userdesc, perms, mlist)[0]
     return (details, options)
 
-def set_options(userdesc, perms, mlist, vals):
-    """ Set the options of a list.
-            @mlist
-            @edit
-            @admin
-    """
+def set_options(userdesc, perms, mlist, opts, vals):
     for (k, v) in vals.iteritems():
         if k not in opts:
             continue
@@ -450,7 +454,6 @@ def mass_subscribe(userdesc, perms, mlist, users):
     """
     members = mlist.getRegularMemberKeys()
     added = []
-    mlist.Lock()
     for user in users:
         email, name = to_forlife(user)
         if ( email is None ) or ( email in members ):
@@ -676,7 +679,7 @@ def get_bogo_level(userdesc, perms, mlist):
             filterlevel = 3
     return (filterlevel << 1) + unsurelevel
 
-def set_bogo_level(userdesc, perms, vhost, listname, level):
+def set_bogo_level(userdesc, perms, mlist, level):
     """ Set filter to the specified level.
             @mlist
             @edit
@@ -786,8 +789,7 @@ def check_options_runner(userdesc, perms, mlist, listname, correct):
     if mlist.real_name.lower() != listname:
         options['real_name'] = listname, mlist.real_name
         if correct: mlist.real_name = listname
-    details = get_list_info(userdesc, perms, mlist)[0]
-    return (details, options)
+    return 1
 
 
 def check_options(userdesc, perms, vhost, listname, correct=False):
@@ -867,25 +869,25 @@ def create_list(userdesc, perms, vhost, listname, desc, advertise, modlevel, ins
         mlist.header_filter_rules = []
         mlist.header_filter_rules.append(('X-Spam-Flag: Unsure, tests=bogofilter', mm_cfg.HOLD, False))
         mlist.header_filter_rules.append(('X-Spam-Flag: Yes, tests=bogofilter', mm_cfg.HOLD, False))
-        mlist.Save()
-        mlist.Unlock()
 
         if ON_CREATE_CMD != '':
             try:    os.system(ON_CREATE_CMD + ' ' + name)
             except: pass
 
-        check_options(userdesc, perms, mlist, True)
+        check_options_runner(userdesc, perms, mlist, listname.lower(), True)
         mass_subscribe(userdesc, perms, mlist, members)
+        mlist.Save()
+    finally:
+        mlist.Unlock()
 
-        # avoid the "-1 mail to moderate" bug
-        mlist = MailList.MailList(name)
+    # avoid the "-1 mail to moderate" bug
+    mlist = MailList.MailList(name)
+    try:
         mlist._UpdateRecords()
         mlist.Save()
-
-        return 1
     finally:
         mlist.Unlock()
-    return 0
+    return 1
 
 def delete_list(userdesc, perms, mlist, del_archives=0):
     """ Delete the list.