QuoteEngine.tcl 18 KB

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