2 from __future__
import generators
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
):
26 (key
, to_be
, garbage
, fact
) = args
27 (factoids
, locked
) = get_fact_dbs(who
, where
)
31 #reply(who, where, "I'm sorry, %s, I'm afraid I can't do that." % who, all=True)
34 factoids
[key
] = (to_be
, fact
)
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
):
44 (factoids
, locked
) = get_fact_dbs(who
, where
)
47 reply(who
, where
, "I'm sorry, %s, I'm afraid I can't do that." % who
, all
=True)
50 reply(who
, where
, "I forgot %s." % key
)
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")
62 (key
, to_be
, garbage
, fact
) = resize(is_re
.split(args
, 1),4)
66 factoids
[key
] = (to_be
, fact
)
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('?'):
76 if not locked
.get(args
):
78 reply(who
, where
, "Done.")
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('?'):
90 reply(who
, where
, "Done.")
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
)
99 on
= extract_my_db(factoids_on
, who
, where
, lambda: True)
102 reply(who
, where
, "Factoids are already enabled.")
104 factoids_on
[real_where(who
, where
)] = True
105 reply(who
, where
, "Factoids enabled.")
108 reply(who
, where
, "Factoids are already disabled.")
110 factoids_on
[real_where(who
, where
)] = False
111 reply(who
, where
, "Factoids disabled.")
112 elif args
== "clear":
115 reply(who
, where
, "Factoids (and locks) cleared.")
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('?'):
126 (to_be
, fact
) = factoids
[args
]
127 if not implicit
and fact
.count("$who"):
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()
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
)
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
)