#!/usr/bin/perl

# Copyright (C) 2000-2003 William R Thomas <corvar@theonering.net>
# Last Changed by: $Author: corvar $
# on: $Date: 2003/04/11 09:39:00 $
# Version: $Revision: 1.1 $

# Maintainer: William R Thomas <corvar@theonering.net>
# Keywords: convenience

# This file is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License
# as published by the Free Software Foundation.

# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#

# Revision History:
# 1.0 Initial version;  Format RSS datasources for convenient display 
#     in a Mozilla browser's sidebar.
# 1.1 3 years worth of misc bug fixes.  None of major note.  Logic to deal
#     with RSS Version 0.92 and RSS Version 2.0

# Requirements:
# LWP::Simple
# CGI
# URI::Escape
# Recommended:
# XML::Parser

use LWP::Simple;
# Uncomment the following line if you are going to use the XML::Parser
# Module.  This is the recommened method
use XML::Parser;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use URI::Escape;



$newStyle=<<END;
           <!--
BODY, P, TABLE, TR, TD {
font-size: 10pt;
font-family: arial,  sans-serif;
color: black;
}
.TOPIC{
font-size: small;
font-weight: bold; color: 000066
}
.BODY{}
.MNN-yes-isascii {} /* for mnn ascii */
.MNN-no-isascii {font-family: None;} /* for mnn ascii */
/* note that A:HOVER isn't supported by all browsers */
A:HOVER {color: red}
.SBARXX {font-size: xx-small;}
.SBARX {font-size: x-small;}
.SBAR {font-size: small;}
.STOCKS, .SPORTS, .WEATHER {font-size: x-small;}
INPUT {
font-family: None;
font-size: small;
}
A:LINK {color: 0066CC}
A:VISITED {color: 660099}
.DATE {color: 990033}
.NAVBAR {background-color: 99CCCC}
.FRAME {}
.FRAMETITLE, A.FRAMETITLE {
font-size: smaller;
color: 000000;
font-weight: bold; 
text-decoration: none;
}
A.FRAMETITLE:HOVER { 
color: 000000;
text-decoration: underline;
}
.WINDOW {background-color: white}
.TOOLWINDOW {background-color: CCCCCC}
.BANNER{background-color: FFFFD7}
.BANNER A:LINK {color: black}
.MYSTYLE {
        color:6600FF;
        font-size:25px;
        font-family:Arial,helvetica,sans-serif;
        font-style:italic;
        font-weight:bold;
}
           -->
END

$page = new CGI;
print $page->header(-Refresh=>'900');
print $page->start_html(-title=>'TheOneRing.net | Sidebar',
                            -author=>'Corvar@theonering.net',
                            'bgcolor'=>'#FFFFFF',
			    'target'=>'_content',
			    'style'=>{-code=>$newStyle});


my $sourceurl = 'http://www.theonering.net/cgi-bin/lotr2rdf.cgi';
my $file = 'foo.rss';
my $path = '/tmp';
my $version = 0;
my $channel = 0;
my $image = 0;
my $item = 0;
my $textinput = 0;
my $title = 0;
my $link = 0;
my $description = 0;
my $url = 0;
my $height = 0;
my $width = 0;
my($titletmp,$width,$height,$desctmp,$urltmp,$heighttmp,$widthtmp,$linktmp);
my($maintitle, $mainlink, $maindesc);

if($page->param('site') && $page->param('path')) {
    if($page->param('path') =~ /^\//) {
	$sourceurl = "http://".$page->param('site').$page->param('path');
    } else {
	$sourceurl = "http://".$page->param('site')."/".$page->param('path');
    }
}

$file = $sourceurl;
$file =~ s/[^a-zA-Z0-9]//g;

if((time() - (stat("$path/$file"))[10]) > (60 * 60)) {
#    print STDERR "Got new\n";
    unlink($path."/".$file);
    getstore($sourceurl,$path."/".$file);
    open(FILE,$path."/".$file);
    open(TMP,">".$path."/".$file.".tmp");
    while(<FILE>) {
	if(/<title>(.*)<\/title>/) {
	    print TMP "<title>".uri_escape($1,"\x00-\x1f\x7f-\xff")."</title>\n";
	} else {
	    print TMP $_;
	}
    }
    close(FILE);
    close(TMP);
    unlink($path."/".$file);
    link($path."/".$file.".tmp",$path."/".$file);
    unlink($path."/".$file.".tmp");
} else {
#    print STDERR "Kept old\n";
}

# Uncomment the following two lines if you are going to use the XML::Parser
# Module.  This is the recommened method
$p1 = new XML::Parser(Style => 'Stream');
$p1->parsefile("$path/$file");

# Uncomment the following 22 lines if you are no going to use the XML::Parser
# Modules.  This method is not recommened.
#open(FOO,"<$path/$file");
#while(<FOO>) {
#    $start = '';
#    $text = '';
#    $end = '';
#
#    if(/^$/) {
#	next;
#    }
#    if(/^<(\w+)>(.*)<\/(\w+)>$/) {
#	$start = $1;
#	$text=$2;
#	$end=$3;
#	$_ = $1;
#	&StartTag;
#	$_ = $2;
#	&Text;
#	$_ = $3;
#	&EndTag;
#    } elsif( /^<(\w+)>$/) {
#	$_ = $1;
#	&StartTag;
#    } elsif( /^<\/(\w+)>$/) {
#	$_ = $1;
#	&EndTag;
#    }
#}
print "</TABLE>";
print $page->end_html;


sub StartTag {
    if( $_ =~ /rss version.*0.91/) {
	$version = '0.91';
    }
    if( $_ =~ /rss version.*0.92/) {
	$version = '0.92';
    }
    if( $_ =~ /rss version.*2.0/) {
	$version = '2.0';
    }
    if( $_ =~ /channel/) {
	$channel = 1;
    }
    if( $_ =~ /item/) {
	$item = 1;
    }
    if( $_ =~ /image/) {
	$image = 1;
    }
    if( $_ =~ /textinput/) {
	$textinput = 1;
    }
    if( $_ =~ /title/) {
	$title = 1;
    }
    if( $_ =~ /link/) {
	$link = 1;
    }
    if( $_ =~ /description/) {
	$description = 1;
    }
    if( $_ =~ /url/) {
	$url = 1;
    }
    if( $_ =~ /height/) {
	$height = 1;
    }
    if( $_ =~ /width/) {
	$width = 1;
    }
}

sub EndTag {
    if( $_ =~ /channel/) {
	$channel = 0;
    }
    if( $_ =~ /title/) {
	$title = 0;
    }
    if( $_ =~ /link/) {
	$link = 0;
    }
    if( $_ =~ /description/) {
	$description = 0;
    }
    if( $_ =~ /url/) {
	$url = 0;
    }
    if( $_ =~ /height/) {
	$height = 0;
    }
    if( $_ =~ /width/) {
	$width = 0;
    }
    if($channel && ($version != '0.91' && $version != '0.92' && $version != '2.0')) { return;}
    if( $_ =~ /item/) {
	$item = 0;
	print qq(<TR>\n);
	print "<TD WIDTH=\"1%\">&#149;</TD>\n";
	print qq(<TD WIDTH="99%">\n);
	print "<a href=\"$linktmp\">".$titletmp."</a><br>\n";
	print qq(</TD>\n);
	$linktmp = '';
	$titletmp = '';
    }
    if( $_ =~ /image/) {
	print qq(<TABLE ALIGN="CENTER" BGCOLOR="#FFFFFF" BORDER="0" CELLPADDING="2" CELLSPACING="0" WIDTH="100%">\n);
	print qq(<TR>\n<TD COLSPAN="2">\n);
	print "<center>\n";
	print "<a href=\"$linktmp\"><img border=\"0\" width=\"$widthtmp\" height=\"$heighttmp\" alt=\"$desctmp\" src=\"$urltmp\"></a>\n";
	print "<br>\n";
	if(length($maindesc) > 50) {
	    $maindesc = substr($maindesc,0,50)."...";
	}
	print "<a href=\"$linktmp\">".uri_unescape($maintitle).": $maindesc</a>\n";
	print qq(<hr></center></TD></TR>\n);
	$image = 0;
    }
}

sub Text {
    if($title) {
	$titletmp = $_;
	if($channel && !$image && !$link && !$testinput) {
	    $maintitle = $titletmp;
	}
    }
    if($description) {
	$desctmp = $_;
	if($channel && !$image && !$link && !$testinput) {
	    $maindesc = $desctmp;
	}
    }
    if($link) {
	$linktmp = $_;
	if($channel && !$image && !$link && !$testinput) {
	    $mainlink = $linktmp;
	}
    }
    if($url) {
	$urltmp = $_;
    }
    if($height) {
	$heighttmp = $_;
    }
    if($width) {
	$widthtmp = $_;
    }
}









