From c464eb855ae463873587fef9494d778addea3fdf Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Sun, 6 Nov 2022 10:59:13 +0100 Subject: more python2 -> python3 fixes --- logbot.py | 89 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/logbot.py b/logbot.py index aa7bbc1..79a2972 100755 --- a/logbot.py +++ b/logbot.py @@ -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" : '* %user% %message%', - "join" : '-!- %user% has joined %channel%', - "kick" : '-!- %user% was kicked from %channel% by %kicker% [%reason%]', - "mode" : '-!- mode/%channel% [%modes% %person%] by %giver%', - "nick" : '%old% is now known as %new%', - "part" : '-!- %user% has parted %channel%', - "pubmsg" : '<%user%> %message%', - "pubnotice" : '-%user%:%channel%- %message%', - "quit" : '-!- %user% has quit [%message%]', - "topic" : '%user% changed topic of %channel% to: %message%', + "action" : '* {user} {message}', + "join" : '-!- {user} has joined {channel}', + "kick" : '-!- {user} was kicked from {channel} by {kicker} [{reason}]', + "mode" : '-!- mode/{channel} [{modes} {person}] by {giver}', + "nick" : '{old} is now known as {new}', + "part" : '-!- {user} has parted {channel}', + "pubmsg" : '<{user}> {message}', + "pubnotice" : '-{user}:{channel}- {message}', + "quit" : '-!- {user} has quit [{message}]', + "topic" : '{user} changed topic of {channel} to: {message}', } html_header = """ - %title% + {title} -

%title%

+

{title}

Back
@@ -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, 'latest (bookmarkable)') # 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) -- cgit v1.2.3-54-g00ecf