BT.AutoUpdate.tcl 19 KB

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