Purpose: A utility intended to provide the user access to the previous day's [The Tcl'ers Wiki] chatroom log. Author: [LV] Contributors: [Csan] [RS] [PT] ---- #!/bin/sh # \ exec tclsh "$0" ${1+"$@"} # Name: grabchat # Author: [lvirden] # Version: 2 # Purpose: to grab yesterday's tcler's wiki chat room log # v2: [Csan]: added proxy support (thx [RS]) and script header. # Give the proxy:port or just proxy as the cmdline arg package require http package require base64 namespace eval http {} proc proxyset { proxy {port 80} } { if {[string compare $port ""]==0} { set port 80 } ::http::config -proxyhost $proxy -proxyport $port } proc buildProxyHeaders {login passwd} { global proxy_auth set proxy_auth [list "Proxy-Authorization" \ [concat "Basic" \ [base64::encode $login:$passwd]]] return $proxy_auth } set proxy [lindex $argv 0] proxyset [lindex [split $proxy :] 0] [lindex [split $proxy :] 1] # If you need Proxy Authentication then "usage: grabchat proxy:port username password" set proxy_auth {} if {$argc > 1} { buildProxyHeaders [lindex $argv 1] [lindex $argv 2] } # goal: grab yesterday's tcler's wiki chat log # filename YYYY-MM-DD.txt proc ::http::geturl_followRedirects {url args} { while {1} { set token [eval [list http::geturl $url] -headers [list $::proxy_auth] $args] switch -glob -- [http::ncode $token] { 30[1237] { ### redirect - see below ### } default { return $token } } upvar #0 $token state array set meta [set ${token}(meta)] if {![info exist meta(Location)]} { return $token } set url $meta(Location) unset meta } } proc ::http::copy { url file {chunk 4096} } { set out [open $file w] set token [::http::geturl $url -channel $out -progress ::http::Progress -blocksize $chunk -headers $::proxy_auth] close $out # This ends the line started by ::http::Progress puts stderr "" upvar #0 $token state set max 0 foreach {name value} $state(meta) { if {[string length $name] > $max} { set max [string length $name] } if {[regexp -nocase -- ^location$ $name]} { # Handle URL redirects puts stderr "Location:$value" return [copy [string trim $value] $file $chunk] } } incr max foreach {name value} $state(meta) { puts [format "%-*s %s" $max $name: $value] } return $token } proc ::http::Progress {args} { puts -nonewline stderr . ; flush stderr } set chatfile [format "%s.txt" [clock format [clock scan "yesterday"] -format {%Y-%m-%d}]] # Verify that the site is up set token [::http::geturl_followRedirects http://mini.net/tchat/logs/$chatfile] if {[regexp -nocase -- {ok} [::http::code $token]]} { ::http::copy http://mini.net/tchat/logs/$chatfile chat.$chatfile } else { puts "site is dead: [::http::code $token]" } ::http::cleanup $token