#!/usr/bin/perl
use MIME::QuotedPrint;
# 基本的には、下記の★もしくは☆を書き換えれば動くことが期待される。
$forwardto = 'hoge@pdx.ne.jp'; # 転送先アドレス★
$pg_address = 'mfd@'; #メール受信用アドレスの一部。ループ処理用なので。★
$sendmail = "/usr/sbin/sendmail"; # sendmail プログラムの位置☆
#変数初期化
$headers     = "";
$honbun      = "";
$qp          = 0;
$h_lines     = 0;

#メインルーチン
&make_mail;
&mail_forward;

#サブルーチン　メール送信
sub mail_forward {
    if ($cte == 1) {
	$honbun = decode_qp($honbun);
    }
    elsif ($cte == 2) {
	$honbun = decode_base64($honbun);
    }
    open(OUT,"| $sendmail $forwardto") || die "Cannnot open $sendmail";
    print OUT $headers;
    print OUT "\n";
    print OUT $honbun;
    close(OUT);
}

#サブルーチン　メール加工
sub make_mail {
    open(STDIN);
    $mainheaderf = 1;                   #ヘッダーフラグセット
    $mixf        = 0;                   #添付ファイル有無フラグクリア
    $headf       = 0;                   #途中のヘッダーフラグ
    $outf        = 0;                   #出力フラグ
    $h_c_t       = 0;
    while(<STDIN>){
        if($mainheaderf){               #メインのヘッダー処理
            if(/^$/){                   #改行だけの行
                $mainheaderf = 0;       #次からは本文
            }
            elsif(/^Return-Path:.*$pg_address/i){  #ループになったら強制終了
                die "Mail loop.";
	    }
            elsif(/^Content-Type:/i){       #コンテントタイプの書き換え
                $headers .= "Content-Type: text/plain; charset=\"ISO-2022-JP\"\n";
		$headers .= "Content-Transfer-Encoding: 7bit\n";
		$h_c_t = 1;
                if(/multipart/i || /mix/i){  #なら
                    $mixf = 1;
                }
		if(/boundary=/i){
                    ($temp,$boundary) = split("=",$_,2);
                    $boundary =~ s/\"//g;
		}
            }
	    elsif(/boundary=/i && $h_c_t <= 1){
		$h_c_t++;
                ($temp,$boundary) = split("=",$_,2);
                $boundary =~ s/\"//g;
	    }
            else{
                $headers .= $_;
            }
        }
	else{                          #本文処理
            if($mixf){                  #添付ファイルありの場合
                if(/$boundary/i){    #区切り文字列
                    $headf = 1;         #ヘッダーの開始
                    $outf  = 0;         #出力フラグクリア
                }
                elsif($headf){             #ヘッダー処理
                    if(/^Content-Type:/i){   #Content-Typeの行
                        if(/text\/plain/i || /message/i){  #text/plainなら
                            $outf = 1;  #出力フラグオン
                        }else{          #text/plainじゃないなら
                            $outf = 0;  #出力しない
                        }
                    }
                    elsif(/^Content-Transfer-Encoding:/i && $outf == 1){   #Content-Typeの行
                        if(/quoted-printable/i){  #quoted-printableなら
                            $cte = 1;  #出力フラグオン
                        }
                        if(/base64/i){  #base64なら
                            $cte = 2;  #出力フラグオン
                        }
                    }
                    elsif(/^$/){           #改行だけの行なら
                        $headf = 0;     #ヘッダー部終了
                    }
                }
		else{                  #本文処理
                    if($outf){          #出力して良いなら
			if($h_lines < 500 && $_ !~ /^>/){
                            $honbun .= $_;     #一時変数に格納
			    $h_lines++;
			}
                    }
                }
            }
	    else{                      #添付ファイルなしの場合
		if($h_lines < 500 && $_ !~ /^>/){
                    $honbun .= $_;     #一時ファイルに格納
		    $h_lines++;
		}
            }
        }
    }
    if($mixf){                  #添付ファイルありの場合
        $honbun .= "\nfile deleted\n";
    }
    close(STDIN);
}

