BT.AutoUpdate.tcl 24 KB

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