0c2531dc74c0d2f1050a1c380fc1aa411ae79774
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

1) #!/usr/bin/perl -w
2) 
3) # # mail2gnokii
4) #
5) # Postfix transport to sending SMS by gnokii
6) #
Benjamin Renard Added informations at the t...

Benjamin Renard authored 11 years ago

7) # Important : The SMS content is the email's subject header.
8) #             Usefull for Nagios notification
9) #
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

10) # Usage : 
11) #
12) #   in master.cf :
13) #
14) #        sms unix    -       n       n       -       1       pipe
Benjamin Renard add smsc option to script m...

Benjamin Renard authored 11 years ago

15) #           flags=Rq user=gnokii:dialout argv=/usr/local/sbin/mail2gnokii ${user} [SMSC]
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

16) #
17) #   in transport :
18) #
19) #        sms.example.tld    sms
20) #
Benjamin Renard Added informations at the t...

Benjamin Renard authored 11 years ago

21) # Dependencies :
22) #
23) #   * Email::Simple in libemail-simple-perl Debian package
24) #
25) # Usage :
26) #
27) #  mail2gnokii [phone number] [SMSC]
28) #       Mail content is sent through STDIN
29) #       SMSC is optionaly. If not provided, default value is used.
30) #
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

31) 
Benjamin Renard Using MIME::Parser instead...

Benjamin Renard authored 10 years ago

32) use MIME::Parser;
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

33) use Encode qw/encode decode/;
34) 
35) # Configuration
36) $smsc="+33609001390";
Benjamin Renard Add check_authorized_number...

Benjamin Renard authored 11 years ago

37) $check_authorized_number=undef;
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

38) @authorized_number=("0612345678","0033600000000");
39) $gnokii_config="/etc/gnokiirc";
Benjamin Renard Using MIME::Parser instead...

Benjamin Renard authored 10 years ago

40) $home_directory="/var/log/gnokii";
41) 
42) my $message;
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

43) 
44) # constantes tirees de <sysexits.h>
45) use constant {
46) 	EX_OK => 0,
47) 	EX_USAGE => 64,
48) 	EX_DATAERR => 65,
49) 	EX_NOINPUT => 66,
50) 	EX_NOUSER => 67,
51) 	EX_NOHOST => 68,
52) 	EX_UNAVAILABLE => 69,
Benjamin Renard Change return code

Benjamin Renard authored 10 years ago

53) 	EX_SOFTWARE => 70,
54) 	EX_TEMPFAIL => 75
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

55) };
56) 
57) 
Benjamin Renard add smsc option to script m...

Benjamin Renard authored 11 years ago

58) if($#ARGV + 1 != 1 and $#ARGV + 1 != 2){
59) 	print "Usage : mail2gnokii [phone number] [SMSC phone number]";
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

60) 	exit EX_USAGE;
61) }
62) 
63) 
64) 
65) # recuperation du mail
Benjamin Renard Using MIME::Parser instead...

Benjamin Renard authored 10 years ago

66) my $parser = MIME::Parser->new;
67) $parser->decode_headers(1);
68) $parser->output_under("/tmp");
69) my $entity = $parser->parse(\*STDIN);
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

70) 
Benjamin Renard Using MIME::Parser instead...

Benjamin Renard authored 10 years ago

71) my $subject = $entity->head->get("Subject");
72) $subject = Encode::encode('utf-8', $subject);
73) my $body = $entity->body_as_string;
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

74) 
Benjamin Renard Using MIME::Parser instead...

Benjamin Renard authored 10 years ago

75) $message = "$subject\n$body\n";
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

76) 
77) 
78) # recuperation du numero de mobile
79) my $mob=$ARGV[0];
80) 
81) if (not $mob =~ m/^[0-9]*$/) {
82) 	print "Invalid phone number : $mob";
83) 	exit EX_DATAERR;
84) }
85) 
Benjamin Renard add smsc option to script m...

Benjamin Renard authored 11 years ago

86) # recuperation du numero du SMSC (si present)
87) if (defined $ARGV[1]) {
88) 	if (not $ARGV[1]=~ m/^[0-9\+]*$/) {
89) 		print "Invalid SMSC phone number : "+$ARGV[1];
Benjamin Renard Change return code

Benjamin Renard authored 10 years ago

90) 		exit EX_CONFIG;
Benjamin Renard add smsc option to script m...

Benjamin Renard authored 11 years ago

91) 	}
92) 	$smsc=$ARGV[1];
93) }
94) 
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

95) sub in_array {
96) 	my ($arr,$search_for) = @_;
97) 	my %items = map {$_ => 1} @$arr; # create a hash out of the array values
98) 	return (exists($items{$search_for}))?1:0;
99) }
100) 
101) # Envoie du SMS
Benjamin Renard Add check_authorized_number...

Benjamin Renard authored 11 years ago

102) if (not $check_authorized_number or $mob ne '' && in_array(\@authorized_number,$mob)){
Benjamin Renard Using MIME::Parser instead...

Benjamin Renard authored 10 years ago

103) 	chdir $home_directory;
104) 	open(GNOKII,  "| /usr/bin/gnokii --config $gnokii_config --sendsms $mob --smsc $smsc");
105) 	print GNOKII $message;
106) 	close(GNOKII);
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

107) 	if($? != 0){
108) 		print "Error sending SMS";
Benjamin Renard Change return code

Benjamin Renard authored 10 years ago

109) 		exit EX_TEMPFAIL;