summaryrefslogtreecommitdiffstats
path: root/util.py
blob: e43de0142f0364c8d33ecc6b4aec8b2015949323 (plain)
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
# -*- 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))