dictionary.tcl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. # Set a term. <botnick>: <term> is <definition>
  155. if {[regexp -nocase -- {^(.+?)\s+is\s+(.+)$} $rest -> term def]} {
  156. if {[dict exists $terms $term]} {
  157. set def [dict get $terms $term def]
  158. putserv "PRIVMSG $chan :`$term' is already `$def'"
  159. return
  160. }
  161. dict set terms $term [dict create \
  162. def $def \
  163. include_term_in_def 1 \
  164. ]
  165. ::dictionary::save
  166. set response [::dictionary::get_affirmative_response $nick]
  167. putserv "PRIVMSG $chan :$response"
  168. return
  169. }
  170. # Set a term. <botnick>: <term>, <definition>
  171. if {[regexp -nocase -- {^(.+?)\s*,\s+(.+)$} $rest -> term def]} {
  172. if {[dict exists $terms $term]} {
  173. set def [dict get $terms $term def]
  174. putserv "PRIVMSG $chan :`$term' is already `$def'"
  175. return
  176. }
  177. dict set terms $term [dict create \
  178. def $def \
  179. include_term_in_def 0 \
  180. ]
  181. ::dictionary::save
  182. set response [::dictionary::get_affirmative_response $nick]
  183. putserv "PRIVMSG $chan :$response"
  184. return
  185. }
  186. # Message the nick all terms we have
  187. if {[string tolower $rest] == "listem"} {
  188. foreach term [lsort -dictionary [dict keys $terms]] {
  189. set def [dict get $terms $term def]
  190. puthelp "PRIVMSG $nick :$term: $def"
  191. }
  192. return
  193. }
  194. set response [::dictionary::get_chatty_response $nick]
  195. putserv "PRIVMSG $chan :$response"
  196. }
  197. # Return 1 if the given line is addressing the bot.
  198. #
  199. # This is the case if the line is of the form:
  200. # <botnick>:
  201. #
  202. # For example if the bot's nick is:
  203. # bot: Hi there
  204. #
  205. # This is checked case insensitively.
  206. proc ::dictionary::is_addressing_bot {text botnick} {
  207. set text [string trim $text]
  208. set text [string tolower $text]
  209. set prefix [string tolower $botnick]
  210. append prefix :
  211. set idx [string first $prefix $text]
  212. return [expr $idx == 0]
  213. }
  214. # Return 1 if the string contains the term. This is tested case insensitively.
  215. #
  216. # The term is present only if it is by itself surrounded whitespace or
  217. # punctuation.
  218. #
  219. # e.g. if the term is 'test' then these strings contain it:
  220. #
  221. # hi test hi
  222. # hi test, hi
  223. # test
  224. #
  225. # But these do not:
  226. #
  227. # hi testing hi
  228. # hitest
  229. proc ::dictionary::string_contains_term {s term} {
  230. set term_lc [string tolower $term]
  231. set term_quoted [::dictionary::quotemeta $term_lc]
  232. set re {\m}
  233. append re $term_quoted
  234. append re {\M}
  235. return [regexp -nocase -- $re $s]
  236. }
  237. # Escape/quote metacharacters so that the string becomes suitable for placing in
  238. # a regular expression. This makes it so any regex metacharacter is quoted.
  239. #
  240. # See http://stackoverflow.com/questions/4346750/regular-expression-literal-text-span/4352893#4352893
  241. proc ::dictionary::quotemeta {s} {
  242. return [regsub -all {\W} $s {\\&}]
  243. }
  244. proc ::dictionary::get_affirmative_response {nick} {
  245. return "OK, $nick"
  246. }
  247. proc ::dictionary::get_negative_response {nick} {
  248. return "Shut up."
  249. }
  250. proc ::dictionary::get_chatty_response {nick} {
  251. set n [llength $::dictionary::chatty_responses]
  252. if {$n == 0} {
  253. return "Hi."
  254. }
  255. set idx [expr int($n*rand())]
  256. set response [lindex $::dictionary::chatty_responses $idx]
  257. return [regsub -all -- "%%nick%%" $response $nick]
  258. }
  259. # Load the term database from our data file.
  260. proc ::dictionary::load_terms {} {
  261. variable term_file
  262. variable terms
  263. set terms [dict create]
  264. if {[catch {open $term_file "r"} fp]} {
  265. return
  266. }
  267. set terms [read -nonewline $fp]
  268. close $fp
  269. set count [llength [dict keys $terms]]
  270. return $count
  271. }
  272. # Load contents of a file into a list.
  273. #
  274. # Each line of the file is made into one element in the list.
  275. #
  276. # Blank lines are skipped.
  277. #
  278. # Path: Path to the file to open
  279. #
  280. # Returns: If we do not find the file or we can't open it then we return an
  281. # empty list.
  282. proc ::dictionary::file_contents_to_list {path} {
  283. if {![file exists $path]} {
  284. return [list]
  285. }
  286. if {[catch {open $path r} fp]} {
  287. return [list]
  288. }
  289. set content [read -nonewline $fp]
  290. close $fp
  291. set l [list]
  292. foreach line [split $content "\n"] {
  293. set line [string trim $line]
  294. if {[string length $line] == 0} {
  295. continue
  296. }
  297. lappend l $line
  298. }
  299. return $l
  300. }
  301. # Load a list of nicks to skip from a data file.
  302. proc ::dictionary::load_skip_nicks {} {
  303. set ::dictionary::skip_nicks [::dictionary::file_contents_to_list \
  304. $::dictionary::skip_nick_file]
  305. }
  306. # Load chatty responses from data file.
  307. proc ::dictionary::load_chatty_responses {} {
  308. set ::dictionary::chatty_responses [::dictionary::file_contents_to_list \
  309. $::dictionary::chatty_responses_file]
  310. }
  311. # Load data from our data files into memory.
  312. proc ::dictionary::load {args} {
  313. set term_count [::dictionary::load_terms]
  314. ::dictionary::load_skip_nicks
  315. ::dictionary::load_chatty_responses
  316. return $term_count
  317. }
  318. # Save the terms and definitions to the data file.
  319. proc ::dictionary::write_db {} {
  320. variable term_file
  321. variable terms
  322. if {![file isdirectory [file dirname $term_file]]} {
  323. file mkdir [file dirname $term_file]
  324. }
  325. set fp [open $term_file w]
  326. puts -nonewline $fp $terms
  327. close $fp
  328. }
  329. set ::dictionary::count [::dictionary::load]
  330. if {$::dictionary::count == 1} {
  331. putlog "dictionary.tcl loaded. $::dictionary::count term."
  332. } else {
  333. putlog "dictionary.tcl loaded. $::dictionary::count terms."
  334. }