BT.AutoUpdate.tcl 25 KB

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