#!/usr/local/bin/perl

$DraftDir = '/home/R10E/newslave/Mail';
$inbox = "$DraftDir/inbox";
$folders = '/home/R10E/newslave/cronjob/folder.txt';
%key_value = ();
%classify = ();
# These are the headers in which we look for keywords
@interesting_headers = ('sender','to', 'cc', 'from', 'apparently-to',
			'reply-to');

$ENV{"PATH"} = "/bin:/usr/local/bin:/usr/local/mh:.";
open(STDERR,"> /dev/null");
system("inc");

if (!&init($folders)) {
    print "Error in init folder array\n";
    exit;
}				# 

# foreach $folder ( keys ( %key_value ) ) {
#     $key = $key_value{$folder};
#     #print "Looking for: $folder $key\n";
#     system("pick +inbox -to $key -or -cc $key -or --sender $key -or -from $key");
# }

opendir(FOLDER,$inbox) || die "unable to open $inbox\n";
$count = 0;
foreach $MSGID (sort by_number readdir(FOLDER)) {
    if ($MSGID =~ /[^0-9]/) { 
	#if ($MSGID =~ /[^.]/) {print "%%%$MSGID%%%\n";}
	next;
    }
    $DraftCur = "$inbox/$MSGID";
    if (-f $DraftCur && -r _) {
	if (&draft_parse($DraftCur)) {$count++;}
    }
}
print "\n**** Total $count mails handled.****\n";
exit 0;

#--------------------------------------------------------------------------
# Usage:	&draft_parse();
# Action:	parse current draft and move to specific folder
#--------------------------------------------------------------------------
sub draft_parse	{
    local($draft) = @_;
    local($p) = ('draft_parse');

    @patterns = keys(%classify); # 

    open(FILE,$draft) || die "Error in $p: Open $draft";
    $state = 'head';		# 
    $found = 'no';		# 
    while(<FILE>)	{	# 
        chop;			# 
	if($state eq 'head')	{ # 
	    # Split up headers so we can examine them
	    if(/^([^\s:]+):\s*(.*)$/)	{ # 
		$key = $1; $content = $2; # 
		#print "$draft key=$key content=$content\n";
		# This is one of the headers we want to check
		if ( grep(/$key/i, @interesting_headers) ) { # 

 		    # See if this header matches any of the key patterns
 		    foreach $i ( @patterns ) { # 
 			if ( grep( /$i/i, ("$content") ) ) {
 			    $group = $classify{$i}; # 
			    $found = 'yes'; # 
 			    last;
 			}
 		    }
		    if ($found eq "no") {next;};
		    close(FILE); # 
		    $folder = "$DraftDir/$group";
 				if (!(-e $folder && -d _)) {
 					system("mkdir $folder");
 					system("echo 0 > $folder/maxid");
 				}
 				open(MAXID,"$folder/maxid");
 				$max_id = <MAXID>;
				#print "maxid = $max_id\n";
 				close(MAXID);
 				$max_id =~ s/\n//;
 				$max_id = $max_id + 1;
 				rename("$draft","$folder/$max_id");
 				#chmod (0644,"$folder/$max_id");
 				chmod (0777,"$folder/$max_id");
 				open(OUT,">$folder/maxid");
 				print OUT "$max_id";
 				close(OUT);
				print "$draft => $folder/$max_id\n";
				return 1;
		}
	    }			# 
	    elsif(/^\s*-*\s*$/) { break; $state = 'body'; }
	    elsif(!$key) { print "Error in $p: HeaderFmt $_"; break; }
        }			# 
	else {break;}		# 
    }
    close(FILE);
    return 0;
}				# 

sub by_number {
    if ($a < $b) {
	-1;
    } elsif ($a == $b) {
	0;
    } elsif ($a > $b) {
	1;
    }
}				# 

sub init {
    local($folder_file) = @_;
    
    if (!(-f $folder_file && -r _)) {
	print "$folder_file not found\n";
	return 0;
    }				# 

    # the file in the format of folder_name keyword
    open(FILE,"$folder_file");	# 
    $total = 0;			# 
    while (<FILE>)   {		# 
	if (/^\s*$/) { next; }
	if (/^#/) { next; }

        ($folder, $key) = split;
	$classify{$key} = $folder;
	$total = $total + 1; # 
        print "##$folder##$key##\n";
    }
	close(FILE);
	print "Total folders: $total\n";
	return 1;		# 
}


