QuoteEngine.tcl 14 KB

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