File Coverage

blib/lib/Siesta/Message.pm
Criterion Covered Total %
statement 65 70 92.8
branch 5 6 83.3
condition 12 20 60.0
subroutine 17 18 94.4
pod 7 9 77.7
total 106 123 86.1


line stmt bran cond sub pod time code
1 18     18   13708 use strict;
  18         37  
  18         885  
2             package Siesta::Message;
3 18     18   97 use Siesta;
  18         37  
  18         235  
4 18     18   375 use Siesta::Deferred;
  18         34  
  18         224  
5 18     18   15054 use Mail::Address;
  18         51896  
  18         236  
6 18     18   651 use Carp qw( carp croak );
  18         40  
  18         1027  
7 18     18   107 use Storable qw(dclone);
  18         37  
  18         825  
8 18     18   96 use base qw( Email::Simple Class::Accessor::Fast );
  18         37  
  18         15936  
9             __PACKAGE__->mk_accessors(qw( plugins ));
10              
11             =head1 NAME
12              
13             Siesta::Message - a message in the system
14              
15             =head1 METHODS
16              
17             =cut
18              
19             # make a bunch of header-based accessors
20             for (qw( to_raw from_raw subject )) {
21             my $header = $_;
22             my $sub_name = $header;
23             $header =~ s/_raw$//;
24             my $sub = sub {
25 44     44   20109 my $self = shift;
26 44 100       160 if (@_) {
27 13         82 $self->header_set( $header, shift );
28             }
29 44         971 return $self->header( $header );
30             };
31 18     18   125398 no strict 'refs';
  18         50  
  18         11893  
32             *{ $sub_name } = $sub;
33             }
34              
35             sub new {
36 31     31 1 6772754 my $referent = shift;
37 31   33     280 my $class = ref $referent || $referent;
38 31   100     221 my $data = shift || "";
39              
40 31 100       140 if (ref $data eq 'GLOB') {
41 2         57 $data = join '', <$data>;
42             }
43             # chomp out From_ lines from naughty MTAs
44 31         120 $data =~ s/^From .+$//m;
45              
46 31         299 my $self = $class->SUPER::new( $data );
47 31         6972 $self->plugins( [] );
48 31         447 return $self;
49             }
50              
51              
52             =head2 to
53              
54             a list of addresses that the message was to
55              
56             =cut
57              
58             sub to {
59 7     7 1 886 my $self = shift;
60 7         38 map { $_->address } Mail::Address->parse( $self->header('To') );
  7         1615  
61             }
62              
63             =head2 from
64              
65             the email address that the message was from
66              
67             =cut
68              
69             sub from {
70 27     27 1 2101 my $self = shift;
71              
72 27         167 ( map { $_->address } Mail::Address->parse( $self->header('From') ) )[0];
  27         5961  
73             }
74              
75             =head2 subject
76              
77             =head2 reply
78              
79             =cut
80              
81             sub reply {
82 13     13 1 2024 my $self = shift;
83 13         78 my %args = @_;
84              
85 13         108 my $new = Siesta::Message->new;
86 13   33     84 $new->body_set( $args{body} || $self->body );
87 13   66     167 $new->header_set( 'To', $args{to} || $self->from );
88 13   66     2626 $new->header_set( 'From', $args{from} || ( $self->to )[0] );
89 13   66     656 $new->header_set( 'Subject', $args{subject} ||
90             "Re: " . ( $self->subject || "Your mail" ) );
91 13         772 $new->header_set( 'In-Reply-To', $self->header( 'Message-Id' ) );
92              
93 13         1818 $new->send;
94 13         92 Siesta->log("Message->reply sending" . $new->as_string, 10);
95             }
96              
97             =head2 send
98              
99             =cut
100              
101             sub send {
102 13     13 1 25 my $self = shift;
103 13         138 return Siesta->sender->send( $self, @_ );
104             }
105              
106             =head2 clone
107              
108             =cut
109              
110             sub clone {
111 1     1 1 3 my $self = shift;
112              
113 1         150 return dclone $self;
114             }
115              
116             =head2 defer
117              
118             =cut
119              
120             sub defer {
121 8     8 1 7202 my $self = shift;
122              
123 8         39 Siesta::Deferred->create({
124             @_,
125 8         26 plugins => join(',', @{ $self->plugins } ),
126             message => $self,
127             });
128             }
129              
130              
131             # XXX compatibility shim, excise soonest
132             sub resume {
133 0     0 0 0 my $self = shift;
134 0         0 my $id = shift;
135              
136 0         0 carp "Siesta::Message->resume is deprected, use resume on a Siesta::Deferred object instead";
137 0         0 my $deferred = Siesta::Deferred->retrieve( $id );
138 0         0 $deferred->resume;
139             }
140              
141             sub process {
142 2     2 0 1858 my $self = shift;
143              
144 2         7 while ( my $plugin = shift @{ $self->plugins } ) {
  19         518  
145             # SIESTA_NON_STOP is used by 20fullsend.t to ensure
146             # excercising of everything. it means "run the next plugin,
147             # even if the last one said to stop"
148 17         3776 Siesta->log("... doing " . $plugin->name, 1);
149 17 50 66     108 return if $plugin->process($self) && !$ENV{SIESTA_NON_STOP};
150             }
151             }
152              
153             1;