BT.AutoUpdate.tcl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. utimer 5 [list blacktools:update_start_download $hand $chan $new_version $last_modify]
  167. unset ::update_version
  168. unset ::update_last_modify
  169. unset ::update_hand
  170. unset ::update_chan
  171. }
  172. ###
  173. proc blacktools:backup_run {} {
  174. global black config
  175. set black(update_old_data) [blacktools:update_data 0 ""]
  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. set userlang [blacktools:update_userlang $hand]
  237. if {![file isdirectory "$black(actdir)/BlackTools"]} {
  238. blacktools:update_put $hand $chan 16 ""
  239. file delete -force $black(backup_dir)
  240. blacktools:update_unsetflag
  241. return
  242. } elseif {![file exists "$black(actdir)/BlackTools.tcl"]} {
  243. blacktools:update_put $hand $chan 16 ""
  244. file delete -force $black(backup_dir)
  245. blacktools:update_unsetflag
  246. return
  247. }
  248. set end_download [unixtime]
  249. set dif [expr $end_download - $black(start_update)]
  250. blacktools:update_put "" "" 17 [list [return_time $userlang $dif]]
  251. set newdata [blacktools:update_data 0 ""]
  252. blacktools:update_put $hand $chan 18 ""
  253. set restore_config [blacktools:update_restore $black(update_old_data) $newdata]
  254. unset black(update_old_data)
  255. set newdata [lindex $restore_config 0]
  256. set num_var [lindex $restore_config 1]
  257. if {$num_var > 0} {
  258. set newdata [blacktools:update_data 1 $newdata]
  259. blacktools:update_put $hand $chan 19 [list $num_var]
  260. } else {
  261. blacktools:update_put "" "" 20 ""
  262. }
  263. blacktools:update_put $hand $chan 21 ""
  264. blacktools:update_restore_files
  265. set ::update_hand $::update_hand
  266. set ::update_chan $::update_chan
  267. every 1000 {
  268. set info_files_num [llength [glob -nocomplain -directory "$black(actdir)/BlackTools/FILES" "*.txt"]]
  269. if {$info_files_num == $black(update_file_saved)} {
  270. blacktools:update_end $info_files_num
  271. break
  272. }
  273. }
  274. }
  275. ###
  276. proc blacktools:update_end {num} {
  277. global black config
  278. set hand $::update_hand
  279. set chan $::update_chan
  280. set userlang [blacktools:update_userlang $hand]
  281. set end_update [unixtime]
  282. set dif [expr $end_update - $black(start_update)]
  283. unset black(start_update)
  284. if {$num == 0} {
  285. blacktools:update_put "" "" 22 ""
  286. } else {
  287. blacktools:update_put $hand $chan 23 [list $num]
  288. }
  289. set file [open $config r]
  290. set data [read -nonewline $file]
  291. close $file
  292. set reg "source $black(actdir)/BlackTools.old.tcl"
  293. regsub $reg $data "source $black(actdir)/BlackTools.tcl" data
  294. set file [open $config w]
  295. puts $file $data
  296. close $file
  297. rehash
  298. if {[info exists ::update_hand]} {
  299. unset ::update_hand
  300. }
  301. if {[info exists ::update_chan]} {
  302. unset ::update_chan
  303. }
  304. blacktools:update_unsetflag
  305. blacktools:update_put $hand $chan 24 [list [return_time $userlang $dif]]
  306. blacktools:update_put $hand $chan 25 [list $black(backup_dir) $black(log_file)]
  307. blacktools:update_put $hand $chan 26 ""
  308. unset black(update_file_saved)
  309. file delete -force "$black(actdir)/BlackTools.old.tcl"
  310. }
  311. ###
  312. proc blacktools:update_restore_files {} {
  313. global black
  314. set files ""
  315. set files [glob -nocomplain -directory "$black(backup_dir)/BlackTools/FILES" "*.txt"]
  316. if {![file isdirectory "$black(actdir)/BlackTools/FILES"]} {
  317. file mkdir "$black(actdir)/BlackTools/FILES"
  318. }
  319. set counter 0
  320. foreach f $files {
  321. incr counter
  322. set filename [file tail $f]
  323. file copy -force $f "$black(actdir)/BlackTools/FILES/$filename"
  324. }
  325. return $counter
  326. }
  327. ###
  328. proc blacktools:update_set_time {num type} {
  329. global black
  330. set bt_file "$black(dirname)/BlackTools.tcl"
  331. set file [open $bt_file r]
  332. set data [read -nonewline $file]
  333. close $file
  334. set regexp_var2 "set black\\(update_time_check\\) \"(.*?)\""
  335. regexp -nocase $regexp_var2 $data -> found
  336. if {$type == 0} {
  337. return $found
  338. } else {
  339. regexp -nocase $regexp_var2 $data found_line
  340. set found_line [string map [list $found $num] $found_line]
  341. regsub $regexp_var2 $data $found_line data
  342. blacktools:update_data 1 $data
  343. }
  344. }
  345. ###
  346. proc blacktools:update_on_off {type} {
  347. global black
  348. set regexp_var2 "set black\\(update_type\\) \"(.*?)\""
  349. set bt_file "$black(dirname)/BlackTools.tcl"
  350. set file [open $bt_file r]
  351. set data [read -nonewline $file]
  352. close $file
  353. regexp -nocase $regexp_var2 $data found_line
  354. regexp -nocase $regexp_var2 $data -> found
  355. if {$type == 0 && $found == 0} {
  356. return 0
  357. }
  358. if {$type == 1 && $found == 1} {
  359. return 1
  360. }
  361. switch $type {
  362. 0 {
  363. set found_line [string map {"1" "0"} $found_line]
  364. regsub $regexp_var2 $data $found_line data
  365. blacktools:update_data 1 $data
  366. return 2
  367. }
  368. 1 {
  369. set found_line [string map {"0" "1"} $found_line]
  370. regsub $regexp_var2 $data $found_line data
  371. blacktools:update_data 1 $data
  372. return 3
  373. }
  374. }
  375. }
  376. ###
  377. proc blacktools:update_restore {data newdata} {
  378. global black
  379. set current_data $newdata
  380. set variables [regexp -all -inline {set black\((.+?)\)} $data]
  381. regsub -all {set black\((.+?)\)} $variables "" variables
  382. set var_nomodif "name author vers site current_modif"
  383. set var_counter 0
  384. foreach var $variables {
  385. if {$var == ""} {continue}
  386. if {[lsearch -nocase $var_nomodif $var] > -1} {continue}
  387. set regexp_var "set black\\($var\\) \\{(.*?)\\}"
  388. set regexp_var2 "set black\\($var\\) \"(.*?)\""
  389. regexp -nocase $regexp_var $data found_old
  390. regexp -nocase $regexp_var2 $data found_old_2
  391. if {[info exists found_old]} {
  392. set found_old [concat $found_old]
  393. regexp -nocase $regexp_var $newdata found_new
  394. if {[info exists found_new]} {
  395. if {[string equal -nocase $found_old $found_new]} {continue}
  396. incr var_counter
  397. regsub $regexp_var $current_data $found_old current_data
  398. unset found_new
  399. }
  400. unset found_old
  401. }
  402. }
  403. foreach var $variables {
  404. if {$var == ""} {continue}
  405. if {[lsearch -nocase $var_nomodif $var] > -1} {continue}
  406. set regexp_var2 "set black\\($var\\) \"(.*?)\""
  407. regexp -nocase $regexp_var2 $data found_old_2
  408. if {[info exists found_old_2]} {
  409. set found_old_2 [concat $found_old_2]
  410. regexp -nocase $regexp_var2 $newdata found_new_2
  411. if {[info exists found_new_2]} {
  412. if {[string equal -nocase $found_old_2 $found_new_2]} {continue}
  413. incr var_counter
  414. regsub $regexp_var2 $current_data $found_old_2 current_data
  415. unset found_new_2
  416. }
  417. unset found_old_2
  418. }
  419. }
  420. return [list $current_data $var_counter]
  421. }
  422. ###
  423. proc blacktools:update_data {type data} {
  424. global black
  425. set bt_file "$black(dirname)/BlackTools.tcl"
  426. if {![file exists $bt_file]} {
  427. return 0
  428. }
  429. if {$type == 0} {
  430. set file [open $bt_file r]
  431. set data [read -nonewline $file]
  432. close $file
  433. return $data
  434. } else {
  435. set file [open $bt_file w]
  436. puts $file $data
  437. close $file
  438. }
  439. }
  440. ###
  441. proc blacktools:update_verify {} {
  442. global black
  443. set link "https://raw.githubusercontent.com/tclscripts/BlackTools-TCL/master/VERSION"
  444. set ipq [http::config -useragent "lynx"]
  445. set error [catch {set ipq [::http::geturl $link -timeout 10000]} eror]
  446. set status [::http::status $ipq]
  447. if {$status != "ok"} {
  448. ::http::cleanup $ipq
  449. return -1
  450. }
  451. set getipq [::http::data $ipq]
  452. ::http::cleanup $ipq
  453. return $getipq
  454. }
  455. ###
  456. proc blacktools:update_userlang {nick} {
  457. global black
  458. if {$nick == ""} {return [string tolower $black(default_lang)]}
  459. set hand [nick2hand $nick]
  460. set userlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  461. if {$userlang == ""} { set userlang "[string tolower $black(default_lang)]" }
  462. return $userlang
  463. }
  464. ###
  465. proc blacktools:update_put {nick chan num var} {
  466. global black
  467. set counter 0
  468. foreach v $var {
  469. incr counter
  470. set replace(%msg.${counter}%) $v
  471. }
  472. if {$nick != "" && $chan != ""} {
  473. set hand [nick2hand $nick]
  474. set getmethod [getuser $hand XTRA OUTPUT_TYPE]
  475. set userlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  476. if {$userlang == ""} { set userlang "[string tolower $black(default_lang)]" }
  477. if {$getmethod == ""} { set getmethod "0" }
  478. set text [black:color:set "" $black(say.${userlang}.autoupdate.${num})]
  479. set reply [join $text]
  480. set reply [string map [array get replace] $reply]
  481. switch $getmethod {
  482. 0 {
  483. putserv "NOTICE $nick :$reply"
  484. }
  485. 1 {
  486. putserv "PRIVMSG $chan :$reply"
  487. }
  488. 2 {
  489. putserv "PRIVMSG $nick :$reply"
  490. }
  491. }
  492. } else {
  493. set lang [string tolower $black(default_lang)]
  494. set text [black:color:set "" $black(say.${lang}.autoupdate.${num})]
  495. set reply [join $text]
  496. set reply [string map [array get replace] $reply]
  497. }
  498. putlog "\[BT\] $reply"
  499. set file [open $black(log_file) a]
  500. puts $file $reply
  501. close $file
  502. }
  503. ###
  504. #https://wiki.tcl-lang.org/page/every
  505. proc every {interval script} {
  506. global everyIds
  507. if {$interval eq {cancel}} {
  508. after cancel $everyIds($script)
  509. return
  510. }
  511. set everyIds($script) [after $interval [namespace code [info level 0]]]
  512. set rc [catch {uplevel #0 $script} result]
  513. if {$rc == [catch break]} {
  514. after cancel $everyIds($script)
  515. set rc 0
  516. } elseif {$rc == [catch continue]} {
  517. # Ignore - just consume the return code
  518. set rc 0
  519. }
  520. # TODO: Need better handling of errorInfo etc...
  521. return -code $rc $result
  522. }