importing dailyzen into git
[dailyzen.git] / do.cgi
blobe2ed204f87952f83838411dc9bfa867c09e331ff
1 #!/usr/bin/env python2.4
2 # -*- mode: python -*-
4 import os, logging
5 import cgi, cgitb
6 cgitb.enable()
8 ## cgi header
9 print "Content-type: text/html"
10 print
12 from dispatch_email import *
14 cgilog = logging.getLogger("cgi")
15 cgilog.addHandler(logging_handler())
16 cgilog.setLevel(logging.DEBUG)
18 PATH_INFO = os.environ.get("PATH_INFO", "")
19 cgilog.debug("Request %s", PATH_INFO)
21 def validate_email(email):
22     """validate email address
23     - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65215
24     """
25     EMAIL_RE = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
26     if not(len(email) > 7 and re.match(EMAIL_RE, email)):
27         raise UserError, "Not a valid email address"
29 def page_done(msg):
30     print "<title>dailyzen</title>"
31     print '<link rel="stylesheet" type="text/css" href="http://nearfar.org/paste.css" />'
32     print "<body>"
33     print "<h3 style='text-align: center'>%s</h3>" % msg
34     print "<p style='text-align: center'>Return to <a href='%s'>dailyzen</></p>" % WWW_URL
36 try:
37     if PATH_INFO == "/subscribe":
38         form = cgi.FieldStorage()
39         try:
40             email = form["email"].value
41         except KeyError:
42             email = ""
43         email = email.strip()
45         # silly clickers
46         if email.endswith("@example.com"):
47             raise UserError, "I need YOUR email"
49         validate_email(email)
50         register_user(email)
51         page_done("To verify your email address, please follow the link sent to you by email.")
53     elif PATH_INFO.startswith("/verify"):
54         args = PATH_INFO[ len("/verify/") : ]
55         email, id = args.split(",")
56         validate_user(email, id)
57         page_done("You are now subscribed. Welcome. :)")
59     elif PATH_INFO.startswith("/unsubscribe"):
60         args = PATH_INFO[ len("/unsubscribe/") : ]
61         email, id = args.split(",")
62         delete_user(email, id)
63         page_done("You are unsubscribed. See ya again.")
65     elif PATH_INFO == "/send_to_all":
66         user_agent = os.environ.get("HTTP_USER_AGENT")
67         cgilog.info("/send_to_all requested by agent: [%s]", user_agent)
68         if user_agent.startswith("WEBCRON"):
69             dispatch_all()
70             print "Ok"
71         else:
72             cgilog.warn("/send_to_all NOT requested by webcron, but [%s]", user_agent)
73             print "Pass"
74         
75     else:
76         raise UserError, "Invalid request: %s" % PATH_INFO
78 except UserError, e:
79     cgilog.error("UserError: %s", e)
80     page_done("Error: %s" % e)