WakeLift
wakelift / posts / ikiwiki setup /

feedback script in python

#!/usr/bin/python
# vim: et
import xmpp
import cgi
import os
from recaptcha.client import captcha
# uncomment these two lines if you don't want to debug stuff
# import cgitb
# cgitb.enable()

recaptcha_public_key = 'foo'
recaptcha_private_key = 'bar'

f = cgi.FieldStorage()
if 'referer' not in f or not f['referer'].value:
        url = os.environ.get("HTTP_REFERER")
else:
        url = f['referer'].value

if url is None:
        url = "without URL"
try:
        ip = os.environ.get("X-FORWARDED-FOR").split(",")[0]
except AttributeError:
        ip = os.environ.get("REMOTE-ADDR")

def make_form(name="", message=""):
        print """<form action="/cgi-bin/feedback.py" method="POST">

        name: <input name="name" value="%(name)s" /><br />
        message: <br />
        <textarea name="message" rows="5" cols="10">%(message)s</textarea><br />
        <input type="hidden" name="referer" value="%(referer)s" />
        %(captcha)s
        <input type="submit" value="send!" />
</form>""" % dict(name=cgi.escape(name, quote=True),
                  message=cgi.escape(message),
                  referer=cgi.escape(url, quote=True),
                  captcha=captcha.displayhtml(pubkey))

try:
        name = f['name'].value
except:
        name = "anonymous_coward"
try:
        message = f['message'].value
except:
        message = ""

if 'referer' not in f:
        print "Content-Type: text/html"
        print
        print
        make_form(name, message)

else:


        result = captcha.submit(f['recaptcha_challenge_field'].value,
                               f['recaptcha_response_field'].value,
                               recaptcha_private_key,
                               ip)

        if result.is_valid:
                print "Status: 302 I know this great restaurant at the end of the universe."
                print "Location: ", f['referer'].value
                print

                message = """%s -- %s wrote:
                "%s" """ % (url, f['name'].value, f['message'].value)

                cl = xmpp.Client("jabber.ccc.de", debug=[])
                cl.connect()
                cl.auth('username','password', 'resource')
                cl.send(xmpp.protocol.Presence(typ = 'online', status = url))

                cl.send(xmpp.protocol.Message('my_jabber@user.de', message, subject='feedback'))

                cl.disconnect()
        else:
                print "Status: 402 Solve the captcha or give me cash."
                print "Content-Type: text/html"
                print
                make_form(name, message)