#!/usr/local/bin/perl
$tmpfile = "/var/tmp/htmlchopper.$$";
#$tmpfile = "/home/nakamura/out.txt";
$type = "";
$attach = 0;
$flag = 0;
$header='';
$body = '';
$in_head = 1;
while() {
s/\r\n/\n/g; s/\r/\n/g;
if($in_head){
if(/^$/){$in_head = 0;}
if(/^Content-Type:\s?multipart\/alternative/i){$type = "htmltext";$flag=1;}
if(/^Content-Type:\s?multipart\/related/i){$attach = 1;$flag=1;}
if(/^Content-Type:\s?multipart\/mixed/i){$attach = 1;$flag=1;}
if(($flag == 1) && (/^\s+/)){
$boundary = $_; chop($boundary);
$boundary =~ s/boundary\s?=\s?"([^"]+)"/\1/;
$boundary =~ s/\s+//g;
$flag = 0;
}
$header .=$_;
}
if(!$in_head){
if(($attach == 1) && (/^Content-Type:\s?text\/html/i)){$type = "htmlattach";}
if(/^\s+boundary/){
$boundary2 = $_; chop($boundary2);
$boundary2 =~ s/boundary\s?=\s?"([^"]+)"/\1/;
$boundary2 =~ s/\s+//g;
}
s/\r\n/\n/g;
if(!/^--$/){$body .= $_;}
# $body .= $_;
}
}
if($type eq ""){
open (TFILE, ">$tmpfile") || die;
print TFILE $header;
print TFILE $body;
close(TFILE);
system("/bin/cat $tmpfile");
unlink($tmpfile);
exit;
}
if($type eq "htmltext"){
$boundary = "--$boundary";
($messg,$text,$html) = split(/$boundary/,$body);
open (TFILE, ">$tmpfile") || die;
print TFILE $header;
print TFILE "$boundary";
print TFILE $text;
print TFILE "\n$boundary";
close(TFILE);
system("/bin/cat $tmpfile");
unlink($tmpfile);
exit;
}
if($type eq "htmlattach"){
$boundary = "--$boundary";
@pics = split(/$boundary/,$body);
$messg = shift(@pics);
$descript = shift(@pics);
$boundary2 = "--$boundary2";
($dummy,$text,$html) = split(/$boundary2/,$descript);
open (TFILE, ">$tmpfile") || die;
print TFILE $header;
print TFILE "$boundary";
print TFILE "$text\n";
print TFILE "$boundary";
foreach (@pics){
print TFILE $_;
print TFILE "$boundary";
}
close(TFILE);
system("/bin/cat $tmpfile");
unlink($tmpfile);
exit;
}
exit;