diff --git a/server.py b/server.py index 5f2e22d..bbc3f31 100644 --- a/server.py +++ b/server.py @@ -7,13 +7,14 @@ import supybot.utils as utils import sqlite3 import collections import urllib +from StringIO import StringIO host = 'http://domain.tld' -port = 6666 -username = 'login' +port = 80 +username = 'username' password = 'password' -filename = '/home/user/bot/data/networkname/ChanTracker.db' -channels = ['#channelA','#channelb'] # empty to allows view of all channels recorded, otherwise restrict the views to channels +filename = '/home/botaccount/data/networkname/ChanTracker.db' +channels = [] # empty to allows view of all channels recorded, otherwise restrict the views to channels # usage python server.py @@ -69,6 +70,7 @@ class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ): def page (self,query): h = '%s:%s/' % (host,port) + body = [] if not query: return if query.startswith('/?username='): @@ -88,20 +90,21 @@ class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ): else: query = '/?hash=%s' % base64string if not query.startswith('/?hash='): - body = '\n\nChanTracker\n' - body += '\n' - body += "\n\n" - body += '
\n' % h - body += '

Username:

\n' - body += '

Password:

\n' - body += '\n' - body += "
\n" - body += "\n\n" + body.append('\n\nChanTracker\n') + body.append('\n') + body.append("\n\n") + body.append('
\n' % h) + body.append('

Username:

\n') + body.append('

Password:

\n') + body.append('\n') + body.append("
\n") + body.append("\n\n") self.send_response(200) self.send_header("Content-type","text/html") - self.send_header("Content-length",str(len(body))) + full = ''.join(body) + self.send_header("Content-length",str(len(full))) self.end_headers() - self.wfile.write(body) + self.wfile.write(full) return if query.startswith('/?hash='): a = query.split('&')[0] @@ -111,19 +114,19 @@ class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ): q = '?hash=%s' % base64string query = urllib.unquote( query ) print query - body = '\n\nBanTracker - %s\n' % query - body += '\n' - body += '\n' - body += '\n' - body += '\n\n' - body += '
\n' - body += '
\n' % q - body += '
' - body += '' % base64string - body += '\n' - body += '\n' - body += '
\n' - body += '
\n' + body.append('\n\nBanTracker - %s\n' % query) + body.append('\n') + body.append('\n') +# body.append('\n') + body.append('\n\n') + body.append('
\n') + body.append('
\n' % q) + body.append('
') + body.append('' % base64string) + body.append('\n') + body.append('\n') + body.append('
\n') + body.append('
\n') db = self._getbandb() c = db.cursor() if query: @@ -131,80 +134,87 @@ class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ): if query.startswith('/&id='): search = query.split('/&id=')[1] c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE id=? ORDER BY id DESC""",(search,)) - if c.rowcount: - ban = c.fetchall()[0] + r = c.fetchall() + if len(r): + ban = r[0] (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ban if not len(channels) or channel in channels: - body += '

#%s

\n' % id - body += '

#%s by %s in %s : +%s : %s

\n' % (id,oper,channel,kind,mask) - body += '

Begin at %s

\n' % time.strftime('%Y-%m-%d %H:%M:%S GMT',time.gmtime(float(begin_at))) + body.append('

#%s

\n' % id) + body.append('

#%s by %s in %s : +%s : %s

\n' % (id,oper,channel,kind,mask)) + body.append('

Begin at %s

\n' % time.strftime('%Y-%m-%d %H:%M:%S GMT',time.gmtime(float(begin_at)))) was = float(begin_at) == float(end_at) if was: was = 'forever' else: was = timeElapsed(float(end_at) - float(begin_at)) - body += '

Original duration : %s

\n' % was + body.append('

Original duration : %s

\n' % was) if not removed_at: if was != 'forever': - body += '

%s

\n' % 'It will expire in %s' % timeElapsed(float(end_at) - time.time()) + body.append('

%s

\n' % 'It will expire in %s' % timeElapsed(float(end_at) - time.time())) else: - body += '

%s

\n' % 'Removed after %s on %s by %s' % (timeElapsed(float(removed_at)-float(begin_at)),time.strftime('%Y-%m-%d %H:%M:%S GMT',time.gmtime(float(removed_at))),removed_by) + body.append('

%s

\n' % 'Removed after %s on %s by %s' % (timeElapsed(float(removed_at)-float(begin_at)),time.strftime('%Y-%m-%d %H:%M:%S GMT',time.gmtime(float(removed_at))),removed_by)) c.execute("""SELECT full, log FROM nicks WHERE ban_id=?""",(id,)) - if c.rowcount: - users = c.fetchall() - body += '

Logs

\n' + r = c.fetchall() + if len(r): + users = r + body.append('

Logs

\n') for u in users: (full,log) = u - body += '

for %s

\n' % full + body.append('

for %s

\n' % full) if log != '': - body +='\n') c.execute("""SELECT oper, at, comment FROM comments WHERE ban_id=?""",(id,)) - if c.rowcount: - body += '

Comments

\n' - body += '\n') + elif query.startswith('/&channel='): search = '#'+query.split('/&channel=')[1] c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE channel=? ORDER BY id DESC""",(search,)) - if c.rowcount: - bans = c.fetchall() + r = c.fetchall() + if len(r): + bans = r for ban in bans: (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ban ar.append([int(id),channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by]) - if query.startswith('/&removed_by='): + elif query.startswith('/&removed_by='): search = query.split('/&removed_by=')[1] c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE removed_by=? ORDER BY id DESC""",(search,)) - if c.rowcount: - bans = c.fetchall() + r = c.fetchall() + if len(r): + bans = r for ban in bans: (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ban ar.append([int(id),channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by]) - if query.startswith('/&oper='): + elif query.startswith('/&oper='): search = query.split('/&oper=')[1] c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE oper=? ORDER BY id DESC""",(search,)) - if c.rowcount: - bans = c.fetchall() + r = c.fetchall() + if len(r): + bans = r for ban in bans: (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ban ar.append([int(id),channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by]) - if query.startswith('/&mask='): + elif query.startswith('/&mask='): search = query.split('/&mask=')[1] glob = '*%s*' % search like = '%'+search+'%' c.execute("""SELECT ban_id, full FROM nicks WHERE full GLOB ? OR full LIKE ? OR log GLOB ? OR log LIKE ? ORDER BY ban_id DESC""",(glob,like,glob,like)) L = [] a = {} - if c.rowcount: - bans = c.fetchall() + r = c.fetchall() + if len(r): + bans = r d = {} for ban in bans: (id,full) = ban @@ -212,14 +222,16 @@ class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ): d[id] = id for id in d: c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE id=? ORDER BY id DESC""",(int(id),)) - if c.rowcount: - bans = c.fetchall() + r = c.fetchall() + if len(r): + bans = r for ban in bans: (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ban a[str(id)] = ban c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE mask GLOB ? OR mask LIKE ? ORDER BY id DESC""",(glob,like)) - if c.rowcount: - bans = c.fetchall() + r = c.fetchall() + if len(r): + bans = r for ban in bans: (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ban a[str(id)] = ban @@ -232,7 +244,7 @@ class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ): return item[0] ar.sort(key=sort_function) ar.sort(reverse=True) - if query.startswith('/&search='): + elif query.startswith('/&search='): search = query.split('/&search=')[1] search = search.replace('+','*') print search @@ -242,42 +254,43 @@ class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ): c.execute("""SELECT ban_id, full FROM nicks WHERE full GLOB ? OR full LIKE ? OR log GLOB ? OR log LIKE ? ORDER BY ban_id DESC""",(s,qu,s,qu)) L = [] a = {} - if c.rowcount: - bans = c.fetchall() + r = c.fetchall() + if len(r): + bans = r d = {} for ban in bans: (id,full) = ban if not id in d: d[id] = id for id in d: - c = db.cursor() - c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE id=? ORDER BY id DESC""",(int(id),)) - if c.rowcount: - bans = c.fetchall() + c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE id=? ORDER BY id DESC""",(int(id),)) + r = c.fetchall() + if len(r): + bans = r for ban in bans: (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ban a[id] = ban - c = db.cursor() c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE mask GLOB ? OR mask LIKE ? OR channel GLOB ? OR channel LIKE ? OR oper GLOB ? OR oper LIKE ? ORDER BY id DESC""",(s,qu,s,qu,s,qu)) - if c.rowcount: - bans = c.fetchall() + r = c.fetchall() + if len(r): + bans = r for ban in bans: (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ban a[id] = ban - c = db.cursor() c.execute("""SELECT ban_id, comment FROM comments WHERE comment GLOB ? OR comment LIKE ? ORDER BY ban_id DESC""",(s,qu)) + r = c.fetchall() d = {} - if c.rowcount: - bans = c.fetchall() + if len(r): + bans = r for ban in bans: (id,full) = ban d[id] = id for id in d: if not id in a: - c = db.cursor() - c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE id=? ORDER BY id DESC""",(int(id),)) - if c.rowcount: - bans = c.fetchall() + c.execute("""SELECT id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by FROM bans WHERE id=? ORDER BY id DESC LIMIT 1""",(int(id),)) + r = c.fetchall() + if len(r): + bans = r for ban in bans: (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ban a[id] = ban @@ -291,56 +304,59 @@ class MyHandler( BaseHTTPServer.BaseHTTPRequestHandler ): ar.sort(key=sort_function) ar.sort(reverse=True) else: - body += '

nothing found

\n' + body.append('

nothing found

\n') if len(ar): + print 'found %s results' % len(ar) i = 0 - body += '

results %s

' % search - body += '\n' - body += '\n' - body += '\n' + body.append('

results %s

' % search) + body.append('
IDChannelOperatorKindTargetBegin dateEnd dateRemoved dateRemoved byaffected
\n') + body.append('\n') + body.append('\n') while i < len(ar): (id,channel,oper,kind,mask,begin_at,end_at,removed_at,removed_by) = ar[i] if not len(channels) or channel in channels: - body += '\n' - body += '\n' % (h,q,id,id) - body += '\n' % (h,q,channel.split('#')[1],channel) - body += '\n' % (h,q,urllib.urlencode({'oper':oper}),oper) - body += '\n' % kind - body += '\n' % (h,q,urllib.urlencode({'mask':mask}),mask) + body.append('\n') + body.append('\n' % (h,q,id,id)) + body.append('\n' % (h,q,channel.split('#')[1],channel)) + body.append('\n' % (h,q,urllib.urlencode({'oper':oper}),oper)) + body.append('\n' % kind) + body.append('\n' % (h,q,urllib.urlencode({'mask':mask}),mask)) s = time.strftime('%Y-%m-%d %H:%M:%S GMT',time.gmtime(float(begin_at))) - body += '\n' % s + body.append('\n' % s) if end_at and end_at != begin_at: s = time.strftime('%Y-%m-%d %H:%M:%S GMT',time.gmtime(float(end_at))) - body += '\n' % s + body.append( '\n' % s) else: - body += '' + body.append( '') if removed_at: s = time.strftime('%Y-%m-%d %H:%M:%S GMT',time.gmtime(float(removed_at))) - body += '' % s + body.append( '' % s) else: - body += '\n' + body.append( '\n' ) if removed_by: - body += '\n' % (h,q,urllib.urlencode({'removed_by':removed_by}),removed_by) + body.append( '\n' % (h,q,urllib.urlencode({'removed_by':removed_by}),removed_by)) else: - body += '\n' - affected = '' - try: - c.execute("""SELECT full, log FROM nicks WHERE ban_id=?""",(id,)) - affected = len(c.fetchall()) - except: - affected = '' - body += '\n' % affected - body += '\n' + body.append( '\n') +# affected = '' +# try: +# c.execute("""SELECT full, log FROM nicks WHERE ban_id=?""",(id,)) +# affected = len(c.fetchall()) +# except: +# affected = '' +# body.append( '\n' % affected) + body.append( '\n') i = i+1 - body += '\n' - body += '
IDChannelOperatorKindTargetBegin dateEnd dateRemoved dateRemoved by
%s%s%s+%s%s
%s%s%s+%s%s%s%s%s%s%s%s%s%s%s
%s
\n' - c.close() - body += "" + body.append('\n') + body.append('
\n') + body.append("") self.send_response(200) self.send_header("Content-type","text/html") - self.send_header("Content-length",str(len(body))) + full = ''.join(body) + print 'html lines %s' % len(full) + self.send_header("Content-length",len(full)) self.end_headers() - self.wfile.write(body) + self.wfile.write(full) + c.close() def _getbandb (self): if os.path.exists(filename):