import cgi
import urllib
from supybot.ircmsgs import privmsg, topic, IrcMsg

def CGI_main(www, parms, woot, sid, sess):
	woot.s.sendall("""<html><head><title>Ask a direct question in IRC</title></head>
<body>
<p>Afraid to join irc? Been kicked often? Maybe even klined? glined? Harassed?
<p>Tough crap
<p>Well, here's your solution.
<p>Ask a question here instead.
<p>Irc2www. No flames. Just answers.
<form action="/cgi-bin/say/it" method="get">
<textarea name="question" cols="40" rows="10"></textarea>
<input type="submit" value="Ask" />
</form>
</body></html>\n""")

def it(www, parms, woot, sid, sess):
	for q in urllib.unquote_plus(parms['question']).split("\n"): woot.plugin.irc.queueMsg(IrcMsg("PRIVMSG #studentenwerk :WWW2IRC question (answer with !answer FOO)%s" % q))
	try: parms.plugin.lastanswer.pop()
	except: pass
	woot.s.sendall("""<html><head><title>Question asked</title></head><body>
<h1>Your question has been posted to the irc channel.</h1>
<p>Now you have to wait 'till you get an <a href="/cgi-bin/say/result">answer</a>
</body></html>\n""")

def result(www, parms, woot, sid, sess):
	try: answer = woot.plugin.lastanswer[0]
	except: answer = "Please wait for an answer"
	woot.s.sendall("""<html><head><title>Wait for yar answer</title>
<meta http-equiv="refresh" content="10" />
</head><body>
<h1>Answer</h1>
<p>%s</body></html>""" % answer)
