Viewing file: messact.pl (21.45 KB) -rwxr-xr-x Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
#!/usr/bin/perl
###########################################################################
# messact.pl - Perform an action on a message
###########################################################################
require "common.pl";
use Socket;
###########################################################################
# Print the header
###########################################################################
if ($button eq $cancel_button) {
&cancel;
} elsif ($remip ne $fromip) {
&diffip;
}
$ftr = "yes";
$opts = "yes";
###########################################################################
# Get the user's POP information
###########################################################################
if ($sessionid =~ /\@/) {
@pop = split (/ /, $sessionid);
$popaccount = $pop[0];
($popusername, $popserver) = split(/\@/, $popaccount);
} else {
@username = split(/ /, $sessionid);
$username = $username[0];
dbmopen(%serv, $serverdb, 0600) || die "Error opening db $serverdb";
$popserver = $serv{$username};
dbmclose(%serv);
dbmopen(%popname, $popdb, 0600) || die "Error opening db $popdb";
$popusername = $popname{$username};
dbmclose(%popname);
$popaccount = $username . "@" . $domain;
}
###########################################################################
# Get the user's POP password
###########################################################################
dbmopen(%sess, $sessiondb, 0600) || die "Error opening db $sessiondb";
$poppass = unpack("u*", $cryptpass);
dbmclose(%sess);
###########################################################################
# Open the socket to the POP server
###########################################################################
if ($folder eq 'POP'){
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto);
$iaddr = gethostbyname($popserver);
$sin = pack('Sna4x8', AF_INET, 110, $iaddr);
connect(SOCK, $sin);
recv SOCK, $junk, 512, 0;
###########################################################################
# Login to the POP server
###########################################################################
send SOCK, "USER $popusername\r\n", 0;
recv SOCK, $junk, 512, 0;
send SOCK, "PASS $poppass\r\n", 0;
recv SOCK, $junk, 512, 0;
}
if ($button eq $read_button) {
###########################################################################
# Just read a message
###########################################################################
if($attachment eq "NONE"){
&print_header;
if ($btns eq 'top' || $btns eq 'both' ) { &print_options; }
}
###########################################################################
# Get the size of the message
###########################################################################
if($folder eq 'POP'){
send SOCK, "LIST $messages[0]\r\n", 0;
recv SOCK, $message_size, 80, 0;
@mess_size = split (/\s/, $message_size, 3);
$message_size = $mess_size[2];
###########################################################################
# It takes some trickery to read the socket right
###########################################################################
$mess = "";
send SOCK, "RETR $messages[0]\r\n", 0;
while (length($mess) < $message_size){
recv SOCK, $buff, 512, 0;
$mess .= $buff;
}
} else {
###########################################################################
# For messages from folders
###########################################################################
($username, $junk) = split (/ /, $sessionid);
$fold = $folderdir . "/" . $username . "/" . $folder;
dbmopen(%fold, $fold, 0600);
$mess = $fold{$messages[0]};
dbmclose(%fold);
}
###########################################################################
# Clean up the message a bit (remove "+OK" response at beginning and
# "." at end)
###########################################################################
if ($folder eq 'POP'){
@message = split (/\n/, $mess, 2);
$mess = $message[1];
chop ($mess);
chop ($mess);
chop ($mess);
}
###########################################################################
# Some attachment handling
###########################################################################
$boundary = "ATDOT_SAYS_THERE_IS_NO_BOUNDARY_SO_NO_ATTACHMENTS";
$thisisaboundary = 0;
@lines = split(/\n/, $mess);
foreach $line (@lines){
if($line =~ /^Content-Type: MULTIPART.*BOUNDARY/i){
($junk, $boundary) = split(/BOUNDARY\=/i, $line, 2);
$thisisaboundary = 0;
} elsif($thisisaboundary == 1) {
($junk, $boundary) = split(/BOUNDARY\=/i, $line, 2);
$thisisaboundary = 0;
} elsif($line =~ /^Content-Type: MULTIPART.*/i){
$thisisaboundary = 1;
}
}
$boundary =~ s/\"//g;
if($boundary ne "ATDOT_SAYS_THERE_IS_NO_BOUNDARY_SO_NO_ATTACHMENTS"){
($mess, @attach) = split(/\-\-$boundary/, $mess);
}
###########################################################################
# Play nicely with HTML
###########################################################################
$mess =~ s/\&/\&\;/g;
$mess =~ s/\"/\"\;/g;
$mess =~ s/</\<\;/g;
$mess =~ s/>/\>\;/g;
###########################################################################
# Split out the header for displaying short/full header info
###########################################################################
($header, $body) = split(/\r\n\r\n/, $mess, 2);
dbmopen(%head, $headerdb, 0600) || die "Error opening db $headerdb";
$headopt = $head{$username};
dbmclose(%head);
if (!$headopt) { $headopt = 'short'; }
if($headopt eq "short") {
@headlines = split(/\n/, $header);
foreach $line (@headlines){
if($line =~ /^From/i){
$from = "$line\n";
} elsif ($line =~ /^To/i) {
$to = "$line\n";
} elsif ($line =~ /^CC/i) {
$cc = "$line\n";
} elsif ($line =~ /^Subject/i) {
$subject = "$line\n";
} elsif ($line =~ /^Date/i) {
$date = "$line\n";
}
}
$header = $from . $to . $cc . $date . $subject;
}
$mess = $header . "\n\n" . $body;
###########################################################################
# We want this for later
###########################################################################
$reply = $mess;
###########################################################################
# Make URL's into hyperlinks
###########################################################################
$sendto_button = CGI::escape($sendto_button);
$mess =~ s/(http:[\w|\d|_|\-|\/|\.]*)/<A HREF\=\"$1\">$1<\/A>/g;
$mess =~ s/(ftp:[\w|\d|_|\-|\/|\.]*)/<A HREF\=\"$1\">$1<\/A>/g;
$mess =~ s/(news:[\w|\d|_|\-|\/|\.]*)/<A HREF\=\"$1\">$1<\/A>/g;
$mess =~ s/(telnet:[\w|\d|_|\-|\/|\.]*)/<A HREF\=\"$1\">$1<\/A>/g;
$mess =~ s/([\w|\d|_|\-|\/|\.]*\@[\w|\d|_|\-\.]*)/<A HREF\=\"$options_pl?SESSIONID\=$sid\&TO\=$1&BUTTON=$sendto_button\">$1<\/A>/g;
$mess =~ s/\n(From:.*)\n/$1/ig;
$mess =~ s/\n(Subject:.*)\n/$1/ig;
###########################################################################
# Print the message
###########################################################################
if($attachment eq "NONE"){
print $query->startform($method, $messact_pl, $CGI::URL_ENCODED);
print $query->hidden('SESSIONID', $sid);
print $query->hidden('FOLDER', $folder);
print $query->hidden('MESSAGES', $messages[0]);
print $query->hidden('MESS', $reply);
if($btns eq 'top' || $btns eq 'both') {
print "<TABLE WIDTH=\"100%\"><TR>\n";
print "<TD WIDTH=\"25%\" ALIGN=\"LEFT\" VALIGN=\"TOP\">\n";
print $query->submit('BUTTON', $delmsg_button);
print "</TD><TD WIDTH=\"25%\" ALIGN=\"CENTER\" VALIGN=\"TOP\">\n";
print $query->submit('BUTTON', $reply_button);
print "</TD><TD WIDTH=\"25%\" ALIGN=\"CENTER\" VALIGN=\"TOP\">\n";
print $query->submit('BUTTON', $forward_button);
print "</TD><TD WIDTH=\"25%\" ALIGN=\"RIGHT\" VALIGN=\"TOP\">\n";
print $query->submit('BUTTON', $redirect_button);
print "</TD></TR>\n";
unless ($sessionid =~ /\@/){
print "<TR><TD ALIGN=\"LEFT\" WIDTH=\"50%\" COLSPAN=\"2\">";
print $query->submit('BUTTON', $movemsg_button);
print "</TD><TD ALIGN=\"RIGHT\" WIDTH=\"50%\" COLSPAN=\"2\">";
$index = $folderdir . "/" . $username . "-index";
dbmopen(%index, $index, 0600) || die "Error opening db $index";
@index = keys %index;
print $query->popup_menu('NEWFOLDER', \@index, '', \%index);
dbmclose(%index);
print "</TD></TR>";
}
print "</TABLE>\n";
}
print $query->pre($mess);
for($i = 0; $i < @attach; $i++){
$stuff = $attach[$i];
$stuff =~ s/^\n*//g;
($header, $temp) = split(/\r\n\r\n/, $stuff, 2);
if(($header =~ /BASE64/i) || ($header =~ /quoted-printable/i)){
$filename = $unknown_attach_info;
@head = split(/\n/, $header);
foreach (@head){
if ($_ =~ /name/) {
$filename = $_;
($junk, $filename) = split(/=/, $filename);
$filename =~ s/\"//g;
}
}
print $query->a({href=>"$messact_pl\?BUTTON=$read_button&SESSIONID=$sid&MESSAGES=$messages[0]&ATTACHMENT=$i&FOLDER=$folder"}, $attach_input, $filename);
print $query->br;
} else {
print "<PRE>$temp</PRE>";
}
}
if($btns eq 'bottom' || $btns eq 'both') {
print "<TABLE WIDTH=\"100%\"><TR>\n";
print "<TD WIDTH=\"25%\" ALIGN=\"LEFT\" VALIGN=\"TOP\">\n";
print $query->submit('BUTTON', $delmsg_button);
print "</TD><TD WIDTH=\"25%\" ALIGN=\"CENTER\" VALIGN=\"TOP\">\n";
print $query->submit('BUTTON', $reply_button);
print "</TD><TD WIDTH=\"25%\" ALIGN=\"CENTER\" VALIGN=\"TOP\">\n";
print $query->submit('BUTTON', $forward_button);
print "</TD><TD WIDTH=\"25%\" ALIGN=\"RIGHT\" VALIGN=\"TOP\">\n";
print $query->submit('BUTTON', $redirect_button);
print "</TD></TR>\n";
unless ($sessionid =~ /\@/) {
print "<TR><TD ALIGN=\"LEFT\" WIDTH=\"50%\" COLSPAN=\"2\">";
print $query->submit('BUTTON', $movemsg_button);
print "</TD><TD ALIGN=\"RIGHT\" WIDTH=\"50%\" COLSPAN=\"2\">";
$index = $folderdir . "/" . $username . "-index";
dbmopen(%index, $index, 0600) || die "Error opening db $index";
@index = keys %index;
print $query->popup_menu('NEWFOLDER', \@index, '', \%index);
dbmclose(%index);
print "</TD></TR>";
}
print "</TABLE>\n";
}
print $query->endform;
} else {
$stuff = $attach[$attachment];
$stuff =~ s/^\n*//g;
($header, $temp) = split(/\r\n\r\n/, $stuff, 2);
@header = split(/\n/, $header);
for ($i = 0; $i <= $#header; $i++){
if (!($header[$i] =~ /:/)) {
$header[$i - 1] =~ s/\r//g;
$header[$i - 1] .= $header[$i];
$header[$i] = '';
}
}
$header = join("\n", @header);
$header =~ s/\n\n//g;
if($header =~ /BASE64/i){
$temp =~ s/\-\-$boundary\-\-//g;
print "$header\n\n";
print decode_base64($temp);
} elsif ($header =~ /quoted-printable/i){
$disp = decode_qp($temp);
$disp =~ s/\-\-$boundary\-\-//g;
print "$header\n\n";
print $disp;
} else {
print $stuff;
}
$ftr = "no";
}
} elsif ($button eq $delmsg_button || $button eq $delmarked_button) {
###########################################################################
# Ask if they REALLY want to delete the message(s)
###########################################################################
&print_header;
$opts = "no";
print $mess_del_ask_info;
print $query->startform($method, $messact_pl, $CGI::URL_ENCODED);
print $query->hidden('SESSIONID', $sid);
print $query->hidden('FOLDER', $folder);
foreach (@messages) {
print $query->hidden('MESSAGES', $_);
}
print $query->submit('BUTTON', $yes_button);
print $query->submit('BUTTON', $cancel_button);
print $query->endform;
} elsif ($button eq $yes_button) {
###########################################################################
# Just delete the message(s)
###########################################################################
&print_header;
foreach (@messages) {
if ($folder eq 'POP'){
send SOCK, "DELE $_\r\n", 0;
recv SOCK, $junk, 512, 0;
} else {
$fold = $folderdir . "/" . $username . "/" . $folder;
dbmopen(%fold, $fold, 0600);
delete $fold{$_};
$fold{'MESSAGES'} =~ s/\t$_//g;
dbmclose(%fold);
}
}
print $message_delete_info;
} elsif ($button eq $reply_button) {
###########################################################################
# Reply to a message
###########################################################################
&print_header;
$opts = "no";
###########################################################################
# Find who it's from and the subject
###########################################################################
$from = "";
$subject = "";
$resent_from = "";
$sent_to = "";
$reply_to = "";
$sent_cc = "";
@reply = split(/\n/, $mess);
foreach $line (@reply) {
if ($line =~ /^From:/i && $from eq ""){
$from = $line;
} elsif ($line =~ /^Resent-From:/i && $resent_from eq ""){
$resent_from = $line;
} elsif ($line =~ /^To:/i && $sent_to eq ""){
$sent_to = $line;
} elsif ($line =~ /^Reply-To:/i && $reply_to eq ""){
$reply_to = $line;
} elsif ($line =~ /^Cc:/i && $sent_cc eq ""){
$sent_cc = $line;
} elsif ($line =~ /^Subject:/i && $subject eq ""){
$subject = $line;
}
}
###########################################################################
# Get just addresses
###########################################################################
@fromaddress = split (/\s/, $from);
foreach $line (@fromaddress) {
if ($line =~ /\@.*\./) {
$fromaddress = $line;
}
}
$fromaddress =~ s/<//g;
$fromaddress =~ s/>//g;
$fromaddress =~ s/\<\;//ig;
$fromaddress =~ s/\>\;//ig;
@resent_add = split (/\s/, $resent_from);
foreach $line (@resent_add) {
if ($line =~ /\@.*\./) {
$resent_add = $line;
}
}
$resent_add =~ s/<//g;
$resent_add =~ s/>//g;
$resent_add =~ s/\<\;//ig;
$resent_add =~ s/\>\;//ig;
@reply_add= split (/\s/, $reply_to);
foreach $line (@reply_add) {
if ($line =~ /\@.*\./) {
$reply_add = $line;
}
}
$repl_add =~ s/<//g;
$repl_add =~ s/>//g;
$repl_add =~ s/\<\;//ig;
$repl_add =~ s/\>\;//ig;
@to_add= split (/\s/, $sent_to);
foreach $line (@to_add) {
if ($line =~ /\@.*\./) {
$sent_add = $line;
}
}
$sent_add =~ s/<//g;
$sent_add =~ s/>//g;
$sent_add =~ s/\<\;//ig;
$sent_add =~ s/\>\;//ig;
dbmopen(%mail, $maildb, 0600) || die "Error opening db $maildb";
$email = $mail{$username};
dbmclose(%mail);
@cc_add = split (/\s/, $sent_cc);
foreach $line (@cc_add) {
if ($line =~ /\@.*\./) {
$cc_add = $line;
}
}
$cc_add =~ s/<//g;
$cc_add =~ s/>//g;
$cc_add =~ s/\<\;//ig;
$cc_add =~ s/\>\;//ig;
$cc_line = "";
foreach $line ($fromaddress, $resent_add, $reply_to, $sent_to, $sent_cc) {
$line =~ s/(From: )//ig;
$line =~ s/(Reply-To: )//ig;
$line =~ s/(To: )//ig;
$line =~ s/(Resent-From: )//ig;
$line =~ s/(Cc: )//ig;
$line =~ s/\s//g;
if ($line ne "" && $cc_line ne "" && !($cc_line =~ /$line/i) && $line ne $mail && $line ne "$username\@$domain" && $line ne "$popusername\@$domain") {
$line =~ s/\(.*\)//g;
unless ($line eq $email || $line eq "$username\@$domain"){
$cc_line = $cc_line . ", " . $line;
}
} elsif ($line ne "" && $cc_line eq "") {
$cc_line = $line;
}
}
@reply_tos = split (/, /, $cc_line);
###########################################################################
# Remove "Subject: " from subject line
###########################################################################
@subj = split (/\s/, $subject, 2);
$subject = $subj[1];
###########################################################################
# Quote the message in the reply and display the form
###########################################################################
$mess =~ s/\n/\n\>\; /g;
if (!($subject =~ /^re:/i)){
$subject = "Re: " . $subject;
}
$cc_line =~ s/\(.*\)//g;
$cc_line =~ s/<|>|\<\;|\>\;//g;
$to = $cc_line;
$disp_mess = $youwrote_input . "\n\n> " . $mess;
&send_form;
} elsif ($button eq $forward_button) {
###########################################################################
# Forward a message
###########################################################################
&print_header;
$opts = "no";
###########################################################################
# Find the subject
###########################################################################
$subject = "";
@reply = split(/\n/, $mess);
foreach $line (@reply) {
if ($line =~ /^Subject:/i && $subject eq ""){
$subject = $line;
}
}
###########################################################################
# Remove "Subject: " from subject line
###########################################################################
@subj = split (/\s/, $subject, 2);
$subject = $subj[1];
###########################################################################
# Get the user's signature
###########################################################################
if (!($username =~ /\@/)) {
dbmopen(%sig, $sigdb, 0600) || die "Error opening db $sigdb";
$signature = $sig{$username};
dbmclose(%sig);
}
###########################################################################
# Display the form
###########################################################################
if (!($subject =~ /^Fwd:/i)) {
$subject = "Fwd: $subject";
}
$disp_mess = $signature . "\n" . $forward_input . "\n\n" . $mess;
&send_form;
} elsif ($button eq $redirect_button) {
###########################################################################
# Redirect a message
###########################################################################
&print_header;
$opts = "no";
###########################################################################
# Find who it's from and the subject
###########################################################################
$from = "";
$subject = "";
@reply = split(/\n/, $mess);
foreach $line (@reply) {
if ($line =~ /^From:/i && $from eq ""){
$from = $line;
} elsif ($line =~ /^Subject:/i && $subject eq ""){
$subject = $line;
chop $subject;
}
}
###########################################################################
# Get just the return address from the From: line
###########################################################################
@fromaddress = split (/\s/, $from);
foreach $line (@fromaddress) {
if ($line =~ /\@.*\./) {
$fromaddress = $line;
}
}
$fromaddress =~ s/<|>|\<\;|\>\;//g;
###########################################################################
# Remove "Subject: " from subject line
###########################################################################
@subj = split (/\s/, $subject, 2);
$subject = $subj[1];
$mess =~ s/</\<\;/g;
$mess =~ s/>/\>\;/g;
$mess =~ s/"/\"\;/g;
###########################################################################
# Display the form
###########################################################################
$message = $mess;
$hide_mess = 'yes';
&send_form;
} elsif(($button eq $movemarked_button) || ($button eq $movemsg_button)) {
###########################################################################
# Move the e-mail(s) to a different folder
###########################################################################
&print_header;
$foldir = $folderdir . "/" . $username;
mkdir $foldir, 0700;
$fold = $foldir . "/" . $newfolder;
dbmopen(%fold, $fold, 0600) || die "Error opening db $fold";
###########################################################################
# Get the message and delete it from the server
###########################################################################
@messagelist = split(/\t/, $fold{'MESSAGES'});
$nextmess = greatest(@messagelist);
foreach $mess_no (@messages) {
if($folder eq 'POP'){
print "Folder is inbox<br>";
send SOCK, "LIST $mess_no\r\n", 0;
recv SOCK, $message_size, 80, 0;
@mess_size = split (/\s/, $message_size, 3);
$message_size = $mess_size[2];
$mess = "";
send SOCK, "RETR $mess_no\r\n", 0;
while (length($mess) < $message_size){
recv SOCK, $buff, 512, 0;
$mess .= $buff;
}
@message = split (/\n/, $mess, 2);
$mess = $message[1];
chop ($mess);
chop ($mess);
chop ($mess);
send SOCK, "DELE $mess_no\r\n", 0;
recv SOCK, $junk, 512, 0;
} else {
$oldfold = $foldir . "/" . $folder;
dbmopen(%oldfold, $oldfold, 0600) || die "Error opening db $oldfold";
$mess = $oldfold{$mess_no};
delete $oldfold{$mess_no};
$oldfold{'MESSAGES'} =~ s/\t$mess_no//;
$oldfolf{'MESSAGES'} =~ s/\t$//g;
dbmclose(%oldfold);
}
$fold{++$nextmess} = $mess;
$fold{'MESSAGES'} .= "\t$nextmess";
}
print $moved_info;
dbmclose(%fold);
}
###########################################################################
# Close the socket
###########################################################################
if ($folder eq 'POP'){
send SOCK, "QUIT\r\n", 0;
close(SOCK);
}
###########################################################################
# Done
###########################################################################
&update_sess;
if ($use_abook eq "Yes") {
dbmclose(%addbook);
}
if ($ftr eq "yes" && $opts eq "yes"){
if ($btns eq 'bottom' || $btns eq 'both') { &print_options; }
}
if ($ftr eq "yes"){
&print_footer;
}
|