BT.AutoUpdate.tcl 21 KB

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