https://rt-wiki.bestpractical.com/api.php?action=feedcontributions&user=Crazy4chrissi&feedformat=atomRequest Tracker Wiki - User contributions [en]2024-03-28T22:49:46ZUser contributionsMediaWiki 1.37.2https://rt-wiki.bestpractical.com/index.php?title=BounceMerge&diff=26436BounceMerge2016-11-17T09:02:49Z<p>Crazy4chrissi: Some bug fixes / Updates. Quotemeta added (e.g. versions from Debian backports contain + characters which caused the regex to fail) and RT-Ticket header is nowadays X-RT-Ticket.</p>
<hr />
<div>This is a scrip which checks if a newly generated ticket was created by a mail bounce.<br />
<br />
If yes, it tries to deduce the original ticket-id and merges this new ticket into the old one.<br />
<br />
The scrip logic is quite conservative, only if the same [[TicketID]] is found both in a quoted Message-ID and a RT-Ticket: header, then the scrip merges the tickets.<br />
<br />
Note that this requires an accurate Return-Path header. This should normally be added by the final MTA in the chain.<br />
<br />
Note also that this causes the logged creation message to be inaccurate, suggesting that the ticket being merged into has just been created.<br />
<br />
== Install ==<br />
<br />
<u>Condition:</u> On Create<br />
<br />
<u>Action:</u> Userdefined<br />
<br />
<u>Template:</u> Blank<br />
<br />
<u>Custom Condition:</u><br />
<br />
return 1; # n/a<br />
<br />
<u>Custom Action Preparation:</u><br />
<br />
return 1;<br />
<br />
<u>Custom Action Cleanup Code:</u><br />
<br />
<pre><br />
#<br />
# Merge mail bounce into old ticket.<br />
#<br />
# Otmar Lendl &lt;ol@bofh.priv.at&gt; 2009/07/24<br />
# Minor Bugfixes/Updates by Christopher Kramer, 2016/11/17<br />
#<br />
my $ticket = $self-&gt;TicketObj;<br />
my $tr = $self-&gt;TransactionObj;<br />
<br />
# possible sources of ticket-IDs:<br />
<br />
# Message-ID of the mail we sent:<br />
# example: Message-ID: &lt;rt-3.8.2-1611-1247727152-952.14150-6-0@CERT.at&gt;<br />
my $msgregex = '&lt;rt-'. quotemeta($RT::VERSION) .'-\d+-\d+-\d+\.(\d+)-\d+-\d+@'. quotemeta(RT-&gt;Config-&gt;Get('Organization')) .'&gt;';<br />
<br />
# RT-Ticket: header:<br />
# example: RT-Ticket: CERT.at #14150<br />
my $rttregex = '^X-RT-Ticket: ' . quotemeta(RT-&gt;Config-&gt;Get('rtname')) .' #(\d+)';<br />
<br />
my $Attachments = $tr-&gt;Attachments;<br />
# get first attachment<br />
my $a = $Attachments-&gt;Next;<br />
return(1) unless ($a);<br />
<br />
my $rp = $a-&gt;GetHeader('Return-Path');<br />
my $ct = $a-&gt;ContentType;<br />
<br />
# bounce?<br />
return(1) unless (($rp eq '&lt;&gt;') or ($ct eq 'multipart/report'));<br />
print STDERR "BounceMerge: Looks like a bounce ($rp/$ct). Trying to find ticket-ID.\n";<br />
# .. which is potentially hidden in other attachments. Collect them all first.<br />
my $mail = "";<br />
do {<br />
my $oh = $a-&gt;Headers;<br />
my $oc = $a-&gt;OriginalContent;<br />
$mail .= "\n----------------- " . $a-&gt;id . " ---------\n" . $oh if ($oh);<br />
$mail .= "\n----------------- " . $a-&gt;id . " ---------\n" . $oc if ($oc);<br />
} while ($a = $Attachments-&gt;Next);<br />
<br />
# look for a message-ID:<br />
<br />
my $id_msg = -1;<br />
my $id_hdr = -2;<br />
<br />
$id_msg = $1 if ($mail =~ /$msgregex/);<br />
$id_hdr = $1 if ($mail =~ /$rttregex/m);<br />
<br />
if (($id_hdr == $id_msg) and ($id_hdr != $ticket-&gt;EffectiveId) ) {<br />
print STDERR "BounceMerge: Removing requestors and merging into $id_msg\n";<br />
my @reqs = split(/[\s,]+/, $ticket-&gt;RequestorAddresses);<br />
foreach (@reqs) {<br />
$ticket-&gt;DeleteWatcher(<br />
Type =&gt; "Requestor",<br />
Email =&gt; $_,<br />
Silent =&gt; 1) ;<br />
}<br />
$ticket-&gt;MergeInto($id_msg);<br />
}<br />
<br />
return(1);<br />
</pre><br />
<br />
<br />
<br />
<br />
It's worth bearing in mind that there are cases where the bounces may not go into the queue you intended. The branch https://github.com/jmdh/rt/tree/3.8%2Fignore_subject_ticket_for_queues may be of interest, if so.</div>Crazy4chrissi