#!/usr/bin/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 sys.path.append('/var/www/bots.ubuntulinux.nl') from commoncgi import * ### Variables db = '/home/dennis/ubotu/data/bans.db' num_per_page = 100 con = sqlite.connect(db) cur = con.cursor() # Login check error = '' user = None # Delete old sessions cur.execute("""DELETE FROM sessions WHERE time < %d""", int(time.time()) - 2592000 * 3) # Session handling if form.has_key('sess'): cookie['sess'] = form['sess'].value if cookie.has_key('sess'): try: sess = cookie['sess'].value cur.execute("""SELECT user FROM sessions WHERE session_id=%s""",sess) user = cur.fetchall()[0][0] except: con.commit() pass # Log if form.has_key('log'): cur.execute("""SELECT log FROM bans WHERE id=%s""", form['log'].value) log = cur.fetchall() con.commit() print q(log[0][0]).replace('\n', '
') send_page('empty.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 '
' # Personal data print '
' if user: print 'Logged in as: %s
' % user print 'Timezone: ' if form.has_key('tz') and form['tz'].value in pytz.common_timezones: tz = form['tz'].value elif cookie.has_key('tz') and cookie['tz'].value in pytz.common_timezones: tz = cookie['tz'].value else: tz = 'UTC' cookie['tz'] = tz print '
' print '
' tz = pytz.timezone(tz) # Search form print '' # Pagination, only when not processing a search if not form.has_key('query'): sort = '' if form.has_key('sort'): sort='&sort=' + form['sort'].value print '
·' cur.execute('SELECT COUNT(id) FROM bans') nump = math.ceil(int(cur.fetchall()[0][0]) / float(num_per_page)) for i in range(nump): print '%d ·' % (i, sort, i+1) print '
' # Empty log div, will be filled with AJAX print '
 
' # Main bans table # Table heading print '' for h in [['Channel',0], ['Nick/Mask',1], ['Operator',2], ['Time',6]]: # Negative integers for backwards searching try: v = int(form['sort'].value) if v < 10: h[1] += 10 except: pass print '' % (h[1],h[0]) print '' # Select and filter bans cur.execute("SELECT channel,mask,operator,time,removal,removal_op,id FROM bans ORDER BY id DESC") bans = cur.fetchall() def myfilter(item, regex, kick, ban, oldban, mute, oldmute): if '!' not in item[1]: if not kick: return False elif item[1][0] == '%': if item[4]: if not oldmute: return False else: if not mute: return False else: if item[4]: if not oldban: return False else: if not ban: return False return regex.search(item[1]) or regex.search(item[2]) or regex.search(item[0]) or (item[5] and regex.search(item[5])) if form.has_key('query'): k = b = ob = m = om = False if form.has_key('kicks'): k = True if form.has_key('oldbans'): ob = True if form.has_key('bans'): b = True if form.has_key('oldmutes'): om = True if form.has_key('mutes'): m = True regex = re.compile(re.escape(form['query'].value).replace('\%','.*'), re.DOTALL | re.I) bans = filter(lambda x: myfilter(x, regex, k, b, ob, m, om), bans) start = 0; end = len(bans) else: page = 0 try: page = int(form['page'].value) except: pass start = page * num_per_page end = (page+1) * num_per_page # Sort the bans def _sortf(x1,x2,field): if x1[field] < x2[field]: return -1 if x1[field] > x2[field]: return 1 return 0 if form.has_key('sort'): try: field = int(form['sort'].value) except: pass else: if field in (0,1,2,6,10,11,12,16): bans.sort(lambda x1,x2: _sortf(x1,x2,field%10)) if field >= 10: bans.reverse() # And finally, display them! i = 0 for b in bans[start:end]: print '' # Channel print '' % ('',b[0]) # Mask print '' # Operator print '' # Time print '' # Log link print """""" % b[6] print '' # Comments print '' print '' print '
%sLog
%s %s%s' % b[1] # Ban removal if b[4]: print '
(Removed)' print'
%s' % b[2] if b[4]: # Ban removal print u'
%s' % b[5] print '
%s' % pickle.loads(b[3]).astimezone(tz).strftime("%b %d %Y %H:%M:%S") if b[4]: # Ban removal print '
%s' % pickle.loads(b[4]).astimezone(tz).strftime("%b %d %Y %H:%M:%S") print '
Show log
' cur.execute("""SELECT who, comment, time FROM comments WHERE ban_id = %s""" % b[6]) comments = cur.fetchall() if len(comments) == 0: print '(No comments) ' else: for c in comments: print q(c[1]) print u'
%s, %s

' % \ (c[0],pickle.loads(c[2]).astimezone(tz).strftime("%b %d %Y %H:%M:%S")) if user: print """Add comment""" % b[6] print """
' # Aaaaaaaaaaaaaaaaand send! send_page('bans.tmpl')