Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Autoupdater script v1.0 (c)2018 OUTsider <outsider@scarynet.org>
- #
- # Usage: Load this script prior to the scripts you want to autoupdate!
- # In the scripts you want to update add this anywhere outside the functions:
- # if {[info procs autoupdate:add]} { autoupdate:add <updateurl> }
- package require http
- # in case you need https
- package require tls
- ::http::register https 443 ::tls::socket
- set autoupdate ""
- proc autoupdate:add {updateurl} {
- global autoupdate
- set scriptname [info script]
- lappend autoupdate(script) $scriptname
- set autoupdate(script,$scriptname) $updateurl
- putlog "\[AUTOUPDATE\] Added $scriptname with url: $updateurl to list."
- }
- bind time "* * * * *" - autoupdate:time
- proc autoupdate:time {min hrs day mnt yrs} {
- global autoupdate
- #
- # put eventual throttles here or go for (u)timer constructs if it should not check every minute
- #
- set updated 0
- foreach script $autoupdate(script) {
- #read old file and get md5sum
- set fd [open $script r]
- set fdata [read $fd]
- close $fd
- set oldmd5 [md5 $fdata]
- #fetch new file from url
- set token [::http::geturl $autoupdate(script,$script)]
- if {[::http::ncode == 200]} {
- set data [::http::data $token]
- set newmd5 [md5 $data]
- #compare old with new md5sum, if same, file is not altered
- if {$oldmd5 == $newmd5} { continue }
- #fetch md5sum file and compare if present
- set token [::http::geturl "${autoupdate(script,$script)}.md5sum"]
- if {[::http::ncode == 200]} {
- set md5data [::http::data $token]
- if {[md5 $data] != $md5data} {
- putlog "\[AUTOUPDATE\] ${autoupdate(script,$script)} failed MD5 check ([md5 $data] vs $md5data)"
- continue
- }
- }
- # all checks passed, overwrite script
- set fd [open $script w]
- puts $fd $data
- close $fd
- incr updated
- } else {
- putlog "\[AUTOUPDATE\] ${autoupdate(script,$script)} returned HTTP Code [::http::ncode]"
- }
- }
- #some files got updated, rehash
- if {$updated > 0} {
- putlog "\[AUTOUPDATE\] $updated scripts have been updated"
- rehash
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement