# -*- coding: utf-8 -*- import config import web, re, time, subprocess, os from random import Random from Mailman import MailList, Errors, UserDesc rng = Random() righthand = '23456qwertasdfgzxcvbQWERTASDFGZXCVB' lefthand = '789yuiophjknmYUIPHJKLNM' allchars = righthand + lefthand def generate_char(i, alternate_hands): if not alternate_hands: return rng.choice(allchars) else: if i%2: return rng.choice(lefthand) else: return rng.choice(righthand) def generate_password(length = 8, alternate_hands = True): return ''.join([generate_char(i, alternate_hands) for i in xrange(length)]) def validate_listname(name): for regex in config.reserved_names: if re.search(regex, name): return False return True def create_list(listname, listadmin, passwd=None): if passwd == None: passwd = generate_password() try: # create list p = subprocess.Popen(['sudo', '-n', '-u', config.mailman_user, os.path.join(config.mailman_path, 'contrib', 'neueliste.bash'), listname, listadmin, passwd], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) (progress, junk) = p.communicate() return (p.returncode == 0, progress) except Exception as e: progress = str(e) return (False, progress) def notify_admins(listname, listadmin, success, progress): # send mail to admins web.sendmail('mailman@list.spline.inf.fu-berlin.de', config.admin_emails, u'Mailingliste "%s" für %s %serzeugt' % (listname, listadmin, 'NICHT ' if not success else ''), u'''Die Mailingliste "%s" wurde am %s für %s von %s aus %serzeugt. Log: %s''' % (listname, time.strftime('%Y-%m-%d um %H:%M:%S', time.gmtime()), listadmin, web.ctx.ip, 'NICHT ' if not success else '', progress) ) def add_to_list(listname, listadmin): try: mlist = MailList.MailList(listname) except Errors.MMUnknownListError: return 'Could not add %s to %s: List not found.' % (listadmin, listname) userdesc = UserDesc.UserDesc(address = listadmin, fullname = '', digest = 0) try: mlist.ApprovedAddMember(userdesc, 0, 0) success = True progress = 'Successfully added %s to %s list.' except Errors.MMAlreadyAMember: success = True progress = '%s was already member of %s list.' except Errors.MembershipIsBanned: success = False progress = 'Could not add %s to %s: Address ist banned.' except Errors.MMBadEmailError: success = False progress = 'Could not add %s to %s: Bad email address.' except Errors.MMHostileAddress: success = False progress = 'Could not add %s to %s: Illegal characters in email address.' finally: mlist.Unlock() return (success, progress % (listadmin, listname))