#!/usr/bin/perl ## ## (Al)ex (Ma)il Reader - Alma ## Web POP3 client ## (c) Alex Bacik 2006 ## use strict; use CGI; use Switch; use Net::POP3; my $url = "https://www.bacik.org/cgi-bin/alma.pl"; my $dir = "/var/www/https.www.bacik.org/htdocs/alma"; my $cgi = new CGI; my $action = $cgi->param('action'); my $pop; print $cgi->header(); &WritePage ("header.txt"); my $username = $cgi->param('username'); my $password = $cgi->param('password'); my $server = $cgi->param('server'); my $msgcount = &LogOn ($username, $password, $server); switch ($action) { case "List" { print "There are $msgcount messages waiting.
\n"; &WritePage ("list_head.txt"); foreach my $msg_id (1 .. $msgcount) { my $header = $pop->top ($msg_id, 0); my ($subject, $from, $status) = &AnalyseHeader ($header); print "
\n";
print "
\n
\n";
foreach (@messages) {
print "Message $_\n";
my $msg = $pop->get($_);
print @$msg;
print "
\n
\n
\n";
}
&WritePage ("list.txt");
}
else {
&WritePage ("login.txt");
}
}
$pop->quit if ($action);
&WritePage ("footer.txt");
exit (1);
#########################
sub Bail {
print "$_[0]\n";
&WritePage ("login.txt");
&WritePage ("footer.txt");
$pop->quit if ($action);
exit (0);
}
sub WritePage {
open (TEMPLATE, "$dir/templates/$_[0]") or &Bail ("Can't open $dir/templates/$_[0]: $!");
local ($/) = undef;
my ($template) = ;
close (TEMPLATE);
$template =~ s/\$URL/$url/g;
$template =~ s/\$USERNAME/$username/g;
$template =~ s/\$PASSWORD/$password/g;
$template =~ s/\$SERVER/$server/g;
print $template;
}
sub LogOn {
return (0) unless ($action);
&Bail ("You need to provide a username!") unless ($_[0]);
&Bail ("You need to provide a password!") unless ($_[1]);
&Bail ("You need to provide a mail server address!") unless ($_[2]);
$pop = Net::POP3->new($_[2], Timeout => 60);
&Bail ("Can't connect to $_[2].") unless ($pop);
my ($msgcount) = $pop->login ($_[0], $_[1]);
&Bail ("No messages waiting") if ($msgcount eq "0E0");
&Bail ("Authentication failure for $_[1] at $_[2]") if ($msgcount == undef);
return ($msgcount);
}
sub AnalyseHeader {
my $header_array_ref = shift;
my $header = join "", @$header_array_ref;
$header =~ s/\</g;
$header =~ s/\>/>/g;
my ($subject) = $header =~ /Subject: (.*)/m;
my ($from ) = $header =~ /From: (.*)/m;
my ($status ) = $header =~ /Status: (.*)/m;
$subject = "No Subject" unless ($subject);
if (defined $status) {
$status = "Unread" if $status eq 'O';
$status = "Read" if $status eq 'R';
$status = "Read" if $status eq 'RO';
$status = "Ne $status = "-";w" if $status eq 'NEW';
$status = "New" if $status eq 'U';
}
else {
$status = "-";
}
return ($subject, $from, $status);
}
#########################