QuoteEngine.tcl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 mysqltcl
  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 "1.3"
  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 db_handle quote_db
  41. set db_handle [mysqlconnect -host $quote_db(host) -user $quote_db(user) -password $quote_db(password) -db $quote_db(database)]
  42. if {$db_handle != ""} {
  43. return 1
  44. } else {
  45. return 0
  46. }
  47. }
  48. ################################################################################
  49. # quote_ping
  50. # Check we're still connected to mysql
  51. ################################################################################
  52. proc quote_ping { } {
  53. global db_handle
  54. if [::mysql::ping $db_handle] {
  55. return 1
  56. } else {
  57. return [quote_connect]
  58. }
  59. }
  60. ################################################################################
  61. # quote_add
  62. # !addquote <text>
  63. # Adds a quote to the database
  64. ################################################################################
  65. proc quote_add { nick host handle channel text } {
  66. global db_handle quote_noflags
  67. if {![channel get $channel quoteengine]} {
  68. return 0
  69. }
  70. if [matchattr $handle $quote_noflags] { return 0 }
  71. if {($handle == "") || ($handle == "*")} {
  72. set handle $nick
  73. }
  74. if {![quote_ping]} {
  75. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  76. return 0
  77. }
  78. set text [string trim $text]
  79. if {$text == ""} {
  80. putserv "PRIVMSG $nick :You forgot the quote text :("
  81. return 0
  82. }
  83. set sql "INSERT INTO quotes VALUES(null, "
  84. append sql "'$handle', "
  85. append sql "'$nick!$host', "
  86. set text [mysqlescape $text]
  87. append sql "'$text', "
  88. append sql "'$channel', "
  89. append sql "'[clock seconds]')"
  90. putloglev d * "QuoteEngine: executing $sql"
  91. set result [mysqlexec $db_handle $sql]
  92. if {$result != 1} {
  93. putlog "An error occurred with the sql :("
  94. } else {
  95. set id [mysqlinsertid $db_handle]
  96. puthelp "PRIVMSG $channel :Quote \002$id\002 added"
  97. if [regexp {[^]> ]\|[[<0-9(]} $text] {
  98. 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."
  99. }
  100. }
  101. }
  102. ################################################################################
  103. # quote_rand
  104. # !randquote [--all|--channel #channel]
  105. # Gets a random quote from the database for the current channel
  106. # --all: Choose from entire database
  107. # --channel: Choose from given channel
  108. # -c: Shortcut for --channel
  109. ################################################################################
  110. proc quote_rand { nick host handle channel text } {
  111. global db_handle quote_noflags quote_shrinkspaces
  112. if {![channel get $channel quoteengine]} {
  113. return 0
  114. }
  115. if [matchattr $handle $quote_noflags] { return 0 }
  116. if {![quote_ping]} {
  117. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  118. return 0
  119. }
  120. set where_clause "WHERE channel='$channel'"
  121. if [regexp -- "--?all" $text] {
  122. set where_clause ""
  123. }
  124. if [regexp -- "--?c(hannel)?( |=)(.+)" $text matches skip1 skip2 newchan] {
  125. set where_clause "WHERE channel='[mysqlescape $newchan]'"
  126. }
  127. set sql "SELECT * FROM quotes $where_clause ORDER BY RAND() LIMIT 1"
  128. putloglev d * "QuoteEngine: executing $sql"
  129. set result [mysqlquery $db_handle $sql]
  130. if {[set row [mysqlnext $result]] != ""} {
  131. set id [lindex $row 0]
  132. set quote [lindex $row 3]
  133. set by [lindex $row 1]
  134. set when [clock format [lindex $row 5] -format "%Y/%m/%d %H:%M"]
  135. catch {
  136. if {$quote_shrinkspaces == 1} {
  137. regsub -all " +" $quote " " quote
  138. }
  139. set quote [stripcodes bcruag $quote]
  140. }
  141. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  142. } else {
  143. puthelp "PRIVMSG $channel :Couldn't find a quote :("
  144. }
  145. mysqlendquery $result
  146. }
  147. ################################################################################
  148. # quote_fetch
  149. # !getquote <id>
  150. # Fetches the given quote from the database
  151. ################################################################################
  152. proc quote_fetch { nick host handle channel text } {
  153. global db_handle quote_noflags quote_shrinkspaces
  154. if {![channel get $channel quoteengine]} {
  155. return 0
  156. }
  157. if [matchattr $handle $quote_noflags] { return 0 }
  158. set verbose ""
  159. if {![regexp {(-v )?([0-9]+)} $text matches verbose quote_id]} {
  160. puthelp "PRIVMSG $channel: Use: !getquote \[-v\] <id>"
  161. return 0
  162. }
  163. if {![quote_ping]} {
  164. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  165. return 0
  166. }
  167. set text [mysqlescape $quote_id]
  168. set sql "SELECT * FROM quotes WHERE id='$text'"
  169. putloglev d * "QuoteEngine: executing $sql"
  170. set result [mysqlquery $db_handle $sql]
  171. if {[set row [mysqlnext $result]] != ""} {
  172. set id [lindex $row 0]
  173. set quote [lindex $row 3]
  174. catch {
  175. if {$quote_shrinkspaces == 1} {
  176. regsub -all " +" $quote " " quote
  177. }
  178. set quote [stripcodes bcruag $quote]
  179. }
  180. set by [lindex $row 1]
  181. set when [clock format [lindex $row 5] -format "%Y/%m/%d %H:%M"]
  182. set chan [lindex $row 4]
  183. if {$chan != $channel} {
  184. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  185. if {$verbose != ""} {
  186. puthelp "PRIVMSG $channel :\[\002$id\002\] From $chan, by added $by at $when."
  187. }
  188. } else {
  189. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  190. if {$verbose != ""} {
  191. puthelp "PRIVMSG $channel :\[\002$id\002\] Added by $by at $when."
  192. }
  193. }
  194. } else {
  195. puthelp "PRIVMSG $channel :Couldn't find quote $text"
  196. }
  197. mysqlendquery $result
  198. }
  199. ################################################################################
  200. # quote_search
  201. # !findquote [--all] [--channel #channel] [--count <int>] <text>
  202. # Find all quotes with "text" in them. (in random order)
  203. # The first 5 (by default) are listed in the channel. The rest are /msg'd to
  204. # you up to the maximum (default 5).
  205. # --all: Search all channels, not just current one
  206. # --channel: Search given channel
  207. # --count <int>: Find this many total quotes
  208. # -c: Shortcut for --channel
  209. # -n: Shortcut for --count
  210. # Note this is a SQL search, so use % as the wildcard (instead of *)
  211. # The script automatically puts %s around your text when searching.
  212. ################################################################################
  213. proc quote_search { nick host handle channel text } {
  214. global db_handle quote_webpage quote_noflags quote_chanmax
  215. if {![channel get $channel quoteengine]} {
  216. return 0
  217. }
  218. if [matchattr $handle $quote_noflags] { return 0 }
  219. if {$text == ""} {
  220. puthelp "PRIVMSG $channel :Use: !findquote <text>"
  221. return 0
  222. }
  223. if {![quote_ping]} {
  224. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  225. return 0
  226. }
  227. set where_clause "AND channel='[mysqlescape $channel]'"
  228. if [regexp -- "--?all " $text matches skip1] {
  229. set where_clause ""
  230. regsub -- $matches $text "" text
  231. }
  232. if [regexp -- {--?c(hannel)?( |=)([^ ]+)} $text matches skip1 skip2 newchan] {
  233. set where_clause "AND channel='[mysqlescape $newchan]'"
  234. regsub -- $matches $text "" text
  235. }
  236. set limit 5
  237. if [regexp -- {--?count( |=)([^ ]+)} $text matches skip1 count] {
  238. set limit [mysqlescape $count]
  239. regsub -- $matches $text "" text
  240. }
  241. if [regexp -- {-n( )?([^ ]+)} $text matches skip1 count] {
  242. set limit [mysqlescape $count]
  243. regsub -- $matches $text "" text
  244. }
  245. set sql "SELECT * FROM quotes WHERE quote LIKE '%[mysqlescape $text]%' $where_clause ORDER BY RAND()"
  246. putloglev d * "QuoteEngine: executing $sql"
  247. if {[mysqlsel $db_handle $sql] > 0} {
  248. set count 0
  249. mysqlmap $db_handle {id qnick qhost quote qchannel qts} {
  250. if {$count == $limit} {
  251. break
  252. }
  253. if {$count == $quote_chanmax} {
  254. puthelp "PRIVMSG $nick :Rest of matches for your search '$text' follow in private:"
  255. }
  256. if {$count < $quote_chanmax} {
  257. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  258. } else {
  259. puthelp "PRIVMSG $nick :\[\002$id\002\] $quote"
  260. }
  261. incr count
  262. }
  263. set remaining [mysqlresult $db_handle rows?]
  264. if {$remaining > 0} {
  265. if {$count < $quote_chanmax} {
  266. set command "PRIVMSG $channel :"
  267. } else {
  268. set command "PRIVMSG $nick :"
  269. }
  270. regsub "#" $channel "" chan
  271. if {$quote_webpage != ""} {
  272. puthelp "${command}(Plus $remaining more matches: $quote_webpage?filter=${text}&channel=${chan}&search=search)"
  273. } else {
  274. puthelp "${command}Plus $remaining other matches"
  275. }
  276. } else {
  277. if {$count < $quote_chanmax} {
  278. set command "PRIVMSG $channel :"
  279. } else {
  280. set command "PRIVMSG $nick :"
  281. }
  282. if {$count == 1} {
  283. puthelp "${command}(All of 1 match)"
  284. } else {
  285. puthelp "${command}(All of $count matches)"
  286. }
  287. }
  288. } else {
  289. puthelp "PRIVMSG $channel :No matches"
  290. }
  291. }
  292. ################################################################################
  293. # quote_url
  294. # !quoteurl
  295. # Gives the web of the web interface
  296. ################################################################################
  297. proc quote_url { nick host handle channel text } {
  298. global quote_webpage quote_noflags
  299. if {![channel get $channel quoteengine]} {
  300. return 0
  301. }
  302. if [matchattr $handle $quote_noflags] { return 0 }
  303. if {$quote_webpage != ""} {
  304. # changed for better url by dubkat
  305. puthelp "PRIVMSG $channel :${quote_webpage}?channel=[string range $channel 1 end]"
  306. } else {
  307. puthelp "PRIVMSG $channel :Not available."
  308. }
  309. }
  310. ################################################################################
  311. # quote_stats
  312. # !quotestats
  313. # Give some simple statistics about the db, channel, and user
  314. ################################################################################
  315. proc quote_stats { nick host handle channel text } {
  316. global db_handle quote_noflags
  317. if {![channel get $channel quoteengine]} {
  318. return 0
  319. }
  320. if [matchattr $handle $quote_noflags] { return 0 }
  321. if {![quote_ping]} {
  322. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  323. return 0
  324. }
  325. set sql "SELECT COUNT(*) AS total FROM quotes WHERE channel='$channel'"
  326. putloglev d * "QuoteEngine: executing $sql"
  327. set result [mysqlquery $db_handle $sql]
  328. set total 0
  329. set chan 0
  330. if {[set row [mysqlnext $result]] != ""} {
  331. set total [lindex $row 0]
  332. }
  333. mysqlendquery $result
  334. set sql "SELECT COUNT(*) AS total FROM quotes"
  335. putloglev d * "QuoteEngine: executing $sql"
  336. set result [mysqlquery $db_handle $sql]
  337. if {[set row [mysqlnext $result]] != ""} {
  338. set chan [lindex $row 0]
  339. }
  340. mysqlendquery $result
  341. set sql "SELECT COUNT(*) AS total FROM quotes WHERE nick='$handle' AND channel='$channel'"
  342. putloglev d * "QuoteEngine: executing $sql"
  343. set result [mysqlquery $db_handle $sql]
  344. if {[set row [mysqlnext $result]] != ""} {
  345. set by_handle [lindex $row 0]
  346. }
  347. mysqlendquery $result
  348. puthelp "PRIVMSG $channel :Quotes for $channel: \002$total\002 (total: $chan). You have added \002$by_handle\002 quotes in this channel."
  349. }
  350. ################################################################################
  351. # quote_delete
  352. # !delquote <id>
  353. # Removes a quote from the database. You can only delete the quote if you
  354. # are a bot/channel master, or if you're the person who added it.
  355. ################################################################################
  356. proc quote_delete { nick host handle channel text } {
  357. global db_handle quote_noflags
  358. if {![channel get $channel quoteengine]} {
  359. return 0
  360. }
  361. if [matchattr $handle $quote_noflags] { return 0 }
  362. if {![quote_ping]} {
  363. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  364. return 0
  365. }
  366. set text [mysqlescape $text]
  367. if {![matchattr $handle m|m $channel]} {
  368. set sql "SELECT nick FROM quotes WHERE id='$text'"
  369. putloglev d * "QuoteEngine: executing $sql"
  370. set result [mysqlquery $db_handle $sql]
  371. set owner [lindex [mysqlnext $result] 0]
  372. mysqlendquery $result
  373. if {$owner != $handle} {
  374. puthelp "NOTICE $nick :You cannot delete that quote."
  375. return 0
  376. }
  377. }
  378. set sql "DELETE FROM quotes WHERE id='$text'"
  379. putloglev d * "QuoteEngine: executing $sql"
  380. set result [mysqlexec $db_handle $sql]
  381. if {$result != 1} {
  382. puthelp "PRIVMSG $channel :An error occurred deleting the quote :("
  383. return 0
  384. } else {
  385. puthelp "PRIVMSG $channel :Deleted quote $text"
  386. }
  387. }
  388. ################################################################################
  389. # quote_version
  390. # !quoteversion
  391. # Gives the version of the script
  392. ################################################################################
  393. proc quote_version { nick host handle channel text } {
  394. global quote_version quote_noflags
  395. if [matchattr $handle $quote_noflags] { return 0 }
  396. puthelp "PRIVMSG $channel :This is the QuoteEngine version $quote_version by JamesOff (http://www.jamesoff.net/go/quoteengine)"
  397. return 0
  398. }
  399. ################################################################################
  400. # quote_help
  401. # !quotehelp
  402. # Handle help requests
  403. ################################################################################
  404. proc quote_help { nick host handle channel text } {
  405. global quote_noflags
  406. if [matchattr $handle $quote_noflags] { return 0 }
  407. puthelp "PRIVMSG $nick :Commands for the QuoteEngine script:"
  408. puthelp "PRIVMSG $nick : !addquote <quote text> - adds a quote to the database"
  409. 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."
  410. 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."
  411. puthelp "PRIVMSG $nick : !getquote \[-v\]<id> - fetches the quote with number <id>. Gives info of who added it if -v is specified."
  412. 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."
  413. puthelp "PRIVMSG $nick : !quoteurl - get the URL for the web interface to the quotes"
  414. puthelp "PRIVMSG $nick : !quotestats - get some information"
  415. puthelp "PRIVMSG $nick : !quoteversion - get the version of the script"
  416. puthelp "PRIVMSG $nick : Some commands have synonyms: !deletequote, !fetchquote, !urlquote, and !searchquote."
  417. puthelp "PRIVMSG $nick : (End of help)"
  418. return 0
  419. }
  420. proc quote_auto { nick host handle channel text } {
  421. global quote_automatic quote_shrinkspaces
  422. if {$quote_automatic == 0} {
  423. return
  424. }
  425. if {![channel get $channel quoteengine]} {
  426. return
  427. }
  428. global quote_auto_last db_handle quote_automatic_minimum
  429. if [info exists quote_auto_last($channel)] {
  430. set diff [expr [clock seconds] - $quote_auto_last($channel)]
  431. putloglev 1 * "diff for $channel is $diff"
  432. } else {
  433. set diff [expr $quote_automatic_minimum + 1]
  434. putloglev d * "initialising diff for $channel"
  435. set quote_auto_last($channel) 0
  436. }
  437. if {$diff < $quote_automatic_minimum} {
  438. return
  439. }
  440. set words [split $text]
  441. set newwords [list]
  442. foreach word $words {
  443. if [regexp -nocase {^[a-z0-9']{4,}$} $word] {
  444. if {[lsearch [list "yeah" "about" "hello" "their" "there" "that's" "can't" "morning" "won't"] $word] > -1} {
  445. continue
  446. }
  447. if [onchan $word] {
  448. continue
  449. }
  450. lappend newwords [mysqlescape $word]
  451. }
  452. }
  453. if {[llength $newwords] == 0} {
  454. return
  455. }
  456. putloglev d * "quoteengine: candidate words for random quote in $channel: $newwords"
  457. if {![quote_ping]} {
  458. return
  459. }
  460. set thisword [pickRandom $newwords]
  461. putloglev d * "quoteengine: using $thisword"
  462. if {[rand 100] < 95} {
  463. putloglev d * "quoteengine: not random enough, ignoring"
  464. return
  465. }
  466. set where_clause "WHERE channel='[mysqlescape $channel]' AND quote LIKE '%$thisword%' ORDER BY RAND() LIMIT 1"
  467. putloglev d * "quoteengine: $where_clause"
  468. set sql "SELECT * FROM quotes $where_clause"
  469. set result [mysqlquery $db_handle $sql]
  470. if {[set row [mysqlnext $result]] != ""} {
  471. set id [lindex $row 0]
  472. set quote [lindex $row 3]
  473. catch {
  474. if {$quote_shrinkspaces == 1} {
  475. regsub -all " +" $quote " " quote
  476. }
  477. set quote [stripcodes bcruag $quote]
  478. }
  479. putloglev d * "RANDOM QUOTE: $quote ($id)"
  480. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  481. set quote_auto_last($channel) [clock seconds]
  482. }
  483. mysqlendquery $result
  484. }
  485. # Define the pickRandom method which is used if bMotion isn't loaded
  486. if {[llength [info procs pickRandom]] == 0} {
  487. proc pickRandom { list } {
  488. return [lindex $list [rand [llength $list]]]
  489. }
  490. }
  491. quote_connect
  492. putlog "QuoteEngine $quote_version loaded"