QuoteEngine.tcl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 == 1} {
  290. puthelp "${command}(All of 1 match)"
  291. } else {
  292. puthelp "${command}(All of $count matches)"
  293. }
  294. }
  295. } else {
  296. puthelp "PRIVMSG $channel :No matches"
  297. }
  298. }
  299. ################################################################################
  300. # quote_url
  301. # !quoteurl
  302. # Gives the web of the web interface
  303. ################################################################################
  304. proc quote_url { nick host handle channel text } {
  305. global php_page quote_noflags
  306. if {![channel get $channel quoteengine]} {
  307. return 0
  308. }
  309. if [matchattr $handle $quote_noflags] { return 0 }
  310. if {$php_page != ""} {
  311. # changed for better url by dubkat
  312. puthelp "PRIVMSG $channel :${php_page}?channel=[string range $channel 1 end]"
  313. } else {
  314. puthelp "PRIVMSG $channel :Not available."
  315. }
  316. }
  317. ################################################################################
  318. # quote_stats
  319. # !quotestats
  320. # Give some simple statistics about the db, channel, and user
  321. ################################################################################
  322. proc quote_stats { nick host handle channel text } {
  323. global db_handle quote_noflags
  324. if {![channel get $channel quoteengine]} {
  325. return 0
  326. }
  327. if [matchattr $handle $quote_noflags] { return 0 }
  328. if {![quote_ping]} {
  329. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  330. return 0
  331. }
  332. set sql "SELECT COUNT(*) AS total FROM quotes WHERE channel='$channel'"
  333. putloglev d * "QuoteEngine: executing $sql"
  334. set result [mysqlquery $db_handle $sql]
  335. set total 0
  336. set chan 0
  337. if {[set row [mysqlnext $result]] != ""} {
  338. set total [lindex $row 0]
  339. }
  340. mysqlendquery $result
  341. set sql "SELECT COUNT(*) AS total FROM quotes"
  342. putloglev d * "QuoteEngine: executing $sql"
  343. set result [mysqlquery $db_handle $sql]
  344. if {[set row [mysqlnext $result]] != ""} {
  345. set chan [lindex $row 0]
  346. }
  347. mysqlendquery $result
  348. set sql "SELECT COUNT(*) AS total FROM quotes WHERE nick='$handle' AND channel='$channel'"
  349. putloglev d * "QuoteEngine: executing $sql"
  350. set result [mysqlquery $db_handle $sql]
  351. if {[set row [mysqlnext $result]] != ""} {
  352. set by_handle [lindex $row 0]
  353. }
  354. mysqlendquery $result
  355. puthelp "PRIVMSG $channel :Quotes for $channel: \002$total\002 (total: $chan). You have added \002$by_handle\002 quotes in this channel."
  356. }
  357. ################################################################################
  358. # quote_delete
  359. # !delquote <id>
  360. # Removes a quote from the database. You can only delete the quote if you
  361. # are a bot/channel master, or if you're the person who added it.
  362. ################################################################################
  363. proc quote_delete { nick host handle channel text } {
  364. global db_handle quote_noflags
  365. if {![channel get $channel quoteengine]} {
  366. return 0
  367. }
  368. if [matchattr $handle $quote_noflags] { return 0 }
  369. if {![quote_ping]} {
  370. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  371. return 0
  372. }
  373. set text [mysqlescape $text]
  374. if {![matchattr $handle m|m $channel]} {
  375. set sql "SELECT nick FROM quotes WHERE id='$text'"
  376. putloglev d * "QuoteEngine: executing $sql"
  377. set result [mysqlquery $db_handle $sql]
  378. set owner [lindex [mysqlnext $result] 0]
  379. mysqlendquery $result
  380. if {$owner != $handle} {
  381. puthelp "NOTICE $nick :You cannot delete that quote."
  382. return 0
  383. }
  384. }
  385. set sql "DELETE FROM quotes WHERE id='$text'"
  386. putloglev d * "QuoteEngine: executing $sql"
  387. set result [mysqlexec $db_handle $sql]
  388. if {$result != 1} {
  389. puthelp "PRIVMSG $channel :An error occurred deleting the quote :("
  390. return 0
  391. } else {
  392. puthelp "PRIVMSG $channel :Deleted quote $text"
  393. }
  394. }
  395. ################################################################################
  396. # quote_version
  397. # !quoteversion
  398. # Gives the version of the script
  399. ################################################################################
  400. proc quote_version { nick host handle channel text } {
  401. global quote_version quote_noflags
  402. if [matchattr $handle $quote_noflags] { return 0 }
  403. puthelp "PRIVMSG $channel :This is the QuoteEngine version $quote_version by JamesOff (http://www.jamesoff.net/go/quoteengine)"
  404. return 0
  405. }
  406. ################################################################################
  407. # quote_help
  408. # !quotehelp
  409. # Handle help requests
  410. ################################################################################
  411. proc quote_help { nick host handle channel text } {
  412. global quote_noflags
  413. if [matchattr $handle $quote_noflags] { return 0 }
  414. puthelp "PRIVMSG $nick :Commands for the QuoteEngine script:"
  415. puthelp "PRIVMSG $nick : !addquote <quote text> - adds a quote to the database"
  416. 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."
  417. 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."
  418. puthelp "PRIVMSG $nick : !getquote \[-v\]<id> - fetches the quote with number <id>. Gives info of who added it if -v is specified."
  419. 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."
  420. puthelp "PRIVMSG $nick : !quoteurl - get the URL for the web interface to the quotes"
  421. puthelp "PRIVMSG $nick : !quotestats - get some information"
  422. puthelp "PRIVMSG $nick : !quoteversion - get the version of the script"
  423. puthelp "PRIVMSG $nick : Some commands have synonyms: !deletequote, !fetchquote, !urlquote, and !searchquote."
  424. puthelp "PRIVMSG $nick : (End of help)"
  425. return 0
  426. }
  427. proc quote_auto { nick host handle channel text } {
  428. global quote_automatic
  429. if {$quote_automatic == 0} {
  430. return
  431. }
  432. if {![channel get $channel quoteengine]} {
  433. return
  434. }
  435. global quote_auto_last db_handle quote_automatic_minimum
  436. if [info exists quote_auto_last($channel)] {
  437. set diff [expr [clock seconds] - $quote_auto_last($channel)]
  438. putloglev 1 * "diff for $channel is $diff"
  439. } else {
  440. set diff [expr $quote_automatic_minimum + 1]
  441. putloglev d * "initialising diff for $channel"
  442. set quote_auto_last($channel) 0
  443. }
  444. if {$diff < $quote_automatic_minimum} {
  445. return
  446. }
  447. set words [split $text]
  448. set newwords [list]
  449. foreach word $words {
  450. if [regexp -nocase {^[a-z0-9']{4,}$} $word] {
  451. if {[lsearch [list "yeah" "about" "hello" "their" "there" "that's" "can't" "morning" "won't"] $word] > -1} {
  452. continue
  453. }
  454. if [onchan $word] {
  455. continue
  456. }
  457. lappend newwords [mysqlescape $word]
  458. }
  459. }
  460. if {[llength $newwords] == 0} {
  461. return
  462. }
  463. putloglev d * "quoteengine: candidate words for random quote in $channel: $newwords"
  464. if {![quote_ping]} {
  465. return
  466. }
  467. set thisword [pickRandom $newwords]
  468. putloglev d * "quoteengine: using $thisword"
  469. if {[rand 100] < 95} {
  470. putloglev d * "quoteengine: not random enough, ignoring"
  471. return
  472. }
  473. set where_clause "WHERE channel='[mysqlescape $channel]' AND quote LIKE '%$thisword%' ORDER BY RAND() LIMIT 1"
  474. putloglev d * "quoteengine: $where_clause"
  475. set sql "SELECT * FROM quotes $where_clause"
  476. set result [mysqlquery $db_handle $sql]
  477. if {[set row [mysqlnext $result]] != ""} {
  478. set id [lindex $row 0]
  479. set quote [lindex $row 3]
  480. putlog "RANDOM QUOTE: $quote ($id)"
  481. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  482. set quote_auto_last($channel) [clock seconds]
  483. }
  484. mysqlendquery $result
  485. }
  486. quote_connect
  487. putlog "QuoteEngine $quote_version loaded"