BT.Quote.tcl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #########################################################################
  2. ## BlackTools - The Ultimate Channel Control Script ##
  3. ## One TCL. One smart Eggdrop ##
  4. #########################################################################
  5. ############################# QUOTE TCL #############################
  6. #########################################################################
  7. ## ##
  8. ## BlackTools : http://blacktools.tclscripts.net ##
  9. ## Bugs report : http://www.tclscripts.net/ ##
  10. ## Online Help : irc://irc.undernet.org/tcl-help ##
  11. ## #TCL-HELP / UnderNet ##
  12. ## You can ask in english or romanian ##
  13. ## ##
  14. #########################################################################
  15. proc quote:announce {} {
  16. global black
  17. set return_time [time_return_minute $black(quote:msgtime)]
  18. set channels ""
  19. foreach chan [channels] {
  20. if {[validchan $chan]} {
  21. if {[setting:get $chan quoteofday]} {
  22. lappend channels $chan
  23. }
  24. }
  25. }
  26. if {$channels != ""} {
  27. quote:time $channels 0
  28. timer $return_time quote:announce
  29. }
  30. }
  31. proc quote:time {channels counter} {
  32. global black
  33. set chan [lindex $channels $counter]
  34. set cc [expr $counter + 1]
  35. if {$chan != ""} {
  36. quoteofday:show $chan
  37. }
  38. if {[lindex $channels $cc] == ""} {
  39. return
  40. } else {
  41. quote:time $channels $cc
  42. }
  43. }
  44. proc quoteofday:show {chan} {
  45. global black botnick
  46. set getlang [string tolower [setting:get $chan lang]]
  47. if {$getlang == ""} { set getlang "[string tolower $black(default_lang)]" }
  48. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  49. set temp_dir "$black(tempdir)/quote_temp.$timestamp"
  50. set day [clock format [clock seconds] -format {%d}]
  51. set file [open $black(quote_file) "r"]
  52. set temp [open $temp_dir "a"]
  53. while {[gets $file line] != -1} {
  54. set read_chan [lindex [split $line] 0]
  55. set enc_chan [encoding convertfrom utf-8 $read_chan]
  56. if {[string match -nocase $enc_chan $chan]} {
  57. set quote [join [lrange [split $line] 6 end]]
  58. set gettime [lindex [split $line] 4]
  59. set getday [clock format $gettime -format %d]
  60. if {$getday == $day} {
  61. puts $temp $quote
  62. }
  63. }
  64. }
  65. close $temp
  66. close $file
  67. set file [open $temp_dir "r"]
  68. set data [read -nonewline $file]
  69. close $file
  70. if {$data != ""} {
  71. if {![info exists black(quoteshow:$chan)]} {
  72. set black(quoteshow:$chan) 0
  73. }
  74. set lines [split $data "\n"]
  75. set line [lindex $lines $black(quoteshow:$chan)]
  76. if {$line == ""} {
  77. set black(quoteshow:$chan) 0
  78. set line [lindex $lines $black(quoteshow:$chan)]
  79. }
  80. set black(quoteshow:$chan) [expr $black(quoteshow:$chan) + 1]
  81. set encoded [encoding convertfrom utf-8 $line]
  82. set replace(%msg%) $encoded
  83. set text [black:color:set "" $black(say.$getlang.quote.16)]
  84. set reply [join $text]
  85. set text2 [black:color:set "" $black(say.$getlang.quote.12)]
  86. set reply2 [join $text2]
  87. set message [join [string map [array get replace] $reply]]
  88. set split_message [split $message "~"]
  89. if {[onchan $botnick $chan]} {
  90. if {$black(quote:outtype) == "1"} {
  91. puthelp "PRIVMSG $chan :\001ACTION $reply2\001"
  92. } else {
  93. puthelp "PRIVMSG $chan :$reply2"
  94. }
  95. foreach mes $split_message {
  96. if {$black(quote:outtype) == "1"} {
  97. puthelp "PRIVMSG $chan :\001ACTION \"$mes\"\001"
  98. } else {
  99. puthelp "PRIVMSG $chan :\"$mes\""
  100. }
  101. }
  102. }
  103. }
  104. file delete $temp_dir
  105. }
  106. proc quote:process {nick host hand chan chan1 type who quote arg next} {
  107. global black
  108. set cmd_status [btcmd:status $chan $hand "q" 0]
  109. if {$cmd_status == "1"} {
  110. return
  111. }
  112. if {[setting:get $chan quote]} {
  113. set show_who $who
  114. if {[matchattr $hand q]} { blacktools:tell $nick $host $hand $chan $chan1 gl.glsuspend none
  115. return
  116. }
  117. if {[matchattr $hand -|q $chan]} { blacktools:tell $nick $host $hand $chan $chan1 gl.suspend none
  118. return
  119. }
  120. switch $who {
  121. list {
  122. if {[matchattr $hand nmo|M $chan]} {
  123. array set nicklist [list]
  124. set getlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  125. if {$getlang == ""} { set getlang "[string tolower $black(default_lang)]" }
  126. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  127. set temp "$black(tempdir)/quote_temp.$timestamp"
  128. set file [open $black(quote_file) "r"]
  129. while {[gets $file line] != -1} {
  130. set read_chan [lindex [split $line] 0]
  131. set enc_chan [encoding convertfrom utf-8 $read_chan]
  132. set read_type [lindex [split $line] 1]
  133. if {[string match -nocase $enc_chan $chan]} {
  134. set msg_num [lindex [split $line] 2]
  135. set by_who [lindex [split $line] 3]
  136. set quote [join [lrange [split $line] 6 end]]
  137. lappend nicklist($msg_num) "$quote :: $black(say.$getlang.quote.15) $by_who"
  138. }
  139. }
  140. close $file
  141. set tempwrite [open $temp w]
  142. foreach msg [lsort -integer -increasing [array names nicklist]] {
  143. puts $tempwrite "$msg [join $nicklist($msg)]"
  144. }
  145. close $tempwrite
  146. set file [open $temp "r"]
  147. set w [read -nonewline $file]
  148. close $file
  149. set data [split $w "\n"]
  150. file delete $temp
  151. module:getinfo $nick $host $hand $chan $chan1 $type $data "quote" "0" $next
  152. }
  153. }
  154. del {
  155. if {[matchattr $hand nmo|M $chan]} {
  156. if {![regexp {^[0-9]} $next]} {
  157. switch $type {
  158. 0 {
  159. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "quote"
  160. }
  161. 1 {
  162. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "quote"
  163. }
  164. 2 {
  165. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "quote"
  166. }
  167. }
  168. return
  169. }
  170. set ret [find:q:num $next $chan]
  171. if {$ret == 0} {
  172. blacktools:tell $nick $host $hand $chan $chan1 quote.13 $next
  173. return
  174. }
  175. set file [open $black(quote_file) "r"]
  176. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  177. set temp "$black(tempdir)/quote_temp.$timestamp"
  178. set tempwrite [open $temp w]
  179. while {[gets $file line] != -1} {
  180. set read_num [lindex [split $line] 2]
  181. set read_chan [lindex [split $line] 0]
  182. set enc_chan [encoding convertfrom utf-8 $read_chan]
  183. if {[string equal $next $read_num] && [string equal -nocase $enc_chan $chan]} {
  184. continue
  185. } else {
  186. puts $tempwrite $line
  187. }
  188. }
  189. close $tempwrite
  190. close $file
  191. file rename -force $temp $black(quote_file)
  192. blacktools:tell $nick $host $hand $chan $chan1 quote.10 $next
  193. }
  194. }
  195. default {
  196. if {[llength [split $arg]] > 1} {
  197. set ret_num [quote:add $nick $host $chan $who $quote]
  198. blacktools:tell $nick $host $hand $chan $chan1 quote.1 $ret_num
  199. }
  200. if {[llength [split $arg]] == 1} {
  201. quote:show $nick $host $hand $chan $chan1 $type $show_who
  202. }
  203. if {[llength [split $arg]] == 0} {
  204. quote:rand $nick $host $hand $chan $chan1 $type $show_who
  205. }
  206. }
  207. }
  208. }
  209. }
  210. proc quote:rand {nick host hand chan chan1 type who} {
  211. global black
  212. set total [quote:total $chan]
  213. set length [llength $total]
  214. if {$length > 0} {
  215. set random [expr int(rand()*$length)]
  216. set quote [lindex $total $random]
  217. set file [open $black(quote_file) "r"]
  218. while {[gets $file line] != -1} {
  219. set read_chan [lindex [split $line] 0]
  220. set num [lindex [split $line] 2]
  221. set enc_chan [encoding convertfrom utf-8 $read_chan]
  222. if {[string equal -nocase $chan $enc_chan] && [string equal -nocase $quote $num]} {
  223. set time [lindex [split $line] 4]
  224. set time [clock format $time -format %D]
  225. set readquote [join [lrange [split $line] 6 end]]
  226. set encoded [encoding convertfrom utf-8 $readquote]
  227. continue
  228. }
  229. }
  230. }
  231. close $file
  232. blacktools:tell $nick $host $hand $chan $chan1 quote.3 "$quote $time"
  233. set split_encoded [split $encoded "~"]
  234. foreach enc $split_encoded {
  235. blacktools:tell $nick $host $hand $chan $chan1 quote.19 $enc
  236. }
  237. }
  238. proc quote:show {nick host hand chan chan1 type who} {
  239. global black
  240. set quote_numbers ""
  241. set find_num 0
  242. set found_quote 0
  243. set show_who $who
  244. if {[regexp {^[0-9]} $who]} {
  245. set quote_num $who
  246. set find_num 1
  247. } else {
  248. set quote_list [quote:count $who $chan]
  249. set length [llength $quote_list]
  250. if {$length > 0} {
  251. set found_quote 1
  252. }
  253. set random [expr int(rand()*$length)]
  254. set quote [lindex $quote_list $random]
  255. set split_quote [split $quote ":"]
  256. set quote_num [lindex $split_quote 0]
  257. set quote_show [lindex $split_quote 1]
  258. foreach q $quote_list {
  259. set q_split [split $q ":"]
  260. set q_num [lindex $q_split 0]
  261. if {$quote_num != $q_num} {
  262. lappend quote_numbers \#$q_num
  263. }
  264. }
  265. if {$found_quote == "0"} {
  266. blacktools:tell $nick $host $hand $chan $chan1 quote.11 none
  267. return
  268. }
  269. }
  270. set file [open $black(quote_file) "r"]
  271. while {[gets $file line] != -1} {
  272. set read_chan [lindex [split $line] 0]
  273. set num [lindex [split $line] 2]
  274. set enc_chan [encoding convertfrom utf-8 $read_chan]
  275. if {[string equal -nocase $chan $enc_chan] && [string equal -nocase $quote_num $num]} {
  276. set found_quote 1
  277. set time [lindex [split $line] 4]
  278. set time [clock format $time -format %D]
  279. set readquote [join [lrange [split $line] 6 end]]
  280. set encoded [encoding convertfrom utf-8 $readquote]
  281. continue
  282. }
  283. }
  284. close $file
  285. if {$found_quote == "0"} {
  286. blacktools:tell $nick $host $hand $chan $chan1 quote.11 none
  287. return
  288. }
  289. if {$find_num == "0"} {
  290. blacktools:tell:h $nick $host $hand $chan $chan1 quote.4 "$quote_show/$length $show_who $time"
  291. }
  292. set split_encoded [split $encoded "~"]
  293. foreach enc $split_encoded {
  294. blacktools:tell $nick $host $hand $chan $chan1 quote.17 "$quote_num $enc"
  295. }
  296. if {$quote_numbers != ""} {
  297. blacktools:tell $nick $host $hand $chan $chan1 quote.18 [join $quote_numbers]
  298. }
  299. }
  300. proc quote:total {chan} {
  301. global black
  302. set nums ""
  303. set file [open $black(quote_file) "r"]
  304. while {[gets $file line] != -1} {
  305. set read_chan [lindex [split $line] 0]
  306. set enc_chan [encoding convertfrom utf-8 $read_chan]
  307. if {[string equal -nocase $chan $enc_chan]} {
  308. set read_num [lindex [split $line] 2]
  309. lappend nums $read_num
  310. }
  311. }
  312. close $file
  313. return $nums
  314. }
  315. proc quote:count {who chan} {
  316. global black
  317. set q_count 0
  318. set return ""
  319. set found_nick 0
  320. set found_quote 0
  321. set file [open $black(quote_file) "r"]
  322. while {[gets $file line] != -1} {
  323. set read_chan [lindex [split $line] 0]
  324. set read_who [lindex [split $line] 5]
  325. set read_who [string map { "<" ""
  326. ">" ""
  327. "@" ""
  328. "+" ""
  329. } $read_who]
  330. set enc_chan [encoding convertfrom utf-8 $read_chan]
  331. set read_who [string map [list \[ {\[} \] {\]} \? {\?} \\ {\\}] $read_who]
  332. if {[string equal -nocase $chan $enc_chan] && [string match -nocase $read_who $who]} {
  333. set found_nick 1
  334. set q_count [expr $q_count + 1]
  335. set num [lindex [split $line] 2]
  336. set return "$return $num:$q_count"
  337. }
  338. }
  339. close $file
  340. if {$found_nick == "0"} {
  341. set file [open $black(quote_file) "r"]
  342. while {[gets $file line] != -1} {
  343. set read_chan [lindex [split $line] 0]
  344. set readquote [lrange [split $line] 6 end]
  345. set enc_chan [encoding convertfrom utf-8 $read_chan]
  346. if {[string equal -nocase $chan $enc_chan] && ([lsearch -exact $readquote $who] > -1)} {
  347. set found_quote 1
  348. set q_count [expr $q_count + 1]
  349. set num [lindex [split $line] 2]
  350. set return "$return $num:$q_count"
  351. }
  352. }
  353. close $file
  354. }
  355. return $return
  356. }
  357. proc quote:add {nick host chan who quote} {
  358. global black
  359. set temp_num 0
  360. set num 0
  361. set time [unixtime]
  362. while {$temp_num == 0} {
  363. set get [find:q:num $num $chan]
  364. if {$get == "$num"} {
  365. set num [expr $num + 1]
  366. } else { set temp_num 1 }
  367. }
  368. set file [open $black(quote_file) a]
  369. set enc_chan [encoding convertto utf-8 $chan]
  370. set encoded [encoding convertto utf-8 $quote]
  371. puts $file "$enc_chan QUOTE $num $nick $time $who $encoded"
  372. close $file
  373. return $num
  374. }
  375. ##############
  376. #########################################################################
  377. ## END ##
  378. #########################################################################