blib/lib/Mail/Exim/MainLogParser.pm

filter: Statementstmt 91.2% 83 / 91 Branchbran 81.0% 47 / 58 Conditioncond 51.1% 22 / 43 MC/DCmcdc 27.2% 6 / 22 Subroutinesub 85.7% 12 / 14 Podpod 100% 5 / 5 totaltotal 75.1% 175 / 233 CC 46 SCAR 35.3
CC
46
Cov
79%
_exim_log_main__parse
55.3
_exim_log_main__compose
13.2
new
2.1
CRAP 34.1
↑ Summary
1 package Mail::Exim::MainLogParser;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 $VERSION = '0.02';
9 2 @ISA = qw(Exporter);
10 2 @EXPORT = qw();
11 2 @EXPORT_OK = qw(&EximMainLoglineParse EximMainLoglineCompose);
12 2 %EXPORT_TAGS = ();
13 }
14  
15 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use vars qw(%EXIM_FLAGS %EXIM_FIELD_IDENFIERS);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # As of 2017-06-08
19 # Source: http://www.exim.org/exim-html-current/doc/html/spec_html/ch-log_files.html
20 2 %EXIM_FLAGS = (
21 '<=' => 'message arrival',
22 '(=' => 'message fakereject',
23 '=>' => 'normal message delivery',
24 '->' => 'additional address in same delivery',
25 '>>' => 'cutthrough message delivery',
26 '*>' => 'delivery suppressed by -N',
27 '**' => 'delivery failed; address bounced',
28 '==' => 'delivery deferred; temporary problem',
29 );
30 2 %EXIM_FIELD_IDENFIERS = (
31 A => 'authenticator name (and optional id and sender)',
32 C => ('SMTP confirmation on delivery'. '; '.
33 'command list for "no mail in SMTP session”'),
34 CV => 'certificate verification status',
35 D => 'duration of "no mail in SMTP session”',
36 DN => 'distinguished name from peer certificate',
37 DS => 'DNSSEC secured lookups',
38 DT => 'on => lines: time taken for a delivery',
39 F => 'sender address (on delivery lines)',
40 H => 'host name and IP address',
41 I => 'local interface used',
42 K => 'CHUNKING extension used',
43 id => 'message id for incoming message',
44 P => ('on <= lines: protocol used'. '; '.
45 'on => and ** lines: return path'),
46 PRDR => 'PRDR extension used',
47 PRX => 'on <= and => lines: proxy address',
48 Q => 'alternate queue name',
49 QT => ('on => lines: time spent on queue so far'. '; '.
50 'on "Completed” lines: time spent on queue'),
51 R => ('on <= lines: reference for local bounce'. '; '.
52 'on => >> ** and == lines: router name'),
53 S => 'size of message in bytes',
54 SNI => 'server name indication from TLS client hello',
55 ST => 'shadow transport name',
56 T => ('on <= lines: message subject (topic)'. '; '.
57 'on => ** and == lines: transport name'),
58 U => 'local user or RFC 1413 identity',
59 X => 'TLS cipher suite'
60 );
61 }
62  
63  
64 sub new
65 2 ▶{
Subroutine new: called CC=2 SCAR=7.6
Pod documented
66 2 my ($class, %parameters) = @_;
67  
68 2 ▶ my $self = bless ({}, ref ($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
69  
70 2 return $self;
71 }
72  
73 15 ▶sub _exim_log_main__parse($) {
Subroutine _exim_log_main__parse: called CC=33 SCAR=40.1
74 15 ▶ my $line = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
150
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
75 15 ▶ if ($line !~ m/^\d{4}\-\d{2}-\d{2}\s\d{2}\:\d{2}\:\d{2}( [\-\+]\d{4})?/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\d{4}\-\d{2}-\d{2}\s\d{2}\:\d{2}\:\d{2}( [\-\+]\d{4})?/o) 0 15 15
76 0 warn __PACKAGE__,": Exim log line not in expected format.";
77 0 return undef;
78 }
79 # Split the line by spaces, and examine each element
80 15 my @line = split(/\s/,$line);
81 # To pass this simple filter the string must have a minimum of three elements
82 # <date> <time> [ <timezone> | <processid> | <eximid> | <reject or panic message> ]
83 15 ▶ return undef unless scalar @line >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @line >= 3 0 15 15
84 15 my $l = {
85 'date' => shift @line,
86 'time' => shift @line
87 };
88 # detect if a time zone is provided # Exim: log_timezone=true
89 # 2003-04-25 11:17:07 +0100 Start queue run: pid=12762
90 15 ▶ if ($line[0] =~ /\+\d{4}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line[0] =~ /\+\d{4}/) 1 14 15
91 1 $l->{'timezone'} = shift @line;
92 }
93  
94 # detect is the Exim process Id is provided # Exim: log_selector = +pid
95 # The parser should not be modifying data, only understanding and separating it
96 # For this reason: 'pid': "[<pid>]" , as given in the log line
97 15 ▶ if ($line[0] =~ /\[\d+\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line[0] =~ /\[\d+\]/) 4 11 15
98 4 $l->{'pid'} = shift @line;
99 }
100  
101 # Exim ID Format: 1dIyz2-0002mc-5x
102 15 ▶ if ($line[0] =~ /[a-zA-Z0-9]{6}\-[a-zA-Z0-9]{6}\-[a-zA-Z0-9]{2}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line[0] =~ /[a-zA-Z0-9]{6}\-[a-zA-Z0-9]{6}\-[a-zA-Z0-9]{2}/) { } 13 2 15
103 13 $l->{'eximid'} = shift @line;
104 } else {
105 # $l->{'eximid'} = undef;
106 }
107  
108 # Exim log line flag
109 15 ▶ if ($line[0] =~ /\<\=|\(\=|\=\>|\-\>|\>\>|\*\>|\*\*|\=\=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line[0] =~ /\<\=|\(\=|\=\>|\-\>|\>\>|\*\>|\*\*|\=\=/) { } 7 8 15
110 7 $l->{'flag'} = shift @line;
111 } else {
112 # mail rejected or completed
113 # $l->{'flag'} = undef;
114 }
115  
116 15 ▶ return $l if ! scalar @line >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if !scalar(@line) >= 1 0 15 15
117  
118 # If the flag is undefined or delivery failure, then detect either the message or the field identifiers
119 # 2020-06-07 18:10:24 1ji4Qp-0002jY-UD Completed
120 # 2020-06-07 18:12:18 1ji4Sf-0002jl-Vk H=ugso.tenet.odessa.ua (ugso.odessa.ua) [195.138.65.238] F=<test@giftvoucherkiosk.com> rejected after DATA: Your message scored 17.4 SpamAssassin point. Report follows:
121 15 ▶ if ( ((!exists $l->{'flag'}) || (!defined $l->{'flag'})) || ($l->{'flag'} eq "**") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $l->{'flag'} && defined $l->{'flag'} or $l->{'flag'} eq '**') { } 9 6 15
Condition83%5/6
not exists $l->{"flag"} && defined $l->{"flag"} or $l->{"flag"} eq "**"
l!l&&r!l&&!r
816
exists $l->{'flag'} && defined $l->{'flag'}
!ll&&!rl&&r
807
MC/DC66%2/3
not exists $l->{"flag"} && defined $l->{"flag"} or $l->{"flag"} eq "**"
exists $l->{'flag'} defined $l->{'flag'} $l->{"flag"} eq "**"
Truth table75%3/4
not(A && B) or C
A= exists $l->{'flag'}B= defined $l->{'flag'}C= $l->{"flag"} eq "**"
ABCresult
0XX1
10X1
1111
1100
122  
123 9 ▶ if ( ($line[0] !~ /^[A-Zid]{1,4}\=.*/) || (($line[0] =~ /^([A-Zid]{1,4})\=.*/) && (!exists $EXIM_FIELD_IDENFIERS{$1})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line[0] =~ /^[A-Zid]{1,4}\=.*/ or $line[0] =~ /^([A-Zid]{1,4})\=.*/ and not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}) 7 2 9
Condition50%3/6
not $line[0] =~ /^[A-Zid]{1,4}\=.*/ or $line[0] =~ /^([A-Zid]{1,4})\=.*/ and not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
l!l&&r!l&&!r
702
$line[0] =~ /^([A-Zid]{1,4})\=.*/ and not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
!ll&&!rl&&r
020
MC/DC33%1/3
not $line[0] =~ /^[A-Zid]{1,4}\=.*/ or $line[0] =~ /^([A-Zid]{1,4})\=.*/ and not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
not $line[0] =~ /^[A-Zid]{1,4}\=.*/ $line[0] =~ /^([A-Zid]{1,4})\=.*/ not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
Truth table50%2/4
not(A) or B and not(C)
A= not $line[0] =~ /^[A-Zid]{1,4}\=.*/B= $line[0] =~ /^([A-Zid]{1,4})\=.*/C= not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
ABCresult
1XX1
0111
00X0
0100
124 # If the element does not start with a known field identifier, we assume the whole line is a message
125 7 ▶ while (scalar @line >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line >= 1) 25 7 32
126 25 ▶ $l->{'message'} .= (" ") if defined $l->{'message'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $l->{"message"} 18 7 25
127 25 $l->{'message'} .= shift @line;
128 }
129 }
130  
131 # If the flag is defined and not a delivery failure, then we expect a mail destination (e.g. email, system pipe, system file, etc)
132 } else {
133  
134 # Exim Address could be email address, pipe, file, and a string combination of several of these elements
135 # 2020-06-07 23:44:57 1ji9ea-0003oh-Tt => :blackhole: <realperson@realdomain> R=pipe_to_useraddress
136 # 2020-06-07 21:49:23 1ji7qj-0003Ud-Ss => |/usr/bin/listmgr-queue listmgr <listmgr@realdomain> R=pipe_to_listmgr T=address_pipe
137 # Skip email detection if element is AAAA=somevalue or id=somevalue, a simple field identifier matcher (where A is alpha)
138 # For deferred deliveries (i.e. == flag) email could be: some-identified-realperson=realdomain@some-domain
139 6 ▶ while ( (scalar @line >= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line >= 1 and !($line[0] =~ /^[A-Zid]{1,4}\=.*/) || $line[0] =~ /^([A-Zid]{1,4})\=.*/ && !exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}) 6 6 12
Condition55%5/9
scalar @line >= 1 and !($line[0] =~ /^[A-Zid]{1,4}\=.*/) || $line[0] =~ /^([A-Zid]{1,4})\=.*/ && !exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
!ll&&!rl&&r
066
not $line[0] =~ /^[A-Zid]{1,4}\=.*/ or $line[0] =~ /^([A-Zid]{1,4})\=.*/ and not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
l!l&&r!l&&!r
606
$line[0] =~ /^([A-Zid]{1,4})\=.*/ and not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
!ll&&!rl&&r
060
MC/DC25%1/4
scalar @line >= 1 and !($line[0] =~ /^[A-Zid]{1,4}\=.*/) || $line[0] =~ /^([A-Zid]{1,4})\=.*/ && !exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
scalar @line >= 1 not $line[0] =~ /^[A-Zid]{1,4}\=.*/ $line[0] =~ /^([A-Zid]{1,4})\=.*/ not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
Truth table40%2/5
A and not(B) or C and not(D)
A= scalar @line >= 1B= not $line[0] =~ /^[A-Zid]{1,4}\=.*/C= $line[0] =~ /^([A-Zid]{1,4})\=.*/D= not exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
140 && ( ($line[0] !~ /^[A-Zid]{1,4}\=.*/)
141 || (($line[0] =~ /^([A-Zid]{1,4})\=.*/) && (!exists $EXIM_FIELD_IDENFIERS{$1}))
142 )
143 ) {
144 # The element is appended to address until matching a known field identifier
145 6 ▶ $l->{'address'} .= (" ") if defined $l->{'address'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $l->{"address"} 0 6 6
146 6 $l->{'address'} .= shift @line;
147 }
148 }
149  
150 15 ▶ return $l if ! scalar @line >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if !scalar(@line) >= 1 7 8 15
151  
152 # Exim field identifiers and identifier messages
153 8 $l->{'args'} = [];
154 8 ▶ while (scalar @line >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line >= 1) 30 8 38
155 # Matching anything that looks like a field identifier, rather than looking each up in the $EXIM_FIELD_IDENFIERS hash
156 30 ▶ if ( ($line[0] =~ /^([A-Zid]{1,4})\=(.*)/) && (exists $EXIM_FIELD_IDENFIERS{$1}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line[0] =~ /^([A-Zid]{1,4})\=(.*)/ and exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}) { } 30 0 30
Condition33%1/3
$line[0] =~ /^([A-Zid]{1,4})\=(.*)/ and exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
!ll&&!rl&&r
0030
MC/DC0%0/2
$line[0] =~ /^([A-Zid]{1,4})\=(.*)/ and exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
$line[0] =~ /^([A-Zid]{1,4})\=(.*)/ exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
Truth table33%1/3
A and B
A= $line[0] =~ /^([A-Zid]{1,4})\=(.*)/B= exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
ABresult
0X0
100
111
157 30 my $this_arg = $1;
158 30 my $this_val = $2;
159 30 shift @line;
160 30 ▶ while ( (scalar @line >= 1) && (($line[0] !~ /^([A-Zid]{1,4})\=(.*)/) || (!exists $EXIM_FIELD_IDENFIERS{$1})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line >= 1 and not $line[0] =~ /^([A-Zid]{1,4})\=(.*)/ && exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}) 44 30 74
Condition83%5/6
scalar @line >= 1 and not $line[0] =~ /^([A-Zid]{1,4})\=(.*)/ && exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
!ll&&!rl&&r
82244
$line[0] =~ /^([A-Zid]{1,4})\=(.*)/ && exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
!ll&&!rl&&r
44022
MC/DC66%2/3
scalar @line >= 1 and not $line[0] =~ /^([A-Zid]{1,4})\=(.*)/ && exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
scalar @line >= 1 $line[0] =~ /^([A-Zid]{1,4})\=(.*)/ exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
Truth table75%3/4
A and not(B && C)
A= scalar @line >= 1B= $line[0] =~ /^([A-Zid]{1,4})\=(.*)/C= exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
ABCresult
0XX0
1110
10X1
1101
161 44 $this_val .= (" " . shift @line);
162 }
163 30 push(@{$l->{'args'}},{$this_arg => $this_val});
164 } else {
165 # If the field identifier is not detected, fall back to message
166 # This should only happen if the text in the element does not match any field identifiers
167 0 $l->{'message'} = shift @line;
168 0 ▶ while ( (scalar @line >= 1) && (($line[0] !~ /^([A-Zid]{1,4})\=(.*)/) || (!exists $EXIM_FIELD_IDENFIERS{$1})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line >= 1 and not $line[0] =~ /^([A-Zid]{1,4})\=(.*)/ && exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}) 0 0 0
Condition0%0/6
scalar @line >= 1 and not $line[0] =~ /^([A-Zid]{1,4})\=(.*)/ && exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
!ll&&!rl&&r
000
$line[0] =~ /^([A-Zid]{1,4})\=(.*)/ && exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
!ll&&!rl&&r
000
MC/DC0%0/3
scalar @line >= 1 and not $line[0] =~ /^([A-Zid]{1,4})\=(.*)/ && exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
scalar @line >= 1 $line[0] =~ /^([A-Zid]{1,4})\=(.*)/ exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
Truth table0%0/4
A and not(B && C)
A= scalar @line >= 1B= $line[0] =~ /^([A-Zid]{1,4})\=(.*)/C= exists $Mail::Exim::MainLogParser::EXIM_FIELD_IDENFIERS{$1}
ABCresult
0XX0
1110
10X1
1101
169 0 $l->{'message'} .= (" " . shift @line);
170 }
171 }
172 }
173  
174 8 ▶ if (scalar @line >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line >= 1) 0 8 8
175 0 warn ("Error Parsing Line: $line\n"."Unparsed log line data: ".join("; ",@line)."\n");
176 }
177  
178 8 return $l;
179 }
180  
181 15 ▶sub _exim_log_main__compose ($) {
Subroutine _exim_log_main__compose: called CC=13 SCAR=25.8
182 15 ▶ my $parsed = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
150
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
183 15 ▶ return undef unless ref $parsed eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parsed eq "HASH" 0 15 15
184 15 my @s_args;
185 15 foreach my $arg (@{$parsed->{'args'}}) {
186 30 push(@s_args, map{qq{$_=$arg->{$_}}} keys %$arg)
187 }
188 15 my @s_line;
189 15 ▶ push(@s_line,$parsed->{'date'}) if exists $parsed->{'date'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parsed->{"date"} 15 0 15
190 15 ▶ push(@s_line,$parsed->{'time'}) if exists $parsed->{'time'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parsed->{"time"} 15 0 15
191 15 ▶ push(@s_line,$parsed->{'timezone'}) if exists $parsed->{'timezone'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parsed->{"timezone"} 1 14 15
192 15 ▶ push(@s_line,$parsed->{'pid'}) if exists $parsed->{'pid'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parsed->{"pid"} 4 11 15
193 15 ▶ push(@s_line,$parsed->{'eximid'}) if exists $parsed->{'eximid'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parsed->{"eximid"} 13 2 15
194 15 ▶ push(@s_line,$parsed->{'flag'}) if exists $parsed->{'flag'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parsed->{"flag"} 7 8 15
195 15 ▶ push(@s_line,$parsed->{'address'}) if exists $parsed->{'address'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parsed->{"address"} 6 9 15
196 15 ▶ push(@s_line, join(" ", @s_args)) if @s_args >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @s_args >= 1 8 7 15
197 15 ▶ push(@s_line,$parsed->{'message'}) if exists $parsed->{'message'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $parsed->{"message"} 7 8 15
198 15 return(join(" ", @s_line));
199 }
200  
201 0 ▶sub EximMainLoglineParse($) {
Subroutine EximMainLoglineParse: not called CC=1 SCAR=6.9
Pod documented
202 0 return _exim_log_main__parse($_[0]);
203 }
204  
205 0 ▶sub EximMainLoglineCompose($) {
Subroutine EximMainLoglineCompose: not called CC=1 SCAR=6.9
Pod documented
206 0 return _exim_log_main__compose($_[0]);
207 }
208  
209 15 ▶sub parse($) {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
210 15 my $self = shift;
211 15 return _exim_log_main__parse($_[0]);
212 }
213  
214 15 ▶sub compose($) {
Subroutine compose: called CC=1 SCAR=0.0
Pod documented
215 15 my $self = shift;
216 15 return _exim_log_main__compose($_[0]);
217 }
218  
219 2 1;
220 __END__
↑ Summary