-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnats2json.pl
executable file
·100 lines (92 loc) · 1.93 KB
/
gnats2json.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/pkg/bin/perl
use JSON;
use Data::Dumper;
my $txt;
my %p, %h;
my $infield = "Header"; #email headers, first field
#split the file into header, info, audit-trail, state-changes, unformatted
my @header, @info, @audit, @state, @unformatted;
@lines = <>; #oh well
#if ($lines[0] =~ m#^From #) { shift @lines; } #discard this line
while ( my $l = shift @lines ) {
if ($l =~ m#^>Number:#) {
unshift @lines, $l;
last;
} else {
chomp $l;
push @header, $l;
}
}
#@header = grep { ! s/^$//g } @headerOrig;
while ( my $l = shift @lines ) {
if ($l =~ m#^>Audit-Trail:#) {
unshift @lines, $l;
last;
} else {
chomp $l;
push @body, $l;
}
}
while ( my $l = shift @lines ) {
if ($l =~ m#^>Unformatted:#) {
unshift @lines, $l;
last;
} else {
chomp $l;
push @audit, $l;
}
}
while ( my $l = shift @lines ) {
chomp $l;
push @unformatted, $l;
}
my $in = "";
foreach my $l (@header) {
chomp $l;
if ($l =~ m#^(.+):\s+(.+)$#) {
$in = $1;
$h{$in} = $2;
} elsif ( $l =~ m#^From .+$#) {
$h{"mailaudit"} = $l;
} else {
$h{$in} .= "$l\n";
}
}
chomp %h;
$in = ""; foreach my $l (@body) {
chomp $l;
if ($l =~ m#^(>[A-Z].+?):\s*(.*$)#) {
$in = $1;
$b{$in} = $2;
} else {
$b{$in} .= "$l\n";
}
}
chomp %b;
$in = "";
my @audittrail;
shift @audit; #throw away '>Audit-Trail:'
foreach my $l (@audit) {
chomp $l;
if ($l =~ m#^From: #) {
push @audittrail, $l;
$in = $#audittrail;
} elsif ($l =~ m#^(?:State|Responsible)-Changed-From-To#) {
push @audittrail, $l;
$in = $#audittrail;
} else {
$audittrail[$in] .= $l;
}
}
#print Dumper \%p;
#my %{$p{Number}} => %p;
#push(@j, \%p);
#my $json = to_json( \@j, {pretty => 1} );
#my $cmd = "curl -vvv localhost:8983/solr/update/json -H 'Content-type:application/json' -d \'$json\'";
#print "$cmd\n";
#qx#"$cmd"#;
#print "$json";
#print Dumper \@audit;
print Dumper \%h;
print Dumper \%b;
print Dumper \@audittrail;