+

Last commit for mail2gnokii: 0c2531dc74c0d2f1050a1c380fc1aa411ae79774

Change return code

Benjamin Renard [2014-02-12 17:37:35]
Change return code
#!/usr/bin/perl -w

# # mail2gnokii
#
# Postfix transport to sending SMS by gnokii
#
# Important : The SMS content is the email's subject header.
#             Usefull for Nagios notification
#
# Usage :
#
#   in master.cf :
#
#        sms unix    -       n       n       -       1       pipe
#           flags=Rq user=gnokii:dialout argv=/usr/local/sbin/mail2gnokii ${user} [SMSC]
#
#   in transport :
#
#        sms.example.tld    sms
#
# Dependencies :
#
#   * Email::Simple in libemail-simple-perl Debian package
#
# Usage :
#
#  mail2gnokii [phone number] [SMSC]
#       Mail content is sent through STDIN
#       SMSC is optionaly. If not provided, default value is used.
#

use MIME::Parser;
use Encode qw/encode decode/;

# Configuration
$smsc="+33609001390";
$check_authorized_number=undef;
@authorized_number=("0612345678","0033600000000");
$gnokii_config="/etc/gnokiirc";
$home_directory="/var/log/gnokii";

my $message;

# constantes tirees de <sysexits.h>
use constant {
	EX_OK => 0,
	EX_USAGE => 64,
	EX_DATAERR => 65,
	EX_NOINPUT => 66,
	EX_NOUSER => 67,
	EX_NOHOST => 68,
	EX_UNAVAILABLE => 69,
	EX_SOFTWARE => 70,
	EX_TEMPFAIL => 75
};


if($#ARGV + 1 != 1 and $#ARGV + 1 != 2){
	print "Usage : mail2gnokii [phone number] [SMSC phone number]";
	exit EX_USAGE;
}



# recuperation du mail
my $parser = MIME::Parser->new;
$parser->decode_headers(1);
$parser->output_under("/tmp");
my $entity = $parser->parse(\*STDIN);

my $subject = $entity->head->get("Subject");
$subject = Encode::encode('utf-8', $subject);
my $body = $entity->body_as_string;

$message = "$subject\n$body\n";


# recuperation du numero de mobile
my $mob=$ARGV[0];

if (not $mob =~ m/^[0-9]*$/) {
	print "Invalid phone number : $mob";
	exit EX_DATAERR;
}

# recuperation du numero du SMSC (si present)
if (defined $ARGV[1]) {
	if (not $ARGV[1]=~ m/^[0-9\+]*$/) {
		print "Invalid SMSC phone number : "+$ARGV[1];
		exit EX_CONFIG;
	}
	$smsc=$ARGV[1];
}

sub in_array {
	my ($arr,$search_for) = @_;
	my %items = map {$_ => 1} @$arr; # create a hash out of the array values
	return (exists($items{$search_for}))?1:0;
}

# Envoie du SMS
if (not $check_authorized_number or $mob ne '' && in_array(\@authorized_number,$mob)){
	chdir $home_directory;
	open(GNOKII,  "| /usr/bin/gnokii --config $gnokii_config --sendsms $mob --smsc $smsc");
	print GNOKII $message;
	close(GNOKII);
	if($? != 0){
		print "Error sending SMS";
		exit EX_TEMPFAIL;
	}
}
else{
	print "Phone number $mob not in authorized list";
	exit EX_DATAERR;
}
exit EX_OK;
ViewGit