File Coverage

blib/lib/Notify/Email.pm
Criterion Covered Total %
statement 29 57 50.8
branch 4 22 18.1
condition 1 3 33.3
subroutine 8 10 80.0
pod 0 4 0.0
total 42 96 43.7


line stmt bran cond sub pod time code
1             package Notify::Email;
2              
3             require 5.00503;
4 1     1   554 use strict;
  1         1  
  1         28  
5 1     1   5 use Carp;
  1         1  
  1         49  
6 1     1   772 use Mail::Box::Manager;
  1         227336  
  1         33  
7 1     1   1306 use Mail::Sender;
  1         107314  
  1         143  
8              
9             require Exporter;
10              
11             our @ISA = qw( Exporter );
12             our %EXPORT_TAGS = ( 'all' => [qw( )] );
13             our @EXPORT_OK = ( );
14             our @EXPORT = ( );
15             #our $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
16             our $VERSION = '0.0.1';
17              
18 1     1   14 use constant DEFAULT_SMTP_HOST => 'localhost';
  1         2  
  1         623  
19              
20             sub new {
21              
22 1     1 0 17 my ($self, $options) = @_;
23 1   33     11 my $class = ref($self) || $self;
24 1         4 my $this = {};
25 1         3 bless $this, $class;
26              
27             # Sanity checking on options
28 1 50       5 confess "Attempted to create Email notification object without registering the application name."
29             unless exists $options->{'app'};
30              
31 1         34 $this->{'__CONFIG'} = $options;
32 1         7 $this->init ();
33              
34 1         30610 return $this;
35              
36             } #end sub new
37              
38             sub init {
39              
40 1     1 0 2 my ($self, $options) = @_;
41              
42 1 50       17 $self->{'__MAILBOX'} = Mail::Box::Manager->new
43             or confess "Error creating mail box manager object: $!";
44              
45 1 50       134 $self->{'__CONFIG'}->{'mbox'} = ($self->{'__CONFIG'}->{'mbox'})
46             ? $self->{'__CONFIG'}->{'mbox'} : $ENV{'MAIL'};
47              
48             # Note we don't check for success here but check before we are
49             # about to receive. This is because it's inappropriate to bomb
50             # during the init
51 1         8 $self->{'__FOLDER'} = $self->{'__MAILBOX'}->open (
52             'folder' => $self->{'__CONFIG'}->{'mbox'},
53             'type' => 'mbox',
54             'access' => "rw",
55             );
56              
57             } #end sub init
58              
59             sub send {
60              
61 0     0 0 0 my ($self, $notice) = @_;
62 0         0 my ($smtp, $sender);
63 0         0 my $notice_attribs = $notice->getNotice ();
64              
65 0 0       0 $smtp = ($self->{'__CONFIG'}->{'smtp'})
66             ? $self->{'__CONFIG'}->{'smtp'} : DEFAULT_SMTP_HOST;
67              
68 0 0       0 ref ($sender = new Mail::Sender ({
69             'smtp' => $smtp,
70             'from' => $notice_attribs->{'src'},
71             })) or confess "Error creating outgoing mail object: $!";
72              
73 0         0 my $subject = "[" . $self->{'__CONFIG'}->{'app'} . " Notification] #" . $notice_attribs->{'id'};
74              
75 0 0       0 $sender->MailMsg ({
76             'from' => $notice_attribs->{'src'},
77             'to' => $notice_attribs->{'dest'},
78             'smtp' => $smtp,
79             'subject' => $subject,
80             'msg' => $notice_attribs->{'message'},
81             }) or return undef;
82              
83 0         0 return 1;
84              
85             } #end sub send
86              
87             sub receive {
88              
89 0     0 0 0 my ($self, $notice) = @_;
90 0         0 my $notice_attribs = $notice->getNotice ();
91              
92             # It's possible the mbox may be removed if its empty. So just
93             # return undef.
94 0 0       0 return undef unless $self->{'__FOLDER'};
95              
96             # If we end with a negative num, then we haven't found any msgs
97             # with our notification number
98 0         0 my $lastindex = -1;
99 0         0 for (my $i = 0; $i < $self->{'__FOLDER'}->messages (); $i++ ) {
100              
101 0         0 my $msg = $self->{'__FOLDER'}->message ($i);
102              
103 0         0 my $subject = $msg->head->get('subject');
104 0         0 chomp ($subject);
105              
106             # Check if the subject matches the notification string
107 0 0       0 if ($subject =~ /^.*(Re:)?\s*[\s*$self->{'__CONFIG'}->{'app'}\s*Notification\s*]\s*#\s*$notice_attribs->{'id'}\s*$/i) {
108              
109 0 0       0 if ($lastindex < 0) {
110 0         0 $lastindex = $i;
111             }
112             else {
113              
114             # We want the newer one and will delete the old one
115 0         0 $msg = $self->{'__FOLDER'}->message ($lastindex);
116 0         0 $msg->delete ();
117 0         0 $lastindex = $i;
118              
119             }
120              
121             }
122              
123             }
124              
125 0 0       0 return undef if $lastindex < 0;
126 0         0 my $msg = $self->{'__FOLDER'}->message ($lastindex);
127 0         0 my $body = join ('', @{ $msg->body () });
  0         0  
128 0         0 $msg->delete ();
129 0         0 return $body;
130              
131             } #end sub receive
132              
133             sub DESTROY {
134              
135 1     1   9 my ($self) = @_;
136              
137 1 50       5 $self->{'__FOLDER'}->close ()
138             if $self->{'__FOLDER'};
139              
140             }
141              
142             1;
143              
144             __END__