;;### 入站(本地用户)用标准过滤器
use Encode qw/ encode decode /;
$cdir = '/home/jdmail/jdmail/Apache2.2/htdocs/JDWA';
;;###
require "$cdir/cgi/filters.common.cgi";
$cttl = 60;		# 宏试行次数
@TO = split(/\@/,$xto);
$target = "$mailroot/domains/$TO[1]/$TO[0]";
&get_config();

open(LOG,">>/tmp/jjjjjjj");
print LOG "$cfg{'NetBIOSName'}\n";
print LOG "$ipmsg\n";
# LAN内通知(for User)
if ($cfg{'NetBIOSName'}){&netsend();}
## -------
# 名单处理器处理 
if ($robot){
	my ($tmp) = &mod_str("$robot");
	if ($to =~ /^$tmp$/i){
		if ($cfg{'Log'}){&logging();}
		require "$cdir/cgi/filters.robot.cgi";
	}
}

# 自动删除(for User)
if ((-f "$mailroot/domains/$TO[1]/$autodelete") || 
	($cfg{'AutoDelete'} && ($cfg{'AutoDeletePeriod'} || $cfg{'AutoDeleteQuota'}))){&autodelete();}

# 垃圾邮件过滤器(for User/ML)
if ($SpamScan){
	%spam = &get_spam_conf();
	while (my($key,$val) = each %spam){$$key = $val;}
	if ((!$SpamScan_SmtpAuth || $userauth eq '-') && $SpamScan_in && 
		!-f "$target/$jdwa/.no$spamscan"){
		&spamfilter('in',$SpamWords);
	}
}

# 病毒扫描 (for User/ML)
if ($scanning && -s "$scanner" && !$scanonline && !-f "$target/$jdwa/.no$avscan" && 
	(-f "$mailroot/domains/$avscan" || -f "$mailroot/domains/$TO[1]/$avscan" 
	|| -f "$target/$jdwa/$avscan")){
	if ($scansize && $msize > $scansize){} else {&vscan('in');}
}

# 空邮件订阅/退订(for ML)
if ($cfg{'Subscribe'} && $x{'ReceiveEnable'} && !$x{'ClosedML'} && $msize < 5 && 
	-f "$target/mlusers.tab"){&subscribe2();}

# Date:字段检查(for User/ML)
if ($cfg{'DateCheck'} || $datecheck){&datecheck();}

# From:标记的固定(for ML)
if ($cfg{'ForceFrom'}){&forcefrom();}

# 受信制限(for User)
if ($cfg{'Allow'} && ($from || $xfrom)){&allow();}

# 受信拒否(for User/ML)
if ($cfg{'Deny'} && ($from || $xfrom)){&deny();}

# 接收邮件的尺寸检查(for ML)
if ($cfg{'MessageSize'}){&sizecheck();}

# 自动应答(for User)
if ($cfg{'AutoReply'} && $from !~ /^$xto0$/i){&autoreply();}

# 通知邮件(邮件到达的通知, for User)
if ($from0 && $cfg{'NotifyFrom'} && $cfg{'NotifyTo'} =~ /^$emreg$/){
	my $op1;
	$op1 = 1 if $cfg{'NotifyFrom'} =~ /_Header_Only_/;
	
	if ($cfg{'NotifyFrom'} =~ /\*/){&notify($op1);}
	elsif ($cfg{'NotifyFrom'}){
		my $check;
		foreach (split(/[\s,]+/,$cfg{'NotifyFrom'})){
			if (/^$emreg$/ && /^($from0|$xfrom0)$/i){
				$check = 1;
				last;
			}
		}
		&notify($op1) if $check;
	}
}

# Message-id:的添加(for User/ML)
if ($cfg{'MessageID'}){&msgid();}

# Subject:标记的添加(for ML)
if ($cfg{'Tag'} =~ /[^\s]/){&tag();}

# 邮件摘要生成(for ML)
if ($cfg{'Digest'} && -f "$target/mlusers.tab"){&digest();}

# Reply-To:标记的添加(for ML)
if (!$x{'UseReplyTo'} && $cfg{'ReplyTo'} =~ /^$emreg$/){&replyto();}

# 附件的删除(for User/ML)
if (!-f "$target/$jdwa/.no$attachmentscan"){
	if ($RemoveAttachment && !$RemoveAttachmentOnline && $RemoveAttachment_in){&remove_attachments('offline');}
	elsif ($cfg{'Attachment'} && !$RemoveAttachment){&remove_attachments('in');}
}

# 页眉/页脚的添加(for ML)
if ($cfg{'Header'} || $cfg{'Footer'}){&header();}


# JDWM过滤(for User)
if ($cfg{'Distribute'} && $distribute){&distribute();}

# 结束前处理
my $code;
if (!$changed){$code = 0;}
else {
	open(FILE,">$xfile");
		flock(FILE,2);
		print FILE @reg;
	close(FILE);
	$code = 7;
}

# 记录(for User/ML)
if ($cfg{'Log'}){&logging();}

# 结束
exit($code);

## -------
sub allow {
	my ($chk,@tmp);
	if (open(FILE,"$target/$jdwa/allow.tab")){
			@tmp = <FILE>;
		close(FILE);
	}
	
	foreach(@tmp){
		chomp;
		s/[.\/(){|}\-\@]/\\$&/g;
		s/[?*]/.$&/g;
		if (/<($from|$xfrom)/i || $from =~ /^$_/i || $xfrom =~ /^$_/i){$chk = 1; last}
	}
	if (!$chk){
		&logerror("收信拒绝","参照接收限制名单");
		exit(5);
	}
}

sub autodelete {
	my ($dir,$period,$quota) = ($cfg{'AutoDeleteDir'},$cfg{'AutoDeletePeriod'},$cfg{'AutoDeleteQuota'});
	if (open(FILE,"$mailroot/domains/$TO[1]/$autodelete")){
			while (<FILE>){
				chomp;
				if (/^AutoDeleteDir\s*=\s*(.*)/){$dir = $1;}
				elsif (/^AutoDeletePeriod\s*=\s*(.*)/){$period = $1;}
				elsif (/^AutoDeleteQuota\s*=\s*(.*)/){$quota = $1;}
			}
		close(FILE);
	}
	return if (!$period && !$quota);
	$dir =~ s/\\/\//g;
	$dir = '' if !-d $dir;
	$quota *= 1000;
	my $time = time() - $period*60*60*24 if $period;
	my $total = $msize*1000;
	my $chk;
	
	my @files;
	foreach (glob("$target/$mailbox/*")){
		next if -d;
		my $stamp = (stat($_))[9];
		if ($time && $time > $stamp){	# 超过指定天数的MSG的处理 
			if ($dir && $period){
				my $name = $& if /[^\/]+$/;
				rename($_,"$dir/$xto\_$name");
			} elsif ($period){unlink($_);}
			$chk = 1;
			next;
		}
		my $size = -s $_;
		$total += $size;
		push(@files,"$_\t$stamp\t$size\n");
	}
	if ($chk){&logerror("自动删除","$period超过日");}
	
	return if !$quota;
	if ($msize*1000 > $quota){
		$quota /= 1000 . 'KB';
		&logerror("自动删除","$quota超过");
		exit(4);
	}
	return if $total < $quota;
	
	foreach (@files){				# 超过指定容量的处理 
		my ($file,$stamp,$size) = split(/\t/,$_);
		if ($dir){
			my $name = $& if $file =~ /[^\/]+$/;
			rename($file,"$dir/$xto\_$name");
		} else {unlink($file);}
		$total -= $size;
		last if $total < $quota;
	}
	$quota /= 1000 . 'KB';
	&logerror("自动删除","$quota超过");
}

sub autoreply {
	local ($aname,$adomain,$count,$frommes,$message,$noreply,$subject);
	if (!@reg1){&get_msg;}
	my ($tmp,$tmp1) = ($from,$from0);
	$tmp = $xfrom if !$tmp;
	$tmp1 = $xfrom0 if !$tmp1;
	
	# 防止乒乓
	if (($cfg{'AutoReply'} == 1 || $cfg{'AutoReply'} == 8) && 
		open(FILE,"$target/$jdwa/autoreplylist.tab")){
			while (<FILE>){
				chomp;
				if (/^$tmp1$/i){return;}
			}
		close(FILE);
	}
	
	$count = 1;
	open(FILE,"$target/$jdwa/autoreply.tab") || return;
		while (<FILE>){
			chomp;
			if ($count == 1 && /^\[noreply\] *(.*)/i){
				$noreply = $1;
				$count++;
				next;
			}
			$message .= "$_\n";
			$count++;
		}
	close(FILE);
	$noreply =~ s/ //g;
	$noreply =~ s/.*/,$&,/;
	$message =~ s/^\n+//;
	$message =~ s/[ \t\n]*$/\n/;
	if ($noreply =~ /,$tmp1,/ || !$message){return;}
	
	$subject = $hd{'subject'};
	if (!$subject){$subject = '(no title)';}
	if ($subject !~ /^Re/i){$subject = "Re: $subject";}
	if ($cfg{'AutoReply'} < 7 && ($UTFok || !$utf)){
		if (!$mybody){$frommes = &strip();}
		else {$frommes = $mybody;}
		$frommes = "(no message)" if $frommes =~ /^\s*$/;
		$message .= "\n---\n$datee\n$from wrote:\n\n$frommes";
	}
	
#szy	&jcode::convert(\$message,'jis');
#szy	&jcode::convert(\$subject,'jis');
	&smtp($to,$tmp,$subject,$message,'','','1');
	if ($cfg{'AutoReply'} == 1 || $cfg{'AutoReply'} == 8){		# 记录应当处
		open(FILE,">>$target/$jdwa/autoreplylist.tab");
			flock(FILE,2);
			print FILE "$tmp\n";
		close(FILE);
	}
}

sub datecheck {
	if (!@reg1){&get_msg;}
	my ($date,$expire) = (lc($hd{'date'}),$datecheck);
	return if !$date;
	$expire = $cfg{'DateCheck'} if $cfg{'DateCheck'};
	
	$date =~ /^[a-z,\s]*(\d\d?)\s+([a-z]+)\s+(\d\d\d\d|\d\d)\s/;
	return if (!$1 || !$2 || !$3);
	my ($day1,$month1,$year1) = ($1,$2,$3);
	if ($year1 < 100){
		if ($year1 >= 70){$year1 += 1900;}
		else {$year1 += 2000;}
	}
	
	if ($year1 >= 1970 && $year1 <= 2036){
		my %mon = ('jan','1','feb','2','mar','3','apr','4','may','5','jun','6',
			'jul','7','aug','8','sep','9','oct','10','nov','11','dec','12');
		$month1 = $mon{$month1};
		my $time = timelocal(0,0,0,$day1,$month1-1,$year1-1900);
		my $count = abs(time() - $time);
		return if ($count < $expire*24*60*60);
	}
	
	mkdir("$target/trash",755);
	my $file = $& if $xfile =~ /[^\/]+$/;
	copy($xfile,"$target/trash/$file");
	
	&logerror("收信拒绝","Date:限制");
	exit(4);
}

sub deny {
	my @tmp;
	open(FILE,"$target/$jdwa/deny.tab") || return;
		@tmp = <FILE>;
	close(FILE);
	foreach(@tmp){
		chomp;
		s/[.\/(){|}\-\@]/\\$&/g;
		s/[?*]/.$&/g;
		if ($from =~ /^$_/i || $xfrom =~ /^$_/i){
			&logerror("收信拒绝","参照拒绝名单");
			exit(5);
		}
	}
}

sub digest {
	if (!$mybody){$mybody = &strip;}
	$mybody = "(no message)" if $mybody =~ /^\s*$/;
	my $droot = "$target/digest";
	
	mkdir($droot,755);
	open(FILE,">>$droot/$year-$month-$mday.digest") || return;
		print FILE <<"EOF";
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Date: $datem
Subject: $hd{'subject'}
From: $hd{'from'}
To: $hd{'to'}
Messaeg-ID: $hd{'message-id'}
In-Reply-To: $hd{'in-reply-to'}
References: $hd{'references'}

$mybody
EOF
	close(FILE);
}

sub distribute {
	local($data,$received);
	if (!@reg1){&get_msg;}
	foreach (@received){$received .= $_.' ';}
	
	my @data;
	open(FILE,"$target/$jdwa/distribute.tab") || return;	# 分配表
		while (<FILE>){
			chomp;
			next if (/^\s*$/ || !/\t/);
			push(@data,$_);
		}
	close(FILE);
	if (!@data){return;}
=cut
	my @folder;
	open(FILE,"$target/$jdwa/folder.tab") ;#|| return;		# 文件夹一览
		while (<FILE>){
			chomp;
			my ($folder,$desc) = split(/\t/,$_);
			if ($folder =~ /^i\d+$/i){push(@folder,$folder);}
		}
	close(FILE);
#HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
	#if (!@folder){return;}
	#mkdir ("$target/trash",755);
	if (!@folder){
	}
#HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
=cut
	mkdir ("$target/trash",755);
	# 分配开始
	foreach my $rule (@data){
		my ($rulename,$header,$word,$method,$folder,$regexp) = split(/\t/,$rule);
		next if (!$header || $word =~ /^\s*$/ || !$method || !$folder);
		next if ($regexp && &chk_regexp($word));
		$header = lc($header);
		$header =~ s/:+$//;
		$data = $hd{$header};
		$data = $received if $header eq 'received';
		$data = &mod_str2($data);	# 半角变换
		next if !$data;
		if (!$regexp){
			($word) = &mod_str($word);
			$word =~ s/\\{3}(\*|\?)/\\$1/g;
			$word =~ s/\\{2}(\*|\?)/.$1/g;
		}
		if (($method == 1 && $data =~ /^$word/i) ||
			($method == 2 && $data =~ /$word/i) ||
			($method == 3 && $data =~ /^$word$/i) ||
			($method == 4 && $data =~ /$word$/i) ||
			($method == 5 && $data !~ /^$word/i) ||
			($method == 6 && $data !~ /$word/i) ||
			($method == 7 && $data !~ /^$word$/i) ||
			($method == 8 && $data !~ /$word$/i)){
			if ($folder eq 'spam'){
				mkdir("$target/spam",755);
				&logerror('接收后移动到垃圾邮件','参照过滤规则');
			} elsif ($folder =~ /^_+trashbox_+$/i){
				$folder = 'trash';
				&logerror('收信后移动到回收站','参照过滤规则');
			} elsif ($folder =~ /_+deleteall_+$/i){
				&logerror('收信后删除','参照过滤规则');
				exit(4);
			} else {
				&logerror('收信后移动到邮件夹','参照过滤规则');
			}
			
			my $count;
			my $fname = $mname;
			$fname = $mailbox . '_' . $fname if $folder eq 'trash';
			open(F1,">$target/$folder/$fname") || next;
				flock(F1,2);
				foreach my $line (@reg1){
					$count++;
					if ($count == 1 .. $line =~ /^<<MAIL-DATA>>/i){next;}
					print F1 $line;
				}
			close(F1);
			
			unlink("$target/temp/msg-$folder.lst");		# 索引删除
			exit(4);	## 不冻结
		}
	}
}

sub forcefrom {
	local($forcefrom,@tmp);
	$forcefrom = $cfg{'ForceFrom'};
	if (!@reg1){&get_msg();}
	
	$forcefrom = &enc_base64($forcefrom);
	
	if (grep(/^From: *.+/,@msgheader)){
		foreach(@msgheader){
			if (/^From:/i ... /^[^\s]/){
				if (/^From:/i){$_ = "From: $forcefrom\n";}
				if (/^\s/){next;}
			}
			push(@tmp,$_);
		}
		@msgheader = @tmp;
	}
	else {push(@msgheader,"From: $forcefrom\n");}
	
	$from = $forcefrom;
	$changed = 1;
	&get_msg1();
}

sub header {
	if (!@reg1){&get_msg();}
	local ($body,$chk,$footer,$from,$header,$i,$subject,$tmp,$to);
	foreach(qw(from subject to)){
		$$_ = $hd{$_};
#szy	&jcode::convert(\$$_,'jis');
	}
	
	foreach $i ('header','footer'){
		open(FILE,"$target/$jdwa/$i.tab") || next;
			while (<FILE>){$$i .= $_;}
		close(FILE);
		$$i =~ s/^\n+//;
		$$i =~ s/\s+$//;
		$$i =~ s/#+_+Date_+#+/$datem/gi;
		$$i =~ s/#+_+From_+#+/$from/gi;
		$$i =~ s/#+_+Subject_+#+/$subject/gi;
		$$i =~ s/#+_+To_+#+/$to/gi;
	}
	
	$body = "@msgbody";
	$body =~ s/ *\n /\n/g;
	
	if ($boundary && $body =~ /$boundary/){
		my $str = '___JDWM___';
		$body =~ s/-+$boundary\n/\n$str\n$&/g;
		foreach(split(/\n$str\n/,$body)){
			if (/^\s*$/){next;}
			my($h,$b) = split(/\n\n+/,$_,2);
			if ($h =~ /Content-Type: *text\/(html|plain)/i){
				if (!$chk && $cfg{'Header'} && $header){$b = "$header\n\n$b"; $chk = 1;}
				if (!$chk && $cfg{'Footer'} && $footer){$b =~ s/\s*$/\n\n$footer\n\n/; $chk = 1;}
			} elsif ($h =~ /$utfcontent/ && $UTFok && $utf){
				if (!$chk && $cfg{'Header'} && $header){
					$header = encode($charsetM,$header);
					$b = "$header\n\n$b";
					$chk = 1;
				}
				if (!$chk && $cfg{'Footer'} && $footer){
					$footer = encode($charsetM,$footer);
					$b =~ s/\s*$/\n\n$footer\n\n/;
					$chk = 1;
				}
			}
			$tmp .= "$h\n\n$b";
		}
		if ($chk){$body = $tmp;}
	} else {
		if ($cfg{'Header'} && $header){
			if ($UTFok && $utf){$header = encode($charsetM,$header);}
			$body = "$header\n\n$body";
			$chk = 1;
		}
		if ($cfg{'Footer'} && $footer){
			if ($UTFok && $utf){$footer = encode($charsetM, $footer);}
			$body =~ s/\s*$/\n\n$footer\n/;
			$chk = 1;
		}
	}
	if ($chk){
		@msgbody = ();
		foreach(split(/\r*\n/,$body)){
			push(@msgbody,"$_\n");
		}
		$changed = 1;
		&get_msg1();
	}
}

sub logging {
	if (!@reg1){&get_msg;}
	mkdir("$target/logs",755);
	my $count = 0;
	
	open(FILE,">$target/logs/$mname");
		foreach (@reg){
			$count++;
			next if ($count == 1 .. /^<<MAIL-DATA>>/i);
			print FILE;
		}
	close(FILE);
}

sub msgid {
	my $id = sprintf("%X",time()) . sprintf(sprintf("%%0%XlX",6),int(rand(0xffffff)));
	if (!@reg1){&get_msg();}
	if (grep(/^Message-Id:/i,@msgheader)){return;}
	push(@msgheader,"Message-Id: <$id.$from>\n");
	
	$changed = 1;
	&get_msg1();
}

sub netsend {
	if (!@reg1){&get_msg;}
	local $msg = $netsendmsg;
	local $subj = $hd{'subject'};
	$subj =~ s/</＜/g;
	$subj =~ s/>/＞/g;
	$subj =~ s/\|/｜/g;
	if (!$msg){$msg = 'You Got Mail!!';}
	
	if (-f "$ipmsg"){
		system("$ipmsg $cfg{'NetBIOSName'} $to $from ".encode("UCS-2",decode("gb2312",$subj))." 1 $msg");
		print LOG "$ipmsg $cfg{'NetBIOSName'} $to $from $subj 1 $msg\n";
	} 
	
}

sub notify {
	my ($op1) = @_;
	if (!@reg1){&get_msg();}
	local ($sender,$subj,$msg1,$msg2) = ($to,$hd{'subject'});
	if ($cfg{'NotifyTo'} =~ /^$to0|$xto0$/i){return;}
	
	$msg1 = <<"EOF";
You got mail!
----------------
Subject: $subj
From: $hd{'from'}
To: $hd{'to'}
Date: $datee
EOF
#szy	&jcode::convert(\$msg1,'jis');
#szy	&jcode::convert(\$subj,'jis');
	
	if (!$op1 && ($UTFok || !$utf)){
		if (!$mybody){$msg2 = &strip();}
		else {$msg2 = $mybody;}
		$msg2 = "(no message)" if $msg2 =~ /^\s*$/;
#		$msg2 = substr($msg2,0,100);
#szy	&jcode::convert(\$msg2,'jis');
		$msg1 .= "----------------\n";
		$msg1 .= $msg2;
		if ($boundary){
			$msg1 .= "----------------\n";
			$msg1 .= "*Attachments.\n";
		}
	}
	&smtp($sender,$cfg{'NotifyTo'},$subj,$msg1,'','','1');
}

sub replyto {
	my @tmp;
	if (!@reg1){&get_msg();}
	
	foreach (@msgheader){
		if (/^Reply-To:/i){next;}
		s/^To:.*/Reply-To: $cfg{'ReplyTo'}\n$&/i;
		push(@tmp,$_);
	}
	
	@msgheader = @tmp;
	$changed = 1;
	&get_msg1();
}

sub sizecheck {
	if ($msize > $cfg{'MessageSize'}){
		&logerror("收信拒绝","尺寸超过");
		exit(6);
	}
}

sub subscribe2 {
	my ($chk,$email,$email1,$file,$msg,$tmp,@members);
	if (!@reg1){&get_msg();}
	my @tmp = @msgbody;
	
	if ($boundary){
		if (!$mybody){$tmp = &strip;}
		else {$tmp = $mybody;}
	} else {
		foreach(@tmp){
			$tmp .= $_;
		}
	}
	$tmp =~ s/[\n\r\s]//;
	return if $tmp;
	
	# 追加或删除
	$email = &get_email($hd{'from'});
	return if $email !~ /^$emreg$/;
	($email1) = &mod_str($email);
	if (open(FILE,"$target/mlusers.tab")){
			while(<FILE>){
				if (/^"*$email1/i){$chk = 1; next;}
				push(@members,$_);
			}
		close(FILE);
	}
	open(FILE,">$target/mlusers.tab");
		flock(FILE,2);
		print FILE @members;
		print FILE "\"$email\"\t\"R\"\n" if $chk eq '';
	close(FILE);
	if ($chk){unlink("$target/members/$email");}
	
	# 不送信剩下消息
	$file = $& if $xfile =~ /[^\/\\]+$/;
	mkdir("$target/logs",755);
	copy($xfile,"$target/logs/$file");
	
	# 通知
	if (!$chk){
		$msg = $cfg{'Subscribe1'};		# 隐藏功能
		$msg = "You subscribed to $xto." if $msg eq '';
	} else {
		$msg = $cfg{'Subscribe2'};
		$msg = "You unsubscribed to $xto." if $msg eq '';
	}
	$msg =~ s/##+_+ml_+##+/$xto/g;
	$msg =~ s/##+_+date_+##+/$datee/g;
#szy	&jcode::convert(\$msg,'jis');
	&smtp($postmaster,$email,$msg,"$msg\n",'','','1');
	
	unlink("$cdir/temp/$xto.txt","$cdir/temp/$xto.csv");
	exit(4);
}

sub tag {
	if (!@reg1){&get_msg();}
	local ($ctmp,$s,$Tag) = ("$target/$jdwa/counter.tmp",$hd{'subject'},$cfg{'Tag'});
	local ($count,$ctmp1,$ctype,$xtype);
	my $restr = 'Re[:\s]*[\^\[({]*\d*[\])}]*[:\s]+';
	my ($Tag0) = &mod_str($Tag);
	
	# 宏处理开始 
	if ($cfg{'Tag'} =~ /%d%/i){				# 日期插入 
		$Tag =~ s/%d%/$year-$month-$mday/i;
	}
	if ($cfg{'Tag'} =~ /%c(\d*)%/i){		# 号码插入
		my $flag = $1;
		if (!$flag){$flag = 5;}
		$ctype = substr($flag,0,1);
		$xtype = substr($flag,1,1);
		#my $tag1;
		#for (1 .. $ctype){$tag1 .= '\\d';}
		#$Tag =~ s/%c\d*%/$tag1/i;
		
		## 锁处理
		if ((stat($ctmp))[9] < time() - $cttl){unlink($ctmp);}
		for (1 .. $cttl){
			if (-f "$ctmp"){sleep(1); next;}
			open(FILE,">$ctmp") && close(FILE);
			if (-f "$ctmp"){$ctmp1 = 1; last;}	# 锁成功
		}
		if ($ctmp1 && open(FILE,"$target/$jdwa/counter.tab")){	# 读计数器
				flock(FILE,1);
				$count = <FILE>;
			close(FILE);
			chomp($count);
		}
		$count =~ s/[^\d]//g;
		$count++;
		my $fcount = sprintf("%0${ctype}s",$count);
		if ($ctmp1){$Tag =~ s/%c\d*%/$fcount/i;}
		else {$Tag =~ s/%c\d*%/__/;}
	}
	
	# Header更新
	my $upok;
	if ($s){
		my @tmp;
		foreach(@msgheader){
			if (!/^Subject:/){
				push(@tmp,$_);
				next;
			}
			chomp();
			s/^Subject: *//i;
			if ($xtype){		# 无条件计数器更新
				s/$Tag0//;
				s/^($restr)+/$1/i;
				$upok = 1;
			} elsif ($s =~ /^$restr/i || $s =~ /\s$restr/i){
				s/$Tag0//;
				$Tag = '';		# 用新发言计数器更新关联发言
			} else {
				$upok = 1;		# 用新发言计数器更新新发言
			}
			push(@tmp,"Subject: $Tag$_\n");
		}
		@msgheader = @tmp;
	} else {
		push(@msgheader,"Subject: $Tag\n");
	}
	
	# 计数器更新
	if ($ctmp1 && $upok){
		open(FILE,">$target/$jdwa/counter.tab");
			flock(FILE,2);
			print FILE "$count\n";
		close(FILE);
	}
	
	unlink($ctmp);
	$changed = 1;
	&get_msg1();
}

sub get_config {
	if (open(FILE,"$target/user.tab")){
			while(<FILE>){
				chomp;
				s/"//g;
				s/^\s+//;
				s/\s+$//;
				next if (/^\s*$/ || /^#/);
				my ($tmp1,$tmp2) = split(/ *\t+ */,$_,2);
				$x{$tmp1} = $tmp2;
			}
		close(FILE);
	}
	if (open(FILE,"$target/$jdwa/config.tab")){
			while(<FILE>){
				chomp;
				if (!/^Tag\s*=/ && !/^ForceFrom/){s/\s+$//; s/"//g;}
				next if (/^\s*$/ || /^#/);
				my ($tmp1,$tmp2) = split(/ *= */,$_,2);
				$cfg{$tmp1} = $tmp2;
			}
		close(FILE);
	}
	open(FILE,"$target/$jdwa/config2.tab") || return;
		while(<FILE>){
			chomp;
			next if (/^\s*$/ || /^#/);
			my ($tmp1,$tmp2) = split(/ *= */,$_,2);
			$cfg2{$tmp1} = $tmp2;
		}
	close(FILE);
}

close(LOG);
