File Coverage

blib/lib/Mariachi/Message.pm
Criterion Covered Total %
statement 60 60 100.0
branch 5 6 83.3
condition 2 2 100.0
subroutine 18 18 100.0
pod 10 10 100.0
total 95 96 98.9


line stmt bran cond sub pod time code
1 3     3   4160 use strict;
  3         7  
  3         146  
2             package Mariachi::Message;
3 3     3   2456 use Email::Simple;
  3         18325  
  3         99  
4 3     3   29 use Digest::MD5 qw(md5_hex);
  3         7  
  3         238  
5 3     3   3353 use Date::Parse qw(str2time);
  3         24257  
  3         242  
6 3     3   2580 use Text::Original ();
  3         98384  
  3         82  
7 3     3   25 use Memoize;
  3         9  
  3         185  
8              
9 3     3   17 use base qw(Class::Accessor::Fast);
  3         6  
  3         2542  
10             __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             sub new {
29 6     6 1 3069 my $class = shift;
30 6         12 my $source = shift;
31              
32 6         40 my $self = $class->SUPER::new;
33 6 50       90 my $mail = Email::Simple->new($source) or return;
34              
35 6         851 $self->linked({});
36 6         60 $self->_header({});
37 6         61 $self->header_set( $_, $mail->header($_) ) for
38             qw( message-id from subject date references in-reply-to );
39 6         53 $self->body( $mail->body );
40              
41 6 100       80 $self->header_set('message-id', $self->_make_fake_id)
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   100     48 my @date = localtime $self->epoch_date(str2time( $self->header('date') )
47             || 0);
48 6         2108 my @ymd = ( $date[5] + 1900, $date[4] + 1, $date[3] );
49 6         23 $self->ymd(\@ymd);
50 6         60 $self->day( sprintf "%04d/%02d/%02d", @ymd );
51 6         54 $self->month( sprintf "%04d/%02d", @ymd );
52 6         49 $self->year( sprintf "%04d", @ymd );
53              
54 6         98 return $self;
55             }
56              
57              
58             sub _make_fake_id {
59 5     5   43 my $self = shift;
60 5         12 my $hash = substr( md5_hex( $self->header('from').$self->date ), 0, 8 );
61 5         356 return "$hash\@made_up";
62             }
63              
64             =head2 ->body
65              
66             =head2 ->header
67              
68             =head2 ->header_set
69              
70             C, C
, and C are provided for interface
71             compatibility with Email::Simple
72              
73             =cut
74              
75             sub header {
76 29     29 1 624 my $self = shift;
77 29         73 $self->_header->{ lc shift() };
78             }
79              
80             sub header_set {
81 41     41 1 901 my $self = shift;
82 41         50 my $hdr = shift;
83 41         98 $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
93              
94             =cut
95              
96             *first_line = \&first_lines;
97             sub first_lines {
98 3     3 1 2266 my $self = shift;
99 3         10 return Text::Original::first_lines( $self->body, @_ );
100             }
101              
102             sub first_paragraph {
103 2     2 1 908 my $self = shift;
104 2         6 return Text::Original::first_paragraph( $self->body );
105             }
106              
107             sub first_sentence {
108 3     3 1 2188 my $self = shift;
109 3         11 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             sub body_sigless {
120 1     1 1 1695 my $self = shift;
121 1         5 my ($body, undef) = split /^-- $/m, $self->body, 2;
122              
123 1         10 return $body;
124             }
125              
126             =head2 ->sig
127              
128             Returns the stripped sig.
129              
130             =cut
131              
132             sub sig {
133 2     2 1 556 my $self = shift;
134 2         8 my (undef, $sig) = split /^-- $/m, $self->body, 2;
135 2 100       27 $sig =~ s/^\n// if $sig;
136 2         11 return $sig;
137             }
138              
139              
140              
141             =head2 ->from
142              
143             A privacy repecting version of the From: header.
144              
145             =cut
146              
147             sub from {
148             my $self = shift;
149              
150             my $from = $self->header('from');
151             $from =~ s/<.*>//;
152             $from =~ s/\@\S+//;
153             $from =~ s/\s+\z//;
154             $from =~ s/"(.*?)"/$1/;
155             return $from;
156             }
157             memoize('from');
158              
159             =head2 ->subject
160              
161             =head2 ->date
162              
163             the C and C headers
164              
165             =cut
166              
167 1     1 1 520 sub subject { $_[0]->header('subject') }
168 6     6 1 598 sub date { $_[0]->header('date') }
169              
170              
171             =head2 ->filename
172              
173             the name of the output file
174              
175             =cut
176              
177             sub filename {
178             my $self = shift;
179              
180             my $msgid = $self->header('message-id');
181              
182             my $filename = substr( md5_hex( $msgid ), 0, 8 ).".html";
183             return $self->day."/".$filename;
184             }
185             memoize('filename');
186              
187             1;
188              
189             __END__