BT.AutoUpdate.tcl 25 KB

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