#!/usr/bin/env python import sys sys.path += ['/usr/lib/mailman'] from web import template, form, application, ctx, wsgi from socket import gethostbyaddr, gethostbyname import re import Mailman.Utils valid_ips = [ r'^160\.0?45\.', r'^130\.133\.', r'^87\.77\.', r'^127\.0{1,3}\.', ] 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 ('--fastcgi' in sys.argv): wsgi.runwsgi = lambda func, addr=None: wsgi.runfcgi(func, addr) app.run()