#!/usr/bin/perl
#
# WIDE InternetFAX
#
# file: ifax_mail_recv
#
# Copyright (c) 1998 by Kei Mochida <mochida@ohnolab.org>,
#                       Ohno Laboratory(Tokyo Institute of Technology) and
#                       WIDE Project.
# ALL RIGHTS RESERVED.
#
# If you need fully defined copyright notice, see COPYRIGHT which is 
# included in this package.
# 
# $Id: ifax_mail_recv,v 1.4 1999/03/08 04:22:58 kimoto Exp $

# ---------------------------------------------------------------------------
# DESCRIPTION OF THIS FILE:
# e-mail֥⥸塼
# ܺ٤README.ifax_mail_recv.jp򻲾ȤΤ
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
#  եɤ߹ࡣ
# ---------------------------------------------------------------------------
# ɸեpath/etc/wifax.confĶѿ
# WIFAXCONFPATHͤǾ񤭤Ǥ롣
# եƤ KEY = VAL ηǤФȤꤢʤǤ
# ŪKEYιܤϷƤ
# ޤKEYʸʸ̵뤹롣
# rulefile: dispatcherѤ롼ǡ١Υե̾
# bindir:   ٤ƤΥ⥸塼μ¹Է֤Ƥǥ쥯ȥ̾
# spooldir: סΥǥ쥯ȥ̾
#
$wifax_conf = "/etc/wifax.conf";
$wifax_conf = $ENV{'WIFAXCONFPATH'} if ($ENV{'WIFAXCONFPATH'} ne "");
undef %CONFIG;
if (-e $wifax_conf) {
    open(CONF,$wifax_conf) || die "cannot open $wifax_conf\n";
    while (<CONF>) {
	chop;
	/\S/ || next;
	/\s*\#/ && next;
	/\s*([^= ]+)\s*=\s*(.*)$/ || next;
	$1 =~ tr/A-Z/a-z/;
	$CONFIG{$1}=$2;
    }
    close(CONF);
}

# ---------------------------------------------------------------------------
# ѿDEFAULT
# ---------------------------------------------------------------------------
$default_rulefile = "/var/wifax/etc/wifax.rule";
$default_spooldir = "/var/wifax/spool/";
$default_bindir   = "/usr/wide/wifax/bin";

# ---------------------------------------------------------------------------
# PATH
# ---------------------------------------------------------------------------

$rulefile = ($CONFIG{'rulefile'} eq "") ? 
    $default_rulefile : $CONFIG{'rulefile'};
$spooldir = ($CONFIG{'spooldir'} eq "") ? 
    $default_spooldir : $CONFIG{'spooldir'};
$bindir = ($CONFIG{'bindir'} eq "") ? 
    $default_bindir : $CONFIG{'bindir'};

# ---------------------------------------------------------------------------

#ޥ()
$nkf = "/usr/local/bin/nkf-1.6";
$munpack = "/usr/local/bin/munpack";

#ե()
$queue = $spooldir;
$head = "header";
$seqfile = "seqf";

#ѿ
@arry = "";
$bodyflag = 0;
$mail_data = "";
$threddir ="";
$parted = "NO";
$only_text = "";
################### main routin' #######################################

chdir("$queue");
$threddir = &get_workdir;
print "$num\n";
chdir("$threddir")||die "can't open $threddir\n"; 

open(TMP,"|$nkf -m -j  > $threddir/tmp");
while(<>){
    print NKF;
    print TMP;
    chop;	
    if($_ eq /\S/){
	$bodyflag = 1;	
    }
    if($bodyflag eq 0){
	@arry = (@arry,$_);
	@arry = (@arry,"\n");
    }
}
close(TMP);

$parted = &check_part;




open(HEAD,">$threddir/$head");
print HEAD @arry;
close(HEAD);
if($parted eq "YES"){
system("$munpack -t $threddir/tmp");
}else{
    &make_text;
}


unlink("$threddir/tmp");

&release_workdir;


sub get_workdir{
    open(SEQF,"$queue/$seqfile");
    flock(SEQF,2);
    $num = <SEQF>;
    chop $num;
    flock(SEQF,8);
    close(SEQF);
    open(SEQF,">$queue/$seqfile");
    $num ++;
    print SEQF "$num\n";
    flock(SEQF,8);
    close(SEQF);

    mkdir("$queue/$num",0300)||die "can\'t create spool dir\.\n";
	return "$queue/$num";
 }



sub release_workdir{
    chmod(0755,"$queue/$num");
}



sub check_part{
    local($bool) = "NO";
    open(TARG,"$queue/$num/tmp");
    while(<TARG>){
	if($_  =~ /^Content\-Type\:\ multipart/){
	    
	    $bool = "YES";
	}
    }
    close(TARG);
    
    return "$bool";
}





sub make_text{  
    local($flag) = 0;

    open(TEXT,"|$nkf -j -m  > $queue/$num/text");
    open(MAIL,"$queue/$num/tmp");
    while(<MAIL>){
	chop;	
	if($_ =~ /\S/){
	    $flag = 1;	
	}
	if($flag eq "1"){
	    print TEXT "$_\n";
	    
	}
    }
    close(TEXT);
    close(MAIL);
}

