index : logbot | |
Archlinux32 log bot | gitolite user |
summaryrefslogtreecommitdiff |
author | Erich Eckner <git@eckner.net> | 2022-11-06 10:59:13 +0100 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2022-11-06 10:59:13 +0100 |
commit | c464eb855ae463873587fef9494d778addea3fdf (patch) | |
tree | 55193d42d2cdee3750ea3a5f15139301aaf69a86 | |
parent | 041e30310f9892c1acc0ebd1fd7c337d3a71652d (diff) |
-rwxr-xr-x | logbot.py | 89 |
@@ -33,7 +33,7 @@ __copyright__ = "Copyright (c) Chris Oliver" __license__ = "GPL2" -import cgi +import html import os import ftplib import sys @@ -93,16 +93,16 @@ DEFAULT_TIMEZONE = 'UTC' default_format = { "help" : HELP_MESSAGE, - "action" : '<span class="person" style="color:%color%">* %user% %message%</span>', - "join" : '-!- <span class="join">%user%</span> has joined %channel%', - "kick" : '-!- <span class="kick">%user%</span> was kicked from %channel% by %kicker% [%reason%]', - "mode" : '-!- mode/<span class="mode">%channel%</span> [%modes% %person%] by %giver%', - "nick" : '<span class="nick">%old%</span> is now known as <span class="nick">%new%</span>', - "part" : '-!- <span class="part">%user%</span> has parted %channel%', - "pubmsg" : '<span class="person" style="color:%color%"><%user%></span> %message%', - "pubnotice" : '<span class="notice">-%user%:%channel%-</span> %message%', - "quit" : '-!- <span class="quit">%user%</span> has quit [%message%]', - "topic" : '<span class="topic">%user%</span> changed topic of <span class="topic">%channel%</span> to: %message%', + "action" : '<span class="person" style="color:{color}">* {user} {message}</span>', + "join" : '-!- <span class="join">{user}</span> has joined {channel}', + "kick" : '-!- <span class="kick">{user}</span> was kicked from {channel} by {kicker} [{reason}]', + "mode" : '-!- mode/<span class="mode">{channel}</span> [{modes} {person}] by {giver}', + "nick" : '<span class="nick">{old}</span> is now known as <span class="nick">{new}</span>', + "part" : '-!- <span class="part">{user}</span> has parted {channel}', + "pubmsg" : '<span class="person" style="color:{color}"><{user}></span> {message}', + "pubnotice" : '<span class="notice">-{user}:{channel}-</span> {message}', + "quit" : '-!- <span class="quit">{user}</span> has quit [{message}]', + "topic" : '<span class="topic">{user}</span> changed topic of <span class="topic">{channel}</span> to: {message}', } html_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" @@ -110,7 +110,7 @@ html_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> - <title>%title%</title> + <title>{title}</title> <style type="text/css"> body { background-color: #F8F8FF; @@ -133,7 +133,7 @@ html_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" </style> </head> <body> - <h1>%title%</h1> + <h1>{title}</h1> <a href="..">Back</a><br /> </body> </html> @@ -169,7 +169,10 @@ def pairs(items): """ items = iter(items) while True: - yield next(items), next(items) + try: + yield next(items), next(items) + except StopIteration: + return def html_color(input): """ @@ -250,26 +253,30 @@ class Logbot(SingleServerIRCBot): def format_event(self, name, event, params): msg = self.format[name] for key, val in params.items(): - msg = msg.replace(key, val) - - # Always replace %user% with e.source() - # and %channel% with e.target() - msg = msg.replace("%user%", nm_to_n(event.source())) - msg = msg.replace("%host%", event.source()) - try: msg = msg.replace("%channel%", event.target()) - except: pass - msg = msg.replace("%color%", self.color(nm_to_n(event.source()))) + msg = msg.format(key = val) + try: - user_message = cgi.escape(event.arguments()[0]) - msg = msg.replace("%message%", html_color(user_message)) - except: pass + user_message = event.arguments()[0] + except: + user_message = '' + + user_message = html.escape(user_message) + + # Always replace {user} with e.source() + # and {channel} with e.target() + msg = msg.format( + user = nm_to_n(event.source()), + host = event.source(), + channel = event.target(), + color = self.color(nm_to_n(event.source())), + message = html_color(user_message)) return msg def write_event(self, name, event, params={}): # Format the event properly if name == 'nick' or name == 'quit': - chans = params["%chan%"] + chans = params["{chan}"] else: chans = event.target() msg = self.format_event(name, event, params) @@ -328,7 +335,7 @@ class Logbot(SingleServerIRCBot): os.makedirs(chan_path) # Create channel index - write_string("%s/index.html" % chan_path, html_header.replace("%title%", "%s | Logs" % channel_title)) + write_string("%s/index.html" % chan_path, html_header.format(title = "%s | Logs" % channel_title)) append_line("%s/index.html" % chan_path, '<a href="latest.html">latest (bookmarkable)</a>') # Append channel to log index @@ -348,7 +355,7 @@ class Logbot(SingleServerIRCBot): # Create the log date index if it doesnt exist if not os.path.exists(log_path): - write_string(log_path, html_header.replace("%title%", "%s | Logs for %s" % (channel_title, date))) + write_string(log_path, html_header.format(title = "%s | Logs for %s" % (channel_title, date))) if os.path.islink("%s/latest.html" % chan_path): os.unlink("%s/latest.html" % chan_path) os.symlink(rel_log_path, "%s/latest.html" % chan_path) @@ -396,17 +403,17 @@ class Logbot(SingleServerIRCBot): def on_kick(self, c, e): self.write_event("kick", e, - {"%kicker%" : e.source(), - "%channel%" : e.target(), - "%user%" : e.arguments()[0], - "%reason%" : e.arguments()[1], + {"{kicker}" : e.source(), + "{channel}" : e.target(), + "{user}" : e.arguments()[0], + "{reason}" : e.arguments()[1], }) def on_mode(self, c, e): self.write_event("mode", e, - {"%modes%" : e.arguments()[0], - "%person%" : e.arguments()[1] if len(e.arguments()) > 1 else e.target(), - "%giver%" : nm_to_n(e.source()), + {"{modes}" : e.arguments()[0], + "{person}" : e.arguments()[1] if len(e.arguments()) > 1 else e.target(), + "{giver}" : nm_to_n(e.source()), }) def on_nick(self, c, e): @@ -415,9 +422,9 @@ class Logbot(SingleServerIRCBot): for chan in self.channels: if old_nick in [x.lstrip('~%&@+') for x in self.channels[chan].users()]: self.write_event("nick", e, - {"%old%" : old_nick, - "%new%" : e.target(), - "%chan%": chan, + {"{old}" : old_nick, + "{new}" : e.target(), + "{chan}": chan, }) def on_part(self, c, e): @@ -440,7 +447,7 @@ class Logbot(SingleServerIRCBot): # Only write the event on channels that actually had the user in the channel for chan in self.channels: if nick in [x.lstrip('~%&@+') for x in self.channels[chan].users()]: - self.write_event("quit", e, {"%chan%" : chan}) + self.write_event("quit", e, {"{chan}" : chan}) def on_topic(self, c, e): self.write_event("topic", e) @@ -463,7 +470,7 @@ def main(): # Create the logs directory if not os.path.exists(LOG_FOLDER): os.makedirs(LOG_FOLDER) - write_string("%s/index.html" % LOG_FOLDER, html_header.replace("%title%", "Chat Logs")) + write_string("%s/index.html" % LOG_FOLDER, html_header.format(title = "Chat Logs")) # Start the bot bot = Logbot(SERVER, PORT, SERVER_PASS, CHANNELS, NICK, NICK_PASS) |