BT.AutoUpdate.tcl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  317. set black(old_config_file) "BlackTools.${timestamp}.tcl"
  318. regsub $reg $data "source $black(dirname)/$black(old_config_file)" data
  319. set file [open $config w]
  320. puts $file $data
  321. close $file
  322. file rename -force "$black(dirname)/$black(tclname)" "$black(dirname)/$black(old_config_file)"
  323. utimer 5 [list blacktools:update_start_download $hand $chan $new_version $last_modify]
  324. }
  325. ###
  326. proc blacktools:size {dir} {
  327. set files [glob-r $dir]
  328. set total 0
  329. foreach f $files {
  330. set size [file size $f]
  331. set total [expr $size + $total]
  332. }
  333. return $total
  334. }
  335. ###
  336. proc blacktools:update_start_download {hand chan new_version last_modify} {
  337. global black
  338. rehash
  339. set black(update_hand) $hand
  340. set black(update_chan) $chan
  341. file delete -force "$black(actdir)/BlackTools"
  342. ::github::github update tclscripts BlackTools-TCL $black(actdir)
  343. blacktools:every 1000 {
  344. if {[file isdirectory $black(actdir)/BlackTools]} {
  345. set size [llength [glob-r "$black(actdir)/BlackTools"]]
  346. if {$size == $black(download_size)} {
  347. blacktools:update_start_restore
  348. break
  349. }
  350. }
  351. }
  352. }
  353. ###
  354. #https://wiki.tcl-lang.org/page/glob
  355. proc glob-r {{dir .}} {
  356. set res {}
  357. foreach i [lsort [glob -nocomplain -dir $dir *]] {
  358. if {[file type $i] eq {directory}} {
  359. eval lappend res [glob-r $i]
  360. } else {
  361. lappend res $i
  362. }
  363. }
  364. set res
  365. } ;# RS
  366. ###
  367. proc blacktools:update_unsetflag {} {
  368. global black
  369. if {[info exists black(backup_update)]} {
  370. unset black(backup_update)
  371. }
  372. }
  373. ###
  374. proc blacktools:update_start_restore {} {
  375. global black
  376. set hand $black(update_hand)
  377. set chan $black(update_chan)
  378. set userlang [blacktools:update_userlang $hand]
  379. if {![file isdirectory "$black(actdir)/BlackTools"]} {
  380. blacktools:update_put $hand $chan 16 ""
  381. file delete -force $black(backup_dir)
  382. blacktools:update_unsetflag
  383. return
  384. } elseif {![file exists "$black(actdir)/$black(tclname)"]} {
  385. blacktools:update_put $hand $chan 16 ""
  386. file delete -force $black(backup_dir)
  387. blacktools:update_unsetflag
  388. return
  389. }
  390. set end_download [unixtime]
  391. set dif [expr $end_download - $black(start_update)]
  392. blacktools:update_put "" "" 17 [list [return_time $userlang $dif]]
  393. set newdata [blacktools:update_data 0 ""]
  394. blacktools:update_put $hand $chan 18 ""
  395. set restore_config [blacktools:update_restore $black(update_old_data) $newdata]
  396. unset black(update_old_data)
  397. set newdata [lindex $restore_config 0]
  398. set num_var [lindex $restore_config 1]
  399. if {$num_var > 0} {
  400. set newdata [blacktools:update_data 1 $newdata]
  401. blacktools:update_put $hand $chan 19 [list $num_var]
  402. } else {
  403. blacktools:update_put "" "" 20 ""
  404. }
  405. blacktools:update_put $hand $chan 21 ""
  406. blacktools:update_restore_files
  407. blacktools:every 1000 {
  408. set info_files_num [llength [glob -nocomplain -directory "$black(actdir)/BlackTools/FILES" "*.txt"]]
  409. if {[file isdirectory "$black(actdir)/BlackTools/FILES/TOPWORDS"]} {
  410. set info_files_topwords [llength [glob -nocomplain -directory "$black(actdir)/BlackTools/FILES/TOPWORDS" "*.txt"]]
  411. set info_files_num [expr $info_files_num + $info_files_topwords]
  412. }
  413. if {$info_files_num == $black(update_file_saved)} {
  414. blacktools:update_end $info_files_num
  415. break
  416. }
  417. }
  418. }
  419. ###
  420. proc blacktools:update_end {num} {
  421. global black config
  422. set hand $black(update_hand)
  423. set chan $black(update_chan)
  424. set userlang [blacktools:update_userlang $hand]
  425. set end_update [unixtime]
  426. set dif [expr $end_update - $black(start_update)]
  427. unset black(start_update)
  428. if {$num == 0} {
  429. blacktools:update_put "" "" 22 ""
  430. } else {
  431. blacktools:update_put $hand $chan 23 [list $num]
  432. }
  433. set file [open $config r]
  434. set data [read -nonewline $file]
  435. close $file
  436. set reg "source $black(actdir)/$black(old_config_file)"
  437. regsub $reg $data "source $black(actdir)/$black(tclname)" data
  438. set file [open $config w]
  439. puts $file $data
  440. close $file
  441. unset black(update_hand)
  442. unset black(update_chan)
  443. blacktools:update_unsetflag
  444. blacktools:update_put $hand $chan 24 [list [return_time $userlang $dif]]
  445. blacktools:update_put $hand $chan 25 [list $black(backup_dir) $black(log_file)]
  446. blacktools:update_put $hand $chan 26 ""
  447. unset black(update_file_saved)
  448. file delete -force "$black(actdir)/$black(old_config_file)"
  449. if {$black(finish_action) == 0} {
  450. rehash
  451. setaway "none"
  452. } else {
  453. if {$black(update_from) == 0} {
  454. rehash
  455. setaway "none"
  456. blacktools:update_put $hand $chan 45 ""
  457. } else {
  458. blacktools:update_put $hand $chan 46 ""
  459. utimer 10 [list restart]
  460. }
  461. }
  462. blacktools:lastupdate 0 $black(update_last_modify) 1
  463. unset black(update_last_modify)
  464. unset black(finish_action)
  465. unset black(update_from)
  466. unset black(download_size)
  467. unset black(old_config_file)
  468. }
  469. ###
  470. proc blacktools:lastupdate {type lastmodif new} {
  471. global black botnick
  472. if {$type == 0} {
  473. if {$new == 1} {
  474. set file [open $black(last_update_file) w]
  475. puts $file "$botnick $lastmodif"
  476. close $file
  477. return
  478. }
  479. if {![file exists $black(last_update_file)]} {
  480. set file [open $black(last_update_file) w]
  481. puts $file "$botnick $lastmodif"
  482. close $file
  483. } else {
  484. set file [open $black(last_update_file) a]
  485. puts $file "$botnick $lastmodif"
  486. close $file
  487. }
  488. } else {
  489. if {![file exists $black(last_update_file)]} {
  490. return 0
  491. }
  492. set file [open $black(last_update_file) r]
  493. set data [read -nonewline $file]
  494. close $file
  495. if {[lsearch -nocase [split $data "\n"] "$botnick $lastmodif"] > -1} {
  496. return 0
  497. } elseif {[lsearch -nocase [split $data "\n"] "* $lastmodif"] > -1} {
  498. return 1
  499. }
  500. }
  501. }
  502. ###
  503. proc blacktools:update_restore_files {} {
  504. global black
  505. set files ""
  506. set counter 0
  507. set files [glob -nocomplain -directory "$black(backup_dir)/BlackTools/FILES" "*.txt"]
  508. if {![file isdirectory "$black(actdir)/BlackTools/FILES"]} {
  509. file mkdir "$black(actdir)/BlackTools/FILES"
  510. }
  511. foreach f $files {
  512. incr counter
  513. set filename [file tail $f]
  514. file copy -force $f "$black(actdir)/BlackTools/FILES/$filename"
  515. }
  516. if {[info exists black(update_file_topwords)]} {
  517. if {![file isdirectory "$black(actdir)/BlackTools/FILES/TOPWORDS"]} {
  518. file mkdir "$black(actdir)/BlackTools/FILES/TOPWORDS"
  519. }
  520. set topwords_files [glob -nocomplain -directory "$black(backup_dir)/BlackTools/FILES/TOPWORDS" "*.txt"]
  521. foreach f $topwords_files {
  522. incr counter
  523. set filename [file tail $f]
  524. file copy -force $f "$black(actdir)/BlackTools/FILES/TOPWORDS/$filename"
  525. }
  526. unset black(update_file_topwords)
  527. }
  528. return $counter
  529. }
  530. ###
  531. proc blacktools:update_set_time {num type} {
  532. global black
  533. set bt_file "$black(dirname)/$black(tclname)"
  534. set file [open $bt_file r]
  535. set data [read -nonewline $file]
  536. close $file
  537. set regexp_var2 "set black\\(update_time_check\\) \"(.*?)\""
  538. regexp -nocase $regexp_var2 $data -> found
  539. if {$type == 0} {
  540. return $found
  541. } else {
  542. regexp -nocase $regexp_var2 $data found_line
  543. set found_line [string map [list $found $num] $found_line]
  544. regsub $regexp_var2 $data $found_line data
  545. blacktools:update_data 1 $data
  546. }
  547. }
  548. ###
  549. proc blacktools:update_on_off {type} {
  550. global black
  551. set regexp_var2 "set black\\(update_type\\) \"(.*?)\""
  552. set bt_file "$black(dirname)/$black(tclname)"
  553. set file [open $bt_file r]
  554. set data [read -nonewline $file]
  555. close $file
  556. regexp -nocase $regexp_var2 $data found_line
  557. regexp -nocase $regexp_var2 $data -> found
  558. if {$type == 0 && $found == 0} {
  559. return 0
  560. }
  561. if {$type == 1 && $found == 1} {
  562. return 1
  563. }
  564. switch $type {
  565. 0 {
  566. set found_line [string map {"1" "0"} $found_line]
  567. regsub $regexp_var2 $data $found_line data
  568. blacktools:update_data 1 $data
  569. return 2
  570. }
  571. 1 {
  572. set found_line [string map {"0" "1"} $found_line]
  573. regsub $regexp_var2 $data $found_line data
  574. blacktools:update_data 1 $data
  575. return 3
  576. }
  577. }
  578. }
  579. ###
  580. proc blacktools:update_restore {data newdata} {
  581. global black
  582. set current_data $newdata
  583. set variables [regexp -all -inline {set black\((.+?)\)} $data]
  584. regsub -all {set black\((.+?)\)} $variables "" variables
  585. set var_nomodif "name author vers site"
  586. set var_counter 0
  587. foreach var $variables {
  588. if {$var == ""} {continue}
  589. if {[lsearch -nocase $var_nomodif $var] > -1} {continue}
  590. set regexp_var "set black\\($var\\) \\{(.*?)\\}"
  591. set regexp_var2 "set black\\($var\\) \"(.*?)\""
  592. regexp -nocase $regexp_var $data found_old
  593. regexp -nocase $regexp_var2 $data found_old_2
  594. if {[info exists found_old]} {
  595. set found_old [concat $found_old]
  596. regexp -nocase $regexp_var $newdata found_new
  597. if {[info exists found_new]} {
  598. if {[string equal -nocase $found_old $found_new]} {continue}
  599. incr var_counter
  600. regsub $regexp_var $current_data $found_old current_data
  601. unset found_new
  602. }
  603. unset found_old
  604. }
  605. }
  606. foreach var $variables {
  607. if {$var == ""} {continue}
  608. if {[lsearch -nocase $var_nomodif $var] > -1} {continue}
  609. set regexp_var2 "set black\\($var\\) \"(.*?)\""
  610. regexp -nocase $regexp_var2 $data found_old_2
  611. if {[info exists found_old_2]} {
  612. set found_old_2 [concat $found_old_2]
  613. regexp -nocase $regexp_var2 $newdata found_new_2
  614. if {[info exists found_new_2]} {
  615. if {[string equal -nocase $found_old_2 $found_new_2]} {continue}
  616. if {[string equal -nocase "default_away" $var] && [string match -nocase "*www.TCLScripts.Net*" $found_old_2]} {continue}
  617. incr var_counter
  618. regsub $regexp_var2 $current_data $found_old_2 current_data
  619. unset found_new_2
  620. }
  621. unset found_old_2
  622. }
  623. }
  624. return [list $current_data $var_counter]
  625. }
  626. ###
  627. proc blacktools:update_data {type data} {
  628. global black
  629. set bt_file "$black(dirname)/$black(tclname)"
  630. if {![file exists $bt_file]} {
  631. return 0
  632. }
  633. if {$type == 0} {
  634. set file [open $bt_file r]
  635. set data [read -nonewline $file]
  636. close $file
  637. return $data
  638. } else {
  639. set file [open $bt_file w]
  640. puts $file $data
  641. close $file
  642. }
  643. }
  644. ###
  645. proc blacktools:update_getconfig {} {
  646. global black
  647. set link "https://raw.githubusercontent.com/tclscripts/BlackTools-TCL/master/BlackTools.tcl"
  648. http::register https 443 [list ::tls::socket -tls1 true]
  649. set ipq [http::config -useragent "lynx"]
  650. set error [catch {set ipq [::http::geturl $link -timeout 10000]} eror]
  651. set status [::http::status $ipq]
  652. if {$status != "ok"} {
  653. ::http::cleanup $ipq
  654. return -1
  655. }
  656. set getipq [::http::data $ipq]
  657. ::http::cleanup $ipq
  658. return $getipq
  659. }
  660. ###
  661. proc blacktools:update_verify {} {
  662. global black
  663. set link "https://raw.githubusercontent.com/tclscripts/BlackTools-TCL/master/VERSION"
  664. http::register https 443 [list ::tls::socket -tls1 true]
  665. set ipq [http::config -useragent "lynx"]
  666. set error [catch {set ipq [::http::geturl $link -timeout 10000]} eror]
  667. set status [::http::status $ipq]
  668. if {$status != "ok"} {
  669. ::http::cleanup $ipq
  670. return -1
  671. }
  672. set getipq [::http::data $ipq]
  673. ::http::cleanup $ipq
  674. return $getipq
  675. }
  676. ###
  677. proc blacktools:update_userlang {nick} {
  678. global black
  679. if {$nick == ""} {return [string tolower $black(default_lang)]}
  680. set hand [nick2hand $nick]
  681. set userlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  682. if {$userlang == ""} { set userlang "[string tolower $black(default_lang)]" }
  683. return $userlang
  684. }
  685. ###
  686. proc blacktools:update_put {nick chan num var} {
  687. global black
  688. set counter 0
  689. foreach v $var {
  690. incr counter
  691. set replace(%msg.${counter}%) $v
  692. }
  693. if {$nick != "" && $chan != ""} {
  694. set hand [nick2hand $nick]
  695. set getmethod [getuser $hand XTRA OUTPUT_TYPE]
  696. set userlang [string tolower [getuser $hand XTRA OUTPUT_LANG]]
  697. if {$userlang == ""} { set userlang "[string tolower $black(default_lang)]" }
  698. if {$getmethod == ""} { set getmethod "0" }
  699. set text [black:color:set "" $black(say.${userlang}.autoupdate.${num})]
  700. set reply [join $text]
  701. set reply [string map [array get replace] $reply]
  702. switch $getmethod {
  703. 0 {
  704. putserv "NOTICE $nick :$reply"
  705. }
  706. 1 {
  707. putserv "PRIVMSG $chan :$reply"
  708. }
  709. 2 {
  710. putserv "PRIVMSG $nick :$reply"
  711. }
  712. }
  713. } else {
  714. set lang [string tolower $black(default_lang)]
  715. set text [black:color:set "" $black(say.${lang}.autoupdate.${num})]
  716. set reply [join $text]
  717. set reply [string map [array get replace] $reply]
  718. }
  719. putlog "\[BT\] $reply"
  720. set file [open $black(log_file) a]
  721. puts $file $reply
  722. close $file
  723. }
  724. ###
  725. #https://wiki.tcl-lang.org/page/every
  726. proc blacktools:every {interval script} {
  727. global everyIds
  728. if {$interval eq {cancel}} {
  729. after cancel $everyIds($script)
  730. return
  731. }
  732. set everyIds($script) [after $interval [namespace code [info level 0]]]
  733. set rc [catch {uplevel #0 $script} result]
  734. if {$rc == [catch break]} {
  735. after cancel $everyIds($script)
  736. set rc 0
  737. } elseif {$rc == [catch continue]} {
  738. # Ignore - just consume the return code
  739. set rc 0
  740. }
  741. # TODO: Need better handling of errorInfo etc...
  742. return -code $rc $result
  743. }