grabchat

Utility intended to provide the user access to the previous day's The Tcler's Wiki chatroom log.

Author: LV

Contributors: Csan RS PT


 #!/bin/sh
 # \
 exec tclsh "$0" ${1+"$@"}

 # Name: grabchat
 # Author: [lvirden]
 # Version: 4
 # 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
 #     [PT]: implemented proxy Basic Authentication.
 #           usage: grabchat ?proxy:port? ?username password?
 # v3: [lvirden]: fixed chatlog date format differences ( 2001-12-1.txt )
 #      - will have to fix each time format changes.
 # v4: [lvirden]: couple minor tweaks to match my local copy - no bugs.

 package require http
 package require base64

 namespace eval http {}

 proc proxyset { proxy {port 80} } {
   if {[string equal $port ""]} {
     set port 80
   }
   ::http::config -proxyhost $proxy -proxyport $port
 }

 proc buildProxyHeaders {login passwd} {
     lappend ::headers "Proxy-Authorization" \
                [concat "Basic" \
                [base64::encode $login:$passwd]]
     return $headers
 }

 set proxy [lindex $argv 0]
 proxyset [lindex [split $proxy :] 0] [lindex [split $proxy :] 1]

 set headers [list "Pragma" "no-cache"]
 if {$argc > 1} {
     buildProxyHeaders [lindex $argv 1] [lindex $argv 2]
 }


 proc ::http::geturl_followRedirects {url args} {
        while {1} {
            set token [eval [list http::geturl $url] -headers [list $::headers] $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 $::headers]
        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
 }

 # goal: grab yesterday's tcler's wiki chat log
 # filename YYYY-M-D.txt

 scan [clock format [clock scan "yesterday"] -format "%e %m %Y"] "%d %d %d" day month year 
 set chatfile [format "%s-%s-%s.txt" $year $month $day]

 # 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 http://mini.net/tchat/logs/$chatfile is dead: [::http::code $token]"
 }
 ::http::cleanup $token

Category Application - Category Internet