|
From: Egon W. <eg...@us...> - 2002-07-29 10:54:01
|
Update of /cvsroot/woc/woc/cgi-bin/usenet
In directory usw-pr-cvs1:/tmp/cvs-serv13954/usenet
Added Files:
Article.pm webinterface.pl
Log Message:
Oops, wrong directory.
--- NEW FILE: Article.pm ---
package Article;
use strict;
sub new {
my $self = {};
$self->{NO} = undef;
$self->{ID} = undef;
$self->{SUBJECT} = undef;
$self->{FROM} = undef;
$self->{DATE} = undef;
$self->{REFS} = undef;
$self->{FUPS} = undef;
$self->{THEME} = undef;
$self->{BODY} = [];
bless($self);
return $self;
}
sub addFup {
#my $self = shift;
#my $fup = shift;
$_[0]->{FUPS} .= " $_[1]";
}
sub theme {
#my $self = shift;
if ($_[1]) { $_[0]->{THEME} = $_[1] }; #= shift };
return $_[0]->{THEME};
}
sub fups {
#my $self = shift;
if ($_[1]) { $_[0]->{FUPS} = $_[1] }; #= shift };
return $_[0]->{FUPS};
}
sub refs {
#my $self = shift;
if ($_[1]) { $_[0]->{REFS} = $_[1] }; #= shift };
return $_[0]->{REFS};
}
sub body {
my $self = shift;
if (@_) { @{$self->{BODY}} = @_ };
return @{$self->{BODY}};
}
sub from {
#my $self = shift;
if ($_[1]) { $_[0]->{FROM} = $_[1] }; #= shift };
return $_[0]->{FROM};
}
sub no {
#my $self = shift;
if ($_[1]) { $_[0]->{NO} = $_[1] }; #= shift };
return $_[0]->{NO};
}
sub date {
#my $self = shift;
if ($_[1]) { $_[0]->{DATE} = $_[1] }; #= shift };
return $_[0]->{DATE};
}
sub id {
#my $self = shift;
if ($_[1]) { $_[0]->{ID} = $_[1] }; #= shift };
return $_[0]->{ID};
}
sub subject {
#my $self = shift;
if ($_[1]) { $_[0]->{SUBJECT} = $_[1] }; #= shift };
return $_[0]->{SUBJECT};
}
1;
--- NEW FILE: webinterface.pl ---
#!/usr/local/bin/perl -w
use strict;
use diagnostics;
use News::NNTPClient;
use Article;
use CGI;
# consts
my $url = "/cgi-bin-woc/usenet/webinterface.pl";
my $server = "nntp-srv.sci.kun.nl";
my $group = "nl.wetenschap";
my $cachedir = "/tmp/woc/usenet";
my @themes = ("chemische verbindingen", "meer info",
"synthese", "chemische software", "anders");
# read params
my $query = new CGI;
my $message = int($query->param("message") || 0);
my $command = $query->param("command") || "overzicht";
my $showheader = int($query->param("header") || undef);
my $theme = $query->param("theme") || undef;
my $tvalue = $query->param("threaded");
my $threaded = 1;
$threaded = int($tvalue) if (defined $tvalue);
# instantiate client
my $client = new News::NNTPClient($server);
my ($first, $last) = ($client->group($group));
my $status = "";
my $content = "";
my %articles = ();
my %index = ();
my %printed = ();
my %fups = ();
if ($command eq "search") {
my $searched = $query->param("query") || "";
my $field = $query->param("field") || "alle";
if ($searched ne "") {
# parse articles
for (my $i = $first; $i <= $last; $i++) {
my $art = getArticleByNo($i);
if (defined $art) {
$articles{$art->id()} = $art;
$index{int($i)} = $art->id();
if (defined $art->refs()) {
foreach my $ref (split(' ', $art->refs())) {
my $nart = $articles{$ref};
if (defined $nart) {
$nart->addFup($art->id());
}
}
}
}
}
# do search
$content .= "<h3>Gezocht naar: $searched</h3>$/<ul>$/";
foreach my $artid (reverse sort keys %index) {
my $index = $index{int($artid)};
my $art = $articles{$index};
my $match = 0;
if (($field eq "onderwerp" || $field eq "alle") && (defined (my $subject = $art->subject()))) {
$match = 1 if ($subject =~ /$searched/ig);
}
if (!$match && ($field eq "schrijver" || $field eq "alle") && (defined (my $author = $art->from()))) {
$match = 1 if ($author =~ /$searched/ig);
}
if (!$match && ($field eq "datum" || $field eq "alle") && (defined (my $date = $art->date()))) {
$match = 1 if ($date =~ /$searched/ig);
}
if (!$match && ($field eq "bericht" || $field eq "alle") && (defined (my @text = $art->body()))) {
foreach my $line (@text) {
#$content .= $line;
$match = 1 if ($line =~ /$searched/ig);
}
}
if ($match == 1) {
$content .= printArticle($index);
}
}
$content .= "</ul>$/";
} else {
$content .= "<H3>Zoeken:</h3>$/";
$content .= "<p><FORM METHOD=\"POST\" ACTION=\"$url\">$/$/";
$content .= " <INPUT TYPE=\"Hidden\" NAME=\"command\" VALUE=\"search\">$/";
$content .= " <TABLE width=\"100%\">$/";
$content .= " <TR>$/";
$content .= " <TD>Veld:</TD>$/";
$content .= " <TD><SELECT name=\"field\" size=\"1\">$/";
$content .= " <OPTION value=\"onderwerp\">onderwerp</OPTION>$/";
$content .= " <OPTION value=\"schrijver\">schrijver</OPTION>$/";
$content .= " <OPTION value=\"bericht\">bericht</OPTION>$/";
$content .= " <OPTION value=\"datum\">datum</OPTION>$/";
$content .= " <OPTION SELECTED value=\"alle\">alle</OPTION>$/";
$content .= " </SELECT></TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Zoekwoord:</TD>$/";
$content .= " <TD><INPUT TYPE=\"Text\" NAME=\"query\" SIZE=\"50\" VALUE=\"\"></TD>$/";
$content .= " </TR>$/";
$content .= " </TABLE>$/";
$content .= " <INPUT TYPE=\"Submit\" VALUE=\"Zoeken\">$/";
$content .= " </FORM>$/";
}
$content .= "<center>";
$content .= "[<a href=\"$url\">overzicht</a>] $/";
$content .= "[<a href=\"$url?command=search\">zoeken</a>] $/" if ($searched ne "");
$content .= "</center>";
} elsif (($command eq "postquest") && $client->postok()) {
my $name = $query->param("name") || "";
my $email = $query->param("email") || "";
my $qsubject = $query->param("qsubject") || "";
my $react = $query->param("content") || "";
my $theme = $query->param("theme") || "Anders";
# email should be checked!
# post message
my @header = ("Newsgroups: $group", "Subject: $qsubject", "From: $email", "X-Newsreader: \"WOC's UseNet WebInterface: http://www.sci.kun.nl/woc/\"", "X-WOC-Theme: $theme");
my @reactbody = ("$react");
my $status = $client->post(@header, "", @reactbody);
# give response
if ($status) {
$content .= "<h3>Verstuurd:</h3>$/<P>$/";
$content .= " <TABLE width=\"100%\">$/";
$content .= " <TR>$/";
$content .= " <TD width=\"25%\">Naam:</TD>$/";
$content .= " <TD>$name</TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Email:</TD>$/";
$content .= " <TD>$email</TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Onderwerp:</TD>$/";
$content .= " <TD>$qsubject</TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Vraag:</TD>$/";
$content .= " <TD>$react</TD>$/";
$content .= " </TR>$/";
$content .= " </TABLE>$/";
} else {
$content .= "<H3>Vraag niet verstuurd:</h3>$/";
$content .= "<p>" . $client->message() . "$/";
}
$content .= "<center>";
$content .= "[<a href=\"$url\">overzicht</a>] $/";
$content .= "</center>";
} elsif (($command eq "postreact") && $message && $client->postok()) {
my $art = getArticleByNo($message);
my $name = $query->param("name") || "";
my $email = $query->param("email") || "";
my $react = $query->param("content") || "";
# email should be checked!
# post message
my @header = ();
if (defined $art->theme()) {
@header = ("Newsgroups: $group", "Subject: Re: " . $art->subject(), "From: $email ($name)", "X-Newsreader: \"WOC's UseNet WebInterface: http://www.sci.kun.nl/woc/\"", "References: " . $art->refs() . " " . $art->id(), "X-WOC-Theme: " . $art->theme());
} else {
@header = ("Newsgroups: $group", "Subject: Re: " . $art->subject(), "From: $email ($name)", "X-Newsreader: \"WOC's UseNet WebInterface: http://www.sci.kun.nl/woc/\"", "References: " . $art->refs() . " " . $art->id());
}
my @reactbody = ("$react");
my $status = $client->post(@header, "", @reactbody);
# give response
if ($status) {
$content .= "<h3>Verstuurd:</h3>$/<P>$/";
$content .= " <TABLE width=\"100%\">$/";
$content .= " <TR>$/";
$content .= " <TD width=\"25%\">Naam:</TD>$/";
$content .= " <TD>$name</TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Email:</TD>$/";
$content .= " <TD>$email</TD>$/";
$content .= " </TR>$/";
if (defined $art->theme()) {
$content .= " <TR>$/";
$content .= " <TD>Thema:</TD>$/";
$content .= " <TD>" . $art->theme() . "</TD>$/";
$content .= " </TR>$/";
}
$content .= " <TR>$/";
$content .= " <TD>Onderwerp:</TD>$/";
$content .= " <TD>Re: " . $art->subject() . "</TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Reactie:</TD>$/";
$content .= " <TD>$react</TD>$/";
$content .= " </TR>$/";
$content .= " </TABLE>$/";
} else {
$content .= "<H3>Bericht niet verstuurd:</h3>$/";
$content .= "<p>" . $client->message() . "$/";
}
$content .= "<center>[<a href=\"$url\">overzicht</a>]</center>";
} elsif ($command eq "getquest") {
$content .= "<h3>Post vraag:</h3>$/";
$content .= "<p><FORM METHOD=\"POST\" ACTION=\"$url\">$/$/";
$content .= " <INPUT TYPE=\"Hidden\" NAME=\"command\" VALUE=\"postquest\">$/";
$content .= " <TABLE width=\"100%\">$/";
$content .= " <TR>$/";
$content .= " <TD>Naam:</TD>$/";
$content .= " <TD><INPUT TYPE=\"Text\" NAME=\"name\" SIZE=\"50\" VALUE=\"\"></TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Email:</TD>$/";
$content .= " <TD><INPUT TYPE=\"Text\" NAME=\"email\" SIZE=\"50\" VALUE=\"\"></TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Thema:</TD>$/";
$content .= " <TD><SELECT name=\"theme\" size=\"1\">$/";
foreach my $atheme (@themes) {
$content .= " <OPTION value=\"$atheme\">$atheme</OPTION>$/";
}
$content .= " </SELECT></TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Onderwerp:</TD>$/";
$content .= " <TD><INPUT TYPE=\"Text\" NAME=\"qsubject\" SIZE=\"50\" VALUE=\"\"></TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Vraag:</TD>$/";
$content .= " <TD><TEXTAREA NAME=\"content\" COLS=\"50\" ROWS=\"10\"></TEXTAREA></TD>$/";
$content .= " </TR>$/";
$content .= " </TABLE>$/";
$content .= " <INPUT TYPE=\"Submit\" VALUE=\"Verstuur\">$/";
$content .= " <INPUT TYPE=\"Reset\" VALUE=\"Wis\">$/";
$content .= " </FORM>$/";
$content .= "<center>[<a href=\"$url\">overzicht</a>]</center>";
} elsif (($command eq "getreact") && $message) {
my $art = getArticleByNo($message);
$content .= "<h3>Reactie op: " . $art->subject() . "</h3>$/";
$content .= "<p>" . addBreaks($art->body()) . "</p><hr />$/";
$content .= "<h3>Reactie:</h3>$/";
$content .= "<p><FORM METHOD=\"POST\" ACTION=\"$url\">$/$/";
$content .= " <INPUT TYPE=\"Hidden\" NAME=\"command\" VALUE=\"postreact\">$/";
$content .= " <INPUT TYPE=\"Hidden\" NAME=\"message\" VALUE=\"$message\">$/";
$content .= " <TABLE width=\"100%\">$/";
$content .= " <TR>$/";
$content .= " <TD>Naam:</TD>$/";
$content .= " <TD><INPUT TYPE=\"Text\" NAME=\"name\" SIZE=\"50\" VALUE=\"\"></TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Email:</TD>$/";
$content .= " <TD><INPUT TYPE=\"Text\" NAME=\"email\" SIZE=\"50\" VALUE=\"\"></TD>$/";
$content .= " </TR>$/";
if (defined $art->theme()) {
$content .= " <TR>$/";
$content .= " <TD>Thema:</TD>$/";
$content .= " <TD>" . $art->theme() . "</TD>$/";
$content .= " </TR>$/";
}
$content .= " <TR>$/";
$content .= " <TD>Onderwerp:</TD>$/";
$content .= " <TD>Re: " . $art->subject() . "</TD>$/";
$content .= " </TR>$/";
$content .= " <TR>$/";
$content .= " <TD>Reactie:</TD>$/";
$content .= " <TD><TEXTAREA NAME=\"content\" COLS=\"50\" ROWS=\"10\"></TEXTAREA></TD>$/";
$content .= " </TR>$/";
$content .= " </TABLE>$/";
$content .= " <INPUT TYPE=\"Submit\" VALUE=\"Verstuur\">$/";
$content .= " <INPUT TYPE=\"Reset\" VALUE=\"Wis\">$/";
$content .= " </FORM>$/";
$content .= "<center>[<a href=\"$url\">overzicht</a>]</center>";
} elsif ($message) {
my $art = getArticleByNo($message);
$content .= "<h3>" . $art->subject() . "</h3>\n";
if ($showheader) {
$content .= "<b>$/";
$content .= addBreaks($client->head($message));
$content .= "</b>$/";
}
$content .= "<p>" . addBreaks($art->body());
$content .= "<center>[<a href=\"$url\">overzicht</a>] ";
$content .= "[<a href=\"$url?command=search\">zoeken</a>] ";
if ($showheader) {
$content .= "[<a href=\"$url?message=$message\">geen header</a>] ";
} else {
$content .= "[<a href=\"$url?message=$message&header=1\">header</a>] ";
}
if ($client->postok()) {
$content .= "[<a href=\"$url?command=getreact&message=$message\">reageer</a>]</center>";
}
} else {
my $max = $query->param("max") || ($last + 1);
my $oldmax = $query->param("max") || 0;
my $needed = 10;
$needed = 20 if (!$threaded);
my $counter = $needed;
# parse articles
my $i = $max - 1;
while ($i > $first && $counter) {
my $art = getArticleByNo($i);
if (defined $art) {
my $id = $art->id();
$articles{$id} = $art;
$index{$i} = $id;
if (defined $art->refs()) {
foreach my $ref (split(' ', $art->refs())) {
if ($fups{$ref}) {
$fups{$ref} = "$id " . $fups{$ref};
} else {
$fups{$ref} = $id;
}
}
} else {
if ($theme) {
$counter-- if ($art->theme() eq $theme);
} else {
$counter--;
}
}
}
$i--;
}
# possibly show theme's
my $link = "";
my $linktext = "";
if (defined $theme) {
$content .= "<center>$/";
my $atheme = $theme;
$atheme = "Alle" if ($theme eq "*");
$content .= "<h3>$atheme</h3>$/";
foreach my $atheme (@themes) {
$link = "$url?command=overzicht&theme=$atheme";
$link =~ s/\ /\%20/g;
if ($threaded) {
$link .= "&threaded=1";
} else {
$link .= "&threaded=0";
}
$content .= "[<a href=\"$link\">$atheme</a>] ";
}
$content .= "[<a href=\"$url?command=overzicht&theme=*\">alle</a>] ";
$content .= "</center><p>$/";
}
# show last 15 articles
$counter = $needed;
$content .= "<ul>$/";
foreach my $artid (reverse sort keys %index) {
if ($counter > 0) {
my $index = $index{int($artid)};
my $art = $articles{$index};
my $print = 1;
if (defined $theme) {
$print = 0;
if (defined (my $arttheme = $art->theme())) {
if ($theme eq "*" || $theme eq $arttheme) {
$print = 1;
}
}
}
if ($print) {
if ($threaded && (!defined $art->refs())) {
$content .= printThreadedArticle($index);
$counter--;
} elsif (!$threaded) {
$content .= printArticle($index);
$counter--;
}
}
$max = $artid;
}
}
$content .= "</ul>$/";
$content .= "<p><center>";
$content .= "[<a href=\"$url?command=getquest\">stel vraag</a>] ";
$content .= "[<a href=\"$url?command=search\">zoeken</a>] ";
$link = "";
if ($max > $first) {
$link = "$url?command=overzicht&max=$max";
if ($threaded) {
$link .= "&threaded=1";
} else {
$link .= "&threaded=0";
}
$content .= "[<a href=\"$link\">oudere berichten</a>] ";
}
$link = "$url?command=overzicht";
$linktext = "threading";
if ($threaded) {
$linktext = "geen $linktext";
$link .= "&threaded=0";
} else {
$link .= "&threaded=1";
}
if ($theme) {
$link .= "&theme=$theme";
}
$link .= "&max=$oldmax" if ($oldmax);
$content .= "[<a href=\"$link\">$linktext</a>] ";
$link = "$url?command=overzicht";
if ($threaded) {
$link .= "&threaded=1";
} else {
$link .= "&threaded=0";
}
if ($theme) {
$linktext = "geen thema's";
} else {
$linktext = "thema's";
$link .= "&theme=*";
}
$content .= "[<a href=\"$link\">$linktext</a>] ";
$content .= "</center>";
}
# print output
print <<END;
Content-type: text/html
<html>
<head>
<title>UseNet Interface: $group</title>
</head>
<body bgcolor="white">
<h1>UseNet Interface: $group</h1><hr />
$status
<p>$content</p><hr />
</body>
</html>
END
sub getArticleByNo() {
my ($no) = shift @_;
my $file = "$cachedir/$group.$no";
$status = "<!-- ? files in cache ($cachedir)-->" if ($status eq "");
my @article = ();
if (-e $file) {
open(CACHEFILE, "<$file");
@article = <CACHEFILE>;
close(CACHEFILE);
} else {
@article = $client->article($no);
return undef if (!@article);
#test if cache exists
`chmod 777 $cachedir`;
mkdir $cachedir, 777 if (!-e $cachedir);
#save to cache
open(CACHEFILE, ">$file");
foreach my $line (@article) {
print CACHEFILE $line;
}
close(CACHEFILE);
}
# parse article
my $art = Article->new();
$art->no($no);
my @body = ();
my $isbody = 0;
foreach my $line (@article) {
if ($isbody) {
push(@body, $line);
} elsif ($line =~ /Subject:\s*(.*)/) {
$art->subject($1);
} elsif ($line =~ /Date:\s*(.*)/) {
$art->date($1);
} elsif ($line =~ /From:\s*(.*)/) {
$art->from($1);
} elsif ($line =~ /References:\s*(.*)/) {
$art->refs($1);
} elsif ($line =~ /Message-ID:\s*(.*)/) {
$art->id($1);
} elsif ($line =~ /X-WOC-Theme:\s*(.*)/) {
$art->theme($1);
} elsif ($line =~ /^(\n|\r)/) {
$isbody = 1;
}
}
$art->body(@body);
return $art;
}
sub addBreaks() {
my @body = @_;
my $result = "";
foreach my $line (@body) {
$line =~ s/\n/<br>/g;
$result .= "$line\n";
}
return $result;
}
sub printArticle() {
my $artid = shift @_;
my $art = $articles{$artid};
my $content = "";
if (!defined $printed{$artid} && (defined $art)) {
$content .= "<li><a href=\"$url?message=" . $art->no() . "\">" . $art->subject() . "</a> " . $art->from() . " (<i>" . $art->date() . "</i>)$/";
$printed{$artid} = "yes";
};
return $content;
}
sub printThreadedArticle() {
my $artid = shift @_;
my $art = $articles{$artid};
my $content = "";
if (defined $art) {
if (!defined $printed{$artid}) {
$content .= "<li><a href=\"$url?message=" . $art->no() . "\">" . $art->subject() . "</a> " . $art->from() . " (<i>" . $art->date() . "</i>)$/";
$printed{$artid} = "yes";
if (defined $fups{$artid}) {
$content .= "<ul>$/";
foreach my $fup (split(' ', $fups{$artid})) {
$content .= printThreadedArticle($fup);
}
$content .= "</ul>$/";
}
}
} else {
$content .= "<li>article (" . $artid. ") not found$/";
}
return $content;
}
|