#!/usr/athena/bin/perl

require 'sys/socket.ph';
require 'chat2.pl';

while(1){
    $topstories=""; $teasestories = "";
    print("Fetching news\n");
    ($head, $bod) = &get_url("http://www.cnn.com/alt_index.html");
    print("Analyzing news\n");
    &getTopStories();
    &getTeasers();
    print("Sending news\n");
    &sendNews();
    print("sleeping\n");
    sleep(30);
}

sub getTeasers {
    if($bod =~ /teasers/){
	$teasers = $';
	$teasers =~ /\/teasers/;
	$teasers = $`;
	$teasers =~/<\/p>/i;
	$teasers= $`;
	@teasers = split("\n\n", $teasers);
	foreach $t (@teasers){
	    if($t=~/<br>/i){
		$t =~s/<[^>]+>//g;
		$t =~ s/\n/ /g;
		if(!$known{$t}){
		    $known{$t}=1;
		    $teasestory .= "$t\n---------\n";
		}
	    }
	}
    }
}

sub sendNews {
    print("Topstories:\n$topstories\n");
    print("Teasestories:\n$teasestory\n");
}

sub getTopStories {
    for $i (1..5){
	if($bod =~ /story$i/){
	    $*=1;
	    $sttmp = $';
	    $sttmp =~ /\/story$i/;
	    $story = $`;
	    $story =~ s/^ *-+>//;
	    if($story =~ /<BR>/){
		$story = $`;
	    }
	    $story=~ s/<!-*//g;
	    $story =~s/<[^>]+>//g;
	    $story =~s/\n+/\n/g;
	    if(!$known{$story}){
		$known{$story}=1;
		$story = &fill($story);
		$topstories .= $story."---------Story------------------\n";
	    }
	}
    }
}

sub fill {
    local($text) = @_;
    local($ret);
    @lines = split("\n", $text);
    foreach $l (@lines){
	if(length($l) > 60){
	    @w = split(" ", $l);
	    $nw = shift @w;
	    while($#w >=0){
		$nl = "";
		while((length($nl)+length($nw)) < 60){
		    $nl .= "$nw ";
		    last if $#w <0;
		    $nw = shift @w;
		}
		$ret .= "$nl\n";
	    }
	    
	}
	else{
	    $ret .= "$l\n";
	}
    }
    $ret;
}

sub zwrite {
    local($inst, $sig, $msg);
    open(ZW, "|/usr/athena/bin/zwrite -c znn -i $inst -s $sig");
    print ZW $msg;
    print("\n@small(See http://www.cnn.com/ for more details)\n");
    close(ZW);
}

sub get_url {
    # Returns headers and document
    local($url) = @_;
    local($mysite, $mypath, $myport);
    local($doc, $handle, $headers, $document);

    ($mysite, $mypath, $myport) = &ParseURL($url);
    $doc = ''; $headers = ''; $document = '';
    $handle =&chat'open_port($mysite, $myport) || do {print("Failed following $mysite:$myport\n");return;};
    &chat'print("GET $mypath HTTP/1.0\nUser-Agent: WebTool\n\n");
    $doc=&listen(30);
    &chat'close($handle);

    if($doc=~/\n\r?\n\r?/){
        $headers = $`;
        $document = $';
    }
    else{
        $doc =~ s/ /\./g;
    }

    ($headers, $document);
}
sub ParseURL {
    # Returns site, path, port
    local($url) = @_;
    
    ($proto, $garbage, $siteport, @path) = split('/', $url);
    next if ($proto ne 'http:');
    $port = 0;
    ($site, $port)=split(':', $siteport);
    if(!$port){
        $site = $siteport; $port = 80;
    }
    $pn = 0;
    for (@path){
        if($_ eq '..'){
            splice(@path, $pn-1, 2);
            $pn -=2;
        }
        if($_ eq '.'){
            splice(@path, $pn, 1);
            $pn -=1;
        }
        $pn++;
    }
    $path = '/'.join('/', @path);
    $path .= '/' if substr($url, length($url)-1,1) eq '/';
    return($site, $path, $port);
}
sub listen {
    local($secs) = @_;
    local($return,$tmp) = "";
    while (length($tmp = &chat'expect($secs, '(.|\n)+', '$&'))) {
        print $tmp if $trace;
        $return .= $tmp;
        (return $return) if (length($return) > 100000);
    }
    $return;
}
