From 8fba54e33dddbfc52eff3e3bcad8826506d04007 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Mon, 26 Jul 2010 18:14:33 -0500 Subject: [PATCH] fact module. gets a random line out of the file, still needs search support, adding, whatever else --- dr.botzo.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/dr.botzo.py b/dr.botzo.py index e643b73..4b9334c 100755 --- a/dr.botzo.py +++ b/dr.botzo.py @@ -511,6 +511,49 @@ class IrcAdmin(Module): else: connection.privmsg(replypath, replystr) +class FactFile(Module): + """Returns a random fact/quote/whatever from one or more files + """ + + def __init__(self, config, server, modlist): + super(FactFile, self).__init__(config, server, modlist) + modlist.append(self) + + def register_handlers(self, server): + server.add_global_handler('pubmsg', self.on_pubmsg) + server.add_global_handler('privmsg', self.on_privmsg) + + def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): + whats = what.split(' ') + try: + filename = self.config.get('fact', whats[0]) + if replypath is not None: + what = self.try_recursion(connection, event, nick, userhost, replypath, what, admin_unlocked) + whats = what.split(' ') + + # open file + if os.path.isfile(filename): + # http://www.regexprn.com/2008/11/read-random-line-in-large-file-in.html + file = open(filename, 'r') + count = 0 + to_print = cur = file.readline() + while cur: + cur = file.readline() + count += 1 + if random.randint(0, count) == 0: + # 1/count chance to replace + to_print = cur + + # return text + if replypath is None: + return to_print.rstrip() + else: + connection.privmsg(replypath, to_print.rstrip()) + else: + print('filename in config file for \'' + whats[0] + '\' is wrong') + + except NoOptionError: pass + ##### # init ##### @@ -545,6 +588,7 @@ modlist = [] count = Countdown(config, server, modlist) dice = Dice(config, server, modlist) +fact = FactFile(config, server, modlist) admin = IrcAdmin(config, server, modlist) gt = GoogleTranslate(config, server, modlist) seen = Seen(config, server, modlist)