QuoteEngine-sqlite.tcl 17 KB

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