Version 5 of grabchat

Updated 2001-11-30 14:45:17

Purpose: A utility intended to provide the user access to the previous day's The Tcl'ers Wiki chatroom log.


 #!/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

 namespace eval http {}

 proc proxyset { proxy {port 80} } {
   if {[string compare $port ""]==0} {
     set port 80
   }
   ::http::config -proxyhost $proxy -proxyport $port
 }
 proxyset [lindex [split $argv :] 0] [lindex [split $argv :] 1]

 # 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] $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]
        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 -validate 1]
 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