BT.AutoUpdate.tcl 24 KB

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