[rt-devel] Draft patch to add references and in-reply-to parsing
Petter Reinholdtsen
pere at hungry.com
Wed Jan 28 13:06:09 EST 2004
[Petter Reinholdtsen]
> Thank you for your suggestion. I'll try to continue, but do not
> really know this DBDx module API yet.
Continuing my stumble in the dark, I came up with this patch. I still
can not say I understand the DBIx::SearchBuilder API, but based on the
example from Ruslan U. Zakirov I continued by adding the MessageId
value and the loop to extract the ticket ids.
The code is completely untested, as I do not have source access to our
RT test installation yet.
Am I on the right track?
diff -ur src-3-0-8/lib/RT/Interface/Email.pm src-3-0-8-386linuxlibc63/lib/RT/Interface/Email.pm
--- src-3-0-8/lib/RT/Interface/Email.pm 2004-01-02 22:23:39.000000000 +0100
+++ src-3-0-8-386linuxlibc63/lib/RT/Interface/Email.pm 2004-01-28 18:54:51.000000000 +0100
@@ -475,6 +475,110 @@
$args{'ticket'} ||= $parser->ParseTicketId($Subject);
+ unless ( $args{'ticket'} ) {
+ # Based on info from <URL:http://www.jwz.org/doc/threading.html>
+ my @msgids = ();
+
+ my $references = $head->get('References') || '';
+ my $inreplyto = $head->get('In-Reply-To') || '';
+
+ push(@msgids, split(/\s+/, $references)) if ($references);
+
+ if ($inreplyto) {
+ if ($inreplyto =~ m/(<[^>]+>)/) {
+ push(@msgids, $1);
+ } else {
+ $RT::Logger->log(level => 'debug',
+ message => "Gateway: Unhandled In-Reply-To format: '$inreplyto'"
+ );
+ }
+ }
+
+ # Map Message-id(s) to ticket id
+ my @ids = ();
+ my %checked;
+ my $msgid;
+ for $msgid (@msgids) {
+ next if $checked{$msgid}; # Already looked up this message-id
+
+ # Look up ticket IDs given MessageID of attachment
+
+ my $Attachs = $RT::Attachments->new($RT::SystemUser);
+ $Attachs->Limit( FIELD => 'MessageId',
+ OPERATOR => '=',
+ VALUE => $msgid
+ );
+ $Attachs->Limit( FIELD => 'ContentType',
+ OPERATOR => '=',
+ VALUE => 'text/plain'
+ );
+ $Attachs->Limit( FIELD => 'ContentType',
+ OPERATOR => '=',
+ VALUE => 'text/html'
+ );
+ $Attachs->Limit( FIELD => 'Parent',
+ OPERATOR => '=',
+ VALUE => '0'
+ );
+
+ my $trs = $Attachs->NewAlias('Transactions');
+ my $tis = $Attachs->NewAlias('Tickets');
+
+ $Attachs->Join( ALIAS1 => 'main',
+ FIELD1 => 'TransactionId',
+ ALIAS2 => $trs,
+ FIELD2 => 'id'
+ );
+
+ $Attachs->Join( ALIAS1 => $trs,
+ FIELD1 => 'Ticket',
+ ALIAS2 => $tis,
+ FIELD2 => 'id'
+ );
+
+ $Attachs->Limit( ALIAS => $trs,
+ FIELD => 'Type',
+ OPERATOR => '=',
+ VALUE => 'Comment'
+ );
+
+ $Attachs->Limit( ALIAS => $trs,
+ FIELD => 'Type',
+ OPERATOR => '=',
+ VALUE => 'Correspond'
+ );
+
+ while (my $ticket = $Attachs->Next) {
+ my $ticketid = $Attachs->Ticket;
+ print "Found ID $ticketid\n";
+ push(@ids, $ticketid) if $ticketid;
+ }
+
+ $checked{$msgid} = 1;
+ }
+
+ # Reduce list of IDs to unique IDs only
+ my $id;
+ my %idhash;
+ for $id (sort @ids) {
+ $idhash{$id} = 1;
+ }
+ @ids = sort keys %idhash;
+
+ # If the Message-id(s) are already in the database, use their
+ # ticked-id
+ if (1 < @ids) {
+ $RT::Logger->log(level => 'debug',
+ message => "Gateway: Several possible tickets: " .
+ join(",", @ids)
+ );
+ }
+
+ # Just pick the first. Not sure how we should handle several
+ # ticket ids
+ $args{'ticket'} = $ids[0] if (@ids);
+ }
+
my $SystemTicket;
if ( $args{'ticket'} ) {
$SystemTicket = RT::Ticket->new($RT::SystemUser);
More information about the Rt-devel
mailing list