BT.AutoUpdate.tcl 24 KB

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