Sisimai/SMTP/Transcript.pm

filter: Statementstmt 98.6% 75 / 76 Branchbran 62.5% 15 / 24 Conditioncond 83.3% 20 / 24 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 87.1% 129 / 148 CC 25 SCAR 29.9
CC
25
Cov
89%
rise
26.7
BEGIN
1.0
BEGIN
1.0
CRAP 19.9
« Sisimai/Lhost/EinsUndEins.pm ↑ Summary Sisimai/RFC1123.pm »
1 package Sisimai::SMTP::Transcript;
2 4 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Sisimai::Eb;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Sisimai::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Sisimai::SMTP::Reply;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Sisimai::SMTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Sisimai::SMTP::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 ▶sub rise {
Subroutine rise: called CC=25 SCAR=32.9
Pod undocumented
12 # Decode the transcript of the SMTP session and makes the structured data
13 # @param [String] argv0 A transcript text MTA returned
14 # @param [String] argv1 A label string of a SMTP client
15 # @param [String] argv2 A label string of a SMTP server
16 # @return [Array] Structured data
17 # @return [undef] Failed to decode or the 1st argument is missing
18 # @since v5.0.0
19 12 my $class = shift;
20 12 ▶ my $argv0 = shift // return undef;
Condition100%2/2
shift() // (return undef)
l!l
111
MC/DC100%1/1
shift() // (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
21 11 ▶ my $argv1 = shift // '>>>'; # Label for an SMTP Client
Condition100%2/2
shift() // ">>>"
l!l
92
MC/DC100%1/1
shift() // ">>>"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
22 11 ▶ my $argv2 = shift // '<<<'; # Label for an SMTP Server
Condition100%2/2
shift() // "<<<"
l!l
83
MC/DC100%1/1
shift() // "<<<"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
23  
24 # 1. Replace label strings of SMTP client/server at the each line
25 11 ▶ $argv0 =~ s/^[ ]+$argv1\s+/>>> /gm; return undef unless index($argv0, '>>> ') > -1;
Branch: 1 branch
BranchTrueFalseTotal
unless index($argv0, ">>> ") != -1 5 6 11
26 6 ▶ $argv0 =~ s/^[ ]+$argv2\s+/<<< /gm; return undef unless index($argv0, '<<< ') > -1;
Branch: 1 branch
BranchTrueFalseTotal
unless index($argv0, "<<< ") != -1 0 6 6
27  
28 # 2. Remove strings until the first '<<<' or '>>>'
29 6 my $esmtp = [];
30 48 ▶ my $table = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 return {
32 48 'command' => undef, # SMTP command
33 'argument' => '', # An argument of each SMTP command sent from a client
34 'parameter' => {}, # Parameter pairs of the SMTP command
35 'response' => { # A Response from an SMTP server
36 'reply' => '', # - SMTP reply code such as 550
37 'status' => '', # - SMTP status such as 5.1.1
38 'text' => [], # - Response text lines
39 }
40 };
41 6 };
42  
43 6 my $cx = undef; # Current session for $esmtp
44 6 my $p1 = index($argv0, '>>>'); # Sent command
45 6 my $p2 = index($argv0, '<<<'); # Server response
46 6 ▶ if( $p2 < $p1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p2 < $p1) { } 6 0 6
47 # An SMTP server response starting with '<<<' is the first
48 6 push @$esmtp, $table->();
49 6 $cx = $esmtp->[-1];
50 6 $cx->{'command'} = $Sisimai::Eb::CeCONN;
51 6 ▶ $argv0 = substr($argv0, $p2,) if $p2 > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $p2 > -1 6 0 6
52  
53 } else {
54 # An SMTP command starting with '>>>' is the first
55 0 ▶ $argv0 = substr($argv0, $p1,) if $p1 > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 > -1 0 0 0
56 }
57  
58 # 3. Remove unused lines, concatenate folded lines
59 6 $argv0 = substr($argv0, 0, index($argv0, "\n\n") - 1); # Remove strings from the first blank line to the tail
60 6 $argv0 =~ s/\n[ ]+/ /g; # Concatenate folded lines to each previous line
61  
62 6 for my $e ( split("\n", $argv0) ) {
63 # 4. Read each SMTP command and server response
64 162 ▶ if( index($e, '>>> ') == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($e, '>>> ') == 0) { } 42 120 162
65 # SMTP client sent a command ">>> SMTP-command arguments"
66 42 ▶ my $thecommand = Sisimai::SMTP::Command->find($e) || next;
Condition50%1/2
'Sisimai::SMTP::Command'->find($e) || (next)
l!l
420
MC/DC0%0/1
'Sisimai::SMTP::Command'->find($e) || (next)
'Sisimai::SMTP::Command'->find($e)
Truth table50%1/2
A
A= 'Sisimai::SMTP::Command'->find($e)
Aresult
11
00
67 42 my $commandarg = Sisimai::String->sweep(substr($e, index($e, $thecommand) + length($thecommand),));
68 42 my $parameters = '';
69  
70 42 push @$esmtp, $table->();
71 42 $cx = $esmtp->[-1];
72 42 $cx->{'command'} = uc $thecommand;
73  
74 42 ▶ if( $thecommand eq $Sisimai::Eb::CeMAIL || $thecommand eq $Sisimai::Eb::CeRCPT || $thecommand eq $Sisimai::Eb::CeXFWD ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thecommand eq $Sisimai::Eb::CeMAIL or $thecommand eq $Sisimai::Eb::CeRCPT or $thecommand eq $Sisimai::Eb::CeXFWD) { } 24 18 42
Condition100%6/6
$thecommand eq $Sisimai::Eb::CeMAIL or $thecommand eq $Sisimai::Eb::CeRCPT or $thecommand eq $Sisimai::Eb::CeXFWD
l!l&&r!l&&!r
121218
$thecommand eq $Sisimai::Eb::CeMAIL or $thecommand eq $Sisimai::Eb::CeRCPT
l!l&&r!l&&!r
6630
MC/DC100%3/3
$thecommand eq $Sisimai::Eb::CeMAIL or $thecommand eq $Sisimai::Eb::CeRCPT or $thecommand eq $Sisimai::Eb::CeXFWD
$thecommand eq $Sisimai::Eb::CeMAIL $thecommand eq $Sisimai::Eb::CeRCPT $thecommand eq $Sisimai::Eb::CeXFWD
Truth table100%4/4
A or B or C
A= $thecommand eq $Sisimai::Eb::CeMAILB= $thecommand eq $Sisimai::Eb::CeRCPTC= $thecommand eq $Sisimai::Eb::CeXFWD
ABCresult
1XX1
01X1
0011
0000
75 # MAIL or RCPT
76 24 ▶ if( index($commandarg, 'FROM:') == 0 || index($commandarg, 'TO:') == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($commandarg, 'FROM:') == 0 or index($commandarg, 'TO:') == 0) { } 12 12 24
Condition100%3/3
index($commandarg, "FROM:") == 0 or index($commandarg, "TO:") == 0
l!l&&r!l&&!r
6612
MC/DC100%2/2
index($commandarg, "FROM:") == 0 or index($commandarg, "TO:") == 0
index($commandarg, "FROM:") == 0 index($commandarg, "TO:") == 0
Truth table100%3/3
A or B
A= index($commandarg, "FROM:") == 0B= index($commandarg, "TO:") == 0
ABresult
1X1
011
000
77 # >>> MAIL FROM: <neko@example.com> SIZE=65535
78 # >>> RCPT TO: <kijitora@example.org>
79 12 my $p4 = index($commandarg, '<');
80 12 my $p5 = index($commandarg, '>');
81 12 $cx->{'argument'} = substr($commandarg, $p4 + 1, $p5 - $p4 - 1);
82 12 $parameters = substr($commandarg, $p5 + 1,);
83  
84 } else {
85 # >>> XFORWARD NAME=neko2-nyaan3.y.example.co.jp ADDR=230.0.113.2 PORT=53672
86 # <<< 250 2.0.0 Ok
87 # >>> XFORWARD PROTO=SMTP HELO=neko2-nyaan3.y.example.co.jp IDENT=2LYC6642BLzFK3MM SOURCE=REMOTE
88 # <<< 250 2.0.0 Ok
89 12 $parameters = $commandarg;
90 12 $commandarg = '';
91 }
92  
93 24 for my $f ( split(" ", $parameters) ) {
94 # SIZE=22022, PROTO=SMTP, and so on
95 54 ▶ next if index($f, '=') < 1 || length $f < 3;
Branch: 1 branch
BranchTrueFalseTotal
if index($f, "=") < 1 or length $f < 3 0 54 54
Condition33%1/3
index($f, "=") < 1 or length $f < 3
l!l&&r!l&&!r
0054
MC/DC0%0/2
index($f, "=") < 1 or length $f < 3
index($f, "=") < 1 length $f < 3
Truth table33%1/3
A or B
A= index($f, "=") < 1B= length $f < 3
ABresult
1X1
011
000
96  
97 54 ▶ my @ee = (split('=', $f)); next unless scalar @ee == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @ee == 2 0 54 54
98 54 $cx->{'parameter'}->{ lc $ee[0] } = $ee[1];
99 }
100 } else {
101 # HELO, EHLO, AUTH, DATA, QUIT or Other SMTP command
102 18 $cx->{'argument'} = $commandarg;
103 }
104 } else {
105 # SMTP server sent a response "<<< response text"
106 120 ▶ my $p3 = index($e, '<<< '); next unless $p3 == 0; substr($e, $p3, 4, '');
Branch: 1 branch
BranchTrueFalseTotal
unless $p3 == 0 0 120 120
107  
108 120 ▶ $cx->{'response'}->{'reply'} = Sisimai::SMTP::Reply->find($e) || '';
Condition50%1/2
'Sisimai::SMTP::Reply'->find($e) || ''
l!l
1200
MC/DC0%0/1
'Sisimai::SMTP::Reply'->find($e) || ''
'Sisimai::SMTP::Reply'->find($e)
Truth table50%1/2
A
A= 'Sisimai::SMTP::Reply'->find($e)
Aresult
11
00
109 120 ▶ $cx->{'response'}->{'status'} = Sisimai::SMTP::Status->find($e) || '';
Condition100%2/2
'Sisimai::SMTP::Status'->find($e) || ''
l!l
3684
MC/DC100%1/1
'Sisimai::SMTP::Status'->find($e) || ''
'Sisimai::SMTP::Status'->find($e)
Truth table100%2/2
A
A= 'Sisimai::SMTP::Status'->find($e)
Aresult
11
00
110 120 push $cx->{'response'}->{'text'}->@*, $e;
111 }
112 }
113 6 ▶ return undef unless scalar @$esmtp;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$esmtp 0 6 6
114 6 return $esmtp;
115 }
116  
117 4 1;
118 __END__
« Sisimai/Lhost/EinsUndEins.pm ↑ Summary Sisimai/RFC1123.pm »