1
0

dictionary.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 '<mynick>:' because those are commands to us.
  78. if {[lindex [split $argv] 0] == "$botnick:"} {
  79. return
  80. }
  81. # if the nick is one to skip, then get out now.
  82. foreach skip_nick $skip_nicks {
  83. if {[string equal -nocase $nick $skip_nick]} {
  84. return
  85. }
  86. }
  87. # look for a word we know about.
  88. set term ""
  89. foreach word [dict keys $terms] {
  90. if {[::dictionary::string_contains_term $argv $word]} {
  91. set term $word
  92. break
  93. }
  94. }
  95. if {$term == ""} {
  96. # They didn't say a term we know something about. In this case the only
  97. # response we'll send is if they said our name. Send them a chatty response
  98. # if so.
  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. set term_dict [dict get $terms $term]
  106. # check if we've responded to the word recently so we don't
  107. # keep saying it.
  108. set flood_key $chan$term
  109. if {![dict exists $flood $flood_key]} {
  110. dict set flood $flood_key 0
  111. }
  112. set last_term_output_time [dict get $flood $flood_key]
  113. if {[unixtime] - $last_term_output_time <= $throttle_time} {
  114. return
  115. }
  116. dict set flood $flood_key [unixtime]
  117. # output the def.
  118. set def [dict get $term_dict def]
  119. # terms get output differently depending on how they were
  120. # added.
  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 interaction to set/clear terms
  128. # and for responding directly by the bot.
  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 to the case where the message starts
  137. # with '<botnick>:'
  138. if {[lindex [split $argv] 0] != "$botnick:"} {
  139. return
  140. }
  141. # try to set a term.
  142. # this can be done by: <botnick>: <term> is <definition>
  143. # and: <botnick>: <term>, <definition>
  144. if {([lsearch $argv "is"] >= 0 && [llength $argv] >= 4) \
  145. || ([string first "," $argv]>-1 && [llength $argv] >= 3)} \
  146. {
  147. # <botnick>: <term> is <definition
  148. set include_term_in_def 1
  149. if {[lsearch $argv "is"] >= 0 && [string first "," $argv] < 0} {
  150. set term [lrange [split $argv] 1 [expr [lsearch $argv "is"] - 1]]
  151. set description "[lrange [split $argv] [expr [lsearch $argv "is"] + 1] end]"
  152. # <botnick>: <term>, <definition>
  153. } else {
  154. set term [lrange [split $argv] 1 end]
  155. set term [string range $term 0 [expr [string first "," $term] - 1]]
  156. set include_term_in_def 0
  157. set description "[string range $argv [expr [string first "," $argv] + 2] end]"
  158. }
  159. if {[dict exists $terms $term]} {
  160. set term_dict [dict get $terms $term]
  161. set def [dict get $term_dict def]
  162. putserv "PRIVMSG $chan :$term is already $def"
  163. return
  164. }
  165. set term [string trim $term]
  166. set description [string trim $description]
  167. if {[string length $term] == 0 || [string length $description] == 0} {
  168. set response [::dictionary::get_negative_response $nick]
  169. putserv "PRIVMSG $chan :$response"
  170. return
  171. }
  172. # set it, and send a random success response.
  173. set term_dict [dict create]
  174. dict set term_dict def $description
  175. dict set term_dict include_term_in_def $include_term_in_def
  176. dict set terms $term $term_dict
  177. set response [::dictionary::get_affirmative_response $nick]
  178. putserv "PRIVMSG $chan :$response"
  179. return
  180. }
  181. # delete a term. <botnick>: forget <term>
  182. if {[lindex [split $argv] 1] == "forget" && [llength $argv] >= 3} {
  183. set term [lrange [split $argv] 2 end]
  184. # if it does not exist, then send a random deny response.
  185. if {![dict exists $terms $term]} {
  186. set response [::dictionary::get_negative_response $nick]
  187. putserv "PRIVMSG $chan :$response"
  188. return
  189. }
  190. dict unset terms $term
  191. putserv "PRIVMSG $chan :I forgot $term."
  192. return
  193. }
  194. # message the nick all terms we have
  195. if {[lindex [split $argv] 1] == "listem" && [llength $argv] == 2} {
  196. foreach term [lsort -dictionary [dict keys $terms]] {
  197. set term_dict [dict get $terms $term]
  198. set def [dict get $term_dict def]
  199. puthelp "PRIVMSG $nick :$term: $def"
  200. }
  201. return
  202. }
  203. # unknown command. send a random chatty response.
  204. set response [::dictionary::get_chatty_response $nick]
  205. putserv "PRIVMSG $chan :$response"
  206. }
  207. # Return 1 if the string contains the term. This is tested case insensitively.
  208. #
  209. # The term is present only if it is by itself surrounded whitespace or
  210. # punctuation.
  211. #
  212. # e.g. if the term is 'test' then these strings contain it:
  213. #
  214. # hi test hi
  215. # hi test, hi
  216. # test
  217. #
  218. # But these do not:
  219. #
  220. # hi testing hi
  221. # hitest
  222. proc ::dictionary::string_contains_term {s term} {
  223. set term_lc [string tolower $term]
  224. set term_quoted [::dictionary::quotemeta $term_lc]
  225. # \m matches at the beginning of a word, \M at the end.
  226. return [regexp -nocase -- \\m$term_quoted\\M $s]
  227. }
  228. # Escape/quote metacharacters so that the string becomes suitable for placing in
  229. # a regular expression. This makes it so any regex metacharacter is quoted.
  230. #
  231. # See http://stackoverflow.com/questions/4346750/regular-expression-literal-text-span/4352893#4352893
  232. proc ::dictionary::quotemeta {s} {
  233. return [regsub -all {\W} $s {\\&}]
  234. }
  235. proc ::dictionary::get_random_response {responses nick} {
  236. # we assume we have responses in the list.
  237. set response_index [rand [llength $responses]]
  238. set response [lindex $responses $response_index]
  239. # replace %%nick%% with %%nick%% if present.
  240. return [regsub -all -- "%%nick%%" $response $nick]
  241. }
  242. proc ::dictionary::get_affirmative_response {nick} {
  243. if {[llength $::dictionary::affirmative_responses] == 0} {
  244. return "OK."
  245. }
  246. return [::dictionary::get_random_response \
  247. $::dictionary::affirmative_responses $nick]
  248. }
  249. proc ::dictionary::get_negative_response {nick} {
  250. if {[llength $::dictionary::negative_responses] == 0} {
  251. return "No."
  252. }
  253. return [::dictionary::get_random_response \
  254. $::dictionary::negative_responses $nick]
  255. }
  256. proc ::dictionary::get_chatty_response {nick} {
  257. if {[llength $::dictionary::chatty_responses] == 0} {
  258. return "Hi."
  259. }
  260. return [::dictionary::get_random_response \
  261. $::dictionary::chatty_responses $nick]
  262. }
  263. # load the term database from our data file.
  264. proc ::dictionary::load_terms {} {
  265. variable term_file
  266. variable terms
  267. set terms [dict create]
  268. if {[catch {open $term_file "r"} fp]} {
  269. return
  270. }
  271. set terms [read -nonewline $fp]
  272. close $fp
  273. set count [llength [dict keys $terms]]
  274. return $count
  275. }
  276. # load contents of a file into a list.
  277. # each line of the file is made into one element in the list.
  278. # blank lines are skipped.
  279. #
  280. # path: path to the file to open
  281. #
  282. # returns: if we do not find the file or we can't open it then we return an
  283. # empty list.
  284. proc ::dictionary::file_contents_to_list {path} {
  285. if {![file exists $path]} {
  286. return [list]
  287. }
  288. if {[catch {open $path r} fp]} {
  289. return [list]
  290. }
  291. set content [read -nonewline $fp]
  292. close $fp
  293. set l [list]
  294. foreach line [split $content "\n"] {
  295. set line [string trim $line]
  296. if {[string length $line] == 0} {
  297. continue
  298. }
  299. lappend l $line
  300. }
  301. return $l
  302. }
  303. # load a list of nicks to skip from a data file.
  304. #
  305. # returns: void
  306. proc ::dictionary::load_skip_nicks {} {
  307. set ::dictionary::skip_nicks [::dictionary::file_contents_to_list \
  308. $::dictionary::skip_nick_file]
  309. }
  310. # load affirmative responses from data file.
  311. #
  312. # returns: void
  313. proc ::dictionary::load_affirmative_responses {} {
  314. set ::dictionary::affirmative_responses [::dictionary::file_contents_to_list \
  315. $::dictionary::affirmative_responses_file]
  316. }
  317. # load negative responses from data file.
  318. #
  319. # returns: void
  320. proc ::dictionary::load_negative_responses {} {
  321. set ::dictionary::negative_responses [::dictionary::file_contents_to_list \
  322. $::dictionary::negative_responses_file]
  323. }
  324. # load chatty responses from data file.
  325. #
  326. # returns: void
  327. proc ::dictionary::load_chatty_responses {} {
  328. set ::dictionary::chatty_responses [::dictionary::file_contents_to_list \
  329. $::dictionary::chatty_responses_file]
  330. }
  331. # load data from our data files into memory.
  332. proc ::dictionary::load {args} {
  333. # the term database.
  334. set term_count [::dictionary::load_terms]
  335. # nicks to skip.
  336. ::dictionary::load_skip_nicks
  337. # responses.
  338. ::dictionary::load_affirmative_responses
  339. ::dictionary::load_negative_responses
  340. ::dictionary::load_chatty_responses
  341. return $term_count
  342. }
  343. # save the term/definitions to the data file.
  344. proc ::dictionary::write_db {} {
  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. # handle save events. write out our data files.
  355. proc ::dictionary::save {args} {
  356. # term database.
  357. ::dictionary::write_db
  358. }
  359. set ::dictionary::count [::dictionary::load]
  360. putlog "dictionary.tcl loaded. $::dictionary::count term(s)."