You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by fe...@apache.org on 2006/08/07 05:14:18 UTC

svn commit: r429231 - in /spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin: Message.pm Message/Node.pm

Author: felicity
Date: Sun Aug  6 20:14:17 2006
New Revision: 429231

URL: http://svn.apache.org/viewvc?rev=429231&view=rev
Log:
message/ parts are now generally handled non-recursively as well (just 1 level).

Modified:
    spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message.pm
    spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message/Node.pm

Modified: spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message.pm
URL: http://svn.apache.org/viewvc/spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message.pm?rev=429231&r1=429230&r2=429231&view=diff
==============================================================================
--- spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message.pm (original)
+++ spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message.pm Sun Aug  6 20:14:17 2006
@@ -79,6 +79,9 @@
 C<spamassassin -d>, which only needs the pristine header and body which
 is always handled when the object is created.
 
+C<subparse> specifies how many MIME recursion levels should be parsed.
+Defaults to 20.
+
 =cut
 
 # month mappings (ripped from Util.pm)
@@ -97,6 +100,11 @@
   my $parsenow = $opts->{'parsenow'} || 0;
   my $normalize = $opts->{'normalize'} || 0;
 
+  # Specifies whether or not to parse message/rfc822 parts into its own tree.
+  # If the # > 0, it'll subparse, otherwise it won't.  By default, do twenty
+  # levels deep.
+  my $subparse = defined $opts->{'subparse'} ? $opts->{'subparse'} : 20;
+
   my $self = $class->SUPER::new({normalize=>$normalize});
 
   $self->{pristine_headers} =	'';
@@ -288,8 +296,9 @@
   # 0: part object, already in the tree
   # 1: boundary used to focus body parsing
   # 2: message content
+  # 3: how many MIME subparts to parse down
   #
-  $self->{'parse_queue'} = [ [ $self, $boundary, \@message ] ];
+  $self->{'parse_queue'} = [ [ $self, $boundary, \@message, $subparse ] ];
 
   # If the message does need to get parsed, save off a copy of the body
   # in a format we can easily parse later so we don't have to rip from
@@ -570,12 +579,33 @@
     # one doesn't, assume it's malformed and send it to be parsed as a
     # non-multipart section
     #
-    if ( $toparse->[0]->{'type'} =~ /^multipart\//i && defined $toparse->[1] ) {
+    if ( $toparse->[0]->{'type'} =~ /^multipart\//i && defined $toparse->[1] && ($toparse->[3] > 0)) {
       $self->_parse_multipart($toparse);
     }
     else {
       # If it's not multipart, go ahead and just deal with it.
       $self->_parse_normal($toparse);
+
+      if ($toparse->[0]->{'type'} =~ /^message\b/i && ($toparse->[3] > 0)) {
+        # Get the part ready...
+        my $message = $toparse->[0]->decode();
+
+        if ($message) {
+          my $msg_obj = Mail::SpamAssassin::Message->new({
+      	    message		=>	$message,
+	    parsenow	=>	0,
+	    normalize	=>	$self->{normalize},
+	    subparse	=>	$toparse->[3]-1,
+	    });
+
+          $toparse->[0]->add_body_part($msg_obj);
+
+	  # now this is sneaky ... take the sub-message's parse_queue and add
+	  # it to ours and we'll handle the sub-message in our normal loop. :)
+	  push(@{$self->{'parse_queue'}}, @{$msg_obj->{'parse_queue'}});
+	  delete $msg_obj->{'parse_queue'};
+        }
+      }
     }
   }
 
@@ -594,7 +624,14 @@
 sub _parse_multipart {
   my($self, $toparse) = @_;
 
-  my ($msg, $boundary, $body) = @{$toparse};
+  my ($msg, $boundary, $body, $subparse) = @{$toparse};
+
+  # we're not supposed to be a leaf, so prep ourselves
+  $msg->{'body_parts'} = [];
+
+  # the next set of objects will be one level deeper
+  $subparse--;
+
   dbg("message: parsing multipart, got boundary: ".(defined $boundary ? $boundary : ''));
 
   # NOTE: The MIME boundary REs here are very specific to be mostly RFC 1521
@@ -657,7 +694,7 @@
       ($part_msg->{'type'}, $p_boundary) = Mail::SpamAssassin::Util::parse_content_type($part_msg->header('content-type'));
       $p_boundary ||= $boundary;
       dbg("message: found part of type ".$part_msg->{'type'}.", boundary: ".(defined $p_boundary ? $p_boundary : ''));
-      push(@{$self->{'parse_queue'}}, [ $part_msg, $p_boundary, $part_array ]);
+      push(@{$self->{'parse_queue'}}, [ $part_msg, $p_boundary, $part_array, $subparse ]);
       $msg->add_body_part($part_msg);
 
       # rfc 1521 says /^--boundary--$/, some MUAs may just require /^--boundary--/
@@ -746,7 +783,6 @@
   $msg->{'type'} = ($ct[0] !~ m@^multipart/@i || defined $boundary ) ? $ct[0] : 'text/plain';
   $msg->{'charset'} = $ct[2];
 
-
   # attempt to figure out a name for this attachment if there is one ...
   my $disp = $msg->header('content-disposition') || '';
   if ($disp =~ /name="?([^\";]+)"?/i) {
@@ -758,32 +794,6 @@
 
   $msg->{'raw'} = $body;
   $msg->{'boundary'} = $boundary;
-
-  # If this part is a message/* part, and the parent isn't also a
-  # message/* part (ie: the main part) go ahead and parse into a tree.
-  if ($msg->{'type'} =~ /^message\b/i) {
-    # Get the part ready...
-    my $message = $msg->decode();
-
-    if ($message) {
-      my $msg_obj = Mail::SpamAssassin::Message->new({
-      	message		=>	$message,
-	parsenow	=>	1,
-	normalize	=>	$self->{normalize},
-	});
-
-      $msg->add_body_part($msg_obj);
-
-      return;
-    }
-  }
-
-  # now that we've added the leaf node, let's go ahead and kill
-  # body_parts (used for sub-trees).  there's no point for a leaf to have it,
-  # and if the main and child parts are the same, we'll end up being recursive,
-  # and well, let's avoid that. ;)
-  #
-  delete $msg->{body_parts};
 }
 
 # ---------------------------------------------------------------------------

Modified: spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message/Node.pm
URL: http://svn.apache.org/viewvc/spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message/Node.pm?rev=429231&r1=429230&r2=429231&view=diff
==============================================================================
--- spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message/Node.pm (original)
+++ spamassassin/branches/tvd-message-queue/lib/Mail/SpamAssassin/Message/Node.pm Sun Aug  6 20:14:17 2006
@@ -55,15 +55,11 @@
   my $self = {
     headers		=> {},
     raw_headers		=> {},
-    body_parts		=> [],
     header_order	=> []
   };
 
   # deal with any parameters
   my($opts) = @_;
-  if (defined $opts->{'subparse'}) {
-    $self->{subparse} = $opts->{'subparse'};
-  }
   $self->{normalize} = $opts->{'normalize'} || 0;
 
   bless($self,$class);