commit cc83cfdbd5e39f8508d8d61fe5a05acafe62758a Author: Jonathan Harker Date: Sun May 30 18:15:52 2010 +1200 Working IRC LOL bot. diff --git a/lolbot b/lolbot new file mode 100755 index 0000000..d621979 --- /dev/null +++ b/lolbot @@ -0,0 +1,2 @@ +#!/bin/sh +/usr/bin/python lolbot.py irc.freenode.net "#zomglol" lolbot diff --git a/lolbot.py b/lolbot.py new file mode 100644 index 0000000..7daf75a --- /dev/null +++ b/lolbot.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python +# +# LolBot +# +# Code originally based on example bot and irc-bot class from +# Joel Rosdahl , author of included python-irclib. + +""" +Useful bot for folks stuck behind censor walls at work +""" + +import sys, string, random, time +from ircbot import SingleServerIRCBot +from irclib import nm_to_n, nm_to_h, irc_lower +import botcommon +import urllib +import libxml2doc + +# Exclamations - wrong input + +exclamations = [ + "Zing!", + "Burns!", + "Tard!", + "Lol.", + "Crazy!", + "WTF?", +] + +# Ponderings + +ponderings = [ + "Hi, can I have a medium lamb roast, with just potatoes.", + "Can I slurp on your Big Cock?", + "Your Mum likes it two in the pink one in the stink.", +] + +class LolBot(SingleServerIRCBot): + def __init__(self, channel, nickname, server, port): + SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname) + + self.channel = channel + self.nickname = nickname + self.urls = [] + + self.helptext = "Adds URLs to a list. Commands: list - prints a bunch of URLs; clear - clears the list; lol - say something funny; - adds the URL to the list; help - this message." + + self.queue = botcommon.OutputManager(self.connection) + self.queue.start() + self.start() + + def on_nicknameinuse(self, connection, event): + self.nickname = connection.get_nickname() + "_" + connection.nick(self.nickname) + + def on_welcome(self, connection, event): + connection.join(self.channel) + + def on_privmsg(self, connection, event): + "Deal with a /msg private message." + from_nick = nm_to_n(event.source()) + self.do_command(event, event.arguments()[0], from_nick) + + def on_pubmsg(self, connection, event): + "Deal with a public message in a channel." + + # TODO: log it + + # TODO: parse it for links and add it to the list + + from_nick = nm_to_n(event.source()) + args = string.split(event.arguments()[0], ":", 1) + if len(args) > 1 and irc_lower(args[0]) == irc_lower(self.nickname): + self.do_command(event, string.strip(args[1]), from_nick) + + def say_public(self, text): + "Print TEXT into public channel, for all to see." + self.queue.send(text, self.channel) + + def say_private(self, nick, text): + "Send private message of TEXT to NICK." + self.queue.send(text, nick) + + def reply(self, text, to_private=None): + "Send TEXT to either public channel or TO_PRIVATE nick (if defined)." + if to_private is not None: + self.say_private(to_private, text) + else: + self.say_public(text) + + def ponder(self): + "Return a random string indicating what Pinky's pondering." + return random.choice(ponderings) + + def exclaim(self): + "Return a random exclamation string." + return random.choice(exclamations) + + def do_command(self, event, cmd, from_private): + """ + This is the function called whenever someone sends a public or + private message addressed to the bot. (e.g. "bot: blah"). + """ + + if event.eventtype() == "pubmsg": + # self.reply() sees 'from_private = None' and sends to public channel. + target = None + else: + # assume that from_private comes from a 'privmsg' event. + target = from_private.strip() + + # Be forgiving about capitalization and whitespace. + #cmd = cmd.replace(" ", "").lower() + + if cmd == 'help': + self.reply(self.helptext, target) + + elif cmd == 'lol': + self.reply(self.ponder(), target) + + elif cmd == 'urls' or cmd == 'list': + urls = ' || '.join(self.urls) + if urls == '': + urls = 'No URLs yet.' + self.reply(urls, target) + + elif cmd.startswith('http:') or cmd.startswith('https:'): + self.urls.append(cmd) + self.reply("URL added.", target) + + elif cmd == 'clear': + del self.urls + self.urls = [] + self.reply('URLs cleared.', target) + + else: + self.reply(self.exclaim(), target) + + +if __name__ == "__main__": + try: + botcommon.trivial_bot_main(LolBot) + except KeyboardInterrupt: + print "Shutting down." +