QuoteEngine.tcl 17 KB

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