BT.AutoUpdate.tcl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. package require tls
  17. set black(backup_dir) "$black(dirname)/BT.backup"
  18. set black(log_file) "$black(dirname)/BT.update.log"
  19. set black(actdir) "scripts"
  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 json} no_json] != 0} {
  28. source $black(dirname)/BlackTools/Addons/json.tcl
  29. package require json
  30. }
  31. if {[catch {package require github} no_github] != 0} {
  32. source $black(dirname)/BlackTools/Addons/github.tcl
  33. package require github
  34. }
  35. return 1
  36. }
  37. ###
  38. proc blacktools:update_check {nick hand host chan} {
  39. global black
  40. set check_addons [blacktools:check_addons $hand $chan]
  41. set status [blacktools:update_verify]
  42. if {$status == -1} {
  43. blacktools:update_put $hand $chan 31 ""
  44. blacktools:tell $nick $host $hand $chan $chan autoupdate.31 ""
  45. return
  46. }
  47. set data [split $status "\n"]
  48. set new_version [lindex [lindex $data 0] 1]
  49. set last_modify [lindex [lindex $data 1] 2]
  50. set status [lindex [lindex $data 2] 1]
  51. if {$black(vers) != $new_version} {
  52. blacktools:tell $nick $host $hand $chan $chan autoupdate.32 "$new_version"
  53. } elseif {$last_modify != $black(current_modif)} {
  54. blacktools:tell $nick $host $hand $chan $chan autoupdate.33 [ctime $last_modify]
  55. } else {
  56. blacktools:tell $nick $host $hand $chan $chan autoupdate.5 ""
  57. return
  58. }
  59. }
  60. ###
  61. proc blacktools:update:timer {} {
  62. global black
  63. if {$black(update_type) == 1} {
  64. if {![info exists black(update_disabled)]} {
  65. if {![info exists black(backup_update)]} {
  66. set update [catch {blacktools:update "" "" ""} error]
  67. } else {
  68. blacktools:update_put "" "" 40 ""
  69. }
  70. } else {
  71. blacktools:update_put "" "" 27 [list [ctime [unixtime]]]
  72. blacktools:update_put "" "" 30 [list $black(update_disabled)]
  73. }
  74. }
  75. timer [time_return_minute $black(update_time_check)] blacktools:update:timer
  76. }
  77. ###
  78. proc blacktools:update {hand host chan} {
  79. global black
  80. if {[info exists black(backup_update)]} {
  81. blacktools:update_put $hand $chan 29 ""
  82. return
  83. }
  84. set error_b ""
  85. set file [open $black(log_file) w]
  86. close $file
  87. blacktools:update_put $hand $chan 27 [list [ctime [unixtime]]]
  88. blacktools:update_put $hand $chan 28 ""
  89. set check_addons [blacktools:check_addons $hand $chan]
  90. if {$check_addons == 0} {
  91. blacktools:update_put $hand $chan 1 ""
  92. return
  93. }
  94. set status [blacktools:update_verify]
  95. if {$status == -1} {
  96. blacktools:update_put $hand $chan 2 ""
  97. return
  98. }
  99. set data [split $status "\n"]
  100. set new_version [lindex [lindex $data 0] 1]
  101. set last_modify [lindex [lindex $data 1] 2]
  102. set status [lindex [lindex $data 2] 1]
  103. if {$black(vers) != $new_version} {
  104. blacktools:update_put $hand $chan 3 [list $new_version]
  105. } elseif {$last_modify != $black(current_modif)} {
  106. blacktools:update_put $hand $chan 4 [list $black(vers)]
  107. } else {
  108. blacktools:update_put $hand $chan 5 ""
  109. return
  110. }
  111. if {![file isdirectory $black(backup_dir)]} {
  112. if {[catch {file mkdir $black(backup_dir)} error] != 0} {
  113. blacktools:update_put $hand $chan 6 [list $error]
  114. return
  115. }
  116. }
  117. if {[file exists $black(backup_dir)/BlackTools.tcl]} {
  118. blacktools:update_put "" "" 7 ""
  119. file delete -force $black(backup_dir)/BlackTools
  120. file delete -force $black(backup_dir)/BlackTools.tcl
  121. }
  122. if {[catch {file copy -force "$black(dirname)/BlackTools" $black(backup_dir)} error_b] == 0} {
  123. blacktools:update_put "" "" 8 ""
  124. } else {
  125. blacktools:update_put $hand $chan 9 [list $error_b]
  126. file delete -force $black(backup_dir)
  127. return
  128. }
  129. if {[catch {file copy -force $black(tclconfig) $black(backup_dir)} error_b] != 0} {
  130. blacktools:update_put $hand $chan 10 ""
  131. file delete -force $black(backup_dir)
  132. return
  133. } else {
  134. blacktools:update_put "" "" 11 ""
  135. }
  136. set ::update_hand $hand
  137. set ::update_version $new_version
  138. set ::update_last_modify $last_modify
  139. set ::update_chan $chan
  140. every 1000 {
  141. if {[file isdirectory "$black(backup_dir)/BlackTools"]} {
  142. set after_file_num [llength [glob-r "$black(backup_dir)/BlackTools"]]
  143. set current_file_num [llength [glob-r "$black(actdir)/BlackTools"]]
  144. if {$current_file_num == $after_file_num} {
  145. blacktools:update_backup
  146. break
  147. }
  148. }
  149. }
  150. }
  151. ###
  152. proc blacktools:update_backup {} {
  153. global black
  154. set hand $::update_hand
  155. set new_version $::update_version
  156. set last_modify $::update_last_modify
  157. set chan $::update_chan
  158. set black(backup_update) 1
  159. set black(start_update) [unixtime]
  160. set black(update_file_saved) [llength [glob -nocomplain -directory "$black(dirname)/BlackTools/FILES" "*.txt"]]
  161. blacktools:update_put "" "" 12 ""
  162. blacktools:update_put "" "" 13 ""
  163. blacktools:update_put "" "" 14 ""
  164. blacktools:update_put $hand $chan 15 [list $new_version [ctime $last_modify]]
  165. blacktools:backup_run
  166. set black(update_old_data) [blacktools:update_data 0 ""]
  167. utimer 5 [list blacktools:update_start_download $hand $chan $new_version $last_modify]
  168. unset ::update_version
  169. unset ::update_last_modify
  170. unset ::update_hand
  171. unset ::update_chan
  172. }
  173. ###
  174. proc blacktools:backup_run {} {
  175. global black config
  176. set bt_file "$black(dirname)/BlackTools.tcl"
  177. set file [open $bt_file r]
  178. set data [read -nonewline $file]
  179. close $file
  180. set regexp_var2 "set black\\(dirname\\) \"(.*?)\""
  181. regexp -nocase $regexp_var2 $data found_line
  182. regexp -nocase $regexp_var2 $data -> found
  183. set found_line [string map [list $black(dirname) $black(backup_dir)] $found_line]
  184. regsub $regexp_var2 $data $found_line data
  185. blacktools:update_data 1 $data
  186. set file [open $config r]
  187. set data [read -nonewline $file]
  188. close $file
  189. set reg "source $black(dirname)/BlackTools.tcl"
  190. regsub $reg $data "source $black(dirname)/BlackTools.old.tcl" data
  191. set file [open $config w]
  192. puts $file $data
  193. close $file
  194. file rename -force "$black(dirname)/BlackTools.tcl" "$black(dirname)/BlackTools.old.tcl"
  195. }
  196. ###
  197. proc blacktools:update_start_download {hand chan new_version last_modify} {
  198. global black
  199. rehash
  200. set ::update_hand $hand
  201. set ::update_chan $chan
  202. file delete -force "$black(actdir)/BlackTools"
  203. ::github::github update tclscripts BlackTools-TCL $black(actdir)
  204. every 1000 {
  205. if {[file isdirectory $black(actdir)/BlackTools]} {
  206. blacktools:update_start_restore
  207. break
  208. }
  209. }
  210. }
  211. ###
  212. #https://wiki.tcl-lang.org/page/glob
  213. proc glob-r {{dir .}} {
  214. set res {}
  215. foreach i [lsort [glob -nocomplain -dir $dir *]] {
  216. if {[file type $i] eq {directory}} {
  217. eval lappend res [glob-r $i]
  218. } else {
  219. lappend res $i
  220. }
  221. }
  222. set res
  223. } ;# RS
  224. ###
  225. proc blacktools:update_unsetflag {} {
  226. global black
  227. if {[info exists black(backup_update)]} {
  228. unset black(backup_update)
  229. }
  230. }
  231. ###
  232. proc blacktools:update_start_restore {} {
  233. global black
  234. set hand $::update_hand
  235. set chan $::update_chan
  236. if {![file isdirectory "$black(actdir)/BlackTools"]} {
  237. blacktools:update_put $hand $chan 16 ""
  238. file delete -force $black(backup_dir)
  239. blacktools:update_unsetflag
  240. return
  241. } elseif {![file exists "$black(actdir)/BlackTools.tcl"]} {
  242. blacktools:update_put $hand $chan 16 ""
  243. file delete -force $black(backup_dir)
  244. blacktools:update_unsetflag
  245. return
  246. }
  247. set end_download [unixtime]
  248. set dif [expr $end_download - $black(start_update)]
  249. blacktools:update_put $hand $chan 17 [list [duration $dif]]
  250. set newdata [blacktools:update_data 0 ""]
  251. blacktools:update_put $hand $chan 18 ""
  252. set restore_config [blacktools:update_restore $black(update_old_data) $newdata]
  253. unset black(update_old_data)
  254. set newdata [lindex $restore_config 0]
  255. set num_var [lindex $restore_config 1]
  256. if {$num_var > 0} {
  257. set newdata [blacktools:update_data 1 $newdata]
  258. blacktools:update_put $hand $chan 19 [list $num_var]
  259. } else {
  260. blacktools:update_put "" "" 20 ""
  261. }
  262. blacktools:update_put $hand $chan 21 ""
  263. blacktools:update_restore_files
  264. set ::update_hand $::update_hand
  265. set ::update_chan $::update_chan
  266. every 1000 {
  267. set info_files_num [llength [glob -nocomplain -directory "$black(actdir)/BlackTools/FILES" "*.txt"]]
  268. if {$info_files_num == $black(update_file_saved)} {
  269. blacktools:update_end $info_files_num
  270. break
  271. }
  272. }
  273. }
  274. ###
  275. proc blacktools:update_end {num} {
  276. global black config
  277. set hand $::update_hand
  278. set chan $::update_chan
  279. set end_update [unixtime]
  280. set dif [expr $end_update - $black(start_update)]
  281. unset black(start_update)
  282. if {$num == 0} {
  283. blacktools:update_put "" "" 22 ""
  284. } else {
  285. blacktools:update_put $hand $chan 23 [list $num]
  286. }
  287. set file [open $config r]
  288. set data [read -nonewline $file]
  289. close $file
  290. set reg "source $black(actdir)/BlackTools.old.tcl"
  291. regsub $reg $data "source $black(actdir)/BlackTools.tcl" data
  292. set file [open $config w]
  293. puts $file $data
  294. close $file
  295. rehash
  296. if {[info exists ::update_hand]} {
  297. unset ::update_hand
  298. }
  299. if {[info exists ::update_chan]} {
  300. unset ::update_chan
  301. }
  302. blacktools:update_unsetflag
  303. blacktools:update_put $hand $chan 24 [list [duration $dif]]
  304. blacktools:update_put $hand $chan 25 [list $black(backup_dir) $black(log_file)]
  305. blacktools:update_put $hand $chan 26 ""
  306. unset black(update_file_saved)
  307. file delete -force "$black(actdir)/BlackTools.old.tcl"
  308. }
  309. ###
  310. proc blacktools:update_restore_files {} {
  311. global black
  312. set files ""
  313. set files [glob -nocomplain -directory "$black(backup_dir)/BlackTools/FILES" "*.txt"]
  314. if {![file isdirectory "$black(actdir)/BlackTools/FILES"]} {
  315. file mkdir "$black(actdir)/BlackTools/FILES"
  316. }
  317. set counter 0
  318. foreach f $files {
  319. incr counter
  320. set filename [file tail $f]
  321. file copy -force $f "$black(actdir)/BlackTools/FILES/$filename"
  322. }
  323. return $counter
  324. }
  325. ###
  326. proc blacktools:update_set_time {num type} {
  327. global black
  328. set bt_file "$black(dirname)/BlackTools.tcl"
  329. set file [open $bt_file r]
  330. set data [read -nonewline $file]
  331. close $file
  332. set regexp_var2 "set black\\(update_time_check\\) \"(.*?)\""
  333. regexp -nocase $regexp_var2 $data -> found
  334. if {$type == 0} {
  335. return $found
  336. } else {
  337. regexp -nocase $regexp_var2 $data found_line
  338. set found_line [string map [list $found $num] $found_line]
  339. regsub $regexp_var2 $data $found_line data
  340. blacktools:update_data 1 $data
  341. }
  342. }
  343. ###
  344. proc blacktools:update_on_off {type} {
  345. global black
  346. set regexp_var2 "set black\\(update_type\\) \"(.*?)\""
  347. set bt_file "$black(dirname)/BlackTools.tcl"
  348. set file [open $bt_file r]
  349. set data [read -nonewline $file]
  350. close $file
  351. regexp -nocase $regexp_var2 $data found_line
  352. regexp -nocase $regexp_var2 $data -> found
  353. if {$type == 0 && $found == 0} {
  354. return 0
  355. }
  356. if {$type == 1 && $found == 1} {
  357. return 1
  358. }
  359. switch $type {
  360. 0 {
  361. set found_line [string map {"1" "0"} $found_line]
  362. regsub $regexp_var2 $data $found_line data
  363. blacktools:update_data 1 $data
  364. return 2
  365. }
  366. 1 {
  367. set found_line [string map {"0" "1"} $found_line]
  368. regsub $regexp_var2 $data $found_line data
  369. blacktools:update_data 1 $data
  370. return 3
  371. }
  372. }
  373. }
  374. ###
  375. proc blacktools:update_restore {data newdata} {
  376. global black
  377. set current_data $newdata
  378. set variables [regexp -all -inline {set black\((.+?)\)} $data]
  379. regsub -all {set black\((.+?)\)} $variables "" variables
  380. set var_nomodif "name author vers site last_modify"
  381. set var_counter 0
  382. foreach var $variables {
  383. if {$var == ""} {continue}
  384. if {[lsearch -nocase $var_nomodif $var] > -1} {continue}
  385. set regexp_var "set black\\($var\\) \\{(.*?)\\}"
  386. set regexp_var2 "set black\\($var\\) \"(.*?)\""
  387. regexp -nocase $regexp_var $data found_old
  388. regexp -nocase $regexp_var2 $data found_old_2
  389. if {[info exists found_old]} {
  390. set found_old [concat $found_old]
  391. regexp -nocase $regexp_var $newdata found_new
  392. if {[info exists found_new]} {
  393. if {[string equal -nocase $found_old $found_new]} {continue}
  394. incr var_counter
  395. regsub $regexp_var $current_data $found_old current_data
  396. unset found_new
  397. }
  398. unset found_old
  399. }
  400. }
  401. foreach var $variables {
  402. if {$var == ""} {continue}
  403. if {[lsearch -nocase $var_nomodif $var] > -1} {continue}
  404. set regexp_var2 "set black\\($var\\) \"(.*?)\""
  405. regexp -nocase $regexp_var2 $data found_old_2
  406. if {[info exists found_old_2]} {
  407. set found_old_2 [concat $found_old_2]
  408. regexp -nocase $regexp_var2 $newdata found_new_2
  409. if {[info exists found_new_2]} {
  410. if {[string equal -nocase $found_old_2 $found_new_2]} {continue}
  411. incr var_counter
  412. regsub $regexp_var2 $current_data $found_old_2 current_data
  413. unset found_new_2
  414. }
  415. unset found_old_2
  416. }
  417. }
  418. return [list $current_data $var_counter]
  419. }
  420. ###
  421. proc blacktools:update_data {type data} {
  422. global black
  423. set bt_file "$black(dirname)/BlackTools.tcl"
  424. if {![file exists $bt_file]} {
  425. return 0
  426. }
  427. if {$type == 0} {
  428. set file [open $bt_file r]
  429. set data [read -nonewline $file]
  430. close $file
  431. return $data
  432. } else {
  433. set file [open $bt_file w]
  434. puts $file $data
  435. close $file
  436. }
  437. }
  438. ###
  439. proc blacktools:update_verify {} {
  440. global black
  441. set link "https://raw.githubusercontent.com/tclscripts/BlackTools-TCL/master/VERSION"
  442. set ipq [http::config -useragent "lynx"]
  443. set error [catch {set ipq [::http::geturl $link -timeout 10000]} eror]
  444. set status [::http::status $ipq]
  445. if {$status != "ok"} {
  446. ::http::cleanup $ipq
  447. return -1
  448. }
  449. set getipq [::http::data $ipq]
  450. ::http::cleanup $ipq
  451. return $getipq
  452. }
  453. ###
  454. proc blacktools:update_put {nick chan num var} {
  455. global black
  456. set counter 0
  457. foreach v $var {
  458. incr counter
  459. set replace(%msg.${counter}%) $v
  460. }
  461. if {$nick != "" && $chan != ""} {
  462. set hand [nick2hand $nick]
  463. set getmethod [getuser $hand XTRA OUTPUT_TYPE]
  464. set userlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  465. if {$userlang == ""} { set userlang "[string tolower $black(default_lang)]" }
  466. if {$getmethod == ""} { set getmethod "0" }
  467. set text [black:color:set "" $black(say.${userlang}.autoupdate.${num})]
  468. set reply [join $text]
  469. set reply [string map [array get replace] $reply]
  470. switch $getmethod {
  471. 0 {
  472. putserv "NOTICE $nick :$reply"
  473. }
  474. 1 {
  475. putserv "PRIVMSG $chan :$reply"
  476. }
  477. 2 {
  478. putserv "PRIVMSG $nick :$reply"
  479. }
  480. }
  481. } else {
  482. set lang [string tolower $black(default_lang)]
  483. set text [black:color:set "" $black(say.${lang}.autoupdate.${num})]
  484. set reply [join $text]
  485. set reply [string map [array get replace] $reply]
  486. }
  487. putlog "\[BT\] $reply"
  488. set file [open $black(log_file) a]
  489. puts $file $reply
  490. close $file
  491. }
  492. ###
  493. #https://wiki.tcl-lang.org/page/every
  494. proc every {interval script} {
  495. global everyIds
  496. if {$interval eq {cancel}} {
  497. after cancel $everyIds($script)
  498. return
  499. }
  500. set everyIds($script) [after $interval [namespace code [info level 0]]]
  501. set rc [catch {uplevel #0 $script} result]
  502. if {$rc == [catch break]} {
  503. after cancel $everyIds($script)
  504. set rc 0
  505. } elseif {$rc == [catch continue]} {
  506. # Ignore - just consume the return code
  507. set rc 0
  508. }
  509. # TODO: Need better handling of errorInfo etc...
  510. return -code $rc $result
  511. }