4f764364e869536db15bb6b600ac47626d283e0e
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 12 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 12 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,
53) 	EX_SOFTWARE => 70
54) };
55) 
56) 
Benjamin Renard add smsc option to script m...

Benjamin Renard authored 12 years ago

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

Benjamin Renard authored 12 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 12 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 12 years ago

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

Benjamin Renard authored 10 years ago

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

Benjamin Renard authored 12 years ago

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

Benjamin Renard authored 12 years ago

85) # recuperation du numero du SMSC (si present)
86) if (defined $ARGV[1]) {
87) 	if (not $ARGV[1]=~ m/^[0-9\+]*$/) {
88) 		print "Invalid SMSC phone number : "+$ARGV[1];
89) 		exit EX_DATAERR;
90) 	}
91) 	$smsc=$ARGV[1];
92) }
93) 
Benjamin Renard initial commit

Benjamin Renard authored 12 years ago

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

Benjamin Renard authored 12 years ago

101) 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

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