#!/usr/bin/env python
###
# Copyright (c) 2005-2007 Dennis Kaarsemaker
#
# 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.
#
###
import os
import sys
import time
import urllib
# This needs to be set to the location of the commoncgi.py file
sys.path.append('/var/www/bot')
from commoncgi import *
### Variables
db = '/home/bot/data/bans.db'
num_per_page = 100
pagename = os.path.basename(sys.argv[0])
disable_anonymous = False # Set this to True to disable anonymous access
t1 = time.time()
con = sqlite.connect(db)
cur = con.cursor()
# Login check
error = ''
user = None
# Delete old sessions
try:
session_timeout = int(time.time()) - (2592000 * 3)
cur.execute('DELETE FROM sessions WHERE time < %d', (session_timeout,))
except:
pass
# Session handling
if form.has_key('sess'):
cookie['sess'] = form['sess'].value
if cookie.has_key('sess'):
sess = cookie['sess'].value
try:
cur.execute('SELECT user FROM sessions WHERE session_id=%s',(sess,))
user = cur.fetchall()[0][0]
except:
con.commit()
pass
if not user and disable_anonymous:
print "Sorry, bantracker is not available for anonymous users
"
print "Join #ubuntu-ops on irc.freenode.net to discuss bans"
send_page('bans.tmpl')
sys.exit(0)
def urlencode(**kwargs):
"""Return the url options as a string, inserting additional ones if given."""
d = dict([ (i.name, i.value) for i in form.list ])
d.update(kwargs)
return urllib.urlencode(d.items())
# Log
if form.has_key('log'):
log_id = form['log'].value
plain = False
mark = False
mark_value = ''
regex = False
regex_value = ''
if form.has_key('plain') and form['plain'].value.lower() in ('1', 'true', 'on'):
plain = True
if form.has_key('mark'):
mark = True
mark_value = form['mark'].value
if form.has_key('regex') and form['regex'].value in ('1', 'true', 'on'):
regex = True
regex_value = 'checked="checked"'
con = sqlite.connect(db)
cur = con.cursor()
cur.execute("SELECT log FROM bans WHERE id=%s", log_id)
log = cur.fetchall()
con.commit()
con.close()
if not log or not log[0] or not log[0][0]:
if plain:
print >> sys.stderr, '
' else: print '' if mark: if regex: mark = re.compile(mark_value, re.I) else: escaped = re.escape(mark_value).replace('%', '.*') mark = re.compile(escaped, re.I) lines = log.splitlines() for line in lines: if plain: print q(line) elif mark: if mark.search(line): print ' %s%s' % (q(line), pad) else: print " %s%s" % (q(line), pad) else: print ' %s%s' % (q(line), pad) if plain: print '' send_page('empty.tmpl') print '
' print '' print ' ' print '' send_page('log.tmpl') # Main page # Process comments if form.has_key('comment') and form.has_key('comment_id') and user: cur.execute('SELECT ban_id FROM comments WHERE ban_id=%s and comment=%s', (form['comment_id'].value, form['comment'].value)) comm = cur.fetchall() if not len(comm): cur.execute('INSERT INTO comments (ban_id, who, comment, time) VALUES (%s, %s, %s, %s)', (form['comment_id'].value,user,form['comment'].value,pickle.dumps(datetime.datetime.now(pytz.UTC)))) con.commit() # Write the page print '