#!/usr/bin/python """IRC bot shell using IRC Async. Sean B. Palmer, 2002. GPL 2. (thanks to deltab)""" # # # # # # # # # # # # # # # # # # # Main config stuff NickName = 'logger' TimeStamp = 1 # 1 for yes, 0 for no _text = "<%s> %s" _action = "* %s %s" _join = "*** %s has joined %s" _part = "*** %s has left %s" _kick = "*** %s was kicked by %s (%s)" _mode = "*** %s sets mode: %s" _topic = "*** %s changes topic to '%s'" _quit = "*** %s has quit IRC (%s)" _nick = "*** %s is now known as %s" # End of main config stuff # # # # # # # # # # # # # # # # # # import ircAsync import re, os, sys, base64, socket, urllib, time, asyncore from ircAsync import PRIVMSG, NOTICE, PING, PONG, USER from ircAsync import NICK, JOIN, PART, INVITE, QUIT from ircAsync import SPC, CR, LF, CRLF, RPL_WELCOME def ctcp(s): return '\x01%s\x01' % s def me(s): return ctcp('ACTION %s' % s) global people people = {} global channels channels = [] # channels that this bot's on import os, os.path global directories directories = [] for entry in os.listdir(os.getcwd()): if os.path.isdir(entry): directories.append(entry) def log(channel, text): fn = time.strftime('%Y-%m-%d.log', time.gmtime(time.time())) if channel not in directories: os.mkdir(channel) # if it fails, the exception may as well be raised directories.append(channel) if TimeStamp: t = time.strftime('[%H:%M]', time.gmtime(time.time())) text = t + ' ' + text text += '\n' open(os.path.join(channel, fn), 'a').write(text) def main(hostName, port, chan): c = ircAsync.T() c.startChannels(chan) c.nick = NickName c.userid = 'logger' # Put any new functions here def hi(m, origin, args, text, c=c): c.tell(args[1], 'hi, %s' % origin.split('!')[0]) c.bind(hi, PRIVMSG, r"(?i)^(Hi|Hey|welcome)(,)? %s(\!)?$" % NickName) def logText(m, origin, args, text, c=c): channel = args[1] nick = origin if '!' in origin: nick = origin.split('!')[0] if not (text.startswith('\x01ACTION ') and text.endswith('\x01')): text = _text % (nick, text) else: text = _action % (nick, text[8:-1]) try: if not text[text.index(' ')+1:].startswith('[off]'): log(channel, text) except ValueError: log(channel, text) c.bind(logText, PRIVMSG, r"(.*)") def logTopic(m, origin, args, text, c=c): channel = args[1] nick = origin if '!' in origin: nick = origin.split('!')[0] topic = _topic % (nick, text) log(channel, topic) c.bind(logTopic, 'TOPIC', r"(.*)") def logMode(m, origin, args, text, c=c): channel = args[1] nick = origin if '!' in origin: nick = origin.split('!')[0] mode = _mode % (nick, ' '.join(args[2:])) log(channel, mode) c.bind(logMode, 'MODE', r"(.*)") def logJoin(m, origin, args, text, c=c): channel = text nick = origin if '!' in origin: nick = origin.split('!')[0] if nick == NickName: if channel not in channels: channels.append(channel) t = time.strftime('%a %b %d %H:%M:%S %Y', time.gmtime(time.time())) log(channel, "Session Start: %s" % t) log(channel, "Session Ident: %s" % channel) join = _join % (nick, channel) log(channel, join) if people.has_key(nick): if channel not in people[nick]: people[nick].append(channel) else: people[nick] = [channel] c.bind(logJoin, 'JOIN', r"(.*)") def logNames(m, origin, args, text, c=c): channel = args[3] raw_nicks = text.split(' ') for nick in raw_nicks: if nick.startswith('@') or nick.startswith('+'): nick = nick[1:] if people.has_key(nick): if channel not in people[nick]: people[nick].append(channel) else: people[nick] = [channel] c.bind(logNames, '353', r"(.*)") def logPart(m, origin, args, text, c=c): if nick == NickName: if channel in channels: channels.remove(channel) channel = args[1] nick = origin if '!' in origin: nick = origin.split('!')[0] part = _part % (nick, text) log(channel, part) try: people[nick].remove(channel) except: print >> sys.stderr, "Something messed up, but it doesn't matter" c.bind(logPart, 'PART', r"(.*)") def logQuit(m, origin, args, text, c=c): nick = origin if '!' in origin: nick = origin.split('!')[0] quit = _quit % (nick, text) if people.has_key(nick): for channel in people[nick]: log(channel, quit) del people[nick] c.bind(logQuit, 'QUIT', r"(.*)") def logKick(m, origin, args, text, c=c): if nick == NickName: if channel in channels: channels.remove(channel) channel = args[1] nick = origin if '!' in origin: nick = origin.split('!')[0] kickee = args[2] kick = _kick % (kickee, nick, text) log(channel, kick) try: people[nick].remove(channel) except: print >> sys.stderr, "Something messed up, but it doesn't matter" c.bind(logKick, 'KICK', r"(.*)") def logNick(m, origin, args, text, c=c): old = origin if '!' in origin: old = origin.split('!')[0] new = text nick = _nick % (old, new) if people.has_key(old): for channel in people[old]: log(channel, nick) people[new] = people[old] del people[old] c.bind(logNick, 'NICK', r"(.*)") c.makeConn(hostName, port) try: asyncore.loop() except: t = time.strftime('%a %b %d %H:%M:%S %Y', time.gmtime(time.time())) for channel in channels: log(channel, "Session Close: %s" % t) sys.exit(0) if __name__=='__main__': if len(sys.argv) > 2: server, chans = sys.argv[1], sys.argv[2:] chans = [('#%s' % c, c)[c.startswith('#')] for c in chans] if ':' in server: server, port = server.split(':') else: port = '6667' main(server, int(port), chans) else: print __doc__