From 25133138c1d97ecdab245ac14e033d5c85c5cb9a Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Mon, 9 Jun 2008 05:59:56 -0600 Subject: [PATCH] almost working now :) --- bot.py | 113 +++++++++++++++++++++++++++++++++++++++++++++ irc.pyc | Bin 0 -> 4366 bytes plugins/filter.py | 24 ++++++++++ plugins/weather.py | 54 ++++++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100755 bot.py create mode 100644 irc.pyc create mode 100644 plugins/filter.py create mode 100755 plugins/weather.py diff --git a/bot.py b/bot.py new file mode 100755 index 0000000..70eb50c --- /dev/null +++ b/bot.py @@ -0,0 +1,113 @@ +#!/usr/bin/python + +network = "irc.synirc.net" +nickname = "skybot" +channel = "#mongbot" + +import sys, os, glob, imp, re +import thread, Queue, copy +import irc, yaml + +os.chdir(sys.path[0]) #do stuff relative to the installation directory + + +class Empty(object): #this is used to store attributes + pass + + +class Bot(object): + def __init__(self): + self.plugins = {} + self.commands = {} + self.listens = {} + self.filters = {} + self.daemons = {} + + def command(self, name, func, **filterargs): + self.commands[name] = (func, filterargs) + + def listen(self, name, func): + self.listens[name] = func + + def filter(self, name, func): + self.filters[name] = func + + def daemon(self, name, func): + self.daemons[name] = func + +bot = Bot() + +print 'Loading plugins' +for filename in glob.glob("plugins/*.py"): + shortname = os.path.splitext(os.path.basename(filename))[0] + try: + bot.plugins[shortname] = imp.load_source(shortname,filename) + except Exception, e: + print e + +print bot.plugins + +print 'Registering plugins' +for name, plugin in bot.plugins.iteritems(): + if hasattr(plugin, 'setup'): + try: + plugin.setup(bot) + except Exception, e: + print e + +print 'Connecting to IRC' + +bot.irc = irc.irc(network, nickname) +bot.irc.join(channel) +bot.commandprefix = '^(?:\.|'+nickname+'[:,]*\s*)' + +if bot.daemons: + print 'Running daemons' + for daemon in bot.daemons.itervalues(): + thread.start_new_thread(daemon, ()) + +print 'Running main loop' + +class Input(object): + def __init__(self, raw, prefix, command, + params, nick, user, host, paraml, msg): + self.raw = raw + self.prefix = prefix + self.command = command + self.params = params + self.nick = nick + self.user = user + self.host = host + self.paraml = paraml + self.msg = msg + +class FakeBot(object): + def __init__(self, bot, input): + self.bot = bot + self.input = input + + def say(self, msg): + self.bot.irc.msg(input.paraml[0], msg) + + def reply(self, msg): + self.say(input.nick + ': ' + msg) + +print bot.commands + +while True: + try: + out = bot.irc.out.get(timeout=1) + #print repr(out) + for func, args in bot.commands.itervalues(): + input = Input(*out) + for filt in bot.filters.itervalues(): + input = filt(bot, func, args, input) + if input == False: + break + if input == False: + continue + thread.start_new_thread(func,(FakeBot(bot, input), input)) + except Queue.Empty: + pass + #except KeyboardInterrupt: + # sys.exit() diff --git a/irc.pyc b/irc.pyc new file mode 100644 index 0000000000000000000000000000000000000000..55f544e0ae35353299160231d05a8acd7c0cb49b GIT binary patch literal 4366 zcmb7HYjYIG6}_|X(E~8HA;d8(7^ha)mPk@%yA&*8%NP`gg|vuMk!_}ynOSMj?#wdX z8&H+>iTs#+&)>+eN#%QT&h3536cuEx`tI$=^xS*ybN~78<%R$J84foksYu$8tR;P1 zSe3tvycGHCZYXIW*@7fBd5M=TPN#L9E={Kmoi0zOO`V>Rq$TNDy)m5Kc#cZ!bf{VO zU;K4l3v65t`jLxAGtC1meuZIw50XM>)sq?qyo%~ol!0z4E)}ckN<)&mGEFf%odz%Y z8suItPX?)N<1OvD$n!LIS&?JD-*a5BnRV@=-yXaEC!e{MsK^34>kt3iMNPsVZ3PCx6n3zWp(=&B(KudJ7d4g|oB@4wZNVceSNnTcP zS)fOk<;Xt|R0a4^h_0=GE-&EyBAv4!ie=9p<}sAQO(HspbV{6~D9bY!MeYnHHg%Cp z%VC!HTv0+SktgGRKP_DoY%$^M;Gd1tajFN3Ih%QQ8T)aW_FNj-B0fl+o|~||=*w@S z?e3jNZV~*>gRO6)ok!-*m-k%_^Zmj)WkwJ{mC~6_2Yo|@%sG$_h9~xy;MVqwVY*fx z_VWETTgGco3b&S(@#^T%@JhbYo(GZOT(BI3_$=Xb0p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OK@e?4`a;Z7*Py97!n8C~i67HK8i8>pY=`We#C>x~acij4v -- queries google weather API for weather data" + q = input.re.groups()[0] + cond = goog_weather(q) + + if cond.has_key('error'): + bot.reply(u'Couldn\'t fetch weather data for "%s", try using a zip/postal code' % (q)) + return + + format = u'%s %sF (%s/%s/%s) (h:%sF,l:%sF)' + args = (cond['place'],cond['temp'],cond['condition'],cond['wind'],cond['humidity'],cond['high'],cond['low']) + + bot.reply(format.encode('utf-8') % args)