BT.Quote.tcl 12 KB

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