You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spamassassin.apache.org by do...@apache.org on 2006/12/17 04:17:08 UTC

svn commit: r487934 - in /spamassassin/trunk/lib/Mail/SpamAssassin: Message/Node.pm PerMsgStatus.pm Plugin/SPF.pm

Author: dos
Date: Sat Dec 16 19:17:07 2006
New Revision: 487934

URL: http://svn.apache.org/viewvc?view=rev&rev=487934
Log:
bug 5239: move hackish M::SA::M:Node crap to M::SA::PMS where it should be

Modified:
    spamassassin/trunk/lib/Mail/SpamAssassin/Message/Node.pm
    spamassassin/trunk/lib/Mail/SpamAssassin/PerMsgStatus.pm
    spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/SPF.pm

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Message/Node.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Message/Node.pm?view=diff&rev=487934&r1=487933&r2=487934
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Message/Node.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Message/Node.pm Sat Dec 16 19:17:07 2006
@@ -652,14 +652,6 @@
 return the raw headers, and the second parameter (optional) is whether
 or not to include the mbox separator.
 
-The third and fourth parameters (optional) define the first and last
-values, respectively, of an index range of Received headers.  Both of
-the Received headers specified by the indexes and the headers found
-between these Received headers will be returned, in order.  Use undef
-as the first index value to start at the first header (possibly before
-the first Received header).  Use undef as the last index value to end
-at the last header (probably after the last received header).
-
 If get_all_header() is called in an array context, an array will be
 returned with each header entry in a different element.  In a scalar
 context, the headers are returned in a single scalar.
@@ -668,30 +660,17 @@
 
 # build it and it will not bomb
 sub get_all_headers {
-  my ($self, $raw, $include_mbox, $start_rcvd_index, $end_rcvd_index) = @_;
+  my ($self, $raw, $include_mbox) = @_;
   $raw ||= 0;
   $include_mbox ||= 0;
-  $start_rcvd_index = -1 unless defined $start_rcvd_index;
 
   my @lines = ();
 
   # precalculate destination positions based on order of appearance
   my $i = 0;
-  my $lines_skipped = 0;
-  my $cur_rcvd_index = -1;
   my %locations;
-
   for my $k (@{$self->{header_order}}) {
-    last if (defined $end_rcvd_index && $end_rcvd_index <= $cur_rcvd_index);
-    my $name = lc($k);
-    $cur_rcvd_index++ if ($name eq 'received');
-    if ($cur_rcvd_index < $start_rcvd_index) {
-      push(@{$locations{$name}}, -1); # indicate we skipped the header
-      $lines_skipped++;
-      $i++;
-      next;
-    }
-    push(@{$locations{$name}}, $i++);
+    push(@{$locations{lc($k)}}, $i++);
   }
 
   # process headers in order of first appearance
@@ -702,10 +681,8 @@
   {
     # get all same-name headers and poke into correct position
     my $positions = $locations{$name};
-    INSTANCE: for my $contents ($self->get_header($name, $raw)) {
+    for my $contents ($self->get_header($name, $raw)) {
       my $position = shift @{$positions};
-      last INSTANCE unless defined $position;
-      next if $position == -1; # any headers we skipped above
       $size += length($name) + length($contents) + 2;
       if ($size > MAX_HEADER_LENGTH) {
 	$self->{'truncated_header'} = 1;
@@ -714,9 +691,6 @@
       $lines[$position] = $self->{header_order}->[$position] . ": $contents";
     }
   }
-
-  # remove these, they'll be undefined if we skipped over them
-  splice @lines, 0, $lines_skipped if $lines_skipped;
 
   # skip undefined lines if we truncated
   @lines = grep { defined $_ } @lines if $self->{'truncated_header'};

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/PerMsgStatus.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/PerMsgStatus.pm?view=diff&rev=487934&r1=487933&r2=487934
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/PerMsgStatus.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/PerMsgStatus.pm Sat Dec 16 19:17:07 2006
@@ -1476,33 +1476,28 @@
   }
   # ALL-TRUSTED: entire trusted raw headers
   elsif ($request eq 'ALL-TRUSTED') {
-    # if we didn't find any trusted relays, none of the headers are trusted
-    return if $self->{last_trusted_relay_index} == -1;
-    $result = $self->{msg}->get_all_headers(1, 0, undef,
-					$self->{last_trusted_relay_index}+1);
+    # '+1' since we added the received header even though it's not considered
+    # trusted, so we know that those headers can be trusted too
+    return $self->get_all_hdrs_in_rcvd_index_range(
+			undef, $self->{last_trusted_relay_index}+1);
   }
   # ALL-INTERNAL: entire internal raw headers
-  elsif ($request eq 'ALL-INTERNAL') { 
-    # if we didn't find any internal relays, none of the headers are internal
-    return if $self->{last_internal_relay_index} == -1;
-    $result = $self->{msg}->get_all_headers(1, 0, undef,
-					$self->{last_internal_relay_index}+1);
+  elsif ($request eq 'ALL-INTERNAL') {
+    # '+1' for the same reason as in ALL-TRUSTED above
+    return $self->get_all_hdrs_in_rcvd_index_range(
+			undef,	$self->{last_internal_relay_index}+1);
   }
   # ALL-UNTRUSTED: entire untrusted raw headers
   elsif ($request eq 'ALL-UNTRUSTED') {
-    # if we didn't find any trusted relays get all the headers, otherwise get
-    # all the headers after the last trusted relay
-    my $start_index = ($self->{last_trusted_relay_index} == -1 ? undef :
-			$self->{last_trusted_relay_index} + 1);
-    $result = $self->{msg}->get_all_headers(1, 0, $start_index);
+    # '+1' for the same reason as in ALL-TRUSTED above
+    return $self->get_all_hdrs_in_rcvd_index_range(
+			$self->{last_trusted_relay_index}+1, undef);
   }
   # ALL-EXTERNAL: entire external raw headers
   elsif ($request eq 'ALL-EXTERNAL') {
-    # if we didn't find any internal relays get all the headers, otherwise get
-    # all the headers after the last internal relay
-    my $start_index = ($self->{last_internal_relay_index} == -1 ? undef :
-			$self->{last_internal_relay_index} + 1);
-    $result = $self->{msg}->get_all_headers(1, 0, $start_index);
+    # '+1' for the same reason as in ALL-TRUSTED above
+    return $self->get_all_hdrs_in_rcvd_index_range(
+			$self->{last_internal_relay_index}+1, undef);
   }
   # EnvelopeFrom: the SMTP MAIL FROM: address
   elsif ($request eq 'EnvelopeFrom') {
@@ -2305,6 +2300,50 @@
   $envf =~ s/^<*//gs;                # remove <
   $envf =~ s/>*\s*$//gs;        # remove >, whitespace, newlines
   return $envf;
+}
+
+###########################################################################
+
+# helper for get(ALL-*).  get() caches its results, so don't call this
+# directly unless you need a range of headers not covered by the ALL-*
+# psuedo-headers!
+
+# Get all the headers found between an index range of received headers, the
+# index doesn't care if we could parse the received headers or not.
+# Use undef for the $start_rcvd or $end_rcvd numbers to start/end with the
+# first/last header in the message, otherwise indicate the index number you
+# want to start/end at.  Set $include_start_rcvd or $include_end_rcvd to 0 to
+# indicate you don't want to include the received header found at the start or
+# end indexes... basically toggles between [s,e], [s,e), (s,e], (s,e).
+sub get_all_hdrs_in_rcvd_index_range {
+  my ($self, $start_rcvd, $end_rcvd, $include_start_rcvd, $include_end_rcvd) = @_;
+
+  # prevent bad input causing us to return the first header found
+  return if (defined $end_rcvd && $end_rcvd < 0);
+
+  $include_start_rcvd = 1 unless defined $include_start_rcvd;
+  $include_end_rcvd = 1 unless defined $include_end_rcvd;
+
+  my $cur_rcvd_index = -1;  # none found yet
+  my $result = '';
+  foreach my $hdr (split("\n", $self->get('ALL'))) {
+    if ($hdr =~ /^received: /i) {
+      $cur_rcvd_index++;
+      next if (defined $start_rcvd && !$include_start_rcvd &&
+		$start_rcvd == $cur_rcvd_index);
+      last if (defined $end_rcvd && !$include_end_rcvd &&
+		$end_rcvd == $cur_rcvd_index);
+    }
+    if ((!defined $start_rcvd || $start_rcvd <= $cur_rcvd_index) &&
+	(!defined $end_rcvd || $cur_rcvd_index < $end_rcvd)) {
+      $result .= $hdr."\n";
+    }
+    elsif (defined $end_rcvd && $cur_rcvd_index == $end_rcvd) {
+      $result .= $hdr."\n";
+      last;
+    }
+  }
+  return ($result eq '' ? undef : $result);
 }
 
 ###########################################################################

Modified: spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/SPF.pm
URL: http://svn.apache.org/viewvc/spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/SPF.pm?view=diff&rev=487934&r1=487933&r2=487934
==============================================================================
--- spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/SPF.pm (original)
+++ spamassassin/trunk/lib/Mail/SpamAssassin/Plugin/SPF.pm Sat Dec 16 19:17:07 2006
@@ -303,18 +303,15 @@
   } else {
     $scanner->{checked_for_received_spf_header} = 1;
     dbg("spf: checking to see if the message has a Received-SPF header that we can use");
-    # if we didn't find any internal relays, none of the headers are internal
-    my @internal_hdrs;
-    unless ($scanner->{last_internal_relay_index} == -1) {	# -1 means there are none
-      @internal_hdrs = $scanner->{msg}->get_all_headers(1, 0, undef,
-					$scanner->{last_internal_relay_index}+1);
-      unless ($scanner->{conf}->{use_newest_received_spf_header}) {
-	@internal_hdrs = reverse(@internal_hdrs);
-      } else {
-	dbg("spf: starting with the newest Received-SPF headers first");
-      }
+
+    my @internal_hdrs = split("\n", $scanner->get('ALL-INTERNAL'));
+    unless ($scanner->{conf}->{use_newest_received_spf_header}) {
+      # look for the LAST (earliest in time) header, it'll be the most accurate
+      @internal_hdrs = reverse(@internal_hdrs);
+    } else {
+      dbg("spf: starting with the newest Received-SPF headers first");
     }
-    # look for the LAST (earliest in time) header, it'll be the most accurate
+
     foreach my $hdr (@internal_hdrs) {
       if ($hdr =~ /^received-spf: /i) {
 	dbg("spf: found a Received-SPF header added by an internal host: $hdr");