diff options
author | Brian Sherson <caretaker82@euclid.shersonb.net> | 2014-02-09 13:25:24 -0800 |
---|---|---|
committer | Brian Sherson <caretaker82@euclid.shersonb.net> | 2014-02-09 13:25:24 -0800 |
commit | 13ca15787aaccb8eb17f4d0b548b17b7036fbaf5 (patch) | |
tree | df87fbb522add8506ca669f097555f0ace2b3308 /quote.py | |
parent | dba21d3b8b4b4252fd6d95a9d1cc92e968e9b56d (diff) |
Introducing quote addon.
Diffstat (limited to 'quote.py')
-rw-r--r-- | quote.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/quote.py b/quote.py new file mode 100644 index 0000000..2243915 --- /dev/null +++ b/quote.py @@ -0,0 +1,55 @@ +import os +import random +import re +import codecs + + +class quote(object): + + def __init__(self, quotefile="quotes.txt", encoding="utf8"): + self.quotefile = quotefile + self.encoding = encoding + if os.path.isfile(quotefile): + with codecs.open(quotefile, encoding=encoding) as f: + self.quotes = [line for line in f] + else: + self.quotes = [] + + def onChanMsg(self, IRC, user, channel, targetprefix, msg): + matches = re.findall( + r"^!quote(?:\s+(--add|--rem|--flush))?(?:\s+(.+?)\s*)?$", msg) + if matches: + cmd, msg = matches[0] + if cmd == "" and msg == "": + if self.quotes: + channel.msg(random.choice(self.quotes)) + else: + channel.msg("%s: There are no quotes!" % user.nick) + elif cmd == "--add": + if msg: + if msg not in self.quotes: + self.quotes.append(msg) + channel.msg("%s: Quote added." % user.nick) + else: + channel.msg("%s: Quote already exists." % user.nick) + else: + channel.msg("%s: What am I adding?" % user.nick) + elif cmd == "--rem": + if msg: + if msg in self.quotes: + self.quotes.remove(msg) + channel.msg("%s: Quote removed." % user.nick) + else: + channel.msg("%s: Quote does not exist." % user.nick) + else: + channel.msg("%s: What am I removing?" % user.nick) + elif cmd == "--flush": + with codecs.open(self.quotefile, "w", encoding=encoding) as f: + for line in self.quotes: + print >>f, line + else: + channel.msg( + "I am sorry, %s, but I cannot do that." % user.nick) + + def onSendChanMsg(self, IRC, channel, targetprefix, msg, origin): + self.onChanMsg(IRC, IRC.identity, channel, targetprefix, msg) |