#!/usr/athena/bin/perl

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

while(1){
    &getNews("http://www.yahoo.com/text/headlines/news/", "topstories");
    &getNews("http://www.yahoo.com/text/headlines/business/", "business");
    &getNews("http://www.yahoo.com/text/headlines/compute/", "technology");
    &getNews("http://www.yahoo.com/text/headlines/international/", "world");
    &getNews("http://www.yahoo.com/text/headlines/sports/", "sports");
    &getNews("http://www.yahoo.com/text/headlines/politics/", "politics");
    &getNews("http://www.yahoo.com/text/headlines/entertainment/", "entertainment");
    sleep(300);
}

sub getNews {
    local($url, $instance) = @_;
    local($news) = "";
    $ct = 0;
    ($head, $bod) = &get_url($url);
    (@sections) = split("<hr>", $bod);
    $stories = $sections[2];
    @stories = split("\n", $stories);
    foreach $s (@stories){
	next unless $s=~/<li>/i;
	$s =~s/<[^>]+>//g;
	if(!$known{$s} && ($ct<7)){
	    $ct++;
	    $known{$s} = 1;
	    $news .= " - $s\n";
	}
    }
    $news .= "\nFull stories are available at $url\n" if $news;
    &zwrite("$instance", "Zephyr Broadcast News", $news) if $news;
}

sub zwrite {
    local($inst, $sig, $msg)=@_;
    open(ZW, "|/usr/athena/bin/zwrite -n -q -c zbn -i $inst -s \"$sig\"");
    print ZW $msg;
    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;
}
