QuoteEngine.tcl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. # $Id: QuoteEngine.tcl,v 1.20 2004/03/30 21:52:12 James Exp $
  2. ###############################################################################
  3. # QuoteEngine for eggdrop bots
  4. # Copyright (C) James Michael Seward 2003
  5. #
  6. # This program is covered by the GPL, please refer the to LICENCE file in the
  7. # distribution.
  8. ###############################################################################
  9. # load the extension
  10. package require sqlite3
  11. # Make sure you edit the sample settings file and save it as "QuoteEngine-settings.tcl"
  12. # in the eggdrop scripts directory!
  13. source "scripts/QuoteEngine-settings.tcl"
  14. # bind commands CHANGE as needed to set who can use
  15. # use ".chanset #channel [+/-]quoteengine" to enable/disable individual
  16. # channels
  17. bind pub "m|fov" !addquote quote_add
  18. bind pub "m|fov" !randquote quote_rand
  19. bind pub "m|fov" !fetchquote quote_fetch
  20. bind pub "m|fov" !getquote quote_fetch
  21. bind pub "m|fov" !findquote quote_search
  22. bind pub "m|fov" !searchquote quote_search
  23. bind pub "m|fov" !urlquote quote_url
  24. bind pub "-|-" !quoteurl quote_url
  25. bind pub "m|ov" !delquote quote_delete
  26. bind pub "m|ov" !deletequote quote_delete
  27. bind pub "m|ov" !quotestats quote_stats
  28. bind pub "-|-" !quoteversion quote_version
  29. bind pub "-|-" !quotehelp quote_help
  30. bind pubm "-|ov" * quote_auto
  31. ################################################################################
  32. #No need to edit beyond this point
  33. ################################################################################
  34. set quote_version "2.0"
  35. set quote_auto_last(blah) 0
  36. #add setting to channel
  37. setudef flag quoteengine
  38. # connect to database
  39. proc quote_connect { } {
  40. global quote_db_handle quote_db_file
  41. sqlite3 quote_db_handle $quote_db_file
  42. }
  43. proc quote_escape { text } {
  44. return [string map {' ''} $text]
  45. }
  46. ################################################################################
  47. # quote_add
  48. # !addquote <text>
  49. # Adds a quote to the database
  50. ################################################################################
  51. proc quote_add { nick host handle channel text } {
  52. global quote_noflags
  53. if {![channel get $channel quoteengine]} {
  54. return 0
  55. }
  56. if [matchattr $handle $quote_noflags] { return 0 }
  57. if {($handle == "") || ($handle == "*")} {
  58. set handle $nick
  59. }
  60. set text [string trim $text]
  61. if {$text == ""} {
  62. putserv "PRIVMSG $nick :You forgot the quote text :("
  63. return 0
  64. }
  65. set nickhost "$nick!$host"
  66. set when [clock seconds]
  67. set sql {INSERT INTO quotes VALUES(null, :handle, :nickhost, :text, :channel, :when)}
  68. putloglev d * "QuoteEngine: executing $sql"
  69. set result [quote_db_handle eval $sql]
  70. set id [quote_db_handle last_insert_rowid]
  71. puthelp "PRIVMSG $channel :Quote \002$id\002 added"
  72. if [regexp {[^]> ]\|[[<0-9(]} $text] {
  73. puthelp "PRIVMSG $nick :It's possible you didn't split the lines quite right on the quote you just added. For best results, split lines in quotes using '|' with a space each side. To delete the quote you just added and fix it, do '!delquote $id' in the channel."
  74. }
  75. }
  76. ################################################################################
  77. # quote_rand
  78. # !randquote [--all|--channel #channel]
  79. # Gets a random quote from the database for the current channel
  80. # --all: Choose from entire database
  81. # --channel: Choose from given channel
  82. # -c: Shortcut for --channel
  83. ################################################################################
  84. proc quote_rand { nick host handle channel text } {
  85. global quote_noflags quote_shrinkspaces
  86. if {![channel get $channel quoteengine]} {
  87. return 0
  88. }
  89. if [matchattr $handle $quote_noflags] { return 0 }
  90. set where_clause "WHERE channel='$channel'"
  91. if [regexp -- "--?all" $text] {
  92. set where_clause ""
  93. }
  94. if [regexp -- "--?c(hannel)?( |=)(.+)" $text matches skip1 skip2 newchan] {
  95. set where_clause "WHERE channel='[quote_escape $newchan]'"
  96. }
  97. set sql "SELECT * FROM quotes $where_clause ORDER BY RANDOM() LIMIT 1"
  98. putloglev d * "QuoteEngine: executing $sql"
  99. quote_db_handle eval $sql result {
  100. set id $result(id)
  101. set quote $result(quote)
  102. set by $result(nick)
  103. set when [clock format $result(timestamp) -format "%Y/%m/%d %H:%M"]
  104. catch {
  105. if {$quote_shrinkspaces == 1} {
  106. regsub -all " +" $quote " " quote
  107. }
  108. set quote [stripcodes bcruag $quote]
  109. }
  110. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  111. }
  112. }
  113. ################################################################################
  114. # quote_fetch
  115. # !getquote <id>
  116. # Fetches the given quote from the database
  117. ################################################################################
  118. proc quote_fetch { nick host handle channel text } {
  119. global quote_db_handle quote_noflags quote_shrinkspaces
  120. if {![channel get $channel quoteengine]} {
  121. return 0
  122. }
  123. if [matchattr $handle $quote_noflags] { return 0 }
  124. set verbose ""
  125. if {![regexp {(-v )?([0-9]+)} $text matches verbose quote_id]} {
  126. puthelp "PRIVMSG $channel: Use: !getquote \[-v\] <id>"
  127. return 0
  128. }
  129. set sql "SELECT * FROM quotes WHERE id=:quote_id"
  130. putloglev d * "QuoteEngine: executing $sql"
  131. set found 0
  132. quote_db_handle eval $sql result {
  133. set id $result(id)
  134. set quote $result(quote)
  135. catch {
  136. if {$quote_shrinkspaces == 1} {
  137. regsub -all " +" $quote " " quote
  138. }
  139. set quote [stripcodes bcruag $quote]
  140. }
  141. set by $result(nick)
  142. set when [clock format $result(timestamp) -format "%Y/%m/%d %H:%M"]
  143. set chan $result(channel)
  144. if {$chan != $channel} {
  145. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  146. if {$verbose != ""} {
  147. puthelp "PRIVMSG $channel :\[\002$id\002\] From $chan, by added $by at $when."
  148. }
  149. } else {
  150. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  151. if {$verbose != ""} {
  152. puthelp "PRIVMSG $channel :\[\002$id\002\] Added by $by at $when."
  153. }
  154. }
  155. set found 1
  156. }
  157. if {!$found} {
  158. puthelp "PRIVMSG $channel :Couldn't find quote $text"
  159. }
  160. }
  161. ################################################################################
  162. # quote_search
  163. # !findquote [--all] [--channel #channel] [--count <int>] <text>
  164. # Find all quotes with "text" in them. (in random order)
  165. # The first 5 (by default) are listed in the channel. The rest are /msg'd to
  166. # you up to the maximum (default 5).
  167. # --all: Search all channels, not just current one
  168. # --channel: Search given channel
  169. # --count <int>: Find this many total quotes
  170. # -c: Shortcut for --channel
  171. # -n: Shortcut for --count
  172. # Note this is a SQL search, so use % as the wildcard (instead of *)
  173. # The script automatically puts %s around your text when searching.
  174. ################################################################################
  175. proc quote_search { nick host handle channel text } {
  176. global quote_webpage quote_noflags quote_chanmax
  177. if {![channel get $channel quoteengine]} {
  178. return 0
  179. }
  180. if [matchattr $handle $quote_noflags] { return 0 }
  181. if {$text == ""} {
  182. puthelp "PRIVMSG $channel :Use: !findquote <text>"
  183. return 0
  184. }
  185. set where_clause "AND channel=:channel"
  186. if [regexp -- "--?all " $text matches skip1] {
  187. set where_clause ""
  188. regsub -- $matches $text "" text
  189. }
  190. if [regexp -- {--?c(hannel)?( |=)([^ ]+)} $text matches skip1 skip2 newchan] {
  191. set where_clause "AND channel=:newchan"
  192. regsub -- $matches $text "" text
  193. }
  194. set limit 5
  195. if [regexp -- {--?count( |=)([^ ]+)} $text matches skip1 count] {
  196. set limit [quote_escape $count]
  197. regsub -- $matches $text "" text
  198. }
  199. if [regexp -- {-n( )?([^ ]+)} $text matches skip1 count] {
  200. set limit [quote_escape $count]
  201. regsub -- $matches $text "" text
  202. }
  203. set sql "SELECT * FROM quotes WHERE quote LIKE '%[quote_escape $text]%' $where_clause ORDER BY RANDOM()"
  204. putloglev d * "QuoteEngine: executing $sql"
  205. set overflow 0
  206. set count 0
  207. quote_db_handle eval $sql result {
  208. set id $result(id)
  209. set qnick $result(nick)
  210. set qhost $result(host)
  211. set quote $result(quote)
  212. set qchannel $result(channel)
  213. set qts $result(timestamp)
  214. if {$count >= $limit} {
  215. incr overflow 1
  216. continue
  217. }
  218. if {$count == $quote_chanmax} {
  219. puthelp "PRIVMSG $nick :Rest of matches for your search '$text' follow in private:"
  220. }
  221. if {$count < $quote_chanmax} {
  222. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  223. } else {
  224. puthelp "PRIVMSG $nick :\[\002$id\002\] $quote"
  225. }
  226. incr count
  227. }
  228. if {$overflow > 0} {
  229. if {$count < $quote_chanmax} {
  230. set command "PRIVMSG $channel :"
  231. } else {
  232. set command "PRIVMSG $nick :"
  233. }
  234. regsub "#" $channel "" chan
  235. puthelp "${command}Plus $overflow other matches"
  236. } else {
  237. if {$count < $quote_chanmax} {
  238. set command "PRIVMSG $channel :"
  239. } else {
  240. set command "PRIVMSG $nick :"
  241. }
  242. if {$count == 1} {
  243. puthelp "${command}(All of 1 match)"
  244. } else {
  245. puthelp "${command}(All of $count matches)"
  246. }
  247. }
  248. if {$count == 0} {
  249. puthelp "PRIVMSG $channel :No matches"
  250. }
  251. }
  252. ################################################################################
  253. # quote_url
  254. # !quoteurl
  255. # Gives the web of the web interface
  256. # Removed in 2.0 with switch to sqlite; i'm not supporting the web interface
  257. # now
  258. ################################################################################
  259. proc quote_url { nick host handle channel text } {
  260. global quote_webpage quote_noflags
  261. if {![channel get $channel quoteengine]} {
  262. return 0
  263. }
  264. if [matchattr $handle $quote_noflags] { return 0 }
  265. puthelp "PRIVMSG $channel :Not available."
  266. }
  267. ################################################################################
  268. # quote_stats
  269. # !quotestats
  270. # Give some simple statistics about the db, channel, and user
  271. ################################################################################
  272. proc quote_stats { nick host handle channel text } {
  273. global quote_db_handle quote_noflags
  274. if {![channel get $channel quoteengine]} {
  275. return 0
  276. }
  277. if [matchattr $handle $quote_noflags] { return 0 }
  278. set sql "SELECT COUNT(*) AS total FROM quotes WHERE channel='$channel'"
  279. putloglev d * "QuoteEngine: executing $sql"
  280. set total 0
  281. set chan 0
  282. set by_handle 0
  283. set total [quote_db_handle onecolumn $sql]
  284. set sql "SELECT COUNT(*) AS total FROM quotes"
  285. putloglev d * "QuoteEngine: executing $sql"
  286. set chan [quote_db_handle onecolumn $sql]
  287. set sql "SELECT COUNT(*) AS total FROM quotes WHERE nick='$handle' AND channel='$channel'"
  288. putloglev d * "QuoteEngine: executing $sql"
  289. set by_handle [quote_db_handle onecolumn $sql]
  290. puthelp "PRIVMSG $channel :Quotes for $channel: \002$total\002 (total: $chan). You have added \002$by_handle\002 quotes in this channel."
  291. }
  292. ################################################################################
  293. # quote_delete
  294. # !delquote <id>
  295. # Removes a quote from the database. You can only delete the quote if you
  296. # are a bot/channel master, or if you're the person who added it.
  297. ################################################################################
  298. proc quote_delete { nick host handle channel text } {
  299. global quote_db_handle quote_noflags
  300. if {![channel get $channel quoteengine]} {
  301. return 0
  302. }
  303. if [matchattr $handle $quote_noflags] { return 0 }
  304. set text [quote_escape $text]
  305. if {![matchattr $handle m|m $channel]} {
  306. set sql "SELECT nick FROM quotes WHERE id='$text'"
  307. putloglev d * "QuoteEngine: executing $sql"
  308. set owner [quote_db_handle onecolume $sql]
  309. if {$owner != $handle} {
  310. puthelp "NOTICE $nick :You cannot delete that quote."
  311. return 0
  312. }
  313. }
  314. set sql "DELETE FROM quotes WHERE id='$text'"
  315. putloglev d * "QuoteEngine: executing $sql"
  316. quote_db_handle eval $sql
  317. puthelp "PRIVMSG $channel :Deleted quote $text"
  318. }
  319. ################################################################################
  320. # quote_version
  321. # !quoteversion
  322. # Gives the version of the script
  323. ################################################################################
  324. proc quote_version { nick host handle channel text } {
  325. global quote_version quote_noflags
  326. if [matchattr $handle $quote_noflags] { return 0 }
  327. puthelp "PRIVMSG $channel :This is the QuoteEngine version $quote_version by JamesOff (https://github.com/jamesoff/eggdrop-scripts)"
  328. return 0
  329. }
  330. ################################################################################
  331. # quote_help
  332. # !quotehelp
  333. # Handle help requests
  334. ################################################################################
  335. proc quote_help { nick host handle channel text } {
  336. global quote_noflags
  337. if [matchattr $handle $quote_noflags] { return 0 }
  338. puthelp "PRIVMSG $nick :Commands for the QuoteEngine script:"
  339. puthelp "PRIVMSG $nick : !addquote <quote text> - adds a quote to the database"
  340. puthelp "PRIVMSG $nick : !delquote <id> - deletes a quote. You must be either a bot/channel master or the person who added the quote to delete it."
  341. puthelp "PRIVMSG $nick : !randquote \[--all\] \[--channel=#channel\] \[-c #channel\] - fetches a random quote from the current channel. --all chooses from all channels, not just the one the command is executed from. --channel and -c choose only from the given channel."
  342. puthelp "PRIVMSG $nick : !getquote \[-v\]<id> - fetches the quote with number <id>. Gives info of who added it if -v is specified."
  343. puthelp "PRIVMSG $nick : !findquote \[--all\] \[--channel=#channel\] \[-c #channel\] \[--count <int>\] \[-n <int>\] <text> - finds up to <int> (default 5) quotes containing 'text'. Optional parameters same as !randquote. -n is a shortcut for --count."
  344. puthelp "PRIVMSG $nick : !quotestats - get some information"
  345. puthelp "PRIVMSG $nick : !quoteversion - get the version of the script"
  346. puthelp "PRIVMSG $nick : Some commands have synonyms: !deletequote, !fetchquote, !urlquote, and !searchquote."
  347. puthelp "PRIVMSG $nick : (End of help)"
  348. return 0
  349. }
  350. proc quote_auto { nick host handle channel text } {
  351. global quote_automatic quote_shrinkspaces
  352. if {$quote_automatic == 0} {
  353. return
  354. }
  355. if {![channel get $channel quoteengine]} {
  356. return
  357. }
  358. global quote_auto_last quote_db_handle quote_automatic_minimum
  359. if [info exists quote_auto_last($channel)] {
  360. set diff [expr [clock seconds] - $quote_auto_last($channel)]
  361. putloglev 1 * "diff for $channel is $diff"
  362. } else {
  363. set diff [expr $quote_automatic_minimum + 1]
  364. putloglev d * "initialising diff for $channel"
  365. set quote_auto_last($channel) 0
  366. }
  367. if {$diff < $quote_automatic_minimum} {
  368. return
  369. }
  370. set words [split $text]
  371. set newwords [list]
  372. foreach word $words {
  373. if [regexp -nocase {^[a-z0-9']{4,}$} $word] {
  374. if {[lsearch [list "yeah" "about" "hello" "their" "there" "that's" "can't" "morning" "won't"] $word] > -1} {
  375. continue
  376. }
  377. if [onchan $word] {
  378. continue
  379. }
  380. lappend newwords [quote_escape $word]
  381. }
  382. }
  383. if {[llength $newwords] == 0} {
  384. return
  385. }
  386. putloglev d * "quoteengine: candidate words for random quote in $channel: $newwords"
  387. set thisword [pickRandom $newwords]
  388. putloglev d * "quoteengine: using $thisword"
  389. if {[rand 100] < 95} {
  390. putloglev d * "quoteengine: not random enough, ignoring"
  391. return
  392. }
  393. set where_clause "WHERE channel='[quote_escape $channel]' AND quote LIKE '%$thisword%' ORDER BY RANDOM() LIMIT 1"
  394. putloglev d * "quoteengine: $where_clause"
  395. set sql "SELECT id,quote FROM quotes $where_clause"
  396. quote_db_handle eval $sql result {
  397. set id $result(id)
  398. set quote $result(quote)
  399. catch {
  400. if {$quote_shrinkspaces == 1} {
  401. regsub -all " +" $quote " " quote
  402. }
  403. set quote [stripcodes bcruag $quote]
  404. }
  405. putloglev d * "RANDOM QUOTE: $quote ($id)"
  406. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  407. set quote_auto_last($channel) [clock seconds]
  408. }
  409. }
  410. # Define the pickRandom method which is used if bMotion isn't loaded
  411. if {[llength [info procs pickRandom]] == 0} {
  412. proc pickRandom { list } {
  413. return [lindex $list [rand [llength $list]]]
  414. }
  415. }
  416. quote_connect
  417. putlog "QuoteEngine $quote_version loaded"