ubuntu-bots/commoncgi.py

78 lines
2.1 KiB
Python
Raw Normal View History

# -*- Encoding: utf-8 -*-
###
# Copyright (c) 2006-2007 Dennis Kaarsemaker
# Copyright (c) 2008-2010 Terence Simpson
2018-02-22 10:45:04 +00:00
# Copyright (c) 2018- Krytarik Raido
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
###
2018-02-22 10:45:04 +00:00
import cgi, cgitb, sys, os, re, math
import supybot.utils as utils
if sys.version_info < (3,0):
import Cookie as cookies
else:
import http.cookies as cookies
2006-06-26 17:57:20 +00:00
2018-02-22 10:45:04 +00:00
cgitb.enable()
2006-06-26 17:57:20 +00:00
form = cgi.FieldStorage()
2018-02-22 10:45:04 +00:00
cookie = cookies.SimpleCookie()
if 'HTTP_COOKIE' in os.environ:
2006-06-26 17:57:20 +00:00
cookie.load(os.environ['HTTP_COOKIE'])
2018-02-22 10:45:04 +00:00
if 'sess' in cookie:
cookie['sess']['max-age'] = 2592000 * 3
2006-06-26 17:57:20 +00:00
cookie['sess']['version'] = 1
2018-02-22 10:45:04 +00:00
if 'tz' in cookie:
cookie['tz']['max-age'] = 2592000 * 3
2006-06-26 17:57:20 +00:00
cookie['tz']['version'] = 1
class IOWrapper:
'''Class to wrap default IO, used with templates'''
def __init__(self):
self.buf = []
def write(self, val):
self.buf.append(val)
def getvalue(self):
return self.buf
sys.stdout = IOWrapper()
sys.stderr = IOWrapper()
2006-06-26 17:57:20 +00:00
def send_page(template):
'''Sends a template page and exit'''
2006-06-26 17:57:20 +00:00
data = sys.stdout.getvalue()
2008-11-01 16:45:33 +00:00
errdata = sys.stderr.getvalue()
2006-06-26 17:57:20 +00:00
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
2018-02-22 10:45:04 +00:00
print("Content-Type: text/html")
print(cookie)
print("")
2006-06-26 17:57:20 +00:00
fd = open(template)
tmpl = fd.read()
fd.close()
2011-11-06 23:11:13 +00:00
sys.stdout.write(tmpl[:tmpl.find('%e')])
for e in errdata:
2011-11-06 23:11:13 +00:00
sys.stdout.write(e)
sys.stdout.write(tmpl[tmpl.find('%e')+2:tmpl.find('%s')])
# print tmpl[:tmpl.find('%s')]
for d in data:
2011-11-06 23:11:13 +00:00
sys.stdout.write(d)
sys.stdout.write(tmpl[tmpl.find('%s')+2:])
2006-06-26 17:57:20 +00:00
sys.exit(0)
def q(txt):
'''Simple HTML entity quoting'''
2006-06-26 17:57:20 +00:00
return txt.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;').replace('"','&quot;')