QuoteEngine.tcl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. # bind commands CHANGE as needed
  31. # use ".chanset #channel [+/-]quoteengine" to enable/disable individual
  32. # channels
  33. bind pub "m|fov" !addquote quote_add
  34. bind pub "m|fov" !randquote quote_rand
  35. bind pub "m|fov" !fetchquote quote_fetch
  36. bind pub "m|fov" !getquote quote_fetch
  37. bind pub "m|fov" !findquote quote_search
  38. bind pub "m|fov" !searchquote quote_search
  39. bind pub "m|fov" !urlquote quote_url
  40. bind pub "-|-" !quoteurl quote_url
  41. bind pub "m|ov" !delquote quote_delete
  42. bind pub "m|ov" !deletequote quote_delete
  43. bind pub "m|ov" !quotestats quote_stats
  44. bind pub "-|-" !quoteversion quote_version
  45. bind pub "-|-" !quotehelp quote_help
  46. # a user with this flag(s) can't use the script at all
  47. set quote_noflags "Q|Q"
  48. # maximum number of quotes to show in channel when searching before switching
  49. # to /msg'ing the user
  50. set quote_chanmax 5
  51. ### code starts here (no need to edit stuff below currently)
  52. #1.00
  53. set quote_version "cvs"
  54. #add setting to channel
  55. setudef flag quoteengine
  56. ### procedures start here
  57. ################################################################################
  58. # quote_ping
  59. # Check we're still connected to mysql
  60. ################################################################################
  61. proc quote_ping { } {
  62. global db_handle
  63. if [::mysql::ping $db_handle] {
  64. return 1
  65. } else {
  66. return [quote_connect]
  67. }
  68. }
  69. ################################################################################
  70. # quote_add
  71. # !addquote <text>
  72. # Adds a quote to the database
  73. ################################################################################
  74. proc quote_add { nick host handle channel text } {
  75. global db_handle quote_noflags
  76. if {![channel get $channel quoteengine]} {
  77. return 0
  78. }
  79. if [matchattr $handle $quote_noflags] { return 0 }
  80. if {($handle == "") || ($handle == "*")} {
  81. set handle $nick
  82. }
  83. if {![quote_ping]} {
  84. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  85. return 0
  86. }
  87. set sql "INSERT INTO quotes VALUES(null, "
  88. append sql "'$handle', "
  89. append sql "'$nick!$host', "
  90. set text [mysqlescape $text]
  91. append sql "'$text', "
  92. append sql "'$channel', "
  93. append sql "'[clock seconds]')"
  94. putloglev d * "QuoteEngine: executing $sql"
  95. set result [mysqlexec $db_handle $sql]
  96. if {$result != 1} {
  97. putlog "An error occurred with the sql :("
  98. } else {
  99. set id [mysqlinsertid $db_handle]
  100. puthelp "PRIVMSG $channel :Quote \002$id\002 added"
  101. if [regexp {[^]> ]\|[[<0-9(]} $text] {
  102. 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."
  103. }
  104. }
  105. }
  106. ################################################################################
  107. # quote_rand
  108. # !randquote [--all|--channel #channel]
  109. # Gets a random quote from the database for the current channel
  110. # --all: Choose from entire database
  111. # --channel: Choose from given channel
  112. # -c: Shortcut for --channel
  113. ################################################################################
  114. proc quote_rand { nick host handle channel text } {
  115. global db_handle quote_noflags
  116. if {![channel get $channel quoteengine]} {
  117. return 0
  118. }
  119. if [matchattr $handle $quote_noflags] { return 0 }
  120. if {![quote_ping]} {
  121. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  122. return 0
  123. }
  124. set where_clause "WHERE channel='$channel'"
  125. if [regexp -- "--?all" $text] {
  126. set where_clause ""
  127. }
  128. if [regexp -- "--?c(hannel)?( |=)(.+)" $text matches skip1 skip2 newchan] {
  129. set where_clause "WHERE channel='[mysqlescape $newchan]'"
  130. }
  131. set sql "SELECT * FROM quotes $where_clause ORDER BY RAND() LIMIT 1"
  132. putloglev d * "QuoteEngine: executing $sql"
  133. set result [mysqlquery $db_handle $sql]
  134. if {[set row [mysqlnext $result]] != ""} {
  135. set id [lindex $row 0]
  136. set quote [lindex $row 3]
  137. set by [lindex $row 1]
  138. set when [clock format [lindex $row 5] -format "%Y/%m/%d %H:%M"]
  139. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  140. } else {
  141. puthelp "PRIVMSG $channel :Couldn't find a quote :("
  142. }
  143. mysqlendquery $result
  144. }
  145. ################################################################################
  146. # quote_fetch
  147. # !getquote <id>
  148. # Fetches the given quote from the database
  149. ################################################################################
  150. proc quote_fetch { nick host handle channel text } {
  151. global db_handle quote_noflags
  152. if {![channel get $channel quoteengine]} {
  153. return 0
  154. }
  155. if [matchattr $handle $quote_noflags] { return 0 }
  156. if {![regexp {[0-9]+} $text]} {
  157. puthelp "PRIVMSG $channel: Use: !getquote <id>"
  158. return 0
  159. }
  160. if {![quote_ping]} {
  161. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  162. return 0
  163. }
  164. set text [mysqlescape $text]
  165. set sql "SELECT * FROM quotes WHERE id='$text'"
  166. putloglev d * "QuoteEngine: executing $sql"
  167. set result [mysqlquery $db_handle $sql]
  168. if {[set row [mysqlnext $result]] != ""} {
  169. set id [lindex $row 0]
  170. set quote [lindex $row 3]
  171. set by [lindex $row 1]
  172. set when [clock format [lindex $row 5] -format "%Y/%m/%d %H:%M"]
  173. set chan [lindex $row 4]
  174. if {$chan != $channel} {
  175. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  176. puthelp "PRIVMSG $channel :\[\002$id\002\] From $chan, added $by at $when."
  177. } else {
  178. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  179. puthelp "PRIVMSG $channel :\[\002$id\002\] Added $by at $when."
  180. }
  181. } else {
  182. puthelp "PRIVMSG $channel :Couldn't find quote $text"
  183. }
  184. mysqlendquery $result
  185. }
  186. ################################################################################
  187. # quote_search
  188. # !findquote [--all] [--channel #channel] [--count <int>] <text>
  189. # Find all quotes with "text" in them. (in random order)
  190. # The first 5 (by default) are listed in the channel. The rest are /msg'd to
  191. # you up to the maxiumum (default 5).
  192. # --all: Search all channels, not just current one
  193. # --channel: Search given channel
  194. # --count <int>: Find this many total quotes
  195. # -c: Shortcut for --channel
  196. # -n: Shortcut for --count
  197. # Note this is a SQL search, so use % as the wildcard (instead of *)
  198. # The script automatically puts %s around your text when searching.
  199. ################################################################################
  200. proc quote_search { nick host handle channel text } {
  201. global db_handle php_page quote_noflags quote_chanmax
  202. if {![channel get $channel quoteengine]} {
  203. return 0
  204. }
  205. if [matchattr $handle $quote_noflags] { return 0 }
  206. if {$text == ""} {
  207. puthelp "PRIVMSG $channel :Use: !findquote <text>"
  208. return 0
  209. }
  210. if {![quote_ping]} {
  211. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  212. return 0
  213. }
  214. set where_clause "AND channel='[mysqlescape $channel]'"
  215. if [regexp -- "--?all " $text matches skip1] {
  216. set where_clause ""
  217. regsub -- $matches $text "" text
  218. }
  219. if [regexp -- {--?c(hannel)?( |=)([^ ]+)} $text matches skip1 skip2 newchan] {
  220. set where_clause "AND channel='[mysqlescape $newchan]'"
  221. regsub -- $matches $text "" text
  222. }
  223. set limit 5
  224. if [regexp -- {--?count( |=)([^ ]+)} $text matches skip1 count] {
  225. set limit [mysqlescape $count]
  226. regsub -- $matches $text "" text
  227. }
  228. if [regexp -- {-n( )?([^ ]+)} $text matches skip1 count] {
  229. set limit [mysqlescape $count]
  230. regsub -- $matches $text "" text
  231. }
  232. set sql "SELECT * FROM quotes WHERE quote LIKE '%[mysqlescape $text]%' $where_clause ORDER BY RAND()"
  233. putloglev d * "QuoteEngine: executing $sql"
  234. if {[mysqlsel $db_handle $sql] > 0} {
  235. set count 0
  236. mysqlmap $db_handle {id qnick qhost quote qchannel qts} {
  237. if {$count == $limit} {
  238. break
  239. }
  240. if {$count == $quote_chanmax} {
  241. puthelp "PRIVMSG $nick :Rest of matches for your search '$text' follow in private:"
  242. }
  243. if {$count < $quote_chanmax} {
  244. puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
  245. } else {
  246. puthelp "PRIVMSG $nick :\[\002$id\002\] $quote"
  247. }
  248. incr count
  249. }
  250. set remaining [mysqlresult $db_handle rows?]
  251. if {$remaining > 0} {
  252. if {$count < $quote_chanmax} {
  253. set command "PRIVMSG $channel :"
  254. } else {
  255. set command "PRIVMSG $nick :"
  256. }
  257. regsub "#" $channel "" chan
  258. if {$php_page != ""} {
  259. puthelp "${command}(Plus $remaining more matches: $php_page?filter=${text}&channel=${chan}&search=search)"
  260. } else {
  261. puthelp "${command}Plus $remaining other matches"
  262. }
  263. } else {
  264. if {$count == 1} {
  265. puthelp "${command}(All of 1 match)"
  266. } else {
  267. puthelp "${command}(All of $count matches)"
  268. }
  269. }
  270. } else {
  271. puthelp "PRIVMSG $channel :No matches"
  272. }
  273. }
  274. ################################################################################
  275. # quote_url
  276. # !quoteurl
  277. # Gives the web of the web interface
  278. ################################################################################
  279. proc quote_url { nick host handle channel text } {
  280. global php_page quote_noflags
  281. if {![channel get $channel quoteengine]} {
  282. return 0
  283. }
  284. if [matchattr $handle $quote_noflags] { return 0 }
  285. if {$php_page != ""} {
  286. # changed for better url by dubkat
  287. puthelp "PRIVMSG $channel :${php_page}?channel=[string range $channel 1 end]"
  288. } else {
  289. puthelp "PRIVMSG $channel :Not available."
  290. }
  291. }
  292. ################################################################################
  293. # quote_stats
  294. # !quotestats
  295. # Give some simple statistics about the db, channel, and user
  296. ################################################################################
  297. proc quote_stats { nick host handle channel text } {
  298. global db_handle quote_noflags
  299. if {![channel get $channel quoteengine]} {
  300. return 0
  301. }
  302. if [matchattr $handle $quote_noflags] { return 0 }
  303. if {![quote_ping]} {
  304. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  305. return 0
  306. }
  307. set sql "SELECT COUNT(*) AS total FROM quotes WHERE channel='$channel'"
  308. putloglev d * "QuoteEngine: executing $sql"
  309. set result [mysqlquery $db_handle $sql]
  310. set total 0
  311. set chan 0
  312. if {[set row [mysqlnext $result]] != ""} {
  313. set total [lindex $row 0]
  314. }
  315. mysqlendquery $result
  316. set sql "SELECT COUNT(*) AS total FROM quotes"
  317. putloglev d * "QuoteEngine: executing $sql"
  318. set result [mysqlquery $db_handle $sql]
  319. if {[set row [mysqlnext $result]] != ""} {
  320. set chan [lindex $row 0]
  321. }
  322. mysqlendquery $result
  323. set sql "SELECT COUNT(*) AS total FROM quotes WHERE nick='$handle' AND channel='$channel'"
  324. putloglev d * "QuoteEngine: executing $sql"
  325. set result [mysqlquery $db_handle $sql]
  326. if {[set row [mysqlnext $result]] != ""} {
  327. set by_handle [lindex $row 0]
  328. }
  329. mysqlendquery $result
  330. puthelp "PRIVMSG $channel :Quotes for $channel: \002$total\002 (total: $chan). You have added \002$by_handle\002 quotes in this channel."
  331. }
  332. ################################################################################
  333. # quote_delete
  334. # !delquote <id>
  335. # Removes a quote from the database. You can only delete the quote if you
  336. # are a bot/channel master, or if you're the person who added it.
  337. ################################################################################
  338. proc quote_delete { nick host handle channel text } {
  339. global db_handle quote_noflags
  340. if {![channel get $channel quoteengine]} {
  341. return 0
  342. }
  343. if [matchattr $handle $quote_noflags] { return 0 }
  344. if {![quote_ping]} {
  345. putquick "PRIVMSG $channel :Sorry, lost database connection :("
  346. return 0
  347. }
  348. set text [mysqlescape $text]
  349. if {![matchattr $handle m|m $channel]} {
  350. set sql "SELECT nick FROM quotes WHERE id='$text'"
  351. putloglev d * "QuoteEngine: executing $sql"
  352. set result [mysqlquery $db_handle $sql]
  353. set owner [lindex [mysqlnext $result] 0]
  354. mysqlendquery $result
  355. if {$owner != $handle} {
  356. puthelp "NOTICE $nick :You cannot delete that quote."
  357. return 0
  358. }
  359. }
  360. set sql "DELETE FROM quotes WHERE id='$text'"
  361. putloglev d * "QuoteEngine: executing $sql"
  362. set result [mysqlexec $db_handle $sql]
  363. if {$result != 1} {
  364. puthelp "PRIVMSG $channel :An error occurred deleting the quote :("
  365. return 0
  366. } else {
  367. puthelp "PRIVMSG $channel :Deleted quote $text"
  368. }
  369. }
  370. ################################################################################
  371. # quote_version
  372. # !quoteversion
  373. # Gives the version of the script
  374. ################################################################################
  375. proc quote_version { nick host handle channel text } {
  376. global quote_version quote_noflags
  377. if [matchattr $handle $quote_noflags] { return 0 }
  378. puthelp "PRIVMSG $channel :This is the QuoteEngine version $quote_version by JamesOff (http://www.jamesoff.net/go/quoteengine)"
  379. return 0
  380. }
  381. ################################################################################
  382. # quote_help
  383. # !quotehelp
  384. # Handle help requests
  385. ################################################################################
  386. proc quote_help { nick host handle channel text } {
  387. global quote_noflags
  388. if [matchattr $handle $quote_noflags] { return 0 }
  389. puthelp "PRIVMSG $nick :Commands for the QuoteEngine script:"
  390. puthelp "PRIVMSG $nick : !addquote <quote text> - adds a quote to the database"
  391. 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."
  392. 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."
  393. puthelp "PRIVMSG $nick : !getquote <id> - fetches the quote with number <id>"
  394. 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."
  395. puthelp "PRIVMSG $nick : !quoteurl - get the URL for the web interface to the quotes"
  396. puthelp "PRIVMSG $nick : !quotestats - get some information"
  397. puthelp "PRIVMSG $nick : !quoteversion - get the version of the script"
  398. puthelp "PRIVMSG $nick : Some commands have synonyms: !deletequote, !fetchquote, !urlquote, and !searchquote."
  399. puthelp "PRIVMSG $nick : (End of help)"
  400. return 0
  401. }
  402. quote_connect
  403. putlog "QuoteEngine $quote_version loaded"