ASNMTAP
view release on metacpan or search on metacpan
lib/ASNMTAP/Asnmtap/Plugins/Mail.pm view on Meta::CPAN
# Class name - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
package ASNMTAP::Asnmtap::Plugins::Mail;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
use Carp qw(carp cluck);
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
use strict;
use warnings; # Must be used in test mode only. This reduces a little process speed
#use diagnostics; # Must be used in test mode only. This reduces a lot of process speed
no warnings 'deprecated';
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
use ASNMTAP::Asnmtap qw(:_HIDDEN %ERRORS %TYPE);
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BEGIN {
use Exporter ();
@ASNMTAP::Asnmtap::Plugins::Mail::ISA = qw(Exporter);
%ASNMTAP::Asnmtap::Plugins::Mail::EXPORT_TAGS = ( ALL => [ qw() ] );
@ASNMTAP::Asnmtap::Plugins::Mail::EXPORT_OK = ( @{ $ASNMTAP::Asnmtap::Plugins::Mail::EXPORT_TAGS{ALL} } );
$ASNMTAP::Asnmtap::Plugins::Mail::VERSION = do { my @r = (q$Revision: 3.002.003$ =~ /\d+/g); sprintf "%d."."%03d" x $#r, @r };
}
# Constructor & initialisation - - - - - - - - - - - - - - - - - - - - -
sub new (@) {
my $classname = shift;
unless ( defined $classname ) { my @c = caller; die "Syntax error: Class name expected after new at $c[1] line $c[2]\n" }
if ( ref $classname) { my @c = caller; die "Syntax error: Can't construct new ".ref($classname)." from another object at $c[1] line $c[2]\n" }
my $self = {};
my @parameters = (_asnmtapInherited => undef,
_SMTP =>
{
smtp => 'localhost',
port => 25,
retries => 3,
delay => 1,
mime => 0,
tz => undef,
debug => 0
},
_IMAP4 =>
{
imap4 => undef,
port => 143,
username => undef,
password => undef,
timeout => 120,
debug => 0
},
_POP3 =>
{
pop3 => undef,
port => 110,
username => undef,
password => undef,
timeout => 120,
debug => 0
},
_mailType => 0,
_text =>
{
SUBJECT => 'uKey=ASNMTAP',
from => 'From:',
to => 'To:',
subject => 'Subject:',
status => 'Status'
},
_mail =>
{
from => undef,
to => undef,
status => undef,
body => undef
}
);
if ( $] < 5.010000 ) {
eval "use fields";
$self = fields::phash (@parameters);
} else {
use ASNMTAP::PseudoHash;
$self = do {
my @array = undef;
while (my ($k, $v) = splice(@parameters, 0, 2)) {
$array[$array[0]{$k} = @array] = $v;
}
bless(\@array, $classname);
};
}
my %args = @_;
$self->{_asnmtapInherited} = $args{_asnmtapInherited} if ( exists $args{_asnmtapInherited} );
if ( exists $args{_SMTP} ) {
$self->{_SMTP}->{smtp} = $args{_SMTP}->{smtp} if ( exists $args{_SMTP}->{smtp} );
$self->{_SMTP}->{port} = $args{_SMTP}->{port} if ( exists $args{_SMTP}->{port} );
$self->{_SMTP}->{retries} = $args{_SMTP}->{retries} if ( exists $args{_SMTP}->{retries} );
$self->{_SMTP}->{delay} = $args{_SMTP}->{delay} if ( exists $args{_SMTP}->{delay} );
$self->{_SMTP}->{mime} = $args{_SMTP}->{mime} if ( exists $args{_SMTP}->{mime} );
$self->{_SMTP}->{tz} = $args{_SMTP}->{tz} if ( exists $args{_SMTP}->{tz} );
$self->{_SMTP}->{debug} = $args{_SMTP}->{debug} if ( exists $args{_SMTP}->{debug} );
}
if ( exists $args{_IMAP4} ) {
$self->{_IMAP4}->{imap4} = $args{_IMAP4}->{imap4} if ( exists $args{_IMAP4}->{imap4} );
$self->{_IMAP4}->{port} = $args{_IMAP4}->{port} if ( exists $args{_IMAP4}->{port} );
$self->{_IMAP4}->{username} = $args{_IMAP4}->{username} if ( exists $args{_IMAP4}->{username} );
$self->{_IMAP4}->{password} = $args{_IMAP4}->{password} if ( exists $args{_IMAP4}->{password} );
$self->{_IMAP4}->{timeout} = $args{_IMAP4}->{timeout} if ( exists $args{_IMAP4}->{timeout} );
$self->{_IMAP4}->{debug} = $args{_IMAP4}->{debug} if ( exists $args{_IMAP4}->{debug} );
}
if ( exists $args{_POP3} ) {
$self->{_POP3}->{pop3} = $args{_POP3}->{pop3} if ( exists $args{_POP3}->{pop3} );
$self->{_POP3}->{port} = $args{_POP3}->{port} if ( exists $args{_POP3}->{port} );
$self->{_POP3}->{username} = $args{_POP3}->{username} if ( exists $args{_POP3}->{username} );
$self->{_POP3}->{password} = $args{_POP3}->{password} if ( exists $args{_POP3}->{password} );
$self->{_POP3}->{timeout} = $args{_POP3}->{timeout} if ( exists $args{_POP3}->{timeout} );
$self->{_POP3}->{debug} = $args{_POP3}->{debug} if ( exists $args{_POP3}->{debug} );
}
$self->{_mailType} = $args{_mailType} if ( exists $args{_mailType} );
if ( exists $args{_text} ) {
$self->{_text}->{SUBJECT} = $args{_text}->{SUBJECT} if ( exists $args{_text}->{SUBJECT} );
$self->{_text}->{from} = $args{_text}->{from} if ( exists $args{_text}->{from} );
$self->{_text}->{to} = $args{_text}->{to} if ( exists $args{_text}->{to} );
$self->{_text}->{subject} = $args{_text}->{subject} if ( exists $args{_text}->{subject} );
$self->{_text}->{status} = $args{_text}->{status} if ( exists $args{_text}->{status} );
}
if ( exists $args{_mail} ) {
$self->{_mail}->{from} = $args{_mail}->{from} if ( exists $args{_mail}->{from} );
$self->{_mail}->{to} = $args{_mail}->{to} if ( exists $args{_mail}->{to} );
$self->{_mail}->{status} = $args{_mail}->{status} if ( exists $args{_mail}->{status} );
$self->{_mail}->{body} = $args{_mail}->{body} if ( exists $args{_mail}->{body} );
}
bless ($self, $classname);
$self->_init();
return ($self);
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub _init {
my $asnmtapInherited = $_[0]->{_asnmtapInherited};
unless ( defined $asnmtapInherited ) { cluck ( 'ASNMTAP::Asnmtap::Plugins::Mail: asnmtapInherited missing' ); exit $ERRORS{UNKNOWN} }
carp ('ASNMTAP::Asnmtap::Pluginw::MAIL: _init') if ( $$asnmtapInherited->{_debug} );
unless ( defined $$asnmtapInherited->{_programName} and $$asnmtapInherited->{_programName} ne 'NOT DEFINED' ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing parent object attribute mName' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ( defined $$asnmtapInherited->{_programDescription} and $$asnmtapInherited->{_programDescription} ne 'NOT DEFINED' ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing parent object attribute mDescription' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ( $$asnmtapInherited->getOptionsArgv('environment') ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing parent object command line option -e|--environment' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
my %environment = ( P => 'PROD', S => 'SIM', A => 'ACC', T => 'TEST', D => 'DEV', L => 'LOCAL' );
$_[0]->[ $_[0]->[0]{_environment_} = @{$_[0]} ] = $environment { $$asnmtapInherited->getOptionsArgv('environment') };
if ( $$asnmtapInherited->getOptionsValue ('debug') ) {
$_[0]->{_SMTP}->{debug} = $$asnmtapInherited->getOptionsValue ('debug') if ( $_[0]->{_SMTP}->{debug} < $$asnmtapInherited->getOptionsValue ('debug') );
$_[0]->{_POP3}->{debug} = $$asnmtapInherited->getOptionsValue ('debug') if ( $_[0]->{_POP3}->{debug} < $$asnmtapInherited->getOptionsValue ('debug') );
$_[0]->{_IMAP4}->{debug} = $$asnmtapInherited->getOptionsValue ('debug') if ( $_[0]->{_IMAP4}->{debug} < $$asnmtapInherited->getOptionsValue ('debug') );
}
$_[0]->{_POP3}->{timeout} = $$asnmtapInherited->timeout() if ( $_[0]->{_POP3}->{timeout} == 120 );
lib/ASNMTAP/Asnmtap/Plugins/Mail.pm view on Meta::CPAN
my $returnCode = (sendmail %mail) ? $ERRORS{OK} : $ERRORS{CRITICAL};
$$asnmtapInherited->pluginValues ( { stateValue => $returnCode, alert => ( defined $parms{perfdataLabel} ? $parms{perfdataLabel} : 'email send' ) . ( $returnCode ? ' failed' : '' ) }, $TYPE{APPEND} );
if ( defined $parms{perfdataLabel} ) {
my $responseTime = $$asnmtapInherited->setEndTime_and_getResponsTime ( $$asnmtapInherited->pluginValue ('endTime') );
$$asnmtapInherited->appendPerformanceData ( "'". $parms{perfdataLabel} ."'=". $responseTime .'ms;;;;' );
}
print "\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log, "\n" if ( $$asnmtapInherited->getOptionsValue ('debug') );
return ( $returnCode );
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub receiving_fingerprint_mails {
my $self = shift; &_checkAccObjRef ( $self );
my $asnmtapInherited = $self->{_asnmtapInherited};
return ( $$asnmtapInherited->pluginValue ('stateValue') ) unless ( exists $self->{_subject_} );
my %defaults = ( custom => undef,
customArguments => undef,
checkFingerprint => 1,
receivedState => 0,
outOfDate => undef,
perfdataLabel => undef
);
my %parms = (%defaults, @_);
if ( $self->{_mailType} ) {
unless ( defined $parms{outOfDate} ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing MAIL receiving_fingerprint_mails parameter {outOfDate => ...}' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
}
use ASNMTAP::Asnmtap::Plugins::XML qw(&extract_XML);
$self->[ $self->[0]{defaultArguments} = @{$self} ] = { date => undef, day => undef, month => undef, year => undef, time => undef, hour => undef, min => undef, sec => undef, numberOfMatches => undef, result => undef };
unless ($parms{checkFingerprint} =~ /^[01]$/ ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Parameter checkFingerprint must be 0 or 1' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ($parms{receivedState} =~ /^[01]$/ ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Parameter receivedState must be 0 or 1' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
my ($numberOfMails, $email);
if ( defined $self->{_IMAP4}->{imap4} ) {
unless ( defined $self->{_IMAP4}->{username} ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing Mail IMAP4 parameter username' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ( defined $self->{_IMAP4}->{password} ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing Mail IMAP4 parameter password' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
use Net::IMAP::Simple;
$email = Net::IMAP::Simple->new ( $self->{_IMAP4}->{imap4}, port => $self->{_IMAP4}->{port}, timeout => $self->{_IMAP4}->{timeout} );
unless ( defined $email ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Cannot connect to IMAP4 server: $self->{_IMAP4}->{imap4}, $Net::IMAP::Simple::errstr" }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ( $email->login( $self->{_IMAP4}->{username}, $self->{_IMAP4}->{password} ) ){
my $errstr = $email->errstr; $errstr =~ s/[\n\r]/ /g; $errstr =~ s/ +$//g;
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Cannot login to IMAP4 server: $self->{_IMAP4}->{imap4}, $errstr" }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
$numberOfMails = $email->select ( 'INBOX' );
unless ( defined $numberOfMails ) {
my $errstr = $email->errstr; $errstr =~ s/[\n\r]/ /g; $errstr =~ s/ +$//g;
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Cannot select my INBOX on IMAP4 server: $self->{_IMAP4}->{imap4}, $errstr" }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
} elsif ( defined $self->{_POP3}->{pop3} ) {
unless ( defined $self->{_POP3}->{username} ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing Mail POP3 parameter username' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ( defined $self->{_POP3}->{password} ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing Mail POP3 parameter password' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
use Mail::POP3Client;
$email = Mail::POP3Client->new ( HOST => $self->{_POP3}->{pop3}, PORT => $self->{_POP3}->{port}, USER => $self->{_POP3}->{username}, PASSWORD => $self->{_POP3}->{password}, TIMEOUT => $self->{_POP3}->{timeout} ); # , DEBUG => ( $self->{_POP3}->{d...
$numberOfMails = $email->Count();
unless ( defined $numberOfMails and $numberOfMails != -1 ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => "Cannot connect/login to POP3 server: $self->{_POP3}->{pop3}" }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
} else {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'NO EMAIL CLIENT SPECIFIED !!!' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
if ( defined $numberOfMails ) {
my $returnCode = $ERRORS{DEPENDENT};
$self->{defaultArguments}->{numberOfMatches} = 0;
if ( $numberOfMails ) {
use MIME::Parser;
my $parser = new MIME::Parser;
$parser->output_to_core(1);
$parser->decode_bodies(1);
use constant HEADER => '<?xml version="1.0" encoding="UTF-8"?>';
use constant SYSTEM => 'dtd/FingerprintEmail-1.0.dtd';
use constant FOOTER => '</FingerprintEmail>';
my $fingerprintXML = HEADER .'<!DOCTYPE FingerprintEmail SYSTEM "'. SYSTEM .'"><FingerprintEmail>';
my $debug = $$asnmtapInherited->getOptionsValue ('debug');
my $label;
for( my $msgnum = 1; $msgnum <= $numberOfMails; $msgnum++ ) {
print "\n", ref ($self), "::receiving_fingerprint_mails(): message number $msgnum\n" if ( $debug );
my ($fromNotFound, $toNotFound, $subjectNotFound, $fingerprintFound) = (1, 1, 1, 3);
my ($messageNotFound, $brandingNotFound, $timestampNotFound, $statusNotFound, $xmlNotFound) = (0, 0, 0, 0, 0);
if ( $parms{checkFingerprint} ) {
$statusNotFound = 1;
if ( $self->{_mailType} ) {
($xmlNotFound, $fingerprintFound) = (1, $fingerprintFound + 2);
} else {
($messageNotFound, $brandingNotFound, $timestampNotFound, $fingerprintFound) = (1, 1, 1, $fingerprintFound + 4);
}
}
$self->{defaultArguments}->{result} = '';
my $msgbuffer;
if ( defined $self->{_IMAP4}->{imap4} ) {
$msgbuffer = $email->top ( $msgnum );
} elsif ( defined $self->{_POP3}->{pop3} ) {
$msgbuffer = $email->Head ( $msgnum );
}
my $entity = $parser->parse_data( $msgbuffer );
my $head = $entity->head;
$head->unfold;
if ( $debug >= 2 ) {
( run in 1.790 second using v1.01-cache-2.11-cpan-5837b0d9d2c )