Advertisement
outsider

Untested attempt for autoupdate.tcl

Sep 29th, 2018
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 2.06 KB | None | 0 0
  1. # Autoupdater script v1.0 (c)2018 OUTsider <outsider@scarynet.org>
  2. #
  3. # Usage: Load this script prior to the scripts you want to autoupdate!
  4. # In the scripts you want to update add this anywhere outside the functions:
  5. # if {[info procs autoupdate:add]} { autoupdate:add <updateurl> }
  6.  
  7. package require http
  8.  
  9. # in case you need https
  10. package require tls
  11. ::http::register https 443 ::tls::socket
  12.  
  13. set autoupdate ""
  14. proc autoupdate:add {updateurl} {
  15.     global autoupdate
  16.     set scriptname [info script]
  17.     lappend autoupdate(script) $scriptname
  18.     set autoupdate(script,$scriptname) $updateurl
  19.     putlog "\[AUTOUPDATE\] Added $scriptname with url: $updateurl to list."
  20. }
  21.  
  22. bind time "* * * * *" - autoupdate:time
  23. proc autoupdate:time {min hrs day mnt yrs} {
  24.     global autoupdate
  25.    
  26.     #
  27.     # put eventual throttles here or go for (u)timer constructs if it should not check every minute
  28.     #
  29.    
  30.     set updated 0
  31.     foreach script $autoupdate(script) {
  32.         #read old file and get md5sum
  33.         set fd [open $script r]
  34.         set fdata [read $fd]
  35.         close $fd
  36.         set oldmd5 [md5 $fdata]
  37.        
  38.         #fetch new file from url
  39.         set token [::http::geturl $autoupdate(script,$script)]
  40.         if {[::http::ncode == 200]} {
  41.             set data [::http::data $token]
  42.             set newmd5 [md5 $data]
  43.            
  44.             #compare old with new md5sum, if same, file is not altered
  45.             if {$oldmd5 == $newmd5} { continue }
  46.            
  47.             #fetch md5sum file and compare if present
  48.             set token [::http::geturl "${autoupdate(script,$script)}.md5sum"]
  49.             if {[::http::ncode == 200]} {
  50.                 set md5data [::http::data $token]
  51.                 if {[md5 $data] != $md5data} {
  52.                     putlog "\[AUTOUPDATE\] ${autoupdate(script,$script)} failed MD5 check ([md5 $data] vs $md5data)"
  53.                     continue
  54.                 }
  55.             }
  56.            
  57.             # all checks passed, overwrite script
  58.             set fd [open $script w]
  59.             puts $fd $data
  60.             close $fd
  61.             incr updated
  62.         } else {
  63.             putlog "\[AUTOUPDATE\] ${autoupdate(script,$script)} returned HTTP Code [::http::ncode]"
  64.         }
  65.     }
  66.    
  67.     #some files got updated, rehash
  68.     if {$updated > 0} {
  69.         putlog "\[AUTOUPDATE\] $updated scripts have been updated"
  70.         rehash
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement