[Rt-commit] rtir branch 4.0/parse-attachments-for-ip created. 4.0.1rc1-87-g5e77e2b1

BPS Git Server git at git.bestpractical.com
Thu Nov 18 15:33:49 UTC 2021


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rtir".

The branch, 4.0/parse-attachments-for-ip has been created
        at  5e77e2b1031df8e3a08410ebec117733a8274dc3 (commit)

- Log -----------------------------------------------------------------
commit 5e77e2b1031df8e3a08410ebec117733a8274dc3
Author: Craig Kaiser <craig at bestpractical.com>
Date:   Mon Nov 19 17:11:42 2018 -0500

    Extract IP from more attachments if main content doesn't have any.
    
    Once there are some IP extracted from an attachment, then the rest of
    attachments won't be checked.

diff --git a/lib/RT/Action/RTIR_FindIP.pm b/lib/RT/Action/RTIR_FindIP.pm
index 6d2be824..34cebfff 100644
--- a/lib/RT/Action/RTIR_FindIP.pm
+++ b/lib/RT/Action/RTIR_FindIP.pm
@@ -99,8 +99,11 @@ sub Commit {
 
     my $how_many_can = $cf->MaxValues;
 
-    my $attach = $self->TransactionObj->ContentObj;
-    return 1 unless $attach && $attach->id;
+    my $correspond = $self->TransactionObj->ContentObj;
+
+    # Grab any attachments on the correspondence
+    my $attachments =  $self->TransactionObj->Attachments;
+    return 1 unless $attachments && $attachments->Count;
 
     my %existing;
     for( @{$cf->ValuesForObject( $ticket )->ItemsArrayRef} ) {
@@ -114,36 +117,79 @@ sub Commit {
 
     my $spots_left = $how_many_can - keys %existing;
 
-    my $content = $attach->Content || '';
-    while ( $content =~ m/$IP_re/go ) {
+    # If we added an IP from the email content, then we do not check the attachments
+    if ( $correspond && $correspond->Id ) {
+        my $correspond_content = $correspond->Content || '';
+
+        my $found_ip_in_content;
+        while ( $correspond_content =~ m/$IP_re/go ) {
+            $found_ip_in_content = $self->FindIPsFromContent(
+                Content  => $correspond_content,
+                Spots    => \$spots_left,
+                Existing => \%existing,
+                CF       => $cf
+            ) || $found_ip_in_content;
+        }
+        return 1 if $found_ip_in_content;
+    }
+
+    # Check our attachments for IPs
+    while ( my $attach = $attachments->Next ) {
+        my $content = $attach->Content || '';
+
+        $self->FindIPsFromContent(
+            Content  => $content,
+            Spots    => \$spots_left,
+            Existing => \%existing,
+            CF       => $cf
+        );
+    }
+
+    return 1;
+}
+
+sub FindIPsFromContent {
+    my $self = shift;
+    my %args = (
+        Content  => '',
+        Spots    => 0,
+        Existing => {},
+        CF       => undef,
+        @_
+    );
+    my $spots_left = $args{'Spots'};
+
+    my $starting_count = $$spots_left;
+
+    while ( $args{'Content'} =~ m/$IP_re/go ) {
         if ( $1 && defined $2 ) { # IPv6/mask
             my $range = $2 == 128 ? $1 : (Net::CIDR::cidr2range( "$1/$2" ))[0]
                 or next;
-            $spots_left -= $self->AddIP(
-                IP => $range, CustomField => $cf, Skip => \%existing
+            $$spots_left -= $self->AddIP(
+                IP => $range, CustomField => $args{'CF'}, Skip => $args{'Existing'}
             );
         }
         elsif ( $1 ) { # IPv6
-            $spots_left -= $self->AddIP(
-                IP => $1, CustomField => $cf, Skip => \%existing
+            $$spots_left -= $self->AddIP(
+                IP => $1, CustomField => $args{'CF'}, Skip => $args{'Existing'}
             );
         }
         elsif ( $3 ) { # IPv4
-            $spots_left -= $self->AddIP(
-                IP => $3, CustomField => $cf, Skip => \%existing
+            $$spots_left -= $self->AddIP(
+                IP => $3, CustomField => $args{'CF'}, Skip => $args{'Existing'}
             );
         }
         elsif ( $4 && defined $5 ) { # IPv4/mask
             my $cidr = join( '.', map { $_||0 } (split /\./, $4)[0..3] ) ."/$5";
             my $range = (Net::CIDR::cidr2range( $cidr ))[0] or next;
-            $spots_left -= $self->AddIP(
-                IP => $range, CustomField => $cf, Skip => \%existing
+            $$spots_left -= $self->AddIP(
+                IP => $range, CustomField => $args{'CF'}, Skip => $args{'Existing'}
             );
         }
-        return 1 unless $spots_left;
+        return 1 unless $$spots_left;
     }
-
-    return 1;
+    # Return true if we found any IPs
+    return $starting_count ne $spots_left;
 }
 
 sub AddIP {

-----------------------------------------------------------------------


hooks/post-receive
-- 
rtir


More information about the rt-commit mailing list