#!/usr/bin/perl -w # # mail2gnokii # # Postfix transport to sending SMS by gnokii # # 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 # # # arg1: phone number use Email::Simple; use Encode qw/encode decode/; # Configuration $smsc="+33609001390"; $check_authorized_number=undef; @authorized_number=("0612345678","0033600000000"); $gnokii_config="/etc/gnokiirc"; # constantes tirees de 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 }; if($#ARGV + 1 != 1 and $#ARGV + 1 != 2){ print "Usage : mail2gnokii [phone number] [SMSC phone number]"; exit EX_USAGE; } # recuperation du mail my $text; while(){ $text .= $_; } if($text eq ''){ print "Mail is empty !"; exit EX_NOINPUT; } my $email = Email::Simple->new($text); # recuperation du sujet my $subject = $email->header("Subject"); if($subject eq ''){ print "No text in Subject header !"; exit EX_NOINPUT; } $subject = decode('MIME-Header', $subject); # 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_DATAERR; } $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)){ `echo "$subject" | gnokii --config $gnokii_config --sendsms $mob --smsc $smsc`; if($? != 0){ print "Error sending SMS"; exit EX_SOFTWARE; } } else{ print "Phone number $mob not in authorized list"; exit EX_DATAERR; } exit EX_OK;