summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Palfrader <peter@palfrader.org>2017-08-17 18:12:15 +0200
committerPeter Palfrader <peter@palfrader.org>2017-08-17 18:12:15 +0200
commit6d2e99d6cdaf88ccdcb28db9951b8347a66feacd (patch)
tree0444ddb0d266dc53d7f9c26630e08b15309a84ef
parent65c638c180c1c12ae1fd3bbb68a956db5520dfcf (diff)
parse-trac: handle nested mime messagesHEADmaster
-rwxr-xr-xparse-trac65
1 files changed, 17 insertions, 48 deletions
diff --git a/parse-trac b/parse-trac
index c20d061..ff0286a 100755
--- a/parse-trac
+++ b/parse-trac
@@ -24,7 +24,8 @@
use strict;
use English;
use MIME::Base64;
-
+use File::Basename;
+use Mail::Message;
my $HOME = '/home/commit/';
my $MAX_LINES = 4;
@@ -48,60 +49,27 @@ die ("Project is not a nice name.\n") unless defined $project;
my $is_base64 = 0;
open (MAIL, $ARGV[1]) || die ("Cannot open $ARGV[1]: $!\n");
-my @mail = <MAIL>;
+my $email = Mail::Message->read(\*MAIL);
close(MAIL);
-my $line;
-
-my @headers;
-my $lastline = undef;
-while (defined($line = shift @mail)) {
- my $chomped = $line;
- chomp $chomped;
- if ($chomped =~ m/^\s+/) {
- $lastline .= ' '.$line;
- } elsif ($chomped =~ m/^$/) {
- push @headers, $lastline if defined $lastline;
- push @headers, $line;
- push @headers, @mail;
- last
- } else {
- push @headers, $lastline if defined $lastline;
- $lastline = $line;
- }
-}
-my $is_a_reply;
-my $tracproject;
-my $ticketid;
-my $url;
-
-while (defined($line = shift @headers)) {
- if ($line =~ m/^Subject:/i) {
- $is_a_reply = ($line =~ m/^Subject:\s*Re:/i);
- } elsif ($line =~ m/^X-Trac-Project:\s*(.*)/i) {
- $tracproject = $1;
- } elsif ($line =~ m/^X-Trac-Ticket-URL:\s*(.*)/i) {
- $url = $1;
- #$url =~ s/^https:/http:/;
- } elsif ($line =~ m/^X-Trac-Ticket-ID:\s*(.*)/i) {
- $ticketid = $1;
- } elsif ($line =~ m/^Content-Transfer-Encoding: base64$/s) {
- $is_base64 = 1;
- } elsif ($line =~ m/^$/) {
- last;
- }
-};
-my $body = join '', @mail;
-$body = decode_base64($body) if ($is_base64);
-@mail = split(/\r?\n/, $body);
+my $is_a_reply = defined $email->head->get('In-Reply-To');
+my $tracproject = $email->head->get('X-Trac-Project');
+my $ticketid = $email->head->get('X-Trac-Ticket-ID');
+my $url = $email->head->get('X-Trac-Ticket-URL');
+
+my $part = $email;
+while ($part->isMultipart()) {
+ $part = $part->body->part(0);
+}
+my @mail = split(/\r?\n/, $part->decoded);
my $batch_job;
my $reporter;
my $comment;
my @summary;
-while (defined($line = shift @mail)) {
+while (defined(my $line = shift @mail)) {
chomp $line;
last if $line =~ /^-----------------/;
last if (defined $batch_job && $line eq "");
@@ -114,7 +82,7 @@ while (defined($line = shift @mail)) {
};
my $summary = join ' ', @summary;
-while (defined($line = shift @mail)) {
+while (defined(my $line = shift @mail)) {
if ($line =~ /Reporter:\s*(.*?)\s*\|/i and not defined $reporter) {
$reporter = $1;
};
@@ -122,7 +90,7 @@ while (defined($line = shift @mail)) {
last if $line =~ /^-----------------/
}
my @body;
-while (defined($line = shift @mail)) {
+while (defined(my $line = shift @mail)) {
last if $line =~ /^--/;
push @body, $line;
}
@@ -148,6 +116,7 @@ if (defined $batch_job) {
}
my $MAXBODYLINES = 5;
+my $body;
if (scalar @body > $MAXBODYLINES+1) { # if it's one more let it through, [...] takes a line too
$body = join "\n", @body[0 .. $MAXBODYLINES-1];
$body .= "[...]\n";