*quietly adds a file that should have been in the tree since 9839a*
[halbot.git] / factoids.py
blob58a0da6c5a9ffa4fa7e88a8444bed5825d986ab2
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, factoids_on, yes, no
7 from basic_commands import reply, troggie
8 from common import extract_my_db, resize, real_where
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 are_factoids_on(who, where):
19 return extract_my_db(factoids_on, who, where, lambda: True)
20 factoids_are_on = are_factoids_on
22 def do_is(who, where, args):
23 "Handles implicit factoid setting."
24 if not factoids_are_on(who, where):
25 return
26 (key, to_be, garbage, fact) = args
27 (factoids, locked) = get_fact_dbs(who, where)
28 if key.endswith('?'):
29 key = key[:-1]
30 if locked.get(key):
31 #reply(who, where, "I'm sorry, %s, I'm afraid I can't do that." % who, all=True)
32 return
33 elif fact:
34 factoids[key] = (to_be, fact)
35 elif key in factoids:
36 del factoids[key]
37 do_is = troggie(do_is)
38 commands['do is'] = (perms.voice, do_is)
40 def forget(who, where, args):
41 "$forget <prompt>: Asks Hal to forget his factoid for the given prompt. For compatibility with troggie, you can omit the $."
42 if not factoids_are_on(who, where):
43 return
44 (factoids, locked) = get_fact_dbs(who, where)
45 key = args
46 if locked.get(key):
47 reply(who, where, "I'm sorry, %s, I'm afraid I can't do that." % who, all=True)
48 elif key in factoids:
49 del factoids[key]
50 reply(who, where, "I forgot %s." % key)
51 else:
52 reply(who, where, "I don't have anything matching '%s'." % key)
53 forget = troggie(forget)
54 commands['forget'] = (perms.voice, forget)
56 def force(who, where, args):
57 "$force <prompt> (is/was/are/were) <something>: Sets a factoid, even if it's locked."
58 (factoids, locked) = get_fact_dbs(who, where)
59 if not is_re.search(args):
60 reply(who, where, "Syntax: $force a (is/are/was/were) b")
61 return
62 (key, to_be, garbage, fact) = resize(is_re.split(args, 1),4)
63 if key.endswith('?'):
64 key = key[:-1]
65 if fact:
66 factoids[key] = (to_be, fact)
67 elif key in factoids:
68 del factoids[key]
69 commands['force'] = (perms.op, force)
71 def lock(who, where, args):
72 "$lock <prompt>: Locks Hal's factoid for a given prompt."
73 (factoids, locked) = get_fact_dbs(who, where)
74 if args.endswith('?'):
75 args = args[:-1]
76 if not locked.get(args):
77 locked[args] = True
78 reply(who, where, "Done.")
79 else:
80 reply(who, where, "It's already locked.")
81 commands['lock'] = (perms.op, lock)
83 def unlock(who, where, args):
84 "$unlock <prompt>: Unlocks the factoid for a given prompt."
85 (factoids, locked) = get_fact_dbs(who, where)
86 if args.endswith('?'):
87 args = args[:-1]
88 if locked.get(args):
89 locked[args] = False
90 reply(who, where, "Done.")
91 else:
92 reply(who, where, "It's not locked.")
93 commands['unlock'] = (perms.op, unlock)
95 def factoids(who, where, args):
96 "$factoids <command>: Administrative functions for Hal's factoids system. enable - Enables factoids. disable - Disables factoids. clear - Clears all factoids and factoid locks."
97 (factoids, locked) = get_fact_dbs(who, where)
98 args = args.lower()
99 on = extract_my_db(factoids_on, who, where, lambda: True)
100 if args in yes:
101 if on:
102 reply(who, where, "Factoids are already enabled.")
103 else:
104 factoids_on[real_where(who, where)] = True
105 reply(who, where, "Factoids enabled.")
106 elif args in no:
107 if not on:
108 reply(who, where, "Factoids are already disabled.")
109 else:
110 factoids_on[real_where(who, where)] = False
111 reply(who, where, "Factoids disabled.")
112 elif args == "clear":
113 factoids.clear()
114 locked.clear()
115 reply(who, where, "Factoids (and locks) cleared.")
116 else:
117 reply(who, where, "Available factoid management tools: enable, disable, clear")
118 commands['factoids'] = (perms.admin, factoids)
120 def factoid(who, where, args, implicit=False):
121 "$fact <prompt> or $factoid <prompt>: Asks Hal to look up his factoid for the given prompt."
122 (factoids, locked) = get_fact_dbs(who, where)
123 if args.endswith('?'):
124 args = args[:-1]
125 if args in factoids:
126 (to_be, fact) = factoids[args]
127 if not implicit and fact.count("$who"):
128 implicit = True
129 response = ""
130 lfact = fact.lower()
131 if lfact.startswith("<"):
132 if lfact.startswith("<raw>"):
133 response = fact[5:].lstrip()
134 elif lfact.startswith("<reply>"):
135 response = fact[7:].lstrip()
136 elif lfact.startswith("<action>"):
137 response = "\x01ACTION %s\x01" % fact[8:].lstrip()
138 if not response:
139 response = "I hear that %s %s %s" % (args, to_be, fact)
140 response = response.replace("$who", who)
141 reply(who, where, response, all=implicit)
142 elif not implicit:
143 reply(who, where, "I don't have anything matching '%s'." % args)
144 commands['fact'] = (perms.voice, factoid)
145 commands['factoid'] = (perms.voice, factoid)
147 def factoid_implicit(who, where, args):
148 "Implicit form of $fact[oid]."
149 if factoids_are_on(who, where):
150 factoid(who, where, args, implicit=True)
151 factoid_implicit = troggie(factoid_implicit)
152 commands['factoid implicit'] = (perms.voice, factoid_implicit)