#!/usr/bin/python import cgi import os import sys import string import re import time import urllib import httplib sys.stderr = open('/home/crew/tbryan/Web/perceps_comments_traceback','a') import traceback from shutil import copyfile sys.path.append('/home/crew/tbryan/Web/') import cgiForm _time = time.asctime(time.gmtime(time.time())) _commentFile = "/home/crew/tbryan/Web/perceps_comments.txt" _commentPage = "/home/crew/tbryan/Web/perceps_comments.html" _commentURL = "perceps_comments.cgi" _footer = """
Return to PERCEPS home page

Last modified %s

""" % _time _doboth = 'Submit a comment and subscribe to the list' _docomment = "Submit a comment, but don't subscribe to the list" _dosubscribe = "Subscribe to the list, but don't submit a comment" _subscribeURL = "http://starship.python.net/mailman/subscribe/perceps" _nameLen = 30 _emailLen = 30 _affilLen = 50 _commentLen = 2048 # The longest comment on the PERCEPS page in two years is 1227 characters. _pwLen = 15 cgiForm._FORM = """Content-type: text/html PERCEPS comments and mailinglist submission

Submit a comment and/or join the mailinglist

You can use the form below to submit comments to the new PERCEPS user comments page. You can also use it to join the PERCEPS mailinglist. The mailinglist should be a low traffic list where I announce new releases and patches. Subscribers can also submit bug reports/fixes directly to the list. (This policy may change in the future, depending upon how much traffic the list gets.) If you subscribe to the list, you will be sent a e-mail message requesting confirmation (to prevent others from subscribing you without your knowledge).

I worked hard to make this script robust, but if you get an Internal Server Error or another error screen, please send me e-mail explaining what happened so that I can fix it.

Action:
Name:
E-mail address:
Company/Affiliation:
Comments:
For those subscibing to the list only: you must enter enter a privacy password. This provides only mild security, but should prevent others from messing with your subscription. Do not use valuable passwords! Once a month, your password will be emailed to you as a reminder. If you're not subscribing to the list, then these fields are ignored.
Mailinglist Password:
Confirm Password:

Last changed by Tom Bryan on 20 June 1999.
""" % (_doboth, _docomment, _dosubscribe, _nameLen, _emailLen, _affilLen, _pwLen, _pwLen) class Message (cgiForm.cgiForm): def __init__(self, form):#name=None, email=None, affil=None, comment=None): # Invoke the parent's constructor. cgiForm.cgiForm.__init__(self,form) # We wamt to make sure that the required info was submitted. # We also need to make sure that no one is trying to break in through # the script. If it looks like the script isn't being invoked through # our form, raise an exception. num_keys = 0 self.error = None self.submit_comment = 0 self.subscribe_list = 0 if self.has_key('action'): num_keys = num_keys + 1 if self['action'] == _doboth: self.submit_comment = 1 self.subscribe_list = 1 elif self['action'] == _docomment: self.submit_comment = 1 elif self['action'] == _dosubscribe: self.subscribe_list = 1 else: self.error = "Missing ACTION field." if self.has_key('name'): # they can enter fields longer than the boxes if len(self['name']) > 2 * _nameLen: self.error = "Field too long" num_keys = num_keys + 1 else: self.error = """Oops! Missing required field: you must provide both a Name and an e-mail address""" if self.has_key('email'): # they can enter fields longer than the boxes if len(self['email']) > 2 * _emailLen: self.error = "Field too long" num_keys = num_keys + 1 else: self.error = """Oops! Missing required field: you must provide both a Name and an e-mail address""" if self.has_key('affil'): if len(self['affil']) > _affilLen: self.error = "Field too long" num_keys = num_keys + 1 if self.has_key('comment'): num_keys = num_keys + 1 if len(self['comment']) > _commentLen: self.error = "Field too long" if self.has_key('pw'): num_keys = num_keys + 1 if len(self['pw']) > _pwLen: self.error = "Field too long" else: if self.subscribe_list: self.error = """Oops! Missing required field: if you are subscribing to the list, you must provide a password.""" if self.has_key('pw-conf'): num_keys = num_keys + 1 if len(self['pw-conf']) > _pwLen: self.error = "Field too long" else: if self.subscribe_list: self.error = """Oops! Missing required field: if you are subscribing to the list, you must provide a password.""" # Somebody's invoking the program from another form: baaaad user! if len(self.keys()) > num_keys: self.error = "Too many fields" def __str__(self): """Format the fields for inclusion on a comments page""" s = '%s
\n' % (escape(self['email']), escape(self['name'])) if self.has_key('affil'): s = s+'%s
\n' % escape(self['affil']) s = s + '%s
' % _time if self.has_key('comment'): s = s+'

\n%s\n
\n' % translate(self['comment']) return s def __repr__(self): s = "Message instance:\n" for el in self.keys(): s = s+'\t%s\t%s\n' % (el, self[el]) return s def addComment(self, file=_commentFile, page=_commentPage): """addComment(self, file) Add a comment to comment file "file" created by formatting the fields of self. """ comments = open(file, 'a+') comments.write(str(self)) comments.write('

\n') comments.close() copyfile(file, page) comments = open(page, 'a') comments.write(_footer) comments.close() def subscribeList(self,url=_subscribeURL): """subscribeList(self,url) This function subscribes self['email'] to the Mailman list with subscription script at the URL url with the password self['pw'] = self['pw-conf'].""" message = 'email='+urllib.quote(self['email'])+'&pw='+urllib.quote(self['pw'])+'&pw-conf='+urllib.quote(self['pw-conf'])+'&digest=0'+'&email-button=Subscribe' httpobj = httplib.HTTP('localhost',80) httpobj.putrequest('POST','/mailman/subscribe/perceps') httpobj.putheader('Accept', '*/*') httpobj.putheader('Connection', 'Keep-Alive') httpobj.putheader('Content-type', 'application/x-www-form-urlencoded') httpobj.putheader('Content-length', '%d' % len(message)) httpobj.endheaders() httpobj.send(message) reply, msg, hdrs = httpobj.getreply() if reply == 200: return 0 else: return -1 # stolen from faqwiz.py translate_prog = None def translate(text, pre=0): global translate_prog if not translate_prog: translate_prog = prog = re.compile( r'\b(http|ftp|https)://\S+(\b|/)|\b[-.\w]+@[-.\w]+') else: prog = translate_prog # We don't modify text; i is the index into the string # indicating how far we've processed. i = 0 # list holds a list of substrings that we've processed. list = [] while 1: # Look for URLs. m = prog.search(text, i) if not m: break # If we don't find any more, exit loop. j = m.start() list.append(escape(text[i:j])) i = j url = m.group(0) while url[-1] in '();:,.?\'"<>': url = url[:-1] # Advance the index to start processing again just after the URL. i = i + len(url) # If the URL has <, >, or &, the href below won't work, will it? url = escape(url) # create an href if not pre or (pre and PROCESS_PREFORMAT): if ':' in url: repl = '%s' % (url, url) else: repl = '%s' % (url, url) else: repl = url list.append(repl) j = len(text) list.append(escape(text[i:j])) # join the list of strings back into one string return string.join(list, '') # stolen from faqwiz def escape(s): s = cgi.escape(s,1) s = emphasize(s) return s # stolen and modified from faqwiz def emphasize(line): return re.sub(r'\*([a-zA-Z]+)\*', r'\1', line) if __name__ == '__main__': try: form = cgi.FieldStorage() except: print """Content-type: text/plain\n\n""" traceback.print_exc() if len(form.keys()) == 0: print cgiForm._FORM else: try: msg = Message(form) if msg.error: raise cgiForm.MessageError, `msg.error` if msg.submit_comment: msg.addComment() if msg.subscribe_list: subscribe_attempt = msg.subscribeList() print """Content-type: text/html Success!

Success!

""" if msg.submit_comment: print """

Thank you! Your comment has been submitted to the PERCEPS users' comments page.""" % _commentURL if msg.subscribe_list: if subscribe_attempt != 0: print """Content-type: text/html

%s was not added to the PERCEPS mailinglist. Perhaps your two passwords didn't match. Try subscribing from the mailinglist page. """ % msg['email'] else: print """

%s has been successfully added to the PERCEPS mailinglist. You should receive a confirmation message at that address soon. If you do not receive a confirmation message, try subscribing from the mailinglist page. """ % msg['email'] print _footer except cgiForm.MessageError, val: if str(val)[1:5] == 'Oops': print """Content-type: text/html Oops. Mistake in form...

Oops! There was a mistake in the form you submitted.

""" print str(val) print """

Please use your browser's BACK button to return to the form and correct the data. Then resubmit the form. """ else: message = "Environment:" for el in os.environ.keys(): message = message+"\t%s\t%s\n" % (el, os.environ[el]) message = `msg`+'\n'+message msg.printError(message,'tbryan@python.net', 'tbryan@python.net') print cgiForm._ERROR except: print "Content-type: text/plain\n\n" traceback.print_exc() print str(msg)