#!/usr/bin/perl -w
# RSS Feed erzeugen
 use strict;
 use XML::RSS;
 use LWP::UserAgent;
 use HTTP::Request;
 my $rss = new XML::RSS (version => '1.0');

 print "Content-Type: text/xml\n\n";

 my $board = '69'; # c't VDR
 my $name = 'ctVDR';
 my $temp=$ENV{'QUERY_STRING'};
 if ($temp =~ /boardid=(.*)/) {
   $board = $1;
 }


 my $ua = LWP::UserAgent->new;
 my $request = HTTP::Request->new(GET => "http://www.vdrportal.de/board/board.php?boardid=$board&sid=");
 my $response = $ua->request($request);
 my $content = $response->content;

 #print "<!-- Content=$content -->\n";
 if ($content =~ m/<option value=\"$board\" selected=\"selected\">(.*?)<\/option>/gs) { $name = $1; }

 $rss->channel(title          => "VDR Portal $name",
               link           => "http://www.vdrportal.de/board/board.php?boardid=$board&sid=",
               language       => 'de',
               description    => "VDR Portal $name"
               );


 $content =~ s/.*<b>Themen<\/b>(.*?)<input type=\"hidden\".*/$1/gs;

 #print "<!-- Content=$content -->\n";

 #my @items = split(/<tr/,$content);
 #while ($content =~ /<a href=\"(\?msg_id=[^\"]*)\"><img src=\"([^\"]*)\"/gs) {
 while ($content =~ /(<tr align=\"center\">.*?<\/table><\/td>\s*<\/tr>)/gs) {
   my $item = $1;
   #print "<!-- Item=$item -->";
   $item =~ m/<a href=\"(thread.php\?threadid=\d*&amp;sid=)[^\"]*\">(.*?)<\/a>/gs; # cut out session id!
   my $titel = $2;
   my $link = $1 . '&amp;goto=lastpost'; # link to directly jump to last message in thread
   #$link =~ m/threadid=\d*/gs;
   #my $identifier = $1;
   $item =~ m/<a href=\"javascript\:who\(\d*\)\">(\d*)<\/a>/gs;
   my $antworten = $1;
   my $description = '<h2>' . $titel . '</h2><p>Antworten: ' . $antworten;

   #$item =~ m/<a href=\"profile\.php\?userid=\d*&amp;sid=[^\"]*\">(.*?)<\/a>/gs;
   #my $author = $1;

   $rss->add_item(title => $titel,
                link  => $link,
                description => $description
                );
 }
 print $rss->as_string;
 #print $content;
#print '<!-- ' . $temp . ' -->';
