QuoteEngine.tcl 18 KB

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