Mariachi/Message.pm

filter: Statementstmt 100% 76 / 76 Branchbran 83.3% 5 / 6 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 20 / 20 Podpod 100% 10 / 10 totaltotal 99.1% 114 / 115 CC 6 SCAR 6.3
CC
6
Cov
99%
new
5.0
sig
2.0
BEGIN
1.0
CRAP 1.9
« Template/Plugin/Mariachi.pm ↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mariachi::Message;
3 3 ▶use Email::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Date::Parse qw(str2time);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Text::Original ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 __PACKAGE__->mk_accessors(qw( body _header next prev root
11 epoch_date day month year ymd linked
12 ));
13  
14 =head1 NAME
15  
16 Mariachi::Message - representation of a mail message
17  
18 =head1 METHODS
19  
20 =head2 ->new($message)
21  
22 C<$message> is a rfc2822 compliant message body
23  
24 your standard constructor
25  
26 =cut
27  
28 6 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
29 6 my $class = shift;
30 6 my $source = shift;
31  
32 6 my $self = $class->SUPER::new;
33 6 ▶ my $mail = Email::Simple->new($source) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $mail = "Email::Simple"->new($source) or return 6 0 6
34  
35 6 $self->linked({});
36 6 $self->_header({});
37 6 $self->header_set( $_, $mail->header($_) ) for
38 qw( message-id from subject date references in-reply-to );
39 6 $self->body( $mail->body );
40  
41 6 ▶ $self->header_set('message-id', $self->_make_fake_id)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->header("message-id") 5 1 6
42 unless $self->header('message-id');
43  
44 # this is a bit ugly to be here but much quicker than making it a
45 # memoized lookup
46 6 ▶ my @date = localtime $self->epoch_date(str2time( $self->header('date') )
Condition100%2/2
str2time($self->header('date')) || 0
l!l
15
MC/DC100%1/1
str2time($self->header('date')) || 0
str2time($self->header('date'))
Truth table100%2/2
A
A= str2time($self->header('date'))
Aresult
11
00
47 || 0);
48 6 my @ymd = ( $date[5] + 1900, $date[4] + 1, $date[3] );
49 6 $self->ymd(\@ymd);
50 6 $self->day( sprintf "%04d/%02d/%02d", @ymd );
51 6 $self->month( sprintf "%04d/%02d", @ymd );
52 6 $self->year( sprintf "%04d", @ymd );
53  
54 6 return $self;
55 }
56  
57  
58 5 ▶sub _make_fake_id {
Subroutine _make_fake_id: called CC=1 SCAR=0.0
59 5 my $self = shift;
60 5 my $hash = substr( md5_hex( $self->header('from').$self->date ), 0, 8 );
61 5 return "$hash\@made_up";
62 }
63  
64 =head2 ->body
65  
66 =head2 ->header
67  
68 =head2 ->header_set
69  
70 C<body>, C<header>, and C<header_set> are provided for interface
71 compatibility with Email::Simple
72  
73 =cut
74  
75 29 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
Pod documented
76 29 my $self = shift;
77 29 $self->_header->{ lc shift() };
78 }
79  
80 41 ▶sub header_set {
Subroutine header_set: called CC=1 SCAR=0.0
Pod documented
81 41 my $self = shift;
82 41 my $hdr = shift;
83 41 $self->_header->{ lc $hdr } = shift;
84 }
85  
86 =head2 ->first_lines
87  
88 =head2 ->first_paragraph
89  
90 =head2 ->first_sentence
91  
92 See L<Text::Original>
93  
94 =cut
95  
96 3 *first_line = \&first_lines;
97 3 ▶sub first_lines {
Subroutine first_lines: called CC=1 SCAR=0.0
Pod documented
98 3 my $self = shift;
99 3 return Text::Original::first_lines( $self->body, @_ );
100 }
101  
102 2 ▶sub first_paragraph {
Subroutine first_paragraph: called CC=1 SCAR=0.0
Pod documented
103 2 my $self = shift;
104 2 return Text::Original::first_paragraph( $self->body );
105 }
106  
107 3 ▶sub first_sentence {
Subroutine first_sentence: called CC=1 SCAR=0.0
Pod documented
108 3 my $self = shift;
109 3 return Text::Original::first_sentence( $self->body );
110 }
111  
112 =head2 ->body_sigless
113  
114 Returns the body with the signature (defined as anything
115 after "\n-- \n") removed.
116  
117 =cut
118  
119 1 ▶sub body_sigless {
Subroutine body_sigless: called CC=1 SCAR=0.0
Pod documented
120 1 my $self = shift;
121 1 my ($body, undef) = split /^-- $/m, $self->body, 2;
122  
123 1 return $body;
124 }
125  
126 =head2 ->sig
127  
128 Returns the stripped sig.
129  
130 =cut
131  
132 2 ▶sub sig {
Subroutine sig: called CC=2 SCAR=6.9
Pod documented
133 2 my $self = shift;
134 2 my (undef, $sig) = split /^-- $/m, $self->body, 2;
135 2 ▶ $sig =~ s/^\n// if $sig;
Branch: 1 branch
BranchTrueFalseTotal
if $sig 1 1 2
136 2 return $sig;
137 }
138  
139  
140  
141 =head2 ->from
142  
143 A privacy repecting version of the From: header.
144  
145 =cut
146  
147 2 ▶sub from {
Subroutine from: called CC=1 SCAR=0.0
148 2 my $self = shift;
149  
150 2 my $from = $self->header('from');
151 2 $from =~ s/<.*>//;
152 2 $from =~ s/\@\S+//;
153 2 $from =~ s/\s+\z//;
154 2 $from =~ s/"(.*?)"/$1/;
155 2 return $from;
156 }
157 3 memoize('from');
158  
159 =head2 ->subject
160  
161 =head2 ->date
162  
163 the C<Subject> and C<Date> headers
164  
165 =cut
166  
167 1 ▶sub subject { $_[0]->header('subject') }
Subroutine subject: called CC=1 SCAR=0.0
Pod documented
168 6 ▶sub date { $_[0]->header('date') }
Subroutine date: called CC=1 SCAR=0.0
Pod documented
169  
170  
171 =head2 ->filename
172  
173 the name of the output file
174  
175 =cut
176  
177 1 ▶sub filename {
Subroutine filename: called CC=1 SCAR=0.0
178 1 my $self = shift;
179  
180 1 my $msgid = $self->header('message-id');
181  
182 1 my $filename = substr( md5_hex( $msgid ), 0, 8 ).".html";
183 1 return $self->day."/".$filename;
184 }
185 3 memoize('filename');
186  
187 3 1;
188  
189 __END__
« Template/Plugin/Mariachi.pm ↑ Summary