#!/usr/bin/python """IRC bot shell using IRC Async. Sean B. Palmer, 2002. GPL 2. (thanks to deltab)""" # # # # # # # # # # # # # # # # # # # Main config stuff NickName = 'utilbot' # End of main config stuff # # # # # # # # # # # # # # # # # # import ircAsync import sys, urllib, urllib2, time, datetime, 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) def parseOrigin(origin): nickid, host = tuple(origin.split('@')) nick, userid = tuple(nickid.split('!')) return (nick, userid, host) def runIRC(hostName, port, chan): c = ircAsync.T() c.startChannels(chan) c.nick = NickName c.userid = 'blogbot' c.info = {} # Put any new functions here def hi(m, origin, (mtype, channel), text, c=c): c.tell(channel, 'hi, %s' % origin.split('!')[0]) c.bind(hi, PRIVMSG, r"(?i)^(Hi|Hey|welcome),? %s!?$" % NickName) def help(m, origin, (mtype, channel), text, c=c): for line in ( "Hi there. I'm a utility bot",): c.tell(channel, line) time.sleep(1.5) c.bind(help, PRIVMSG, r"(?i)^(help),? %s!?$" % NickName) c.bind(help, PRIVMSG, r"(?i)^%s[:,]? help!?$" % NickName) def ping(m, origin, (mtype, channel), text, c=c): c.tell(channel, 'pong') c.bind(ping, PRIVMSG, r"(?i)^ping$") def showTime(m, origin, (mtype, channel), test, c=c): nick, userid, host = parseOrigin(origin) there = 0 hour, minute, second = m.group(1).split(':') print hour, minute, second there += (int(hour.lstrip('0')) * 3600) there += (int(minute.lstrip('0')) * 60) there += int(second.lstrip('0')) here = 0 now = datetime.datetime.now() here += (now.hour * 3600) here += (now.minute * 60) here += now.second offset = (here - there) / (30 * 60) # in half hour periods... t = now - datetime.timedelta(seconds=offset*(3600/2)) if offset < 0: offset = -(24 % abs(offset / 2)) elif offset > 0: offset = 24 % (offset / 2) t = t.strftime('%H:%M:%S' + (' (timezone: %s)' % offset)) c.tell(c.info.get((nick, 'time')) or nick, t) c.bind(showTime, NOTICE, r"TIME .+(\d\d:\d\d:\d\d)") def getTime(m, origin, (mtype, channel), text, c=c): nick, userid, host = parseOrigin(origin) c.tell(nick, ctcp('TIME')) c.info[(nick, 'time')] = channel c.bind(getTime, PRIVMSG, r"^time\?$") def bug(m, origin, (mtype, channel), text, c=c): nickname = m.group(1) while 1: for line in ( "hey, %s", "%s, are you there? hello?", "uh... %s?", "hey! %s. I'm talking to you", "damnit %s, answer me!", "%s?", "%s?", "hey, %s, I'm talking to you, bitch", "ehy! %s!", "argh... %s?"): c.tell(channel, line % nickname) time.sleep(3.5) # c.bind(bug, PRIVMSG, r"^bug (\S+)$") c.makeConn(hostName, port) while 1: try: asyncore.loop() except: time.sleep(10) def main(argv=None): if argv is None: argv = sys.argv if len(argv) > 2: server, chans = argv[1], argv[2:] chans = [('#%s' % c, c)[c.startswith('#')] for c in chans] if ':' in server: server, port = server.split(':') else: port = '6667' runIRC(server, int(port), chans) else: print __doc__ if __name__=='__main__': main()