| 1 | ########################################################################### | ||
| 2 | # | ||
| 3 | # Mail.pm | ||
| 4 | # | ||
| 5 | # Copyright (C) 1999 Raphael Manfredi. | ||
| 6 | # Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org; | ||
| 7 | # all rights reserved. | ||
| 8 | # | ||
| 9 | # See the README file included with the | ||
| 10 | # distribution for license information. | ||
| 11 | # | ||
| 12 | ########################################################################## | ||
| 13 | |||
| 14 | package Log::Agent::Driver::Mail; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use Mail::Mailer; | ||
| 18 | require Log::Agent::Driver; | ||
| 19 | |||
| 20 | use vars qw(@ISA); | ||
| 21 | @ISA = qw(Log::Agent::Driver); | ||
| 22 | |||
| 23 | ########################################################################### | ||
| 24 | # | ||
| 25 | # Public Methods | ||
| 26 | # | ||
| 27 | ########################################################################### | ||
| 28 | |||
| 29 | # | ||
| 30 | # make -- driver constructor | ||
| 31 | # | ||
| 32 | sub make { | ||
| 33 | my $self = bless { | ||
| 34 | prefix => '', | ||
| 35 | to => (getpwuid $<)[0], | ||
| 36 | cc => '', | ||
| 37 | bcc => '', | ||
| 38 | subject => '', | ||
| 39 | from => '', | ||
| 40 | priority => '', | ||
| 41 | reply_to => '', | ||
| 42 | mailer => [] | ||
| 43 | }, shift; | ||
| 44 | |||
| 45 | my (%args) = @_; | ||
| 46 | |||
| 47 | foreach my $key (keys %args) { | ||
| 48 | if ($key =~ /^-(to|cc|bcc|prefix|subject|from|priority|reply_to| | ||
| 49 | mailer)$/x) { | ||
| 50 | $self->{$1} = $args{$key}; | ||
| 51 | } else { | ||
| 52 | use Carp; | ||
| 53 | croak "invalid argument: $key"; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | $self->_init($self->{prefix}, 0); | ||
| 58 | |||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | # | ||
| 63 | # chan_eq -- not much of anything at the moment | ||
| 64 | # | ||
| 65 | sub chan_eq { | ||
| 66 | my($self, $chan0, $chan1) = @_; | ||
| 67 | return $chan0 eq $chan1; | ||
| 68 | } | ||
| 69 | |||
| 70 | # | ||
| 71 | # write -- send a message to the channel | ||
| 72 | # | ||
| 73 | sub write { | ||
| 74 | my($self, $chan, $prio, $mesg) = @_; | ||
| 75 | |||
| 76 | my(%headers); | ||
| 77 | foreach my $hdr (qw( to cc bcc subject from priority reply_to )) { | ||
| 78 | my $fhdr = ucfirst($hdr); | ||
| 79 | $fhdr =~ s/_/-/g; | ||
| 80 | $headers{$fhdr} = $self->{$hdr} unless $self->{$hdr} eq ''; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $mailer = Mail::Mailer->new(@{$self->{mailer}}); | ||
| 84 | $mailer->open(\%headers); | ||
| 85 | print $mailer $mesg, "\n"; | ||
| 86 | $mailer->close; | ||
| 87 | } | ||
| 88 | |||
| 89 | # | ||
| 90 | # prefix_msg -- add prefix | ||
| 91 | # | ||
| 92 | sub prefix_msg { | ||
| 93 | my($self, $str) = @_; | ||
| 94 | return ($self->{prefix} ? $self->{prefix} . ' ' : '') . $str; | ||
| 95 | } | ||
| 96 | |||
| 97 | __END__ |