dictionary.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 chatty responses.
  29. #
  30. # These are really just random phrases
  31. # for the bot to respond with assuming it has been addressed in some way and
  32. # has nothing really to say about it. Newline separated.
  33. variable chatty_responses_file "scripts/dictionary_chatty_list.txt"
  34. # Time to not respond to the same word in the same channel. This is
  35. # so we don't respond to the same word in quick succession.
  36. variable throttle_time [expr 10*60]
  37. # Dictionary terms.
  38. #
  39. # Each key is a term and associates with another dict.
  40. #
  41. # The sub-dict has keys:
  42. # - def, the definition
  43. # - include_term_in_def, which controls whether we output "<term> is <def>"
  44. # or just "<def>"
  45. variable terms [dict create]
  46. # Nicks to not respond to terms for. e.g., bots.
  47. variable skip_nicks [list]
  48. variable chatty_responses [list]
  49. # Dict with keys <channel><term> with values containing the unixtime the last
  50. # time the term was output, if any.
  51. #
  52. # This is for throttling term outputs.
  53. variable flood [dict create]
  54. bind pubm -|- "*" ::dictionary::public
  55. bind pubm -|- "*" ::dictionary::publearn
  56. setudef flag dictionary
  57. }
  58. # Respond to terms in the channel
  59. proc ::dictionary::public {nick host hand chan argv} {
  60. variable flood
  61. variable terms
  62. variable throttle_time
  63. variable skip_nicks
  64. global botnick
  65. if {![channel get $chan dictionary]} {
  66. return
  67. }
  68. # Ignore cases of '<botnick>:' because those are commands to us. We deal with
  69. # them in a different proc.
  70. if {[::dictionary::is_addressing_bot $argv $botnick]} {
  71. return
  72. }
  73. # If the person saying something has a nick that is one we skip, we're done.
  74. foreach skip_nick $skip_nicks {
  75. if {[string equal -nocase $nick $skip_nick]} {
  76. return
  77. }
  78. }
  79. # Look for a word we know about for us to respond to.
  80. set term ""
  81. foreach word [dict keys $terms] {
  82. if {[::dictionary::string_contains_term $argv $word]} {
  83. set term $word
  84. break
  85. }
  86. }
  87. # If they didn't say a term we know something about, then the only response
  88. # we'll send is if they said our name. Send them a chatty response if so.
  89. if {$term == ""} {
  90. if {[::dictionary::string_contains_term $argv $botnick]} {
  91. set response [::dictionary::get_chatty_response $nick]
  92. putserv "PRIVMSG $chan :$response"
  93. }
  94. return
  95. }
  96. # They said a word we know something about. We'll potentially output the
  97. # definition.
  98. set term_dict [dict get $terms $term]
  99. # We throttle how often we output the term's definition.
  100. set flood_key $chan$term
  101. if {![dict exists $flood $flood_key]} {
  102. dict set flood $flood_key 0
  103. }
  104. set last_term_output_time [dict get $flood $flood_key]
  105. if {[unixtime] - $last_term_output_time <= $throttle_time} {
  106. return
  107. }
  108. dict set flood $flood_key [unixtime]
  109. # Output the definition. Note that terms get output differently depending on
  110. # how they were added.
  111. set def [dict get $term_dict def]
  112. if {[dict get $term_dict include_term_in_def]} {
  113. puthelp "PRIVMSG $chan :$term is $def"
  114. return
  115. }
  116. puthelp "PRIVMSG $chan :$def"
  117. }
  118. # Public trigger. This handles commands such as setting, deleting, and listing
  119. # terms the bot knows about.
  120. proc ::dictionary::publearn {nick host hand chan argv} {
  121. global botnick
  122. variable terms
  123. if {![channel get $chan dictionary]} {
  124. return
  125. }
  126. set argv [stripcodes "uacgbr" $argv]
  127. set argv [string trim $argv]
  128. # We only respond if we are directly addressed (botnick: ). This indicates
  129. # someone is giving us a command.
  130. if {![::dictionary::is_addressing_bot $argv $botnick]} {
  131. return
  132. }
  133. if {![regexp -nocase -- {^\S+\s+(.+)} $argv -> rest]} {
  134. set response [::dictionary::get_negative_response $nick]
  135. putserv "PRIVMSG $chan :$response"
  136. return
  137. }
  138. # Delete a term. <botnick>: forget <term>
  139. #
  140. # Note this means we can't set a term using the "is" syntax (e.g. forget blah
  141. # is x).
  142. if {[regexp -nocase -- {^forget\s+(.+)} $rest -> term]} {
  143. if {![dict exists $terms $term]} {
  144. set response [::dictionary::get_negative_response $nick]
  145. putserv "PRIVMSG $chan :I don't know `$term'."
  146. return
  147. }
  148. set def [dict get $terms $term def]
  149. dict unset terms $term
  150. ::dictionary::save
  151. putserv "PRIVMSG $chan :I forgot `$term'. (It was `$def'.)"
  152. return
  153. }
  154. if {[regexp -nocase -- {^remember this:\s+(.+)} $rest -> response]} {
  155. lappend ::dictionary::chatty_responses $response
  156. if {[catch {::dictionary::list_to_file $::dictionary::chatty_responses \
  157. $::dictionary::chatty_responses_file} err]} {
  158. putserv "PRIVMSG $chan :Error! $err"
  159. return
  160. }
  161. putserv "PRIVMSG $chan :OK, $nick."
  162. return
  163. }
  164. # Set a term. <botnick>: <term> is <definition>
  165. if {[regexp -nocase -- {^(.+?)\s+is\s+(.+)$} $rest -> term def]} {
  166. if {[dict exists $terms $term]} {
  167. set def [dict get $terms $term def]
  168. putserv "PRIVMSG $chan :`$term' is already `$def'"
  169. return
  170. }
  171. dict set terms $term [dict create \
  172. def $def \
  173. include_term_in_def 1 \
  174. ]
  175. ::dictionary::save
  176. set response [::dictionary::get_affirmative_response $nick]
  177. putserv "PRIVMSG $chan :$response"
  178. return
  179. }
  180. # Set a term. <botnick>: <term>, <definition>
  181. if {[regexp -nocase -- {^(.+?)\s*,\s+(.+)$} $rest -> term def]} {
  182. if {[dict exists $terms $term]} {
  183. set def [dict get $terms $term def]
  184. putserv "PRIVMSG $chan :`$term' is already `$def'"
  185. return
  186. }
  187. dict set terms $term [dict create \
  188. def $def \
  189. include_term_in_def 0 \
  190. ]
  191. ::dictionary::save
  192. set response [::dictionary::get_affirmative_response $nick]
  193. putserv "PRIVMSG $chan :$response"
  194. return
  195. }
  196. # Message the nick all terms we have
  197. if {[string tolower $rest] == "listem"} {
  198. foreach term [lsort -dictionary [dict keys $terms]] {
  199. set def [dict get $terms $term def]
  200. puthelp "PRIVMSG $nick :$term: $def"
  201. }
  202. return
  203. }
  204. if {[string tolower $rest] == "braindump"} {
  205. set i 1
  206. foreach response $::dictionary::chatty_responses {
  207. puthelp "PRIVMSG $nick :$i. $response"
  208. incr i
  209. }
  210. return
  211. }
  212. set response [::dictionary::get_chatty_response $nick]
  213. putserv "PRIVMSG $chan :$response"
  214. }
  215. # Return 1 if the given line is addressing the bot.
  216. #
  217. # This is the case if the line is of the form:
  218. # <botnick>:
  219. #
  220. # For example if the bot's nick is:
  221. # bot: Hi there
  222. #
  223. # This is checked case insensitively.
  224. proc ::dictionary::is_addressing_bot {text botnick} {
  225. set text [string trim $text]
  226. set text [string tolower $text]
  227. set prefix [string tolower $botnick]
  228. append prefix :
  229. set idx [string first $prefix $text]
  230. return [expr $idx == 0]
  231. }
  232. # Return 1 if the string contains the term. This is tested case insensitively.
  233. #
  234. # The term is present only if it is by itself surrounded whitespace or
  235. # punctuation.
  236. #
  237. # e.g. if the term is 'test' then these strings contain it:
  238. #
  239. # hi test hi
  240. # hi test, hi
  241. # test
  242. #
  243. # But these do not:
  244. #
  245. # hi testing hi
  246. # hitest
  247. proc ::dictionary::string_contains_term {s term} {
  248. set term_lc [string tolower $term]
  249. set term_quoted [::dictionary::quotemeta $term_lc]
  250. set re {\m}
  251. append re $term_quoted
  252. append re {\M}
  253. return [regexp -nocase -- $re $s]
  254. }
  255. # Escape/quote metacharacters so that the string becomes suitable for placing in
  256. # a regular expression. This makes it so any regex metacharacter is quoted.
  257. #
  258. # See http://stackoverflow.com/questions/4346750/regular-expression-literal-text-span/4352893#4352893
  259. proc ::dictionary::quotemeta {s} {
  260. return [regsub -all {\W} $s {\\&}]
  261. }
  262. proc ::dictionary::get_affirmative_response {nick} {
  263. return "OK, $nick"
  264. }
  265. proc ::dictionary::get_negative_response {nick} {
  266. return "Shut up."
  267. }
  268. proc ::dictionary::get_chatty_response {nick} {
  269. set n [llength $::dictionary::chatty_responses]
  270. if {$n == 0} {
  271. return "Hi."
  272. }
  273. set idx [expr int($n*rand())]
  274. set response [lindex $::dictionary::chatty_responses $idx]
  275. return [regsub -all -- "%%nick%%" $response $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. proc ::dictionary::list_to_file {l path} {
  320. set fh [open $path w]
  321. foreach e $l {
  322. puts $fh $e
  323. }
  324. close $fh
  325. }
  326. # Load a list of nicks to skip from a data file.
  327. proc ::dictionary::load_skip_nicks {} {
  328. set ::dictionary::skip_nicks [::dictionary::file_contents_to_list \
  329. $::dictionary::skip_nick_file]
  330. }
  331. # Load chatty responses from data file.
  332. proc ::dictionary::load_chatty_responses {} {
  333. set ::dictionary::chatty_responses [::dictionary::file_contents_to_list \
  334. $::dictionary::chatty_responses_file]
  335. }
  336. # Load data from our data files into memory.
  337. proc ::dictionary::load {args} {
  338. set term_count [::dictionary::load_terms]
  339. ::dictionary::load_skip_nicks
  340. ::dictionary::load_chatty_responses
  341. return $term_count
  342. }
  343. # Save the terms and definitions to the data file.
  344. proc ::dictionary::save {} {
  345. variable term_file
  346. variable terms
  347. if {![file isdirectory [file dirname $term_file]]} {
  348. file mkdir [file dirname $term_file]
  349. }
  350. set fp [open $term_file w]
  351. puts -nonewline $fp $terms
  352. close $fp
  353. }
  354. set ::dictionary::count [::dictionary::load]
  355. if {$::dictionary::count == 1} {
  356. putlog "dictionary.tcl loaded. $::dictionary::count term."
  357. } else {
  358. putlog "dictionary.tcl loaded. $::dictionary::count terms."
  359. }