Fixed a bug where channel modes would crash the bot and cleaned up excess whitespace

This commit is contained in:
Chris Oliver 2011-09-23 17:11:42 -05:00
parent 97a20675ca
commit 0460e401b7

View file

@ -142,20 +142,20 @@ class Logbot(SingleServerIRCBot):
[(server, port, server_pass)], [(server, port, server_pass)],
nick, nick,
nick) nick)
self.chans = [x.lower() for x in channels] self.chans = [x.lower() for x in channels]
self.format = format self.format = format
self.set_ftp() self.set_ftp()
self.count = 0 self.count = 0
self.nick_pass = nick_pass self.nick_pass = nick_pass
print "Logbot %s" % __version__ print "Logbot %s" % __version__
print "Connecting to %s:%i..." % (server, port) print "Connecting to %s:%i..." % (server, port)
print "Press Ctrl-C to quit" print "Press Ctrl-C to quit"
def quit(self): def quit(self):
self.connection.disconnect("Quitting...") self.connection.disconnect("Quitting...")
def color(self, user): def color(self, user):
return "#%s" % md5(user).hexdigest()[:6] return "#%s" % md5(user).hexdigest()[:6]
@ -168,7 +168,7 @@ class Logbot(SingleServerIRCBot):
msg = msg.replace(key, val) msg = msg.replace(key, val)
# Always replace %user% with e.source() # Always replace %user% with e.source()
# and %channel% with e.target() # and %channel% with e.target()
msg = msg.replace("%user%", nm_to_n(event.source())) msg = msg.replace("%user%", nm_to_n(event.source()))
msg = msg.replace("%host%", event.source()) msg = msg.replace("%host%", event.source())
try: msg = msg.replace("%channel%", event.target()) try: msg = msg.replace("%channel%", event.target())
@ -176,20 +176,20 @@ class Logbot(SingleServerIRCBot):
msg = msg.replace("%color%", self.color(nm_to_n(event.source()))) msg = msg.replace("%color%", self.color(nm_to_n(event.source())))
try: msg = msg.replace("%message%", event.arguments()[0]) try: msg = msg.replace("%message%", event.arguments()[0])
except: pass except: pass
return msg return msg
def write_event(self, name, event, params={}): def write_event(self, name, event, params={}):
# Format the event properly # Format the event properly
chans = event.target() chans = event.target()
msg = self.format_event(name, event, params) msg = self.format_event(name, event, params)
# Quit goes across all channels # Quit goes across all channels
if not chans or not chans.startswith("#"): if not chans or not chans.startswith("#"):
chans = self.chans chans = self.chans
else: else:
chans = [chans] chans = [chans]
for chan in chans: for chan in chans:
self.append_log_msg(chan, msg) self.append_log_msg(chan, msg)
@ -207,7 +207,7 @@ class Logbot(SingleServerIRCBot):
remote_fname = "/".join(full_fname.split("/")[1:]) remote_fname = "/".join(full_fname.split("/")[1:])
self.ftp.storbinary("STOR %s" % remote_fname, open(full_fname, "rb")) self.ftp.storbinary("STOR %s" % remote_fname, open(full_fname, "rb"))
print "Finished uploading" print "Finished uploading"
def append_log_msg(self, channel, msg): def append_log_msg(self, channel, msg):
print "%s >>> %s" % (channel, msg) print "%s >>> %s" % (channel, msg)
@ -215,36 +215,36 @@ class Logbot(SingleServerIRCBot):
chan_path = "logs/%s" % channel chan_path = "logs/%s" % channel
if not os.path.exists(chan_path): if not os.path.exists(chan_path):
os.makedirs(chan_path) os.makedirs(chan_path)
# Create channel index # Create channel index
write_string("%s/index.html" % chan_path, html_header.replace("%title%", "%s | Logs" % channel)) write_string("%s/index.html" % chan_path, html_header.replace("%title%", "%s | Logs" % channel))
# Append channel to log index # Append channel to log index
append_line("logs/index.html", '<a href="%s/index.html">%s</a>' % (channel.replace("#", "%23"), channel)) append_line("logs/index.html", '<a href="%s/index.html">%s</a>' % (channel.replace("#", "%23"), channel))
# Current log # Current log
time = strftime("%H:%M:%S") time = strftime("%H:%M:%S")
date = strftime("%Y-%m-%d") date = strftime("%Y-%m-%d")
log_path = "logs/%s/%s.html" % (channel, date) log_path = "logs/%s/%s.html" % (channel, date)
# Create the log date index if it doesnt exist # Create the log date index if it doesnt exist
if not os.path.exists(log_path): if not os.path.exists(log_path):
write_string(log_path, html_header.replace("%title%", "%s | Logs for %s" % (channel, date))) write_string(log_path, html_header.replace("%title%", "%s | Logs for %s" % (channel, date)))
# Append date log # Append date log
append_line("%s/index.html" % chan_path, '<a href="%s.html">%s</a>' % (date, date)) append_line("%s/index.html" % chan_path, '<a href="%s.html">%s</a>' % (date, date))
# Append current message # Append current message
message = "<a href=\"#%s\" name=\"%s\" class=\"time\">[%s]</a> %s" % \ message = "<a href=\"#%s\" name=\"%s\" class=\"time\">[%s]</a> %s" % \
(time, time, time, msg) (time, time, time, msg)
append_line(log_path, message) append_line(log_path, message)
### These are the IRC events ### These are the IRC events
def on_all_raw_messages(self, c, e): def on_all_raw_messages(self, c, e):
"""Display all IRC connections in terminal""" """Display all IRC connections in terminal"""
if DEBUG: print e.arguments()[0] if DEBUG: print e.arguments()[0]
def on_welcome(self, c, e): def on_welcome(self, c, e):
"""Join channels after successful connection""" """Join channels after successful connection"""
if self.nick_pass: if self.nick_pass:
@ -252,25 +252,25 @@ class Logbot(SingleServerIRCBot):
for chan in self.chans: for chan in self.chans:
c.join(chan) c.join(chan)
def on_nicknameinuse(self, c, e): def on_nicknameinuse(self, c, e):
"""Nickname in use""" """Nickname in use"""
c.nick(c.get_nickname() + "_") c.nick(c.get_nickname() + "_")
def on_invite(self, c, e): def on_invite(self, c, e):
"""Arbitrarily join any channel invited to""" """Arbitrarily join any channel invited to"""
c.join(e.arguments()[0]) c.join(e.arguments()[0])
#TODO: Save? Rewrite config file? #TODO: Save? Rewrite config file?
### Loggable events ### Loggable events
def on_action(self, c, e): def on_action(self, c, e):
"""Someone says /me""" """Someone says /me"""
self.write_event("action", e) self.write_event("action", e)
def on_join(self, c, e): def on_join(self, c, e):
self.write_event("join", e) self.write_event("join", e)
def on_kick(self, c, e): def on_kick(self, c, e):
self.write_event("kick", e, self.write_event("kick", e,
{"%kicker%" : e.source(), {"%kicker%" : e.source(),
@ -282,16 +282,16 @@ class Logbot(SingleServerIRCBot):
def on_mode(self, c, e): def on_mode(self, c, e):
self.write_event("mode", e, self.write_event("mode", e,
{"%modes%" : e.arguments()[0], {"%modes%" : e.arguments()[0],
"%person%" : e.arguments()[1], "%person%" : e.arguments()[1] if len(e.arguments()) > 1 else "",
"%giver%" : nm_to_n(e.source()), "%giver%" : nm_to_n(e.source()),
}) })
def on_nick(self, c, e): def on_nick(self, c, e):
self.write_event("nick", e, self.write_event("nick", e,
{"%old%" : nm_to_n(e.source()), {"%old%" : nm_to_n(e.source()),
"%new%" : e.target(), "%new%" : e.target(),
}) })
def on_part(self, c, e): def on_part(self, c, e):
self.write_event("part", e) self.write_event("part", e)
@ -299,27 +299,27 @@ class Logbot(SingleServerIRCBot):
if e.arguments()[0].startswith(NICK): if e.arguments()[0].startswith(NICK):
c.privmsg(e.target(), self.format["help"]) c.privmsg(e.target(), self.format["help"])
self.write_event("pubmsg", e) self.write_event("pubmsg", e)
def on_pubnotice(self, c, e): def on_pubnotice(self, c, e):
self.write_event("pubnotice", e) self.write_event("pubnotice", e)
def on_privmsg(self, c, e): def on_privmsg(self, c, e):
print nm_to_n(e.source()), e.arguments() print nm_to_n(e.source()), e.arguments()
c.privmsg(nm_to_n(e.source()), self.format["help"]) c.privmsg(nm_to_n(e.source()), self.format["help"])
def on_quit(self, c, e): def on_quit(self, c, e):
self.write_event("quit", e) self.write_event("quit", e)
def on_topic(self, c, e): def on_topic(self, c, e):
self.write_event("topic", e) self.write_event("topic", e)
def main(): def main():
# Create the logs directory # Create the logs directory
if not os.path.exists("logs"): if not os.path.exists("logs"):
os.makedirs("logs") os.makedirs("logs")
write_string("logs/index.html", html_header.replace("%title%", "Chat Logs")) write_string("logs/index.html", html_header.replace("%title%", "Chat Logs"))
# Start the bot # Start the bot
bot = Logbot(SERVER, PORT, SERVER_PASS, CHANNELS, NICK, NICK_PASS) bot = Logbot(SERVER, PORT, SERVER_PASS, CHANNELS, NICK, NICK_PASS)
try: try:
@ -334,7 +334,7 @@ def main():
except KeyboardInterrupt: except KeyboardInterrupt:
if FTP_SERVER: f.quit() if FTP_SERVER: f.quit()
bot.quit() bot.quit()
if __name__ == "__main__": if __name__ == "__main__":
main() main()