ParseFollowupMessageForTicketCcs

From Request Tracker Wiki
Revision as of 16:20, 6 April 2016 by Admin (talk | contribs) (2 revisions imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

ParseFollowupMessageForTicketCcs

After becoming more proficient in writing RT scrips, I decided to convert the patch below into a scrip so we wouldn't have to worry about patching after upgrading. It also works with correspondences made via the web instead of only via email like the patch below. See AddWatchersOnCorrespond for more information on the scrip version.

Automatically add ticket [CC]s from followup emails

The ParseNewMessageForTicketCcs option in SiteConfig will automatically add CC Watchers to a ticket during new ticket creation. We wanted the same behavior for email followups to existing tickets, so I patched the RT/Interface/Email.pm module to do this if the ParseFollowupMessageForTicketCcs option is also set in your SiteConfig.

Note: Both options use the RTAddressRegexp variable to avoid problems.

Patch written for and tested with RT version 3.4.5.


RT/Interface/Email.pm

diff -urN /usr/lib/rt/RT/Interface/Email.pm-dist /usr/lib/rt/RT/Interface/Email.pm --- /usr/lib/rt/RT/Interface/Email.pm-dist 2005-11-14 17:35:40.000000000 -0500 +++ /usr/lib/rt/RT/Interface/Email.pm 2006-07-21 13:30:02.000000000 -0400 @@ -746,6 +746,7 @@ my $Ticket = RT::Ticket->new($CurrentUser); <code><pre> # {{{ If we don't have a ticket Id, we're creating a new ticket </pre></code> + my $new_ticket = 1; if ( (!$SystemTicket || !$SystemTicket->Id) && grep /^(comment|correspond)$/, @actions ) { @@ -784,6 +785,8 @@ $args{'ticket'} = $id; <code><pre> # }}} </pre></code> + } else { + $new_ticket = 0; } <code><pre> $Ticket-&gt;Load( $args{'ticket'} ); </pre></code> @@ -799,6 +802,21 @@ return ( 0, $message ); } <ul> <li># JAS - Add Ticket CCs for followups to current tickets also:</li> <li>if (not $new_ticket and $RT::ParseFollowupMessageForTicketCcs) {</li> <li>my @Cc = ParseCcAddressesFromHead(</li> <li>Head => $head,</li> <li>CurrentUser => $CurrentUser,</li> <li>QueueObj => $SystemQueueObj</li> <li>);</li> <li>foreach (@Cc) {</li> <li>$Ticket->AddWatcher(</li> <li>Type => 'Cc',</li> <li>Email => $_,</li> <li>);</li> <li>}</li> <li>}</li> <li># }}} foreach my $action( @actions ) { # If the action is comment, add a comment.


Later we decided to make RT also add senders as Cc watchers, so I created a new version of the patch. This version also verifies that the email address is not already a watcher of the ticket or queue before it is added.

Note: The person being added needs to have permission to see the ticket, which means you will probably have to grant the System group Everyone, Watch rights (globally or for the necessary Queues) for this to work.

Warning: "It's worth noting that this patch opens up a potentially dangerous hole. A malicious user could easily make themselves CC of ...all your tickets. In some organizations, this may not matter. But it's a showstopper here." --JV (from rt-users mailing list). Therefore, be aware and take appropriate precautions if you use this.


diff -urN /usr/lib/rt/RT/Interface/Email.pm-dist /usr/lib/rt/RT/Interface/Email.pm --- /usr/lib/rt/RT/Interface/Email.pm-dist 2005-11-14 17:35:40.000000000 -0500 +++ /usr/lib/rt/RT/Interface/Email.pm 2007-02-07 14:52:44.000000000 -0500 @@ -346,13 +346,17 @@ <code><pre> my (@Addresses); </pre></code> <ul> <li># JAS - Add new From addresses in case the ticket was privately forwarded</li> <li># to a new interested third party who then replies to the ticket.</li> <li>my $From = $args{'Head'}->get('Reply-To') || $args{'Head'}->get('From') || $args{'Head'}->get('Sender');</li> <li>my @FromObjs = Mail::Address->parse($From); my @ToObjs = Mail::Address->parse($args{'Head'}->get('To')); my @CcObjs = Mail::Address->parse($args{'Head'}->get('Cc'));</li> <li>foreach my $AddrObj (@ToObjs, @CcObjs) {</li> <li>foreach my $AddrObj (@FromObjs, @ToObjs, @CcObjs) { my $Address = $AddrObj->address; $Address = $args{'CurrentUser'}->UserObj->CanonicalizeEmailAddress($Address);</li> <li>next if ($args{'CurrentUser'}->EmailAddress =~ /^\Q$Address\E$/i);</li> <li><code><pre> next if ($args{'CurrentUser'}-&gt;EmailAddress and $args{'CurrentUser'}-&gt;EmailAddress =~ /^\Q$Address\E$/i); next if ($args{'QueueObj'}-&gt;CorrespondAddress =~ /^\Q$Address\E$/i); next if ($args{'QueueObj'}-&gt;CommentAddress =~ /^\Q$Address\E$/i); next if (RT::EmailParser-&gt;IsRTAddress($Address)); </pre></code> @@ -746,6 +750,7 @@ my $Ticket = RT::Ticket->new($CurrentUser); # {{{ If we don't have a ticket Id, we're creating a new ticket</li> <li>my $new_ticket = 1; if ( (!$SystemTicket || !$SystemTicket->Id) && grep /^(comment|correspond)$/, @actions ) { @@ -784,6 +789,8 @@ $args{'ticket'} = $id; <code><pre> # }}} </pre></code></li> <li>} else {</li> <li><code><pre>$new_ticket = 0; </pre></code> } $Ticket->Load( $args{'ticket'} ); @@ -799,6 +806,41 @@ return ( 0, $message ); }</li> <li># JAS - Add Ticket CCs for followups to current tickets also:</li> <li># - Allow the CurrentUser to be added if they are not already on this ticket list.</li> <li>if (not $new_ticket and $RT::ParseFollowupMessageForTicketCcs) {</li> <li>my @Cc = ParseCcAddressesFromHead(Head => $head,</li> <li>CurrentUser => $RT::SystemUser, # Don't ignore the CurrentUser.</li> <li>QueueObj => $SystemQueueObj,</li> <li>);</li> <li>foreach (@Cc) {</li> <li>my $user = RT::User->new($RT::SystemUser);</li> <li>$user->LoadByEmail($_);</li> <li>my $principal;</li> <li>$principal = $user->PrincipalId if $user->Id;</li> <li># Only add people to this ticket if they are not already a watcher of the ticket or queue:</li> <li>if (not ($Ticket->IsWatcher(Type => 'Requestor', Email => $_)</li> <li>or $Ticket->IsWatcher(Type => 'Cc', Email => $_)</li> <li>or $Ticket->IsWatcher(Type => 'AdminCc', Email => $_)</li> <li>or $Ticket->QueueObj->IsWatcher(Type => 'Cc', PrincipalId => $principal)</li> <li>or $Ticket->QueueObj->IsWatcher(Type => 'AdminCc', PrincipalId => $principal))) {</li> <li># NOTE: Passing both the Email address & PrincipalId for that address to the RT::Ticket:AddWatcher</li> <li># function will allow it to pass the ACL checks with only Watch rights, otherwise ModifyTicket</li> <li># rights will be needed since it assumes that someone is trying to add another person.</li> <li># See: RT/Ticket_Overlay.pm:AddWatcher for more info.</li> <li>my ($ret, $msg) = $Ticket->AddWatcher(Type => 'Cc',</li> <li>Email => $_,</li> <li>PrincipalId => $principal,</li> <li>);</li> <li>if ($ret) {</li> <li>$RT::Logger->info("New Cc watcher added to ticket #". $Ticket->Id .": ". $_);</li> <li>} else {</li> <li>$RT::Logger->error("Failed to add new Cc watcher to ticket #". $Ticket->Id .": ". $_ ." - ". $msg);</li> <li>}</li> <li>}</li> <li>}</li> <li>}</li> <li> # }}} foreach my $action( @actions ) { # If the action is comment, add a comment.