Index: mail-in.pl
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/og2list/mail-in.pl,v
retrieving revision 1.31
diff -u -p -r1.31 mail-in.pl
--- mail-in.pl	24 Aug 2006 19:35:21 -0000	1.31
+++ mail-in.pl	15 Nov 2006 19:02:19 -0000
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-# $Id: mail-in.pl,v 1.31 2006/08/24 19:35:21 bengen Exp $
+# $Id: mail-in.pl,v 1.27.2.1 2006/08/31 08:20:41 killes Exp $
 
 # THIS IS PROTOTYPE CODE AND SHOULD NOT BE USED IN PRODUCTION SYSTEMS!
 
@@ -231,6 +231,8 @@ use DBI;
 use Getopt::Std;
 use Encode qw(decode);
 
+use File::Basename 'basename';
+
 use vars qw(
 	    $dsn $dbh
 
@@ -241,6 +243,8 @@ use vars qw(
 	    $add_threadinfo_stmt $add_threadinfo_handle
 	    $add_dsn_stmt $add_dsn_handle
 
+	    $add_attachment_stmt $add_attachment_handle
+
 	    $dbtype $dbuser $dbauth $dbhost $dbase
 	    $mail_domain
 	    $sendmail_binary @sendmail_args
@@ -254,6 +258,7 @@ use vars qw(
 
 	    %opts
 	    @references
+	    %attachments
 	   );
 
 sub simplify_body($);
@@ -300,6 +305,13 @@ SET status = 5
 WHERE (mid = ?) AND (recipient_address = ?)
 EOF
 
+$add_attachment_stmt = <<EOF;
+INSERT INTO og2list_attachment
+(mid, filename, content_type, payload, disposition) 
+VALUES ((SELECT mid FROM og2list_incoming_content
+         WHERE msgid=?), ?, ?, ?, ?);
+EOF
+
 # ----------------------------------------------------------------------
 
 getopts('dt:f:', \%opts);
@@ -391,6 +403,8 @@ EOF
     $add_threadinfo_handle = $dbh->prepare($add_threadinfo_stmt) or die;
     $add_group_handle = $dbh->prepare($add_group_stmt) or die;
 
+    $add_attachment_handle = $dbh->prepare($add_attachment_stmt) or die;
+
     $add_content_handle->execute($from_address,
 				 $from_name,
 				 $subject,
@@ -398,6 +412,17 @@ EOF
 				 $content_type,
 				 $body);
     $add_group_handle->execute($msgid, $envelope_to);
+
+    foreach (keys %attachments) {
+       my %attch = %{$attachments{$_}}; 
+
+       $add_attachment_handle->execute($msgid, 
+                                       $attch{'name'},
+                                       $attch{'type'},
+                                       $attch{'content'},
+                                       $attch{'disp'});
+    }
+
     foreach (@references) {
       $add_threadinfo_handle->execute($msgid, $_);
     }
@@ -405,16 +430,46 @@ EOF
 }
 
 # ----------------------------------------------------------------------
+sub save_attachment(@) {
+  my $entity = shift;
+  my $name = $entity->head()->recommended_filename;
+  my $type = $entity->effective_type;
+  my $disp = $entity->head()->get('Content-Disposition');
+
+  my $filename = basename $name;
+
+  my $maxLen = 16777216; # size of MEDIUMBLOB
+
+  ${$attachments{$filename}}{'name'} = $filename;
+  ${$attachments{$filename}}{'type'} = $type;
+  ${$attachments{$filename}}{'disp'} = $disp;
+
+  if(my $bh = $entity->bodyhandle) {
+    my $io = $bh->open("r");
+
+    ${$attachments{$filename}}{'length'} = 
+        $io->read(${$attachments{$filename}}{'content'}, $maxLen);
+
+    $io->close;
+  }
+
+  if ($opts{'d'}) {
+    my $attachment = File::Spec->catfile('/tmp', $filename);
+
+    unless( -f $attachment ) {
+        open( FH, ">$attachment");
+        print FH ${$attachments{$filename}}{'content'};
+        close(FH);
+    }
+  }
+}
+
+# ----------------------------------------------------------------------
 
 sub simplify_body($) {
   my $entity = shift;
   my $body;
 
-  # Skip attachments
-  return if (defined $entity->head()->mime_attr('content-disposition') &&
-	     $entity->head()->mime_attr('content-disposition') =~
-	     /attachment/);
-
   foreach ($entity->mime_type) {
     chomp;
 
@@ -437,7 +492,7 @@ sub simplify_body($) {
 	if ($type =~ /^text/) {
 	  next if (exists $alternative{$type});
 	  $alternative{$type}=$body;
-	}
+        }
       }
       if ($alternative{'text/html'}) {
 	return 'text/html', $alternative{'text/html'};
@@ -460,6 +515,12 @@ sub simplify_body($) {
 	if ($type =~ /^text/) {
 	  $alternative{$type}.=$body;
 	}
+        else {
+            # For all non-text based components
+            # store the attachments for use by
+            # mail-out
+            save_attachment( $part );
+        }
       }
       if ($alternative{'text/html'}) {
 	return 'text/html', $alternative{'text/html'};
@@ -477,8 +538,6 @@ sub simplify_body($) {
     # RfC 2046, Section 5.1.7
     m/^multipart\/(.*)$/ && do {
     };
-
-    warn "FIXME: $_ MIME type not implemented yet";
   }
   return undef;
 }
