Debian Codegraph @

eg/send-sms

raw
1 #!/usr/bin/perl
2
3 # $Id$
4 # A simple script to be called by other scripts, taking either two parameters - a
5 # mobile number (or comma-separated list of numbers) and a message, or just a
6 # message, in which case the default_destination in the config file is used.
7 #
8 # Reads www.aql.com account details from a YANL
9 # file ~/.aql_login, which should contain username, password and sender number
10 # (if a sender number is not included, the machine's hostname will be used).
11 # (Yes, a sender can be a mobile number, or any text).
12
13 use strict;
14 use SMS::AQL;
15 use Sys::Hostname;
16 use YAML;
17
18 my $conf_file = $ENV{HOME} . '/.aql_login';
19 my $conf = YAML::LoadFile($conf_file)
20 or die "Failed to read $conf_file";
21
22
23 my ($destinations, $message);
24
25 if (@ARGV == 2) {
26 $destinations = shift @ARGV;
27 $message = join ' ', @ARGV;
28 } elsif (@ARGV == 1) {
29 $destinations = $conf->{default_destination};
30 if (!$destinations) {
31 die "Called with only one parameter, and no default_destination "
32 ."specified in $conf_file";
33 }
34 $message = shift @ARGV;
35 } else {
36 show_usage();
37 }
38
39
40 my $sms = SMS::AQL->new({
41 username => $conf->{username},
42 password => $conf->{password},
43 options => {
44 sender => $conf->{sender} || Sys::Hostname::hostname(),
45 },
46 });
47
48 if (!$sms) {
49 die "Failed to get SMS::AQL object";
50 }
51
52 my $failures;
53
54 for my $destination (split /,/, $destinations) {
55 my ($ok, $why) = $sms->send_sms($destination, $message);
56 if (!$ok) {
57 warn "Failed to send to $destination - $why";
58 $failures++;
59 }
60 }
61
62 if ($failures) {
63 exit -1;
64 } else {
65 exit 0;
66 }
67
68
69
70 sub show_usage {
71 print <<USAGE;
72
73 Usage: $0 destination message
74
75 E.g. $0 +44788123456 "Here is a nice message"
76 USAGE
77 exit;
78 }
79