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