From 661617e90d01c6dd6f62b320c40e00712f1123c0 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 27 Oct 2008 18:55:10 +0100 Subject: check for valid fu-berlin.de email address --- index.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/index.py b/index.py index f6e08e1..ebb4c60 100644 --- a/index.py +++ b/index.py @@ -109,11 +109,57 @@ def set_random_password(req, user_id, length): return None +# Ditch nonsense email addresses. +# taken from http://commandline.org.uk/python/2008/may/3/email-syntax-check/ + +GENERIC_DOMAINS = "aero", "asia", "biz", "cat", "com", "coop", \ + "edu", "gov", "info", "int", "jobs", "mil", "mobi", "museum", \ + "name", "net", "org", "pro", "tel", "travel" + +def invalid(emailaddress, domains = GENERIC_DOMAINS): + """Checks for a syntactically invalid email address.""" + + # Email address must be 7 characters in total. + if len(emailaddress) < 7: + return True # Address too short. + + # Split up email address into parts. + try: + localpart, domainname = emailaddress.rsplit('@', 1) + host, toplevel = domainname.rsplit('.', 1) + except ValueError: + return True # Address does not have enough parts. + + # Check for Country code or Generic Domain. + if len(toplevel) != 2 and toplevel not in domains: + return True # Not a domain name. + + for i in '-_.%+.': + localpart = localpart.replace(i, "") + for i in '-_.': + host = host.replace(i, "") + + if localpart.isalnum() and host.isalnum(): + return False # Email address is fine. + else: + return True # Email address has funny characters. + + def validate_and_register(req, login, email): # check for invalid entries + + # empty login + if login == "": + return "please enter a username"; + + # no fu-berlin.de address if not email.endswith('.fu-berlin.de'): return "you did not give a  *.fu-berlin.de address" + # regex checker for valid email + if invalid(email): + return "please enter a valid email address"; + req.cursor.execute("SELECT login FROM user WHERE login = %s", (login, )) if req.cursor.fetchone(): return "username already taken, please choose another one" -- cgit v1.2.3