From b5ce2fa675de86da11c4865fd2685a72de0c9cd8 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 25 Apr 2011 21:26:05 -0400 Subject: [PATCH] moving watcher rules to their own file in config/ so bot is easier to customize --- config/irc.py | 2 -- config/watcher.py | 33 +++++++++++++++++++++++++++++++++ irc/rc.py | 2 +- irc/watcher.py | 27 +++++++-------------------- 4 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 config/watcher.py diff --git a/config/irc.py b/config/irc.py index 1bd35d1..ee9ef3e 100644 --- a/config/irc.py +++ b/config/irc.py @@ -19,8 +19,6 @@ REALNAME = "[[w:en:User:EarwigBot]]" # channels to join on main server's startup CHANS = ["##earwigbot", "##earwig", "#wikipedia-en-afc"] -AFC_CHANS = ["#wikipedia-en-afc"] # report recent AfC changes/give AfC status messages upon join -BOT_CHANS = ["##earwigbot", "#wikipedia-en-afc"] # report edits containing "!earwigbot" # hardcoded hostnames of users with certain permissions OWNERS = ["wikipedia/The-Earwig"] # can use owner-only commands (!restart and !git) diff --git a/config/watcher.py b/config/watcher.py new file mode 100644 index 0000000..5830c99 --- /dev/null +++ b/config/watcher.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +# EarwigBot Configuration File +# This file contains rules for the bot's watcher component. + +import re + +AFC_CHANS = ["#wikipedia-en-afc"] # report recent AfC changes/give AfC status messages upon join +BOT_CHANS = ["##earwigbot", "#wikipedia-en-afc"] # report edits containing "!earwigbot" + +def process(rc): + chans = set() # channels to report this message to + page_name = rc.page.lower() + + if "!earwigbot" in rc.msg.lower(): + chans.update(BOT_CHANS) + + if re.match("wikipedia( talk)?:(wikiproject )?articles for creation", page_name): + chans.update(AFC_CHANS) + + elif re.match("wikipedia( talk)?:files for upload", page_name): + chans.update(AFC_CHANS) + + elif page_name.startswith("template:afc submission"): + chans.update(AFC_CHANS) + + elif rc.flags == "delete" and re.match("deleted \"\[\[wikipedia( talk)?:(wikiproject )?articles for creation", rc.comment.lower()): + chans.update(AFC_CHANS) + + elif rc.flags == "protect" and re.match("protected \"\[\[wikipedia( talk)?:(wikiproject )?articles for creation", rc.comment.lower()): + chans.update(AFC_CHANS) + + return chans diff --git a/irc/rc.py b/irc/rc.py index f1c4213..109e77e 100644 --- a/irc/rc.py +++ b/irc/rc.py @@ -27,7 +27,7 @@ class RC: self.page, self.flags, self.url, self.user, self.comment = page, flags, url, user, comment - def pretty(self): + def get_pretty(self): """make a nice, colorful message from self.msg to send to the front-end""" pretty = self.msg return pretty diff --git a/irc/watcher.py b/irc/watcher.py index cbaac80..3e215b2 100644 --- a/irc/watcher.py +++ b/irc/watcher.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- ## Imports -import re - from config.irc import * +from config.watcher import * from irc.connection import Connection from irc.rc import RC @@ -48,23 +47,11 @@ def main(connection, f_conn): if line[1] == "376": # Join the recent changes channel when we've finished starting up connection.join(WATCHER_CHAN) -def report(msg, chans): - """send a message to a list of report channels on our front-end server""" - for chan in chans: - frontend_conn.say(chan, msg) - def check(rc): """check if we're supposed to report this message anywhere""" - page_name = rc.page.lower() - pretty_msg = rc.pretty() - - if "!earwigbot" in rc.msg.lower(): - report(pretty_msg, chans=BOT_CHANS) - if re.match("wikipedia( talk)?:(wikiproject )?articles for creation", page_name): - report(pretty_msg, chans=AFC_CHANS) - elif re.match("wikipedia( talk)?:files for upload", page_name): - report(pretty_msg, chans=AFC_CHANS) - elif page_name.startswith("template:afc submission"): - report(pretty_msg, chans=AFC_CHANS) - if rc.flags == "delete" and re.match("deleted \"\[\[wikipedia( talk)?:(wikiproject )?articles for creation", rc.comment.lower()): - report(pretty_msg, chans=AFC_CHANS) + results = process(rc) # process the message in config/watcher.py, and get a list of channels to send it to + if not results: + return + pretty = rc.get_pretty() + for chan in results: + frontend_conn.say(chan, pretty)