summaryrefslogtreecommitdiffstats
path: root/index.py
blob: c0a38f94a5af6fbf1bc70b70dec06883c22d970e (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python

import sys
sys.path += ['/usr/lib/mailman']

from web import template, form, application, ctx, wsgi, config
from socket import gethostbyaddr, gethostbyname
import re
import Mailman.Utils

valid_ips = [
    r'^160\.0?45\.',
    r'^130\.133\.',
    r'^87\.77\.',
    r'^2001:6f8:1c3c:67f0:',
    ]

reserved_names = [
    r'-admin$',
    r'-join$',
    r'-leave$',
    r'-owner$',
    r'-request$',
    ]

proxy = gethostbyname('squid.fu-berlin.de')

urls = (
    r'/', 'index',
    r'/neu.*', 'new',
    r'/new.*', 'new',
    r'/help/spam.*', 'spam',
    r'/help/unsubscribe\.([a-z]+).*', 'unsubscribe',
)

render = template.render('templates/', base='layout');
app = application(urls, globals(), autoreload=False)

def validate_name(name):
    for regex in reserved_names:
        if re.search(regex, name):
            return False
    return True

create_form = form.Form(
    form.Textbox('name',
                 form.notnull,
                 form.regexp(r'^[a-zA-Z0-9+.-]{2,}$', 'This name should least be two characters long ' +
                             'and may only consist of letters, digits, plus (+), minus (-), and underlines (_).'),
                 form.Validator('This name ends with a reserved suffix. Please choose another name.',
                                validate_name),
                 form.Validator('A list with this name allready exists. Please choose another name.',
                                lambda name: Mailman.Utils.list_exists(name) == False),
                 description = 'Name of your list'),
    form.Textbox('email',
                 form.notnull,
                 form.regexp(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\b', 'This should be a valid email address.'),
                 form.regexp(r'.*fu-berlin\.de', 'E-mail address should end with fu-berlin.de'),
                 description = 'Your E-mail address'),
    )

class index:
    def GET(self):
        return render.index()

class spam:
    def GET(self):
        return render.spam()

class unsubscribe:
    def GET(self, lang):
        if (lang.startswith('de')):
            return render.unsubscribe_de()
        else:
            return render.unsubscribe_en()

class new:
    def validate(self, ip):
        if ip == proxy:
            return False

        for regex in valid_ips:
            if re.match(regex, ip):
                return True

        return False

    def get_user_information(self):
        return {'ip': ctx.ip,
                'host': gethostbyaddr(ctx.ip)[0],
                'valid': self.validate(ctx.ip)}

    def GET(self):
        form = create_form()
        user = self.get_user_information()
        return render.new(form, user)

    def POST(self):
        form = create_form()
        user = self.get_user_information()

        if not form.validates():
            return render.new(form, user)
        else:
            # create list
            return "Done."


if __name__ == "__main__":
    if not '--debug' in sys.argv:
        config.debug = False

    if '--fastcgi' in sys.argv:
        wsgi.runwsgi = lambda func, addr=None: wsgi.runfcgi(func, addr)

    app.run()