[PATCH] Shred users not matching a given pattern

Sometimes it can be useful to shred users that have email addresses or names that don’t match a given pattern. This patch adds two extra options to the Users shredder plugin called not_email and not_name to allow users that don’t match a given email and/or name to be selected.

 lib/RT/Shredder/Plugin/Users.pm | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/lib/RT/Shredder/Plugin/Users.pm b/lib/RT/Shredder/Plugin/Users.pm
index 43bf624..81f9dea 100644
--- a/lib/RT/Shredder/Plugin/Users.pm
+++ b/lib/RT/Shredder/Plugin/Users.pm
@@ -68,10 +68,18 @@ B<< Default value is C<disabled>. >>
 
 User name mask.
 
+=head2 not_name - mask
+
+A "not matching" user name mask.
+
 =head2 email - mask
 
 Email address mask.
 
+=head2 not_email - mask
+
+A "not matching" email address mask.
+
 =head2 member_of - group identifier
 
 Using this option users that are members of a particular group can
@@ -122,7 +130,7 @@ want to use C<replace_relations> option.
 sub SupportArgs
 {
     return $_[0]->SUPER::SupportArgs,
-           qw(status name email member_of not_member_of replace_relations no_tickets no_ticket_transactions);
+           qw(status name not_name email not_email member_of not_member_of replace_relations no_tickets no_ticket_transactions);
 }
 
 sub TestArgs
@@ -139,9 +147,15 @@ sub TestArgs
     if( $args{'email'} ) {
         $args{'email'} = $self->ConvertMaskToSQL( $args{'email'} );
     }
+    if( $args{'not_email'} ) {
+        $args{'not_email'} = $self->ConvertMaskToSQL( $args{'not_email'} );
+    }
     if( $args{'name'} ) {
         $args{'name'} = $self->ConvertMaskToSQL( $args{'name'} );
     }
+    if( $args{'not_name'} ) {
+        $args{'not_name'} = $self->ConvertMaskToSQL( $args{'not_name'} );
+    }
     if( $args{'member_of'} or $args{'not_member_of'} ) {
         foreach my $group_option ( qw(member_of not_member_of) ){
             next unless $args{$group_option};
@@ -196,6 +210,12 @@ sub Run
                   VALUE => $self->{'opt'}{'email'},
                 );
     }
+    if( $self->{'opt'}{'not_email'} ) {
+        $objs->Limit( FIELD => 'EmailAddress',
+                  OPERATOR => 'NOT MATCHES',
+                  VALUE => $self->{'opt'}{'not_email'},
+                );
+    }
     if( $self->{'opt'}{'name'} ) {
         $objs->Limit( FIELD => 'Name',
                   OPERATOR => 'MATCHES',
@@ -203,6 +223,13 @@ sub Run
                   CASESENSITIVE => 0,
                 );
     }
+    if( $self->{'opt'}{'not_name'} ) {
+        $objs->Limit( FIELD => 'Name',
+                  OPERATOR => 'NOT MATCHES',
+                  VALUE => $self->{'opt'}{'not_name'},
+                  CASESENSITIVE => 0,
+                );
+    }
     if( $self->{'opt'}{'member_of'} ) {
         $objs->MemberOfGroup( $self->{'opt'}{'member_of'} );
     }
-- 
2.1.4

It should be noted that this also requires the fix for the bug in shredding users in 4.4.2.