ASNMTAP
view release on metacpan or search on metacpan
lib/ASNMTAP/Asnmtap/Plugins/Mail.pm view on Meta::CPAN
# ----------------------------------------------------------------------------------------------------------
# © Copyright 2000-2011 by Alex Peeters [alex.peeters@citap.be]
# ----------------------------------------------------------------------------------------------------------
# 2011/mm/dd, v3.002.003, package ASNMTAP::Asnmtap::Plugins::Mail Object-Oriented Perl
# ----------------------------------------------------------------------------------------------------------
# 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;
lib/ASNMTAP/Asnmtap/Plugins/Mail.pm view on Meta::CPAN
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 );
$_[0]->{_IMAP4}->{timeout} = $$asnmtapInherited->timeout() if ( $_[0]->{_IMAP4}->{timeout} == 120 );
unless ($_[0]->{_mailType} =~ /^[01]$/ ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Parameter _mailType must be 0 or 1' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ( defined $_[0]->{_mail}->{from} ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing MAIL parameter _mail => {from => ...}' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ( defined $_[0]->{_mail}->{to} ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing MAIL parameter _mail => {to => ...}' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ( defined $_[0]->{_mail}->{status} ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing MAIL parameter _mail => {status => ...}' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
unless ( defined $_[0]->{_mail}->{body} ) {
$$asnmtapInherited->pluginValues ( { stateValue => $ERRORS{UNKNOWN}, error => 'Missing MAIL parameter _mail => {body => ...}' }, $TYPE{APPEND} );
return ( $ERRORS{UNKNOWN} );
}
$_[0]->[ $_[0]->[0]{_subject_} = @{$_[0]} ] = $_[0]->{_text}->{SUBJECT} .' / '. $_[0]->{_text}->{from} .' '. $_[0]->{_mail}->{from} .' '. $_[0]->{_text}->{to} .' '. $_[0]->{_mail}->{to};
unless ( $_[0]->{_mailType} ) {
$_[0]->[ $_[0]->[0]{_branding_} = @{$_[0]} ] = '<'. $$asnmtapInherited->{_programName} .'> <'. $$asnmtapInherited->{_programDescription} .'>';
$_[0]->[ $_[0]->[0]{_timestamp_} = @{$_[0]} ] = 'Timestamp <'. $_[0]->{_mail}->{from} .'>:';
$_[0]->[ $_[0]->[0]{_status_} = @{$_[0]} ] = $_[0]->{_text}->{status} .' <'. $_[0]->{_mail}->{status} .'>';
}
if ( $$asnmtapInherited->{_debug} ) {
use Data::Dumper;
print "\n". ref ($_[0]) .": Now we'll dump data\n\n", Dumper ( $_[0] ), "\n\n";
}
}
# Utility methods - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub sending_fingerprint_mail {
my $self = shift; &_checkAccObjRef ( $self );
my $asnmtapInherited = $self->{_asnmtapInherited};
return ( $$asnmtapInherited->pluginValue ('stateValue') ) unless ( exists $self->{_subject_} );
my %defaults = ( perfdataLabel => undef );
my %parms = (%defaults, @_);
$$asnmtapInherited->setEndTime_and_getResponsTime ( $$asnmtapInherited->pluginValue ('endTime') ) if ( defined $parms{perfdataLabel} );
use Mail::Sendmail qw(sendmail %mailcfg);
$mailcfg {smtp} = $self->{_SMTP}->{smtp};
$mailcfg {port} = $self->{_SMTP}->{port};
$mailcfg {retries} = $self->{_SMTP}->{retries};
$mailcfg {delay} = $self->{_SMTP}->{delay};
$mailcfg {mime} = $self->{_SMTP}->{mime};
$mailcfg {tz} = $self->{_SMTP}->{tx} if ( defined $self->{_SMTP}->{tx} );
$mailcfg {debug} = $$asnmtapInherited->getOptionsValue ('debug');
my $message;
if ( $self->{_mailType} ) {
use Time::Local;
my ($localYear, $localMonth, $currentYear, $currentMonth, $currentDay, $currentHour, $currentMin, $currentSec) = ((localtime)[5], (localtime)[4], ((localtime)[5] + 1900), ((localtime)[4] + 1), (localtime)[3,2,1,0]);
my $mailEpochtime = timelocal($currentSec, $currentMin, $currentHour, $currentDay, $localMonth, $localYear);
$message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE FingerprintEmail SYSTEM \"dtd/FingerprintEmail-1.0.dtd\"><FingerprintEmail><Schema Value=\"1.0\"/><Fingerprint From=\"". $self->{_mail}->{from} ."\" To=\"". $self->{_mail}->{to} ."\"...
} else {
use ASNMTAP::Time qw(&get_datetimeSignal);
$message = $self->{_subject_} ."\n". $self->{_branding_} ."\n". $self->{_timestamp_} .' '. get_datetimeSignal() ."\n". $self->{_status_} ."\n";
}
$message .= $self->{_mail}->{body} ."\n";
my %mail = ( To => $self->{_mail}->{to}, From => $self->{_mail}->{from}, Subject => $self->{_subject_}, Message => $message );
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} );
lib/ASNMTAP/Asnmtap/Plugins/Mail.pm view on Meta::CPAN
$msgbuffer =~ /$regexp/;
$self->{defaultArguments}->{result} = ( $' ) ? $' : '';
} else {
$self->{defaultArguments}->{result} = $msgbuffer;
}
print "\n", ref ($self), "::receiving_fingerprint_mails(): BODY MESSAGE\n". $self->{defaultArguments}->{result}. "\n" if ($debug >= 2);
if ( defined $parms{custom} ) {
$returnCode = ( defined $parms{customArguments} ) ? $parms{custom}->($self, $self->{_asnmtapInherited}, $email, $msgnum, $parms{customArguments}) : $parms{custom}->($self, $self->{_asnmtapInherited}, $email, $msgnum);
} else {
$self->{defaultArguments}->{numberOfMatches}++;
unless ( $debug or $$asnmtapInherited->getOptionsValue ('onDemand') ) {
if ( defined $self->{_IMAP4}->{imap4} ) {
$email->delete ( $msgnum );
} elsif ( defined $self->{_POP3}->{pop3} ) {
$email->Delete( $msgnum );
}
}
}
}
$self->{defaultArguments}->{date} = undef;
$self->{defaultArguments}->{day} = undef;
$self->{defaultArguments}->{month} = undef;
$self->{defaultArguments}->{year} = undef;
$self->{defaultArguments}->{time} = undef;
$self->{defaultArguments}->{hour} = undef;
$self->{defaultArguments}->{min} = undef;
$self->{defaultArguments}->{sec} = undef;
undef $head;
$entity->purge;
undef $entity;
}
undef $parser;
}
if ( defined $self->{defaultArguments}->{numberOfMatches} and $self->{defaultArguments}->{numberOfMatches} ) {
$$asnmtapInherited->pluginValues ( { alert => $self->{defaultArguments}->{numberOfMatches} .' '. ( defined $parms{perfdataLabel} ? $parms{perfdataLabel} : 'email(s) received' ) }, $TYPE{APPEND} );
} else {
$returnCode = $parms{receivedState} ? $ERRORS{OK} : $ERRORS{CRITICAL};
$$asnmtapInherited->pluginValues ( { stateValue => $returnCode, alert => 'No '. ( defined $parms{perfdataLabel} ? $parms{perfdataLabel} : 'email(s) received' ) }, $TYPE{APPEND} );
}
if ( defined $self->{_IMAP4}->{imap4} ) {
$email->quit;
} elsif ( defined $self->{_POP3}->{pop3} ) {
$email->Close;
}
$$asnmtapInherited->appendPerformanceData ( "'". $parms{perfdataLabel} ."'=". $self->{defaultArguments}->{numberOfMatches} .';;;;' ) if ( defined $parms{perfdataLabel} );
return ( $returnCode, $self->{defaultArguments}->{numberOfMatches} );
}
}
# Destructor - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub DESTROY { print (ref ($_[0]), "::DESTROY: ()\n") if ( exists $$_[0]->{_asnmtapInherited} and $$_[0]->{_asnmtapInherited}->{_debug} ); }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
__END__
=head1 NAME
ASNMTAP::Asnmtap::Plugins::Mail is a Perl module that provides Mail functions used by ASNMTAP-based plugins.
=head1 SEE ALSO
ASNMTAP::Asnmtap, ASNMTAP::Asnmtap::Plugins
=head1 AUTHOR
Alex Peeters [alex.peeters@citap.be]
=head1 COPYRIGHT NOTICE
(c) Copyright 2000-2011 by Alex Peeters [alex.peeters@citap.be],
All Rights Reserved.
ASNMTAP is based on 'Process System daemons v1.60.17-01', Alex Peeters [alex.peeters@citap.be]
Purpose: CronTab (CT, sysdCT),
Disk Filesystem monitoring (DF, sysdDF),
Intrusion Detection for FW-1 (ID, sysdID)
Process System daemons (PS, sysdPS),
Reachability of Remote Hosts on a network (RH, sysdRH),
Rotate Logfiles (system activity files) (RL),
Remote Socket monitoring (RS, sysdRS),
System Activity monitoring (SA, sysdSA).
'Process System daemons' is based on 'sysdaemon 1.60' written by Trans-Euro I.T Ltd
=head1 LICENSE
This ASNMTAP CPAN library and Plugin templates are free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The other parts of ASNMTAP may be used and modified free of charge by anyone so long as this copyright notice and the comments above remain intact. By using this code you agree to indemnify Alex Peeters from any liability that might arise from it's u...
Selling the code for this program without prior written consent is expressly forbidden. In other words, please ask first before you try and make money off of my program.
Obtain permission before redistributing this software over the Internet or in any other medium. In all cases copyright and header must remain intact.
=cut
( run in 0.770 second using v1.01-cache-2.11-cpan-39bf76dae61 )