"""cpirclog, a CherryPy front-end to irclog2html.
You'll have to obtain irclog2html yourself, from http://mg.pov.lt/irclog2html/
Then, either stick this file in that folder, or put irclog2html in your
Python path (and any .css files, etc in this folder).
"""
import os
try:
import cStringIO as StringIO
except ImportError:
import StringIO
import sys
import threading
import irclog2html
import cherrypy
thisdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
class Root:
style = irclog2html.XHTMLStyle
logdir = thisdir
irclog_css = cherrypy.tools.staticfile.handler(
filename=os.path.join(thisdir, 'irclog.css'))
def default(self, channel, page=None, q=None):
outfile = StringIO.StringIO()
chandir = os.path.join(self.logdir, "#%s" % channel)
try:
fnames = [fname.rsplit(".", 1)[0] for fname
in os.listdir(chandir) if fname.endswith(".log")]
fnames.sort()
except:
raise cherrypy.NotFound()
if page is None:
# Grab the last page; assume they're in order
pos = len(fnames) - 1
elif page in fnames:
pos = fnames.index(page)
else:
# Grab the nearest previous page.
fnames.append(page)
fnames.sort()
pos = fnames.index(page) - 1
fnames.remove(page)
if pos < 0:
pos = 0
elif pos > len(fnames) - 1:
pos = len(fnames) - 1
page = fnames[pos]
if pos > 0:
prev = fnames[pos - 1]
else:
prev = ''
if pos < len(fnames) - 1:
next = fnames[pos + 1]
else:
next = ''
chanfile = os.path.join(self.logdir, chandir, "%s.log" % page)
# Prevent uplevel attacks
if not os.path.normpath(chanfile).startswith(self.logdir):
raise cherrypy.NotFound()
try:
infile = open(chanfile, 'rb')
except IOError:
raise cherrypy.NotFound()
parser = irclog2html.LogParser(infile)
formatter = self.style(outfile)
irclog2html.convert_irc_log(
parser, formatter, "IRC Log for #%s %s" % (channel, page),
('Previous', prev), ('', ''), ('Next', next), searchbox=False)
outfile.seek(0)
return outfile.read()
default.exposed = True
if __name__ == '__main__':
cherrypy.quickstart(Root(), '/')