dictionary.tcl 12 KB

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