QuoteEngine.tcl 15 KB

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