Contact: fumanchu@aminus.org

Log in as guest/misc to create tickets

root/cpirclog.py

Revision 141 (checked in by fumanchu, 3 years ago)

cpirclog.py set to eol-style: native

  • Property svn:eol-style set to native
Line 
1 """cpirclog, a CherryPy front-end to irclog2html.
2
3 You'll have to obtain irclog2html yourself, from http://mg.pov.lt/irclog2html/
4 Then, either stick this file in that folder, or put irclog2html in your
5 Python path (and any .css files, etc in this folder).
6 """
7
8 import os
9 try:
10     import cStringIO as StringIO
11 except ImportError:
12     import StringIO
13 import sys
14 import threading
15
16 import irclog2html
17 import cherrypy
18
19
20 thisdir = os.path.join(os.getcwd(), os.path.dirname(__file__))
21
22
23 class Root:
24    
25     style = irclog2html.XHTMLStyle
26     logdir = thisdir
27    
28     irclog_css = cherrypy.tools.staticfile.handler(
29         filename=os.path.join(thisdir, 'irclog.css'))
30    
31     def default(self, channel, page=None, q=None):
32         outfile = StringIO.StringIO()
33         chandir = os.path.join(self.logdir, "#%s" % channel)
34        
35         try:
36             fnames = [fname.rsplit(".", 1)[0] for fname
37                       in os.listdir(chandir) if fname.endswith(".log")]
38             fnames.sort()
39         except:
40             raise cherrypy.NotFound()
41        
42         if page is None:
43             # Grab the last page; assume they're in order
44             pos = len(fnames) - 1
45         elif page in fnames:
46             pos = fnames.index(page)
47         else:
48             # Grab the nearest previous page.
49             fnames.append(page)
50             fnames.sort()
51             pos = fnames.index(page) - 1
52             fnames.remove(page)
53        
54         if pos < 0:
55             pos = 0
56         elif pos > len(fnames) - 1:
57             pos = len(fnames) - 1
58        
59         page = fnames[pos]
60         if pos > 0:
61             prev = fnames[pos - 1]
62         else:
63             prev = ''
64         if pos < len(fnames) - 1:
65             next = fnames[pos + 1]
66         else:
67             next = ''
68        
69         chanfile = os.path.join(self.logdir, chandir, "%s.log" % page)
70        
71         # Prevent uplevel attacks
72         if not os.path.normpath(chanfile).startswith(self.logdir):
73             raise cherrypy.NotFound()
74        
75         try:
76             infile = open(chanfile, 'rb')
77         except IOError:
78             raise cherrypy.NotFound()
79        
80         parser = irclog2html.LogParser(infile)
81         formatter = self.style(outfile)
82         irclog2html.convert_irc_log(
83             parser, formatter, "IRC Log for #%s %s" % (channel, page),
84             ('Previous', prev), ('', ''), ('Next', next), searchbox=False)
85         outfile.seek(0)
86         return outfile.read()
87     default.exposed = True
88
89
90 if __name__ == '__main__':
91     cherrypy.quickstart(Root(), '/')
Note: See TracBrowser for help on using the browser.