BT.AutoUpdate.tcl 24 KB

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