--- sms.alert 2003/02/04 15:16:37 1.2 +++ sms.alert 2003/10/28 17:37:05 1.8 @@ -3,17 +3,35 @@ # sms.alert - send an alert via smstools available at # http://www.isis.de/~s.frings/smstools/ # -# based on qpage.alert by Jim Trocki, trockij@transmeta.com +# based on qpage.alert and mail.alert by Jim Trocki, trockij@transmeta.com # # usage: -# sms.alter [-f][-l][-u] 385abxxxxxx|name +# sms.alter [flags...] -g group -s system 385abxxxxxx|name[ 12345|name...] +# +# flags which are standard: # -# flags: # -u this is upalert +# -g grp group +# -s sys system +# -l time time to next alert +# +# flags specific to sms.alert: (all optional) +# # -f send flash sms (directly to screen) -# -l send long message (all output, smstools with split it in +# -x send extended message (all output, smstools with split it in # multiple smses if needed) +# -i hh:mm-hh:mm send sms only if it falls in this interval +# +# it users file /usr/local/etc/sms.addressbook as addressbook in format +# +# 123456 name +# +# where 123456 is phone number (with country prefix, but without +) and +# name is alias used like: +# +# sms.alert -f dobrica # +# in mon.cf my $sms_outgoing = "/var/spool/sms/outgoing"; my $sms_addressbook = "/usr/local/etc/sms.addressbook"; @@ -21,7 +39,7 @@ use strict; my %opt; use Getopt::Std; -getopts ("s:g:fu", \%opt); +getopts ("S:s:g:h:t:l:ufi:", \%opt); # read addressbook my %name2phone; @@ -46,21 +64,44 @@ my $summary = shift @MSG; chomp $summary; +if ($opt{i}) { + if ($opt{i} =~ /(\d+):(\d+)-(\d+):(\d+)/) { + my $from = $1 * 60 + $2; + my $to = $3 * 60 + $4; + my ($sec,$min,$hour) = localtime(time); + my $t = $hour * 60 + $min; + exit 0 if ($t < $from || $t > $to); + } else { + die "interval time format: hh:mm-hh:mm" + } +} + +$summary = $opt{S} if $opt{S}; + my ($wday,$mon,$day,$tm) = split (/\s+/, localtime); my $ALERT = $opt{u} ? "UPALERT" : "ALERT"; my $sms = "From: mon\n"; -$sms .= "Flash: yes\n" if $opt{'f'}; -$sms .= "\n$ALERT $opt{g}/$opt{s} $summary ($wday $mon $day $tm)\n"; -$sms .= join("\n",@MSG) if $opt{'l'}; +$sms .= "Flash: yes\n" if $opt{f}; +$sms .= "\n$ALERT"; +$sms .= " $opt{g}/$opt{s}" if ($opt{g} && $opt{s}); +$sms .= " $summary ($wday $mon $day $tm)\n"; +# add next alter time +$sms .= "Secs until next alert: $opt{l}\n" if $opt{l}; +# add rest of text +$sms .= join("\n",@MSG) if $opt{x}; + +my $suffix = 0; foreach my $to (@ARGV) { - if (open(SMS, "> $sms_outgoing/mon$$")) { + if (open(SMS, "> $sms_outgoing/mon-$$-$suffix")) { my $phone = $to; $phone = $name2phone{lc($to)} if ($name2phone{lc($to)}); + die "$phone is not phone number! " if ($phone !~ m/^\d+$/); print SMS "To: $phone\n$sms"; close SMS; + $suffix++; } else { die "could not open sms file in '$sms_outgoing': $!"; }