QuoteEngine.tcl 15 KB

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