From 97a73c931a145b84492da2649fd9c12063f9a2cf Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 20 Aug 2011 23:42:38 +0200 Subject: Add optional logging of channels --- miniircd | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/miniircd b/miniircd index 3764727..ab306a3 100755 --- a/miniircd +++ b/miniircd @@ -29,6 +29,7 @@ import string import sys import re import time +from datetime import datetime from optparse import OptionParser @@ -178,11 +179,12 @@ class Client(object): self.reply_461("JOIN") return if arguments[0] == "0": - for channelname, channel in self.channels.items(): + for (channelname, channel) in self.channels.items(): self.message_channel( channel, ":%s PART %s" % (self.prefix, channelname), True) + self.channel_log(channel, "left", meta=True) server.remove_member_from_channel(self, channelname) self.channels = {} else: @@ -200,6 +202,7 @@ class Client(object): channel, ":%s JOIN %s" % (self.prefix, channelname), True) + self.channel_log(channel, "joined", meta=True) if channel.topic: self.reply( "332 %s %s :%s" @@ -272,6 +275,9 @@ class Client(object): self.reply("432 %s %s :Erroneous Nickname" % (self.nickname, newnick)) else: + for x in self.channels.values(): + self.channel_log( + x, "changed nickname to %s" % newnick, meta=True) oldnickname = self.nickname self.nickname = newnick server.client_changed_nickname(self, oldnickname) @@ -304,11 +310,9 @@ class Client(object): if channel: self.message_channel( channel, - ":%s %s %s :%s" % ( - self.prefix, - command, - channel.name, - message)) + ":%s %s %s :%s" + % (self.prefix, command, channel.name, message)) + self.channel_log(channel, message) else: self.reply("401 %s %s :No such nick/channel" % (self.nickname, targetname)) @@ -337,6 +341,7 @@ class Client(object): channelname, partmsg), True) + self.channel_log(channel, "left (%s)" % partmsg, meta=True) del self.channels[irc_lower(channelname)] server.remove_member_from_channel(self, channelname) @@ -373,6 +378,8 @@ class Client(object): channelname, newtopic), True) + self.channel_log( + channel, "set topic to %r" % newtopic, meta=True) else: if channel.topic: self.reply("332 %s %s :%s" @@ -493,6 +500,19 @@ class Client(object): if client != self or include_self: client.message(line) + def channel_log(self, channel, message, meta=False): + if not self.server.logdir: + return + if meta: + format = "[%s] * %s %s\n" + else: + format = "[%s] <%s> %s\n" + timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") + logname = channel.name.replace("_", "__").replace("/", "_") + fp = open("%s/%s.log" % (self.server.logdir, logname), "a") + fp.write(format % (timestamp, self.nickname, message)) + fp.close() + def message_related(self, msg, include_self=False): clients = set() if include_self: @@ -522,10 +542,13 @@ class Server(object): self.motdfile = options.motd self.verbose = options.verbose self.debug = options.debug + self.logdir = options.logdir self.name = socket.getfqdn()[:63] # Server name limit from the RFC. self.channels = {} # irc_lower(Channel name) --> Channel instance. self.clients = {} # Socket --> Client instance. self.nicknames = {} # irc_lower(Nickname) --> Client instance. + if self.logdir and not os.path.isdir(self.logdir): + os.makedirs(self.logdir) def daemonize(self): try: @@ -595,8 +618,9 @@ class Server(object): def remove_client(self, client, quitmsg): client.message_related(":%s QUIT :%s" % (client.prefix, quitmsg)) - for chan in client.channels.values(): - chan.remove_client(client) + for x in client.channels.values(): + client.channel_log(x, "quit (%s)" % quitmsg, meta=True) + x.remove_client(client) if client.nickname \ and irc_lower(client.nickname) in self.nicknames: del self.nicknames[irc_lower(client.nickname)] @@ -666,6 +690,10 @@ def main(argv): "--debug", action="store_true", help="print debug messages to stdout") + op.add_option( + "--logdir", + metavar="X", + help="store channel log in directory X") op.add_option( "--motd", metavar="X", -- cgit v1.2.3