QuoteEngine.tcl 18 KB

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