BT.Quote.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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-script ##
  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. if {$black(quote:outtype) == "1"} {
  101. puthelp "PRIVMSG $chan :\001ACTION $reply2\001"
  102. } else {
  103. puthelp "PRIVMSG $chan :$reply2"
  104. }
  105. foreach mes $split_message {
  106. set encoded [encoding convertto utf-8 $mes]
  107. if {$black(quote:outtype) == "1"} {
  108. puthelp "PRIVMSG $chan :\001ACTION \"[join $encoded]\"\001"
  109. } else {
  110. puthelp "PRIVMSG $chan :\"[join $encoded]\""
  111. }
  112. }
  113. } else {
  114. set encoded [encoding convertto utf-8 $message]
  115. if {$black(quote:outtype) == "1"} {
  116. puthelp "PRIVMSG $chan :\001ACTION $reply2 [join $encoded]\001"
  117. } else {
  118. puthelp "PRIVMSG $chan :$reply2 [join $encoded]"
  119. }
  120. }
  121. }
  122. }
  123. file delete $temp_dir
  124. }
  125. proc quote:process {nick host hand chan chan1 type who quote arg next} {
  126. global black
  127. set cmd_status [btcmd:status $chan $hand "q" 0]
  128. if {$cmd_status == "1"} {
  129. return
  130. }
  131. if {[setting:get $chan quote]} {
  132. set show_who $who
  133. if {[matchattr $hand q]} { blacktools:tell $nick $host $hand $chan $chan1 gl.glsuspend none
  134. return
  135. }
  136. if {[matchattr $hand -|q $chan]} { blacktools:tell $nick $host $hand $chan $chan1 gl.suspend none
  137. return
  138. }
  139. set split_host [split $host ":"]
  140. if {[lindex $split_host 1] == "chan"} {
  141. set host [lindex $split_host 1]
  142. set charbind [lindex $split_host 0]
  143. } else {
  144. set charbind "$host"
  145. }
  146. switch $who {
  147. list {
  148. if {[matchattr $hand nmo|M $chan]} {
  149. array set nicklist [list]
  150. set getlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  151. if {$getlang == ""} { set getlang "[string tolower $black(default_lang)]" }
  152. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  153. set temp "$black(tempdir)/quote_temp.$timestamp"
  154. set file [open $black(quote_file) "r"]
  155. while {[gets $file line] != -1} {
  156. set read_chan [lindex [split $line] 0]
  157. set enc_chan [encoding convertfrom utf-8 $read_chan]
  158. set read_type [lindex [split $line] 1]
  159. if {[string match -nocase $enc_chan $chan]} {
  160. set msg_num [lindex [split $line] 2]
  161. set by_who [lindex [split $line] 3]
  162. set time [lindex [split $line] 4]
  163. set time [clock format $time -format %D]
  164. set quote [join [lrange [split $line] 6 end]]
  165. lappend nicklist($msg_num) "$quote :: $black(say.$getlang.quote.15) $by_who :: $black(say.$getlang.quote.20) \002$time\002"
  166. }
  167. }
  168. close $file
  169. set tempwrite [open $temp w]
  170. foreach msg [lsort -integer -increasing [array names nicklist]] {
  171. puts $tempwrite "$msg [join $nicklist($msg)]"
  172. }
  173. close $tempwrite
  174. set file [open $temp "r"]
  175. set w [read -nonewline $file]
  176. close $file
  177. set data [split $w "\n"]
  178. file delete $temp
  179. module:getinfo $nick $charbind $hand $chan $chan1 $type $data "quote" "0" $next
  180. }
  181. }
  182. del {
  183. if {[matchattr $hand nmo|M $chan]} {
  184. if {![regexp {^[0-9]} $next]} {
  185. switch $type {
  186. 0 {
  187. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "quote"
  188. }
  189. 1 {
  190. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "quote"
  191. }
  192. 2 {
  193. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "quote"
  194. }
  195. }
  196. return
  197. }
  198. set ret [find:q:num $next $chan]
  199. if {$ret == 0} {
  200. blacktools:tell $nick $host $hand $chan $chan1 quote.13 $next
  201. return
  202. }
  203. set file [open $black(quote_file) "r"]
  204. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  205. set temp "$black(tempdir)/quote_temp.$timestamp"
  206. set tempwrite [open $temp w]
  207. while {[gets $file line] != -1} {
  208. set read_num [lindex [split $line] 2]
  209. set read_chan [lindex [split $line] 0]
  210. set enc_chan [encoding convertfrom utf-8 $read_chan]
  211. if {[string equal $next $read_num] && [string equal -nocase $enc_chan $chan]} {
  212. continue
  213. } else {
  214. puts $tempwrite $line
  215. }
  216. }
  217. close $tempwrite
  218. close $file
  219. file rename -force $temp $black(quote_file)
  220. blacktools:tell $nick $charbind $hand $chan $chan1 quote.10 $next
  221. }
  222. }
  223. default {
  224. if {[llength [split $arg]] > 1} {
  225. set ret_num [quote:add $nick $host $chan $who $quote]
  226. blacktools:tell $nick $charbind $hand $chan $chan1 quote.1 $ret_num
  227. }
  228. if {[llength [split $arg]] == 1} {
  229. quote:show $nick $host $hand $chan $chan1 $type $show_who
  230. }
  231. if {[llength [split $arg]] == 0} {
  232. quote:rand $nick $host $hand $chan $chan1 $type $show_who
  233. }
  234. }
  235. }
  236. }
  237. }
  238. proc quote:rand {nick host hand chan chan1 type who} {
  239. global black
  240. set total [quote:total $chan]
  241. set length [llength $total]
  242. if {$length > 0} {
  243. set random [expr int(rand()*$length)]
  244. set quote [lindex $total $random]
  245. set file [open $black(quote_file) "r"]
  246. while {[gets $file line] != -1} {
  247. set read_chan [lindex [split $line] 0]
  248. set num [lindex [split $line] 2]
  249. set enc_chan [encoding convertfrom utf-8 $read_chan]
  250. if {[string equal -nocase $chan $enc_chan] && [string equal -nocase $quote $num]} {
  251. set readquote [join [lrange [split $line] 6 end]]
  252. set encoded [encoding convertfrom utf-8 $readquote]
  253. continue
  254. }
  255. }
  256. }
  257. close $file
  258. set split_encoded [split $encoded "~"]
  259. foreach enc $split_encoded {
  260. blacktools:tell $nick $host $hand $chan $chan1 quote.3 "$quote $enc"
  261. }
  262. }
  263. proc quote:show {nick host hand chan chan1 type who} {
  264. global black
  265. set quote_numbers ""
  266. set find_num 0
  267. set found_quote 0
  268. set show_who $who
  269. if {[regexp {^[0-9]} $who]} {
  270. set quote_num $who
  271. set find_num 1
  272. } else {
  273. set quote_list [quote:count $who $chan]
  274. set length [llength $quote_list]
  275. if {$length > 0} {
  276. set found_quote 1
  277. }
  278. set random [expr int(rand()*$length)]
  279. set quote [lindex $quote_list $random]
  280. set split_quote [split $quote ":"]
  281. set quote_num [lindex $split_quote 0]
  282. set quote_show [lindex $split_quote 1]
  283. foreach q $quote_list {
  284. set q_split [split $q ":"]
  285. set q_num [lindex $q_split 0]
  286. if {$quote_num != $q_num} {
  287. lappend quote_numbers \#$q_num
  288. }
  289. }
  290. if {$found_quote == "0"} {
  291. blacktools:tell $nick $host $hand $chan $chan1 quote.11 none
  292. return
  293. }
  294. }
  295. set file [open $black(quote_file) "r"]
  296. while {[gets $file line] != -1} {
  297. set read_chan [lindex [split $line] 0]
  298. set num [lindex [split $line] 2]
  299. set enc_chan [encoding convertfrom utf-8 $read_chan]
  300. if {[string equal -nocase $chan $enc_chan] && [string equal -nocase $quote_num $num]} {
  301. set found_quote 1
  302. set readquote [join [lrange [split $line] 6 end]]
  303. set encoded [encoding convertfrom utf-8 $readquote]
  304. continue
  305. }
  306. }
  307. close $file
  308. if {$found_quote == "0"} {
  309. blacktools:tell $nick $host $hand $chan $chan1 quote.11 none
  310. return
  311. }
  312. if {$find_num == "0"} {
  313. blacktools:tell:h $nick $host $hand $chan $chan1 quote.4 "$quote_show/$length $show_who"
  314. }
  315. set split_encoded [split $encoded "~"]
  316. foreach enc $split_encoded {
  317. blacktools:tell $nick $host $hand $chan $chan1 quote.17 "$quote_num $enc"
  318. }
  319. if {$quote_numbers != ""} {
  320. blacktools:tell $nick $host $hand $chan $chan1 quote.18 [join $quote_numbers]
  321. }
  322. }
  323. proc quote:total {chan} {
  324. global black
  325. set nums ""
  326. set file [open $black(quote_file) "r"]
  327. while {[gets $file line] != -1} {
  328. set read_chan [lindex [split $line] 0]
  329. set enc_chan [encoding convertfrom utf-8 $read_chan]
  330. if {[string equal -nocase $chan $enc_chan]} {
  331. set read_num [lindex [split $line] 2]
  332. lappend nums $read_num
  333. }
  334. }
  335. close $file
  336. return $nums
  337. }
  338. proc quote:count {who chan} {
  339. global black
  340. set q_count 0
  341. set return ""
  342. set found_nick 0
  343. set found_quote 0
  344. set file [open $black(quote_file) "r"]
  345. while {[gets $file line] != -1} {
  346. set read_chan [lindex [split $line] 0]
  347. set read_who [lindex [split $line] 5]
  348. set read_who [string map { "<" ""
  349. ">" ""
  350. "@" ""
  351. "+" ""
  352. } $read_who]
  353. set enc_chan [encoding convertfrom utf-8 $read_chan]
  354. set read_who [string map [list \[ {\[} \] {\]} \? {\?} \\ {\\}] $read_who]
  355. if {[string equal -nocase $chan $enc_chan] && [string match -nocase $read_who $who]} {
  356. set found_nick 1
  357. set q_count [expr $q_count + 1]
  358. set num [lindex [split $line] 2]
  359. set return "$return $num:$q_count"
  360. }
  361. }
  362. close $file
  363. if {$found_nick == "0"} {
  364. set file [open $black(quote_file) "r"]
  365. while {[gets $file line] != -1} {
  366. set read_chan [lindex [split $line] 0]
  367. set readquote [color:filter [lrange [split $line] 6 end]]
  368. set enc_chan [encoding convertfrom utf-8 $read_chan]
  369. if {[string equal -nocase $chan $enc_chan] && ([lsearch -exact $readquote $who] > -1)} {
  370. set found_quote 1
  371. set q_count [expr $q_count + 1]
  372. set num [lindex [split $line] 2]
  373. set return "$return $num:$q_count"
  374. }
  375. }
  376. close $file
  377. }
  378. return $return
  379. }
  380. proc quote:add {nick host chan who quote} {
  381. global black
  382. set temp_num 0
  383. set num 0
  384. set time [unixtime]
  385. while {$temp_num == 0} {
  386. set get [find:q:num $num $chan]
  387. if {$get == "$num"} {
  388. set num [expr $num + 1]
  389. } else { set temp_num 1 }
  390. }
  391. set file [open $black(quote_file) a]
  392. set enc_chan [encoding convertto utf-8 $chan]
  393. set encoded [encoding convertto utf-8 $quote]
  394. puts $file "$enc_chan QUOTE $num $nick $time $who $encoded"
  395. close $file
  396. return $num
  397. }
  398. ##############
  399. #########################################################################
  400. ## END ##
  401. #########################################################################