$onr, $xkcd
[halbot.git] / factoids.py
blob9137d46d2b428d9b43033acb89ad66d1f8379d02
1 #!/usr/bin/env python
2 from __future__ import generators
3 import re, ircbot
5 import perms
6 from globals import commands, nick, factoid_dbs, locked_dbs
7 from basic_commands import reply, troggie
8 from common import extract_my_db, resize
10 is_re = re.compile(" (is|are|was|were)( |$)", re.IGNORECASE)
11 nick_re = re.compile(nick + "[:,] ", re.IGNORECASE)
13 def get_fact_dbs(who, where):
14 factoids = extract_my_db(factoid_dbs, who, where, ircbot.IRCDict)
15 locked = extract_my_db(locked_dbs, who, where, ircbot.IRCDict)
16 return (factoids, locked)
18 def do_is(who, where, args):
19 "Handles implicit factoid setting."
20 (key, to_be, garbage, fact) = args
21 (factoids, locked) = get_fact_dbs(who, where)
22 if key.endswith('?'):
23 key = key[:-1]
24 if locked.get(key):
25 #reply(who, where, "I'm sorry, %s, I'm afraid I can't do that." % who, all=True)
26 return
27 elif fact:
28 factoids[key] = (to_be, fact)
29 elif key in factoids:
30 del factoids[key]
31 do_is = troggie(do_is)
32 commands['do is'] = (perms.voice, do_is)
34 def forget(who, where, args):
35 "$forget <prompt>: Asks Hal to forget his factoid for the given prompt. For compatibility with troggie, you can omit the $."
36 (factoids, locked) = get_fact_dbs(who, where)
37 key = args
38 if locked.get(key):
39 reply(who, where, "I'm sorry, %s, I'm afraid I can't do that." % who, all=True)
40 elif key in factoids:
41 del factoids[key]
42 reply(who, where, "I forgot %s." % key)
43 else:
44 reply(who, where, "I don't have anything matching '%s'." % key)
45 forget = troggie(forget)
46 commands['forget'] = (perms.voice, forget)
48 def force(who, where, args):
49 "$force <prompt> (is/was/are/were) <something>: Sets a factoid, even if it's locked."
50 (factoids, locked) = get_fact_dbs(who, where)
51 if not is_re.search(args):
52 reply(who, where, "Syntax: $force a (is/are/was/were) b")
53 return
54 (key, to_be, garbage, fact) = resize(is_re.split(args, 1),4)
55 if key.endswith('?'):
56 key = key[:-1]
57 if fact:
58 factoids[key] = (to_be, fact)
59 elif key in factoids:
60 del factoids[key]
61 commands['force'] = (perms.op, force)
63 def lock(who, where, args):
64 "$lock <prompt>: Locks Hal's factoid for a given prompt."
65 (factoids, locked) = get_fact_dbs(who, where)
66 if args.endswith('?'):
67 args = args[:-1]
68 if not locked.get(args):
69 locked[args] = True
70 reply(who, where, "Done.")
71 else:
72 reply(who, where, "It's already locked.")
73 commands['lock'] = (perms.op, lock)
75 def unlock(who, where, args):
76 "$unlock <prompt>: Unlocks the factoid for a given prompt."
77 (factoids, locked) = get_fact_dbs(who, where)
78 if args.endswith('?'):
79 args = args[:-1]
80 if locked.get(args):
81 locked[args] = False
82 reply(who, where, "Done.")
83 else:
84 reply(who, where, "It's not locked.")
85 commands['unlock'] = (perms.op, unlock)
87 def factoid(who, where, args, implicit=False):
88 "$fact <prompt> or $factoid <prompt>: Asks Hal to look up his factoid for the given prompt."
89 (factoids, locked) = get_fact_dbs(who, where)
90 if args.endswith('?'):
91 args = args[:-1]
92 if args in factoids:
93 (to_be, fact) = factoids[args]
94 if not implicit and fact.count("$who"):
95 implicit = True
96 response = ""
97 lfact = fact.lower()
98 if lfact.startswith("<"):
99 if lfact.startswith("<raw>"):
100 response = fact[5:].lstrip()
101 elif lfact.startswith("<reply>"):
102 response = fact[7:].lstrip()
103 elif lfact.startswith("<action>"):
104 response = "\x01ACTION %s\x01" % fact[8:].lstrip()
105 if not response:
106 response = "I hear that %s %s %s" % (args, to_be, fact)
107 response = response.replace("$who", who)
108 reply(who, where, response, all=implicit)
109 elif not implicit:
110 reply(who, where, "I don't have anything matching '%s'." % args)
111 commands['fact'] = (perms.voice, factoid)
112 commands['factoid'] = (perms.voice, factoid)
114 def factoid_implicit(who, where, args):
115 "Implicit form of $fact[oid]."
116 factoid(who, where, args, implicit=True)
117 factoid_implicit = troggie(factoid_implicit)
118 commands['factoid implicit'] = (perms.voice, factoid_implicit)