[BACK]Return to ByQueueRegex.pm CVS log [TXT][DIR] Up to [local] / RT / Interface / Email / Filter

File: [local] / RT / Interface / Email / Filter / ByQueueRegex.pm (download)

Revision 1.1, Fri Jan 25 02:34:21 2013 UTC (11 years, 5 months ago) by andrew
Branch: MAIN
CVS Tags: HEAD

Not sure why this wasn't here.

package RT::Interface::Email::Filter::ByQueueRegex;

# $AFresh1: ByQueueRegex.pm,v 1.1 2013/01/25 02:34:21 andrew Exp $
########################################################################
# Filter::ByQueueRegex *** Change RT Queue based on plussed address
########################################################################
# Copyright (c) 2011 Andrew Fresh <andrew@afresh1.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
########################################################################
use strict;
use warnings;

use Email::Address;
use RT::EmailParser;

use Data::Dumper;

sub GetCurrentUser {
    my %args = (@_);

    unless ( $args{'CurrentUser'} ) {
        $RT::Logger->error(
            "Filter::TakeAction executed when "
            ."CurrentUser (actor) is not authorized. "
            ."Most probably you want to add Auth::MailFrom plugin before "
            ."Filter::TakeAction in the \@MailPlugins config."
        );
        return ( $args{'CurrentUser'}, $args{'AuthLevel'} );
    }

    # If a ticket is being modified, or for some reason doesn't 
    # have a queue, we don't need to bother.
    if ( $args{'Ticket'}->id || !$args{'Queue'} ) {
        return ( $args{'CurrentUser'}, $args{'AuthLevel'} );
    }

    my @addresses;
    foreach my $address (
	map { $_->address } 
        map { Email::Address->parse( $_ ) }
        map { $args{'Message'}->head->get( $_ )  }
 	qw/ from to cc /
    ) {
       $RT::Logger->debug("Found address [$address]");
       push @addresses, $address;
    }
    
    my $qs = RT::Queues->new($RT::SystemUser); #$args{'CurrentUser'});
    $qs->UnLimit;
    $qs->FindAllRows;

    my %queues;
    while (my $q = $qs->Next) {
        my $value = $q->FirstCustomFieldValue('EmailRegex');
        next unless $value;
        $RT::Logger->debug("Queue [" . $q->id . "] " . $q->Name . " -> EmailRegex " . $value);
        $queues{ $value } = $q;
    }

    ADDRESS: foreach my $address (@addresses) {
        while (my ($re, $q) = each %queues) {
            RT::Logger->debug("Checking address $address =~ /$re/");
            my $match = 0;
            eval { $match = $address =~ /$re/ };
            if ($@) {
                $RT::Logger->warning(
                    "Error in EmailRegex for " . $q->Name . " /$re/: $@");
            }
            elsif ($match) {
                $args{'Queue'}->Load($q->id);

                my $msgid = $args{'Message'}->head->get('Message-Id')
                    || 'message';
                $msgid =~ s/\s+//;

                $RT::Logger->info(
                    "Routing $msgid to queue " . $args{'Queue'}->Name );

                last ADDRESS;
            }
        }
    }

    return ( $args{'CurrentUser'}, $args{'AuthLevel'} );
}

=head1 NAME

RT::Interface::Email::Filter::ByQueueRegex - Change Queue based on Regex

=head1 SYNOPSIS

    @RT::MailPlugins = ("Filter::ByQueueRegex", ...);

=head1 DESCRIPTION

Chooses queue based on plussed email addresses.  If the queue
specified by the plussed address doesn't exist, defaults to the
queue it would have gone to anyway.

Uses RT::EmailParser->isRTAddress to decide which addresses to
check.

For example rt+support@example.com would end up in a support queue
if it exists.

=cut

eval "require RT::Interface::Email::Filter::ByQueueRegex_Vendor";
die $@
    if ( $@
    && $@
    !~ qr{^Can't locate RT/Interface/Email/Filter/ByQueueRegex_Vendor.pm} );
eval "require RT::Interface::Email::Filter::ByQueueRegex_Local";
die $@
    if ( $@
    && $@
    !~ qr{^Can't locate RT/Interface/Email/Filter/ByQueueRegex_Local.pm} );

1;