Index: /gsdl/trunk/perllib/plugins/EmailPlugin.pm
===================================================================
--- /gsdl/trunk/perllib/plugins/EmailPlugin.pm	(revision 16340)
+++ /gsdl/trunk/perllib/plugins/EmailPlugin.pm	(revision 16341)
@@ -62,5 +62,5 @@
 #       * RFC 1806 - Content Dispositions (ie inline/attachment)
 
-# 12/05/02 Added usage datastructure - John Thompson
+
 package EmailPlugin;
 
@@ -108,8 +108,4 @@
 		'args'     => $arguments };
 
-# Create a new EmailPlugin object with which to parse a file.
-# Accomplished by creating a new SplitTextFile and using bless to 
-# turn it into an EmailPlugin.
-
 sub new {
     my ($class) = shift (@_);
@@ -123,4 +119,5 @@
 
     $self->{'assoc_filenames'} = {}; # to save attach names so we don't clobber
+    $self->{'tmp_file_paths'} = (); # list of tmp files to delete after processing is finished
 
     # this might not actually be true at read-time, but after processing
@@ -226,46 +223,6 @@
 	$value =~ s/^\s+//;
 	$value =~ s/\s+$//;
-	# decode headers if stored using =?<charset>?[BQ]?<data>?= (rfc2047)
-	if ($value =~ /=\?.*\?[BbQq]\?.*\?=/) {
-	    my $original_value=$value;
-	    my $encoded=$value;
-	    $value="";
-	    # we should ignore spaces between consecutive encoded-texts
-	    $encoded =~ s@\?=\s+=\?@\?==\?@g;
-	    while ($encoded =~ s/(.*?)=\?([^\?]*)\?([bq])\?([^\?]+)\?=//i) {
-		my ($charset, $encoding, $data)=($2,$3,$4);
-		my ($decoded_data);
-		$value.="$1"; # any leading chars
-		$data=~s/^\s*//; $data=~s/\s*$//; # strip whitespace from ends
-		chomp $data;
-		$encoding =~ tr/BQ/bq/;
-		if ($encoding eq "q") { # quoted printable
-		    $data =~ s/_/\ /g;  # from rfc2047 (sec 4.2.2)
-		    $decoded_data=qp_decode($data);
-		    # qp_decode adds \n, which is default for body text
-		    chomp($decoded_data);
-		} else { # base 64
-		    $decoded_data=base64_decode($data);
-		}
-		$self->convert2unicode($charset, \$decoded_data);
-		$value .= $decoded_data;
-	      } # end of while loop
-	    
-	    # get any trailing characters
-	    $self->convert2unicode($default_header_encoding, \$encoded);
-	    $value.=$encoded;
-
-	    if ($value =~ /^\s*$/) { # we couldn't extract anything...
-	      $self->convert2unicode($default_header_encoding,
-				     \$original_value);
-	      $value=$original_value;
-	    }
-        } # end of if =?...?=
-
-        # In the absense of other charset information, assume the
-        # header is the default (usually "iso_8859_1") and convert to unicode.
-        else {
-	    $self->convert2unicode($default_header_encoding, \$value);
-	}
+	# decode header values, using either =?<charset>?[BQ]?<data>?= (rfc2047) or default_header_encoding
+	$self->decode_header_value($default_header_encoding, \$value);
 	
 	# Store the metadata
@@ -407,5 +364,5 @@
     } elsif ($mimetype ne "text/plain") {
 	$self->{'doc_obj'} = $doc_obj; # in case we need to associate files...
-	$$textref=$self->text_from_mime_message($mimetype,$mimeinfo,$$textref);
+	$$textref=$self->text_from_mime_message($mimetype,$mimeinfo,$default_header_encoding,$$textref);
     } else { # mimetype eq text/plain
 
@@ -457,4 +414,15 @@
 }
 
+# delete any temp files that we have created
+sub clean_up_after_doc_obj_processing {
+    my $self = shift(@_);
+    
+    foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
+        if (-e $tmp_file_path) {
+            &util::rm($tmp_file_path);
+        }
+    }
+    
+}
 
 # Convert a text string into HTML.
@@ -507,5 +475,5 @@
 sub text_from_mime_message {
     my $self = shift(@_);
-    my ($mimetype,$mimeinfo,$text)=(@_);
+    my ($mimetype,$mimeinfo,$default_header_encoding,$text)=(@_);
     my $outhandle=$self->{'outhandle'};
     # Check for multiparts - $mimeinfo will be a boundary
@@ -597,5 +565,6 @@
 		}
 
-		$text .= $self->process_multipart_part($message_part,
+		$text .= $self->process_multipart_part($default_header_encoding,
+						       $message_part,
 						       $is_first_part);
 	    } # foreach message part.
@@ -618,5 +587,6 @@
 	    my $msg_text;
 	    if ($mimetype =~ m@multipart/@) {
-		$msg_text = text_from_mime_message($self, $mimetype, $mimeinfo,
+		$msg_text = $self->text_from_mime_message($mimetype, $mimeinfo,
+						   $default_header_encoding,
 						   $text);
 	    } else {
@@ -655,4 +625,5 @@
 sub process_multipart_part {
     my $self = shift;
+    my $default_header_encoding = shift;
     my $message_part = shift;
     my $is_first_part = shift;
@@ -691,4 +662,7 @@
 	$filename=$1;
 	$filename =~ s@\r?\s*$@@; # remove trailing space, if any
+	# decode the filename
+	$self->decode_header_value($default_header_encoding, \$filename);
+
     }
     
@@ -745,4 +719,5 @@
 		    $self->text_from_mime_message($message_content_type,
 						  $message_content_info,
+						  $default_header_encoding,
 						  $message_part_body);
 	    } else {
@@ -770,4 +745,5 @@
 	my $tmptext= $self->text_from_mime_message($part_content_type,
 						   $part_content_info,
+						   $default_header_encoding,
 						   $part_body);
 	$return_text.=$tmptext;
@@ -792,5 +768,5 @@
 		$encoding=$1; $encoding =~ tr/A-Z/a-z/;
 	    }
-	    my $tmpdir=$ENV{'GSDLHOME'} . "/tmp";
+	    my $tmpdir=&util::filename_cat($ENV{'GSDLHOME'}, "tmp");
 	    my $save_filename=$filename;
 	    
@@ -807,6 +783,8 @@
 		$assoc_files->{$filename}=1;
 	    }
-	    open (SAVE, ">$tmpdir/$save_filename") ||
-		warn "EmailPlugin: Can't save attachment as $tmpdir/$save_filename: $!";
+	    my $tmp_filename = &util::filename_cat($tmpdir, $save_filename);
+	    open (SAVE, ">$tmp_filename") ||
+		warn "EMAILPlug: Can't save attachment as $tmp_filename: $!";
+	    binmode(SAVE); # needed on Windows
 	    my $part_text = $message_part;
 	    $part_text =~ s/(.*?)\r?\n\r?\n//s; # remove header
@@ -820,11 +798,11 @@
 	    close SAVE;
 	    my $doc_obj=$self->{'doc_obj'};
-	    $doc_obj->associate_file("$tmpdir/$save_filename",
+	    $doc_obj->associate_file("$tmp_filename",
 				     "$save_filename",
 				     $part_content_type # mimetype
 				     );
-	    # clean up tmp area...
-	    # Can't do this as it hasn't been copied/linked yet!!!
-#			&util::rm("$tmpdir/$save_filename");
+	    # add this file to the list of tmp files for deleting later
+	    push(@{$self->{'tmp_file_paths'}}, $tmp_filename);
+
 	    my $outhandle=$self->{'outhandle'};
 	    print $outhandle "EmailPlugin: saving attachment \"$filename\"\n"; #
@@ -1002,4 +980,65 @@
     }
     return 0;
+}
+
+# words with non ascii characters in header values must be encoded in the 
+# following manner =?<charset>?[BQ]?<data>?= (rfc2047)
+
+sub decode_header_value {
+    my $self = shift(@_);
+    my ($default_header_encoding, $textref) = @_;
+    
+    if (!$$textref) {
+	# nothing to do!
+	return;
+    }
+    my $value = $$textref;
+    # decode headers if stored using =?<charset>?[BQ]?<data>?= (rfc2047)
+    if ($value =~ /=\?.*\?[BbQq]\?.*\?=/) {
+	my $original_value=$value;
+	my $encoded=$value;
+	$value="";
+	# we should ignore spaces between consecutive encoded-texts
+	$encoded =~ s@\?=\s+=\?@\?==\?@g;
+	while ($encoded =~ s/(.*?)=\?([^\?]*)\?([bq])\?([^\?]+)\?=//i) {
+	    my ($charset, $encoding, $data)=($2,$3,$4);
+	    my ($decoded_data);
+	    my $leading_chars = "$1";
+	    $self->convert2unicode($default_header_encoding, \$leading_chars);
+	    $value.=$leading_chars; 
+	    
+	    $data=~s/^\s*//; $data=~s/\s*$//; # strip whitespace from ends
+	    chomp $data;
+	    $encoding =~ tr/BQ/bq/;
+	    if ($encoding eq "q") { # quoted printable
+		$data =~ s/_/\ /g;  # from rfc2047 (sec 4.2.2)
+		$decoded_data=qp_decode($data);
+		# qp_decode adds \n, which is default for body text
+		chomp($decoded_data);
+	    } else { # base 64
+		$decoded_data=base64_decode($data);
+	    }
+	    $self->convert2unicode($charset, \$decoded_data);
+	    $value .= $decoded_data;
+	} # end of while loop
+	
+	# get any trailing characters
+	$self->convert2unicode($default_header_encoding, \$encoded);
+	$value.=$encoded;
+	
+	if ($value =~ /^\s*$/) { # we couldn't extract anything...
+	    $self->convert2unicode($default_header_encoding,
+				   \$original_value);
+	    $value=$original_value;
+	}
+	$$textref = $value;
+    } # end of if =?...?=
+    
+    # In the absense of other charset information, assume the
+    # header is the default (usually "iso_8859_1") and convert to unicode.
+    else {
+	$self->convert2unicode($default_header_encoding, $textref);
+    }   
+    
 }
 
