[Rt-devel] [script] Automatic removal of bounce-generating addresses from ticket

Petter Reinholdtsen pere at hungry.com
Fri Jan 13 10:26:21 EST 2006


Our RT installation are mostly feed using email, and our users have
not quite learned to limit their request to only one queue.  This
typically give us two tickets, one in each queue, with CC set to the
address of the other queue.  When someone add correspondence to these
tickets, RT tries to send an email to the queue address of the other
queue, and the RT administrators (for example me), get an email from
RT with the report of a suspected bounce.

I am getting tired of this, so I sat down to write a script to
automatically get rid of these Requestor, CC and AdminCC entries.  The
script is below, and it seem to work.  The only problem is that it is
dead slow.  It uses several seconds per ticket.  I see 2-3 seconds
between '.' is printed in debug mode.  Am I doing something wrong
here?


#!/site/perl-5.8.6/bin/perl
#
# Author: Petter Reinholdtsen
# Date:   2006-01-13
#
# Look at all open tickets, and remove all queue addresses from
# requestor, cc and admincc.  This reduces the amount of bounce emails
# sent to the RT admins.


use warnings;
use strict;

use lib ("/site/rttest/local/lib", "/site/rttest/lib");

package RT;

use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);
use RT::Date;
use RT::Queue;
use RT::Queues;
use RT::Tickets;

my $debug = 1;

# Find all queue addresses of enabled queues
my @queueaddrs = ();

$| = 1;
initialize();

my $user = RT::User->new( $RT::SystemUser );

load_queue_addresses();

# Loop over all tickets, remove addresses (log changes)

my $tickets = new RT::Tickets($RT::SystemUser);
$tickets->LimitStatus(VALUE => 'new');
$tickets->LimitStatus(VALUE => 'open');
$tickets->LimitStatus(VALUE => 'stalled');

if ($debug) {
    my $queue = new RT::Queue($RT::SystemUser);
    $queue->Load("www-drift");
    $tickets->LimitQueue(VALUE => $queue->Id);
}

# Sort tickets in to lists for each owner
my $principal = RT::Principal->new( $RT::SystemUser );
my $group = RT::Group->new( $RT::SystemUser );
while (my $ticket = $tickets->Next) {
    print ":" if $debug;
    # check Requestor, Cc, AdminCc, remove the addresses listed in @queueaddrs
    foreach my $type ( "Cc", "AdminCc", "Requestor" ) {
        print "." if $debug;
        $group->LoadTicketRoleGroup( Type => $type, Ticket => $ticket->Id );
        next unless ( $group->id );

        for my $address (@queueaddrs) {
            $user->LoadByEmail( $address );
            $principal->Load( $user->Id );
            if ($group->HasMember( $principal )) {
                my $id = $ticket->Id;
                if ($debug) {
                    print "Want to remove $address as $type from ticket $id\n";
                } else {
                    $RT::Logger->info("Removed queue address $address ".
                                      "as $type from ticket $id");
                    $ticket->DeleteWatcher(Type => $type,
                                           PrincipalId => $principal->Id);
                }
            }
        }
    }
}


sub initialize {
    CleanEnv();       # Clean our the environment
    RT::LoadConfig(); # Load the RT configuration
    RT::Init();       # Initialise RT
}

sub load_queue_addresses {
    my $queues = new RT::Queues($RT::SystemUser);
    $queues->LimitToEnabled();
    foreach my $queue (@{$queues->ItemsArrayRef()}) {
        for my $address ($queue->CorrespondAddress, $queue->CommentAddress) {
            next unless $user->LoadByEmail( $address );
            push @queueaddrs, $address;
            print "Found queue address '$address'\n" if $debug;
        }
    }
}


More information about the Rt-devel mailing list