#!/usr/bin/env python # -*- coding: utf-8 -*- # own modules import config, util import sys, os, re, socket, web from web import form from Mailman.Utils import list_exists urls = ( r'/', 'index', r'/neu.*', 'new', r'/new.*', 'new', r'/help/spam.*', 'spam', r'/help/unsubscribe\.([a-z]+).*', 'unsubscribe', ) render = web.template.render('templates/', base='layout'); app = web.application(urls, globals(), autoreload=False) 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.', util.validate_listname), form.Validator('A list with this name already exists. Please choose another name.', lambda name: 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): running = not os.path.exists('./offline') return render.index(running) 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): for regex in config.invalid_ips: if re.match(regex, ip): return False for regex in config.valid_ips: if re.match(regex, ip): return True return False def get_user_information(self): try: host = socket.gethostbyaddr(web.ctx.ip)[0] except: host = web.ctx.ip return {'ip': web.ctx.ip, 'host': host, 'valid': self.validate(web.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: listname = form.d.name.lower() listadmin = form.d.email (success, progress) = util.create_list(listname, listadmin) util.notify_admins(listname, listadmin, success, progress) if success and config.listadmins_list: (is_member, message) = util.add_to_list(config.listadmins_list, listadmin) progress += '\n' + message return render.progress(listname, not success, progress) if __name__ == "__main__": if '--debug' in sys.argv: sys.argv.remove('--debug') else: web.config.debug = False if '--fastcgi' in sys.argv: web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr) sys.argv.remove('--fastcgi') app.run()