#!/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 sys import time import urllib import ConfigParser CONFIG_FILENAME = "bantracker.conf" config = ConfigParser.RawConfigParser() config.add_section('webpage') # set default values config.set('webpage', 'database', '/home/bot/data/bans.db') config.set('webpage', 'results_per_page', '100') config.set('webpage', 'anonymous_access', 'True') config.set('webpage', 'PLUGIN_PATH', '/var/www/bot') config.set('webpage', 'irc_network', 'irc.freenode.net') config.set('webpage', 'irc_channel', '#ubuntu-ops') try: config.readfp(open(CONFIG_FILENAME)) except IOError: try: config.write(open(CONFIG_FILENAME, 'w')) except IOError: pass # This needs to be set to the location of the commoncgi.py file PLUGIN_PATH = config.get('webpage', 'PLUGIN_PATH') if PLUGIN_PATH: sys.path.append(PLUGIN_PATH) try: from commoncgi import * except: print "Content-Type: text/html" print print "
Failed to load the module commoncgi
" print "Check that the configured option PLUGIN_PATH is correct.
" sys.exit(-1) db = config.get('webpage', 'database') num_per_page = config.getint('webpage', 'results_per_page') anonymous_access = config.getboolean('webpage', 'anonymous_access') irc_network = config.get('webpage', 'irc_network') irc_channel = config.get('webpage', 'irc_channel') t1 = time.time() try: con = sqlite.connect(db) cur = con.cursor() except sqlite.DatabaseError: print >> sys.stderr, "Unable to connect to to database '%s'" % db send_page('bans.tmpl') def db_execute(query, args): try: cur.execute(query, args) return cur except sqlite.OperationalError: print >> sys.stderr, "The database is locked, wait a bit and try again." send_page('bans.tmpl') # 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 'sess' in form: cookie['sess'] = form['sess'].value if 'sess' in cookie: 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 not anonymous_access: print "Sorry, bantracker is not available for anonymous users' else: print '' if mark: if regex: try: mark = re.compile(mark_value, re.I) except: print >> sys.stderr, "Malformed regex %r" % mark_value mark = False 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 'comment' in form and 'comment_id' in form 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): try: 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() except sqlite.DatabaseError: con.rollback() print >> sys.stderr, "Sorry, failed to submit comment to the database. Please try again later." # Write the page print '