BT.AutoUpdate.tcl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. #########################################################################
  2. ## BlackTools - The Ultimate Channel Control Script ##
  3. ## One TCL. One smart Eggdrop ##
  4. #########################################################################
  5. ######################### AutoUpdate TCL ############################
  6. #########################################################################
  7. ## ##
  8. ## BlackTools : http://$black(tclname)scripts.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. set black(backup_dir) "$black(dirtcl)/BT.backup"
  17. set black(log_file) "$black(dirtcl)/BT.update.log"
  18. set black(actdir) $black(dirtcl)
  19. ###
  20. proc blacktools:check_addons {hand chan} {
  21. global black
  22. if {[catch {package require http} no_http] != 0} {
  23. source $black(dirname)/BlackTools/Addons/http.tcl
  24. package require http
  25. }
  26. if {[catch {package require tls} no_tls] != 0} {
  27. return 0
  28. }
  29. if {[catch {package require json} no_json] != 0} {
  30. source $black(dirname)/BlackTools/Addons/json.tcl
  31. package require json
  32. }
  33. if {[catch {package require github} no_github] != 0} {
  34. source $black(dirname)/BlackTools/Addons/github.tcl
  35. package require github
  36. }
  37. return 1
  38. }
  39. ###
  40. proc blacktools:update_check {nick hand host chan type} {
  41. global black
  42. set check_addons [blacktools:check_addons $hand $chan]
  43. if {$check_addons == 0} {
  44. blacktools:update_put $hand $chan 1 [list "CHECK UPDATE"]
  45. return
  46. }
  47. set status [blacktools:update_verify]
  48. if {$status == -1} {
  49. blacktools:update_put $hand $chan 31 ""
  50. blacktools:tell $nick $host $hand $chan $chan autoupdate.31 ""
  51. return 0
  52. }
  53. set data [split $status "\n"]
  54. set new_version [lindex [lindex $data 0] 1]
  55. set last_modify [lindex [lindex $data 1] 2]
  56. set status [lindex [lindex $data 2] 1]
  57. if {$black(vers) != $new_version} {
  58. if {$type == 0} {
  59. blacktools:tell $nick $host $hand $chan $chan autoupdate.32 "$new_version"
  60. } else {
  61. return [list $new_version $last_modify]
  62. }
  63. } elseif {$last_modify != $black(current_modif)} {
  64. if {$type == 0} {
  65. blacktools:tell $nick $host $hand $chan $chan autoupdate.33 [ctime $last_modify]
  66. }
  67. return [list $new_version $last_modify]
  68. } else {
  69. if {$type == 0} {
  70. blacktools:tell $nick $host $hand $chan $chan autoupdate.5 ""
  71. }
  72. return 0
  73. }
  74. }
  75. ###
  76. proc blacktools:update:timer {} {
  77. global black
  78. if {$black(update_type) == 0} {
  79. if {![info exists black(update_disabled)]} {
  80. if {![info exists black(backup_update)]} {
  81. set update [catch {blacktools:update "" "" "" 1} error]
  82. } else {
  83. blacktools:update_put "" "" 40 ""
  84. }
  85. } else {
  86. blacktools:update_put "" "" 27 [list [ctime [unixtime]]]
  87. blacktools:update_put "" "" 30 [list $black(update_disabled)]
  88. }
  89. } else {
  90. if {$black(update_note) == 1} {
  91. if {![info exists black(update_disabled)]} {
  92. set check [blacktools:update_check "" "" "" "" 1]
  93. if {$check != 0} {
  94. set found_version [lindex $check 0]
  95. set last_modif [lindex $check 1]
  96. blacktools:update:note $last_modif $found_version
  97. }
  98. }
  99. }
  100. }
  101. timer [time_return_minute $black(update_time_check)] blacktools:update:timer
  102. }
  103. ###
  104. proc blacktools:update:note {num version} {
  105. global black botnick
  106. set time [unixtime]
  107. foreach user [userlist n] {
  108. set nonotes [getuser $user XTRA NO_NOTES]
  109. if {$nonotes == ""} {
  110. set check_note [blacktools:update_note_check $user $num]
  111. if {$check_note == 1} {continue}
  112. set getlang [string tolower [getuser $user XTRA OUTPUT_LANG]]
  113. if {$getlang == ""} { set getlang "[string tolower $black(default_lang)]" }
  114. set black(notes:announce:$user) 1
  115. set replace(%msg.1%) $version
  116. set replace(%msg.2%) [ctime $num]
  117. set text [black:color:set $botnick $black(say.$getlang.autoupdate.43)]
  118. set text [string map [array get replace] $text]
  119. notes:add $botnick "" $user "DB" "INBOX" $text "AUTOUPDATE:$num" 0
  120. }
  121. }
  122. }
  123. ###
  124. proc blacktools:update_note_check {hand num} {
  125. global black
  126. set found_it 0
  127. set file [open $black(notes_file) "r"]
  128. while {[gets $file line] != -1} {
  129. set handle [lindex [split $line] 4]
  130. if {[string equal -nocase $handle $hand]} {
  131. set sender [lindex [split $line] 6]
  132. set split_send [split $sender ":"]
  133. set last_update [lindex $split_send 1]
  134. set sender [lindex $split_send 0]
  135. if {[string equal -nocase $sender "AUTOUPDATE"] && [string equal -nocase $num $last_update]} {
  136. set found_it 1
  137. break
  138. }
  139. }
  140. }
  141. close $file
  142. return $found_it
  143. }
  144. ###
  145. proc blacktools:update {hand host chan type} {
  146. global black
  147. if {[info exists black(backup_update)]} {
  148. blacktools:update_put $hand $chan 29 ""
  149. return 0
  150. }
  151. set error_b ""
  152. set file [open $black(log_file) w]
  153. close $file
  154. blacktools:update_put $hand $chan 27 [list [ctime [unixtime]]]
  155. blacktools:update_put $hand $chan 28 ""
  156. set check_addons [blacktools:check_addons $hand $chan]
  157. if {$check_addons == 0} {
  158. blacktools:update_put $hand $chan 1 [list "UPDATE"]
  159. return
  160. }
  161. set status [blacktools:update_verify]
  162. if {$status == -1} {
  163. blacktools:update_put $hand $chan 2 ""
  164. return 0
  165. }
  166. set data [split $status "\n"]
  167. set new_version [lindex [lindex $data 0] 1]
  168. set last_modify [lindex [lindex $data 1] 2]
  169. set status [lindex [lindex $data 2] 1]
  170. if {$status == 1} {set black(finish_action) 1} else {set black(finish_action) 0}
  171. if {$black(vers) != $new_version} {
  172. blacktools:update_put $hand $chan 3 [list $new_version]
  173. } elseif {$last_modify != $black(current_modif)} {
  174. blacktools:update_put $hand $chan 4 [list $black(vers)]
  175. } else {
  176. blacktools:update_put $hand $chan 5 ""
  177. return 0
  178. }
  179. if {$type == 0} {
  180. set black(update_from) 0
  181. } else {
  182. set black(update_from) 1
  183. }
  184. if {![file isdirectory $black(backup_dir)]} {
  185. if {[catch {file mkdir $black(backup_dir)} error] != 0} {
  186. blacktools:update_put $hand $chan 6 [list $error]
  187. return 0
  188. }
  189. }
  190. if {[file exists $black(backup_dir)/$black(tclname)]} {
  191. blacktools:update_put "" "" 7 ""
  192. file delete -force $black(backup_dir)/BlackTools
  193. file delete -force $black(backup_dir)/$black(tclname)
  194. }
  195. if {[catch {file copy -force "$black(dirname)/BlackTools" $black(backup_dir)} error_b] == 0} {
  196. blacktools:update_put "" "" 8 ""
  197. } else {
  198. blacktools:update_put $hand $chan 9 [list $error_b]
  199. file delete -force $black(backup_dir)
  200. return 0
  201. }
  202. if {[catch {file copy -force $black(tclconfig) $black(backup_dir)} error_b] != 0} {
  203. blacktools:update_put $hand $chan 10 ""
  204. file delete -force $black(backup_dir)
  205. return 0
  206. } else {
  207. blacktools:update_put "" "" 11 ""
  208. }
  209. set black(update_hand) $hand
  210. set black(update_version) $new_version
  211. set black(update_last_modify) $last_modify
  212. set black(update_chan) $chan
  213. blacktools:every 1000 {
  214. if {[file isdirectory "$black(backup_dir)/BlackTools"]} {
  215. set after_file_num [blacktools:size "$black(backup_dir)/BlackTools"]
  216. set current_file_num [blacktools:size "$black(actdir)/BlackTools"]
  217. if {$current_file_num == $after_file_num} {
  218. blacktools:update_backup
  219. break
  220. }
  221. }
  222. }
  223. }
  224. ###
  225. proc blacktools:update_backup {} {
  226. global black
  227. set hand $black(update_hand)
  228. set new_version $black(update_version)
  229. set last_modify $black(update_last_modify)
  230. set chan $black(update_chan)
  231. set black(backup_update) 1
  232. set black(start_update) [unixtime]
  233. set black(update_file_saved) [llength [glob -nocomplain -directory "$black(dirname)/BlackTools/FILES" "*.txt"]]
  234. if {[file isdirectory "$black(dirname)/BlackTools/FILES/TOPWORDS"]} {
  235. set black(update_file_topwords) 1
  236. set black(update_file_saved) [expr $black(update_file_saved) + [llength [glob -nocomplain -directory "$black(dirname)/BlackTools/FILES/TOPWORDS" "*.txt"]]]
  237. }
  238. blacktools:update_put "" "" 12 ""
  239. blacktools:update_put "" "" 13 ""
  240. blacktools:update_put "" "" 14 ""
  241. blacktools:update_put $hand $chan 15 [list $new_version [ctime $last_modify]]
  242. blacktools:backup_run $hand $chan $new_version $last_modify
  243. unset black(update_version)
  244. unset black(update_last_modify)
  245. unset black(update_hand)
  246. unset black(update_chan)
  247. }
  248. ###
  249. proc blacktools:backup_run {hand chan new_version last_modify} {
  250. global black config
  251. set black(update_old_data) [blacktools:update_data 0 ""]
  252. set bt_file "$black(dirname)/$black(tclname)"
  253. set file [open $bt_file r]
  254. set data [read -nonewline $file]
  255. close $file
  256. set regexp_var2 "set black\\(dirname\\) \"(.*?)\""
  257. regexp -nocase $regexp_var2 $data found_line
  258. regexp -nocase $regexp_var2 $data -> found
  259. set found_line [string map [list $black(dirname) $black(backup_dir)] $found_line]
  260. regsub $regexp_var2 $data $found_line data
  261. blacktools:update_data 1 $data
  262. set file [open $config r]
  263. set data [read -nonewline $file]
  264. close $file
  265. set reg "source $black(dirname)/$black(tclname)"
  266. regsub $reg $data "source $black(dirname)/BlackTools.old.tcl" data
  267. set file [open $config w]
  268. puts $file $data
  269. close $file
  270. file rename -force "$black(dirname)/$black(tclname)" "$black(dirname)/BlackTools.old.tcl"
  271. utimer 5 [list blacktools:update_start_download $hand $chan $new_version $last_modify]
  272. }
  273. ###
  274. proc blacktools:size {dir} {
  275. set files [glob-r $dir]
  276. set total 0
  277. foreach f $files {
  278. set size [file size $f]
  279. set total [expr $size + $total]
  280. }
  281. return $total
  282. }
  283. ###
  284. proc blacktools:update_start_download {hand chan new_version last_modify} {
  285. global black
  286. rehash
  287. set black(update_hand) $hand
  288. set black(update_chan) $chan
  289. file delete -force "$black(actdir)/BlackTools"
  290. ::github::github update tclscripts BlackTools-TCL $black(actdir)
  291. blacktools:every 1000 {
  292. if {[file isdirectory $black(actdir)/BlackTools]} {
  293. set size [llength [glob-r "$black(actdir)/BlackTools"]]
  294. if {$size == $black(current_size)} {
  295. blacktools:update_start_restore
  296. break
  297. }
  298. }
  299. }
  300. }
  301. ###
  302. #https://wiki.tcl-lang.org/page/glob
  303. proc glob-r {{dir .}} {
  304. set res {}
  305. foreach i [lsort [glob -nocomplain -dir $dir *]] {
  306. if {[file type $i] eq {directory}} {
  307. eval lappend res [glob-r $i]
  308. } else {
  309. lappend res $i
  310. }
  311. }
  312. set res
  313. } ;# RS
  314. ###
  315. proc blacktools:update_unsetflag {} {
  316. global black
  317. if {[info exists black(backup_update)]} {
  318. unset black(backup_update)
  319. }
  320. }
  321. ###
  322. proc blacktools:update_start_restore {} {
  323. global black
  324. set hand $black(update_hand)
  325. set chan $black(update_chan)
  326. set userlang [blacktools:update_userlang $hand]
  327. if {![file isdirectory "$black(actdir)/BlackTools"]} {
  328. blacktools:update_put $hand $chan 16 ""
  329. file delete -force $black(backup_dir)
  330. blacktools:update_unsetflag
  331. return
  332. } elseif {![file exists "$black(actdir)/$black(tclname)"]} {
  333. blacktools:update_put $hand $chan 16 ""
  334. file delete -force $black(backup_dir)
  335. blacktools:update_unsetflag
  336. return
  337. }
  338. set end_download [unixtime]
  339. set dif [expr $end_download - $black(start_update)]
  340. blacktools:update_put "" "" 17 [list [return_time $userlang $dif]]
  341. set newdata [blacktools:update_data 0 ""]
  342. blacktools:update_put $hand $chan 18 ""
  343. set restore_config [blacktools:update_restore $black(update_old_data) $newdata]
  344. unset black(update_old_data)
  345. set newdata [lindex $restore_config 0]
  346. set num_var [lindex $restore_config 1]
  347. if {$num_var > 0} {
  348. set newdata [blacktools:update_data 1 $newdata]
  349. blacktools:update_put $hand $chan 19 [list $num_var]
  350. } else {
  351. blacktools:update_put "" "" 20 ""
  352. }
  353. blacktools:update_put $hand $chan 21 ""
  354. blacktools:update_restore_files
  355. blacktools:every 1000 {
  356. set info_files_num [llength [glob -nocomplain -directory "$black(actdir)/BlackTools/FILES" "*.txt"]]
  357. if {[file isdirectory "$black(actdir)/BlackTools/FILES/TOPWORDS"]} {
  358. set info_files_topwords [llength [glob -nocomplain -directory "$black(actdir)/BlackTools/FILES/TOPWORDS" "*.txt"]]
  359. set info_files_num [expr $info_files_num + $info_files_topwords]
  360. }
  361. if {$info_files_num == $black(update_file_saved)} {
  362. blacktools:update_end $info_files_num
  363. break
  364. }
  365. }
  366. }
  367. ###
  368. proc blacktools:update_end {num} {
  369. global black config
  370. set hand $black(update_hand)
  371. set chan $black(update_chan)
  372. set userlang [blacktools:update_userlang $hand]
  373. set end_update [unixtime]
  374. set dif [expr $end_update - $black(start_update)]
  375. unset black(start_update)
  376. if {$num == 0} {
  377. blacktools:update_put "" "" 22 ""
  378. } else {
  379. blacktools:update_put $hand $chan 23 [list $num]
  380. }
  381. set file [open $config r]
  382. set data [read -nonewline $file]
  383. close $file
  384. set reg "source $black(actdir)/BlackTools.old.tcl"
  385. regsub $reg $data "source $black(actdir)/$black(tclname)" data
  386. set file [open $config w]
  387. puts $file $data
  388. close $file
  389. unset black(update_hand)
  390. unset black(update_chan)
  391. blacktools:update_unsetflag
  392. blacktools:update_put $hand $chan 24 [list [return_time $userlang $dif]]
  393. blacktools:update_put $hand $chan 25 [list $black(backup_dir) $black(log_file)]
  394. blacktools:update_put $hand $chan 26 ""
  395. unset black(update_file_saved)
  396. file delete -force "$black(actdir)/BlackTools.old.tcl"
  397. if {$black(finish_action) == 0} {
  398. rehash
  399. setaway "none"
  400. } else {
  401. if {$black(update_from) == 0} {
  402. rehash
  403. setaway "none"
  404. blacktools:update_put $hand $chan 45 ""
  405. } else {
  406. blacktools:update_put $hand $chan 46 ""
  407. utimer 10 [list restart]
  408. }
  409. }
  410. unset black(finish_action)
  411. unset black(update_type)
  412. }
  413. ###
  414. proc blacktools:update_restore_files {} {
  415. global black
  416. set files ""
  417. set counter 0
  418. set files [glob -nocomplain -directory "$black(backup_dir)/BlackTools/FILES" "*.txt"]
  419. if {![file isdirectory "$black(actdir)/BlackTools/FILES"]} {
  420. file mkdir "$black(actdir)/BlackTools/FILES"
  421. }
  422. foreach f $files {
  423. incr counter
  424. set filename [file tail $f]
  425. file copy -force $f "$black(actdir)/BlackTools/FILES/$filename"
  426. }
  427. if {[info exists black(update_file_topwords)]} {
  428. if {![file isdirectory "$black(actdir)/BlackTools/FILES/TOPWORDS"]} {
  429. file mkdir "$black(actdir)/BlackTools/FILES/TOPWORDS"
  430. }
  431. set topwords_files [glob -nocomplain -directory "$black(backup_dir)/BlackTools/FILES/TOPWORDS" "*.txt"]
  432. foreach f $topwords_files {
  433. incr counter
  434. set filename [file tail $f]
  435. file copy -force $f "$black(actdir)/BlackTools/FILES/TOPWORDS/$filename"
  436. }
  437. unset black(update_file_topwords)
  438. }
  439. return $counter
  440. }
  441. ###
  442. proc blacktools:update_set_time {num type} {
  443. global black
  444. set bt_file "$black(dirname)/$black(tclname)"
  445. set file [open $bt_file r]
  446. set data [read -nonewline $file]
  447. close $file
  448. set regexp_var2 "set black\\(update_time_check\\) \"(.*?)\""
  449. regexp -nocase $regexp_var2 $data -> found
  450. if {$type == 0} {
  451. return $found
  452. } else {
  453. regexp -nocase $regexp_var2 $data found_line
  454. set found_line [string map [list $found $num] $found_line]
  455. regsub $regexp_var2 $data $found_line data
  456. blacktools:update_data 1 $data
  457. }
  458. }
  459. ###
  460. proc blacktools:update_on_off {type} {
  461. global black
  462. set regexp_var2 "set black\\(update_type\\) \"(.*?)\""
  463. set bt_file "$black(dirname)/$black(tclname)"
  464. set file [open $bt_file r]
  465. set data [read -nonewline $file]
  466. close $file
  467. regexp -nocase $regexp_var2 $data found_line
  468. regexp -nocase $regexp_var2 $data -> found
  469. if {$type == 0 && $found == 0} {
  470. return 0
  471. }
  472. if {$type == 1 && $found == 1} {
  473. return 1
  474. }
  475. switch $type {
  476. 0 {
  477. set found_line [string map {"1" "0"} $found_line]
  478. regsub $regexp_var2 $data $found_line data
  479. blacktools:update_data 1 $data
  480. return 2
  481. }
  482. 1 {
  483. set found_line [string map {"0" "1"} $found_line]
  484. regsub $regexp_var2 $data $found_line data
  485. blacktools:update_data 1 $data
  486. return 3
  487. }
  488. }
  489. }
  490. ###
  491. proc blacktools:update_restore {data newdata} {
  492. global black
  493. set current_data $newdata
  494. set variables [regexp -all -inline {set black\((.+?)\)} $data]
  495. regsub -all {set black\((.+?)\)} $variables "" variables
  496. set var_nomodif "name author vers site"
  497. set var_counter 0
  498. foreach var $variables {
  499. if {$var == ""} {continue}
  500. if {[lsearch -nocase $var_nomodif $var] > -1} {continue}
  501. set regexp_var "set black\\($var\\) \\{(.*?)\\}"
  502. set regexp_var2 "set black\\($var\\) \"(.*?)\""
  503. regexp -nocase $regexp_var $data found_old
  504. regexp -nocase $regexp_var2 $data found_old_2
  505. if {[info exists found_old]} {
  506. set found_old [concat $found_old]
  507. regexp -nocase $regexp_var $newdata found_new
  508. if {[info exists found_new]} {
  509. if {[string equal -nocase $found_old $found_new]} {continue}
  510. incr var_counter
  511. regsub $regexp_var $current_data $found_old current_data
  512. unset found_new
  513. }
  514. unset found_old
  515. }
  516. }
  517. foreach var $variables {
  518. if {$var == ""} {continue}
  519. if {[lsearch -nocase $var_nomodif $var] > -1} {continue}
  520. set regexp_var2 "set black\\($var\\) \"(.*?)\""
  521. regexp -nocase $regexp_var2 $data found_old_2
  522. if {[info exists found_old_2]} {
  523. set found_old_2 [concat $found_old_2]
  524. regexp -nocase $regexp_var2 $newdata found_new_2
  525. if {[info exists found_new_2]} {
  526. if {[string equal -nocase $found_old_2 $found_new_2]} {continue}
  527. if {[string equal -nocase "default_away" $var] && [string match -nocase "*www.TCLScripts.Net*" $found_old_2]} {continue}
  528. incr var_counter
  529. regsub $regexp_var2 $current_data $found_old_2 current_data
  530. unset found_new_2
  531. }
  532. unset found_old_2
  533. }
  534. }
  535. return [list $current_data $var_counter]
  536. }
  537. ###
  538. proc blacktools:update_data {type data} {
  539. global black
  540. set bt_file "$black(dirname)/$black(tclname)"
  541. if {![file exists $bt_file]} {
  542. return 0
  543. }
  544. if {$type == 0} {
  545. set file [open $bt_file r]
  546. set data [read -nonewline $file]
  547. close $file
  548. return $data
  549. } else {
  550. set file [open $bt_file w]
  551. puts $file $data
  552. close $file
  553. }
  554. }
  555. ###
  556. proc blacktools:update_verify {} {
  557. global black
  558. set link "https://raw.githubusercontent.com/tclscripts/BlackTools-TCL/master/VERSION"
  559. http::register https 443 [list ::tls::socket -tls1 true]
  560. set ipq [http::config -useragent "lynx"]
  561. set error [catch {set ipq [::http::geturl $link -timeout 10000]} eror]
  562. set status [::http::status $ipq]
  563. if {$status != "ok"} {
  564. ::http::cleanup $ipq
  565. return -1
  566. }
  567. set getipq [::http::data $ipq]
  568. ::http::cleanup $ipq
  569. return $getipq
  570. }
  571. ###
  572. proc blacktools:update_userlang {nick} {
  573. global black
  574. if {$nick == ""} {return [string tolower $black(default_lang)]}
  575. set hand [nick2hand $nick]
  576. set userlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  577. if {$userlang == ""} { set userlang "[string tolower $black(default_lang)]" }
  578. return $userlang
  579. }
  580. ###
  581. proc blacktools:update_put {nick chan num var} {
  582. global black
  583. set counter 0
  584. foreach v $var {
  585. incr counter
  586. set replace(%msg.${counter}%) $v
  587. }
  588. if {$nick != "" && $chan != ""} {
  589. set hand [nick2hand $nick]
  590. set getmethod [getuser $hand XTRA OUTPUT_TYPE]
  591. set userlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  592. if {$userlang == ""} { set userlang "[string tolower $black(default_lang)]" }
  593. if {$getmethod == ""} { set getmethod "0" }
  594. set text [black:color:set "" $black(say.${userlang}.autoupdate.${num})]
  595. set reply [join $text]
  596. set reply [string map [array get replace] $reply]
  597. switch $getmethod {
  598. 0 {
  599. putserv "NOTICE $nick :$reply"
  600. }
  601. 1 {
  602. putserv "PRIVMSG $chan :$reply"
  603. }
  604. 2 {
  605. putserv "PRIVMSG $nick :$reply"
  606. }
  607. }
  608. } else {
  609. set lang [string tolower $black(default_lang)]
  610. set text [black:color:set "" $black(say.${lang}.autoupdate.${num})]
  611. set reply [join $text]
  612. set reply [string map [array get replace] $reply]
  613. }
  614. putlog "\[BT\] $reply"
  615. set file [open $black(log_file) a]
  616. puts $file $reply
  617. close $file
  618. }
  619. ###
  620. #https://wiki.tcl-lang.org/page/every
  621. proc blacktools:every {interval script} {
  622. global everyIds
  623. if {$interval eq {cancel}} {
  624. after cancel $everyIds($script)
  625. return
  626. }
  627. set everyIds($script) [after $interval [namespace code [info level 0]]]
  628. set rc [catch {uplevel #0 $script} result]
  629. if {$rc == [catch break]} {
  630. after cancel $everyIds($script)
  631. set rc 0
  632. } elseif {$rc == [catch continue]} {
  633. # Ignore - just consume the return code
  634. set rc 0
  635. }
  636. # TODO: Need better handling of errorInfo etc...
  637. return -code $rc $result
  638. }