QuoteEngine.tcl 17 KB

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