dictionary.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. # vim: expandtab
  2. #
  3. # This script makes the bot talk a bit. You can teach it terms to respond to. It
  4. # also has random responses if it sees its nick mentioned.
  5. #
  6. # This is a heavily modified version of dictionary.tcl 2.7 by perpleXa.
  7. #
  8. # To enable the script on a channel type (partyline):
  9. # .chanset #channel +dictionary
  10. #
  11. # Dictionary
  12. # Copyright (C) 2004-2007 perpleXa
  13. # http://perplexa.ugug.org / #perpleXa on QuakeNet
  14. #
  15. # Redistribution, with or without modification, are permitted provided
  16. # that redistributions retain the above copyright notice, this condition
  17. # and the following disclaimer.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY, to the extent permitted by law; without
  21. # even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  22. # PARTICULAR PURPOSE.
  23. namespace eval dictionary {
  24. # Definition file. The format is a tcl dict.
  25. variable term_file "scripts/dbase/dictionary.db"
  26. # File containing nicks to not respond to. Newline separated.
  27. variable skip_nick_file "scripts/dictionary_skip_nicks.txt"
  28. # File containing affirmative responses. Newline separated.
  29. variable affirmative_responses_file "scripts/dictionary_affirmative_list.txt"
  30. # File containing negative responses. Newline separated.
  31. variable negative_responses_file "scripts/dictionary_negative_list.txt"
  32. # File containing chatty responses.
  33. #
  34. # These are really just random phrases
  35. # for the bot to respond with assuming it has been addressed in some way and
  36. # has nothing really to say about it. Newline separated.
  37. variable chatty_responses_file "scripts/dictionary_chatty_list.txt"
  38. # Time to not respond to the same word in the same channel. This is
  39. # so we don't respond to the same word in quick succession.
  40. variable throttle_time [expr 10*60]
  41. # Dictionary terms.
  42. #
  43. # Each key is a term and associates with another dict.
  44. #
  45. # The sub-dict has keys:
  46. # - def, the definition
  47. # - include_term_in_def, which controls whether we output "<term> is <def>"
  48. # or just "<def>"
  49. variable terms [dict create]
  50. # Nicks to not respond to terms for. e.g., bots.
  51. variable skip_nicks [list]
  52. variable affirmative_responses [list]
  53. variable negative_responses [list]
  54. variable chatty_responses [list]
  55. # Dict with keys <channel><term> with values containing the unixtime the last
  56. # time the term was output, if any.
  57. #
  58. # This is for throttling term outputs.
  59. variable flood [dict create]
  60. bind pubm -|- "*" ::dictionary::public
  61. bind pubm -|- "*" ::dictionary::publearn
  62. setudef flag dictionary
  63. }
  64. # Respond to terms in the channel
  65. proc ::dictionary::public {nick host hand chan argv} {
  66. variable flood
  67. variable terms
  68. variable throttle_time
  69. variable skip_nicks
  70. global botnick
  71. if {![channel get $chan dictionary]} {
  72. return
  73. }
  74. # Ignore cases of '<botnick>:' because those are commands to us. We deal with
  75. # them in a different proc.
  76. if {[::dictionary::is_addressing_bot $argv $botnick]} {
  77. return
  78. }
  79. # If the person saying something has a nick that is one we skip, we're done.
  80. foreach skip_nick $skip_nicks {
  81. if {[string equal -nocase $nick $skip_nick]} {
  82. return
  83. }
  84. }
  85. # Look for a word we know about for us to respond to.
  86. set term ""
  87. foreach word [dict keys $terms] {
  88. if {[::dictionary::string_contains_term $argv $word]} {
  89. set term $word
  90. break
  91. }
  92. }
  93. # If they didn't say a term we know something about, then the only response
  94. # we'll send is if they said our name. Send them a chatty response if so.
  95. if {$term == ""} {
  96. if {[::dictionary::string_contains_term $argv $botnick]} {
  97. set response [::dictionary::get_chatty_response $nick]
  98. putserv "PRIVMSG $chan :$response"
  99. }
  100. return
  101. }
  102. # They said a word we know something about. We'll potentially output the
  103. # definition.
  104. set term_dict [dict get $terms $term]
  105. # We throttle how often we output the term's definition.
  106. set flood_key $chan$term
  107. if {![dict exists $flood $flood_key]} {
  108. dict set flood $flood_key 0
  109. }
  110. set last_term_output_time [dict get $flood $flood_key]
  111. if {[unixtime] - $last_term_output_time <= $throttle_time} {
  112. return
  113. }
  114. dict set flood $flood_key [unixtime]
  115. # Output the definition. Note that terms get output differently depending on
  116. # how they were added.
  117. set def [dict get $term_dict def]
  118. if {[dict get $term_dict include_term_in_def]} {
  119. puthelp "PRIVMSG $chan :$term is $def"
  120. return
  121. }
  122. puthelp "PRIVMSG $chan :$def"
  123. }
  124. # Public trigger. This handles commands such as setting, deleting, and listing
  125. # terms the bot knows about.
  126. proc ::dictionary::publearn {nick host hand chan argv} {
  127. global botnick
  128. variable terms
  129. if {![channel get $chan dictionary]} {
  130. return
  131. }
  132. set argv [stripcodes "uacgbr" $argv]
  133. set argv [string trim $argv]
  134. # We only respond if we are directly addressed (botnick: ). This indicates
  135. # someone is giving us a command.
  136. if {![::dictionary::is_addressing_bot $argv $botnick]} {
  137. return
  138. }
  139. if {![regexp -nocase -- {^\S+\s+(.+)} $argv -> rest]} {
  140. set response [::dictionary::get_negative_response $nick]
  141. putserv "PRIVMSG $chan :$response"
  142. return
  143. }
  144. # Delete a term. <botnick>: forget <term>
  145. #
  146. # Note this means we can't set a term using the "is" syntax (e.g. forget blah
  147. # is x).
  148. if {[regexp -nocase -- {^forget\s+(.+)} $rest -> term]} {
  149. if {![dict exists $terms $term]} {
  150. set response [::dictionary::get_negative_response $nick]
  151. putserv "PRIVMSG $chan :I don't know `$term'."
  152. return
  153. }
  154. set def [dict get $terms $term def]
  155. dict unset terms $term
  156. ::dictionary::save
  157. putserv "PRIVMSG $chan :I forgot `$term'. (It was `$def'.)"
  158. return
  159. }
  160. # Set a term. <botnick>: <term> is <definition>
  161. if {[regexp -nocase -- {^(.+?)\s+is\s+(.+)$} $rest -> term def]} {
  162. if {[dict exists $terms $term]} {
  163. set def [dict get $terms $term def]
  164. putserv "PRIVMSG $chan :`$term' is already `$def'"
  165. return
  166. }
  167. dict set terms $term [dict create \
  168. def $def \
  169. include_term_in_def 1 \
  170. ]
  171. ::dictionary::save
  172. set response [::dictionary::get_affirmative_response $nick]
  173. putserv "PRIVMSG $chan :$response"
  174. return
  175. }
  176. # Set a term. <botnick>: <term>, <definition>
  177. if {[regexp -nocase -- {^(.+?)\s*,\s+(.+)$} $rest -> term def]} {
  178. if {[dict exists $terms $term]} {
  179. set def [dict get $terms $term def]
  180. putserv "PRIVMSG $chan :`$term' is already `$def'"
  181. return
  182. }
  183. dict set terms $term [dict create \
  184. def $def \
  185. include_term_in_def 0 \
  186. ]
  187. ::dictionary::save
  188. set response [::dictionary::get_affirmative_response $nick]
  189. putserv "PRIVMSG $chan :$response"
  190. return
  191. }
  192. # Message the nick all terms we have
  193. if {[string tolower $rest] == "listem"} {
  194. foreach term [lsort -dictionary [dict keys $terms]] {
  195. set def [dict get $terms $term def]
  196. puthelp "PRIVMSG $nick :$term: $def"
  197. }
  198. return
  199. }
  200. set response [::dictionary::get_chatty_response $nick]
  201. putserv "PRIVMSG $chan :$response"
  202. }
  203. # Return 1 if the given line is addressing the bot.
  204. #
  205. # This is the case if the line is of the form:
  206. # <botnick>:
  207. #
  208. # For example if the bot's nick is:
  209. # bot: Hi there
  210. #
  211. # This is checked case insensitively.
  212. proc ::dictionary::is_addressing_bot {text botnick} {
  213. set text [string trim $text]
  214. set text [string tolower $text]
  215. set prefix [string tolower $botnick]
  216. append prefix :
  217. set idx [string first $prefix $text]
  218. return [expr $idx == 0]
  219. }
  220. # Return 1 if the string contains the term. This is tested case insensitively.
  221. #
  222. # The term is present only if it is by itself surrounded whitespace or
  223. # punctuation.
  224. #
  225. # e.g. if the term is 'test' then these strings contain it:
  226. #
  227. # hi test hi
  228. # hi test, hi
  229. # test
  230. #
  231. # But these do not:
  232. #
  233. # hi testing hi
  234. # hitest
  235. proc ::dictionary::string_contains_term {s term} {
  236. set term_lc [string tolower $term]
  237. set term_quoted [::dictionary::quotemeta $term_lc]
  238. set re {\m}
  239. append re $term_quoted
  240. append re {\M}
  241. return [regexp -nocase -- $re $s]
  242. }
  243. # Escape/quote metacharacters so that the string becomes suitable for placing in
  244. # a regular expression. This makes it so any regex metacharacter is quoted.
  245. #
  246. # See http://stackoverflow.com/questions/4346750/regular-expression-literal-text-span/4352893#4352893
  247. proc ::dictionary::quotemeta {s} {
  248. return [regsub -all {\W} $s {\\&}]
  249. }
  250. proc ::dictionary::get_random_response {responses nick} {
  251. # We assume we have responses in the list.
  252. set response_index [rand [llength $responses]]
  253. set response [lindex $responses $response_index]
  254. return [regsub -all -- "%%nick%%" $response $nick]
  255. }
  256. proc ::dictionary::get_affirmative_response {nick} {
  257. if {[llength $::dictionary::affirmative_responses] == 0} {
  258. return "OK."
  259. }
  260. return [::dictionary::get_random_response \
  261. $::dictionary::affirmative_responses $nick]
  262. }
  263. proc ::dictionary::get_negative_response {nick} {
  264. if {[llength $::dictionary::negative_responses] == 0} {
  265. return "No."
  266. }
  267. return [::dictionary::get_random_response \
  268. $::dictionary::negative_responses $nick]
  269. }
  270. proc ::dictionary::get_chatty_response {nick} {
  271. if {[llength $::dictionary::chatty_responses] == 0} {
  272. return "Hi."
  273. }
  274. return [::dictionary::get_random_response \
  275. $::dictionary::chatty_responses $nick]
  276. }
  277. # Load the term database from our data file.
  278. proc ::dictionary::load_terms {} {
  279. variable term_file
  280. variable terms
  281. set terms [dict create]
  282. if {[catch {open $term_file "r"} fp]} {
  283. return
  284. }
  285. set terms [read -nonewline $fp]
  286. close $fp
  287. set count [llength [dict keys $terms]]
  288. return $count
  289. }
  290. # Load contents of a file into a list.
  291. #
  292. # Each line of the file is made into one element in the list.
  293. #
  294. # Blank lines are skipped.
  295. #
  296. # Path: Path to the file to open
  297. #
  298. # Returns: If we do not find the file or we can't open it then we return an
  299. # empty list.
  300. proc ::dictionary::file_contents_to_list {path} {
  301. if {![file exists $path]} {
  302. return [list]
  303. }
  304. if {[catch {open $path r} fp]} {
  305. return [list]
  306. }
  307. set content [read -nonewline $fp]
  308. close $fp
  309. set l [list]
  310. foreach line [split $content "\n"] {
  311. set line [string trim $line]
  312. if {[string length $line] == 0} {
  313. continue
  314. }
  315. lappend l $line
  316. }
  317. return $l
  318. }
  319. # Load a list of nicks to skip from a data file.
  320. proc ::dictionary::load_skip_nicks {} {
  321. set ::dictionary::skip_nicks [::dictionary::file_contents_to_list \
  322. $::dictionary::skip_nick_file]
  323. }
  324. # Load affirmative responses from data file.
  325. proc ::dictionary::load_affirmative_responses {} {
  326. set ::dictionary::affirmative_responses [::dictionary::file_contents_to_list \
  327. $::dictionary::affirmative_responses_file]
  328. }
  329. # Load negative responses from data file.
  330. proc ::dictionary::load_negative_responses {} {
  331. set ::dictionary::negative_responses [::dictionary::file_contents_to_list \
  332. $::dictionary::negative_responses_file]
  333. }
  334. # Load chatty responses from data file.
  335. proc ::dictionary::load_chatty_responses {} {
  336. set ::dictionary::chatty_responses [::dictionary::file_contents_to_list \
  337. $::dictionary::chatty_responses_file]
  338. }
  339. # Load data from our data files into memory.
  340. proc ::dictionary::load {args} {
  341. set term_count [::dictionary::load_terms]
  342. ::dictionary::load_skip_nicks
  343. ::dictionary::load_affirmative_responses
  344. ::dictionary::load_negative_responses
  345. ::dictionary::load_chatty_responses
  346. return $term_count
  347. }
  348. # Save the terms and definitions to the data file.
  349. proc ::dictionary::write_db {} {
  350. variable term_file
  351. variable terms
  352. if {![file isdirectory [file dirname $term_file]]} {
  353. file mkdir [file dirname $term_file]
  354. }
  355. set fp [open $term_file w]
  356. puts -nonewline $fp $terms
  357. close $fp
  358. }
  359. set ::dictionary::count [::dictionary::load]
  360. if {$::dictionary::count == 1} {
  361. putlog "dictionary.tcl loaded. $::dictionary::count term."
  362. } else {
  363. putlog "dictionary.tcl loaded. $::dictionary::count terms."
  364. }