BT.AutoUpdate.tcl 24 KB

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