]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/commitdiff
multipart/alternative handling 0.6
authorDavid Bremner <bremner@unb.ca>
Sun, 28 Sep 2008 12:31:23 +0000 (09:31 -0300)
committerDavid Bremner <bremner@unb.ca>
Sun, 28 Sep 2008 12:31:23 +0000 (09:31 -0300)
IkiWiki/Plugin/mailbox.pm

index 039512c0c2f99613fb088cf709d3be687d4627b3..77dff8394c77e8bb3faaeffbf1c91305ce72bbf6 100644 (file)
@@ -171,35 +171,36 @@ sub format_message(@){
     
     my @headers=map { make_pair($message,$_) } @names;
     
-    
-
     $template->param(HEADERS=>[@headers]);
 
     my $allowed_attachments=$params{allowed_attachments} || 
-       "maxsize(100kb) and mimetype(text/plain)";
-
-    my @parts=$message->parts;
-
-    my $partcount=1;
-    foreach(@parts){
-       #this sucks. But someone would need to modify filecheck to
-       #accept a blob of content. Or maybe hacking with IO::Scalar
-       my $tmpfile=File::Temp->new();
-
-       binmode $tmpfile,':utf8';
-       print $tmpfile $_->body();
+       "maxsize(100kb) and (mimetype(text/plain) or mimetype(text/html))";
 
-       my $allowed=pagespec_match($dest, $allowed_attachments, file=>$tmpfile);
+    my @rawparts=$message->parts;
+    my @parts=();
 
-       if (!$allowed) {
-           debug("clobbering attachment $partcount");
-           $_->content_type_set('text/plain');
-           $_->body_set("[ omitting part $partcount: $allowed ]");
-
-       }           
-
-       $partcount++;
+    if ($message->content_type =~ m|^multipart/alternative|){
+       #according to RFC 1521, the last part is the most 'faithful'
+       while (scalar(@rawparts) && !scalar(@parts)){
+           my $part=pop(@rawparts);
+           if (check_part($part,$dest,$allowed_attachments)){
+               push(@parts,$part);
+           }
+       }
+    } else {
+       my $partcount=1;
+       foreach(@rawparts){
+           my $allowed=check_part($_,$dest,$allowed_attachments );
+           if (!$allowed) {
+               $_->content_type_set('text/plain');
+               $_->body_set("[ omitting part $partcount: $allowed ]");
+               
+           }       
+           push(@parts,$_);
+           $partcount++;
+       }
     }
+
     my $body= join("\n", map { format_part($_->content_type, $_->body) } 
                   @parts);
 
@@ -209,6 +210,22 @@ sub format_message(@){
     return $output;
 }
 
+sub check_part($$$){
+    my $part=shift;
+    my $dest=shift;
+    my $allowed_attachments=shift;
+
+
+    #this sucks. But someone would need to modify filecheck to
+    #accept a blob of content. Or maybe hacking with IO::Scalar
+
+    my $tmpfile=File::Temp->new();
+    binmode $tmpfile,':utf8';
+    print $tmpfile $part->body();
+
+    return pagespec_match($dest, $allowed_attachments, file=>$tmpfile);
+}
+
 sub format_part($$){
     my $mime_type=shift;
     my $body=shift;
@@ -216,7 +233,7 @@ sub format_part($$){
     my $rval="";
 
     # for debugging:
-    $rval .= "[ $mime_type ]\n";
+    $rval .= "[ $mime_type ]\n";
 
     if ($mime_type =~ "^text/html"){
        $rval.= $body;