From 7e527d5a847d2513569889a75fe8a23e0d03bffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20Gullikstad=20Hem?= Date: Mon, 15 Nov 2021 21:53:32 +0100 Subject: [PATCH 1/4] Make loop-protection allow mail between different RT queues --- lib/RT/Action/SendEmail.pm | 3 ++- lib/RT/Interface/Email.pm | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/RT/Action/SendEmail.pm b/lib/RT/Action/SendEmail.pm index 345d3a503a5..6c66e3db178 100644 --- a/lib/RT/Action/SendEmail.pm +++ b/lib/RT/Action/SendEmail.pm @@ -670,7 +670,8 @@ sub SetRTSpecialHeaders { } } - $self->SetHeader( 'X-RT-Loop-Prevention', RT->Config->Get('rtname') ); + my $looppreventionheader = $self->TransactionObj->Message->First->GetHeader( 'X-RT-Loop-Prevention' ); + $self->SetHeader( 'X-RT-Loop-Prevention', $self->TicketObj->QueueObj->Name . '.' . RT->Config->Get('rtname') . ',' . $looppreventionheader ); $self->SetHeader( 'X-RT-Ticket', RT->Config->Get('rtname') . " #" . $self->TicketObj->id() ); $self->SetHeader( 'X-Managed-by', diff --git a/lib/RT/Interface/Email.pm b/lib/RT/Interface/Email.pm index 21abf0ec468..20ebd59d8cd 100644 --- a/lib/RT/Interface/Email.pm +++ b/lib/RT/Interface/Email.pm @@ -206,6 +206,7 @@ sub Gateway { if IsMachineGeneratedMail( Message => $Message, Subject => $Subject, + Queue => $SystemQueueObj->Name; ); # Make all errors from here on out bounce back to $ErrorsTo @@ -487,12 +488,13 @@ sub IsMachineGeneratedMail { my %args = ( Message => undef, Subject => undef, + Queue => undef, @_ ); my $head = $args{'Message'}->head; my $IsAutoGenerated = CheckForAutoGenerated($head); - my $IsALoop = CheckForLoops($head); + my $IsALoop = CheckForLoops($head, $args{'Queue'}); my $owner_mail = RT->Config->Get('OwnerEmail'); @@ -542,11 +544,13 @@ this RT instance, by checking the C header. sub CheckForLoops { my $head = shift; + my $queue = shift; # If this instance of RT sent it our, we don't want to take it in my $RTLoop = Encode::decode( "UTF-8", $head->get("X-RT-Loop-Prevention") || "" ); chomp ($RTLoop); # remove that newline - if ( $RTLoop eq RT->Config->Get('rtname') ) { + if ( $RTLoop =~ ( $queue . '.' . RT->Config->Get('rtname') . ',') ) { + $RT::Logger->warning("Dropping mail because X-RT-Loop-Protection contains " . $queue . '.' . RT->Config->Get('rtname') . ','; return 1; } From 523157cbd9b7b85886847412c0b4e24510f9900a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20Gullikstad=20Hem?= Date: Mon, 15 Nov 2021 21:54:10 +0100 Subject: [PATCH 2/4] Get correct tag id when multiple tags in subject --- lib/RT/Interface/Email.pm | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/RT/Interface/Email.pm b/lib/RT/Interface/Email.pm index 20ebd59d8cd..c779c14cb36 100644 --- a/lib/RT/Interface/Email.pm +++ b/lib/RT/Interface/Email.pm @@ -216,7 +216,7 @@ sub Gateway { $bare_MailError->(To => $ErrorsTo, MIMEObj => $Message, @_) }; - $args{'ticket'} ||= ExtractTicketId( $Message ); + $args{'ticket'} ||= ExtractTicketId( $Message, $SystemQueueObj->SubjectTag ); my $SystemTicket = RT::Ticket->new( RT->SystemUser ); $SystemTicket->Load( $args{'ticket'} ) if ( $args{'ticket'} ) ; @@ -611,10 +611,11 @@ will be used during ticket creation. sub ExtractTicketId { my $entity = shift; + my $tag = shift; my $subject = Encode::decode( "UTF-8", $entity->head->get('Subject') || '' ); chomp $subject; - return ParseTicketId( $subject, $entity ); + return ParseTicketId( $subject, $entity, $tag ); } =head3 ParseTicketId @@ -631,9 +632,10 @@ Returns the id if a match is found. Otherwise returns undef. sub ParseTicketId { my $Subject = shift; my $Entity = shift; + my $tag = shift; my $rtname = RT->Config->Get('rtname'); - my $test_name = RT->Config->Get('EmailSubjectTagRegex') || qr/\Q$rtname\E/i; + my $test_name = qr/\Q$tag\E/; # We use @captures and pull out the last capture value to guard against # someone using (...) instead of (?:...) in $EmailSubjectTagRegex. From 6452bb96ab49af2ef607ef78f14eb35bb11acc76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20Gullikstad=20Hem?= Date: Mon, 15 Nov 2021 22:23:17 +0100 Subject: [PATCH 3/4] Fix typo --- lib/RT/Interface/Email.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/RT/Interface/Email.pm b/lib/RT/Interface/Email.pm index c779c14cb36..b62e43dd77b 100644 --- a/lib/RT/Interface/Email.pm +++ b/lib/RT/Interface/Email.pm @@ -206,7 +206,7 @@ sub Gateway { if IsMachineGeneratedMail( Message => $Message, Subject => $Subject, - Queue => $SystemQueueObj->Name; + Queue => $SystemQueueObj->Name, ); # Make all errors from here on out bounce back to $ErrorsTo From 0626d44d1d3132216b377cbb5d17f0a1355e0149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20Gullikstad=20Hem?= Date: Mon, 15 Nov 2021 22:32:33 +0100 Subject: [PATCH 4/4] Fix typo --- lib/RT/Interface/Email.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/RT/Interface/Email.pm b/lib/RT/Interface/Email.pm index b62e43dd77b..184ffedc726 100644 --- a/lib/RT/Interface/Email.pm +++ b/lib/RT/Interface/Email.pm @@ -550,7 +550,7 @@ sub CheckForLoops { my $RTLoop = Encode::decode( "UTF-8", $head->get("X-RT-Loop-Prevention") || "" ); chomp ($RTLoop); # remove that newline if ( $RTLoop =~ ( $queue . '.' . RT->Config->Get('rtname') . ',') ) { - $RT::Logger->warning("Dropping mail because X-RT-Loop-Protection contains " . $queue . '.' . RT->Config->Get('rtname') . ','; + $RT::Logger->warning("Dropping mail because X-RT-Loop-Protection contains " . $queue . '.' . RT->Config->Get('rtname')); return 1; }