# SMSmsg.pm mod_perl module # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # #NAME # SMSmsg.pm - First test for sanity of mod_perl inside apache # #SYNOPSIS # http://host.dom.ain/hello/world # #DESCRIPTION # This is a mod_perl module to test web requests, and show you everything # that the client has sent us. We first display any form elements, followed # by the environment vector. # #OPTIONS # #EXAMPLES # #FILES # #BUGS # #SEE ALSO # #AUTHOR # John Chambers # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # package Apache::SMSmsg; sub new { my $self = {}; bless $self; $self->initialize (); return $self; } sub initialize { return 1; } use strict; #se Apache::Constants qw(:common); use CGI; use CGI::Carp 'fatalsToBrowser'; use diagnostics; my %rcpts = (); # SMS recipient ids my %name = (); # SMS recipient names my %phone = (); # SMS recipient phone numbers my %carrier = (); # SMS recipient carrier names my %email = (); # SMS recipient email hosts my $rcptcnt = 0; # Number of SMS recipients sub sendMsg { my $id = shift; my $V = $main::V || 1; my($ad,$cm,$ln,$pn,$st); print "sendMsg: id='$id'
\r" if $V>1; ($pn = $phone{$id}) =~ s/\D+//g; print "sendMsg: pn='$pn'
\r" if $V>1; $ad = $pn . '@' . $email{$id}; print "sendMsg: ad='$ad'
\r" if $V>1; $cm = "SendMsg to $ad"; print "sendMsg: cm='$cm'
\r" if $V>1; if (open(MAIL,"| $cm")) { for $ln (@_) { print "$ln\n" if $V>1; print MAIL "$ln\n"; } close MAIL; $st = system $cm; print "sendMsg: st='$st'
\r" if $V>1; } else { print "### Can't run '$cm' ($!)
\n" if $V>0; } } sub showSMSrcpts { my $rfile = shift; my $V = $main::V || 2; my($i,$id,$l,$line,$n,$v); unless (open(RFILE,$rfile)) { print "### Can't read recipient file '$rfile' [$!]
\r"; return 0; } print "Recipient file='$rfile' opened.
\r" if $V>1; while ($l = ) { $l =~ s"[\r\s]+$""; $line ++; print "RFILE line $line: '$l'
\n" if $V>4; if ($l =~ /^\s*#/) { # Comments ignored } elsif ($l eq '') { # Blank lines ignored } elsif (($n,$v) = ($l =~ /^([^:]+):\s*(.*)/)) { $n = lc($n); # Names are caseless print "$n: '$v'
\n" if $V>3; if ($n eq 'id') { $id = $v; # Remember this recipient's id print "+++ Warning: Duplicate id '$id' ($rcpts{$id} earlier)
\n" if $rcpts{$id}; $rcpts{$id}++; # Count the occurrences of the ids print "--- $id
\n" if $V>1; $rcptcnt++; } elsif ($n eq 'name') { $name{$id} = $v; print "--- $id name='$v'
\n" if $V>1; } elsif ($n eq 'phone') { $phone{$id} = $v; print "--- $id phone='$v'
\n" if $V>1; } elsif ($n eq 'carrier') { $carrier{$id} = $v; print "--- $id carrier='$v'
\n" if $V>1; } elsif ($n eq 'email') { $email{$id} = $v; print "--- $id email='$v'
\n" if $V>1; } else { print "### Unknown name '$n'
\n" if $V>1; } } else { print "### Can't parse '$l'
\n" if $V>1; } } print "Recipient file='$rfile' done ($rcptcnt rcpts)
\r" if $V>2; close RFILE; return $rcptcnt; # Return recipient count } # This routine is called by apache when it gets a request sub handler { my $r = shift; my $V = $main::V || 2; my($msg,$n,$rcpt,@rcpt,$rfile,$scdir,$v,@vals); my $rcptfile = 'SMSdata.txt'; my($usec,$umin,$uhour,$umday,$umon,$uyear) = gmtime(time); $uyear += 1900; $umon += 1; my($lsec,$lmin,$lhour,$lmday,$lmon,$lyear) = localtime(time); $lyear += 1900; $lmon += 1; my $udt = sprintf "%04d/%02d/%02d %02d:%02d:%02d",$uyear,$umon,$umday,$uhour,$umin,$usec; my $ldt = sprintf "%04d/%02d/%02d %02d:%02d:%02d",$lyear,$lmon,$lmday,$lhour,$lmin,$lsec; $r->content_type('text/html'); $r->send_http_header; return 1 if $r->header_only; my $host = $r->get_remote_host; my $cwd = `pwd`; $cwd =~ s"[\r\s]+$""; $r->print("\n"); $r->print("
SMSmsg.pm
\n"); $r->print("\n"); $r->print(" Client: $host
\n"); $r->print(" Universal time: $udt.
\n"); $r->print(" Our local time: $ldt.
\n"); $r->print(" Current dir: $cwd
\n"); $r->print(" Verbosity: $V
\n"); $r->print("

\n"); print "


Here are our form elements:\n"; my $query = new CGI; my @names = $query->param(); print "
\n"; for $n (sort @names) { if (@vals = $query->param($n)) { for $v (@vals) { $v =~ s"([,;:])"$1 "g; print "
$n
'$v'\n"; } } elsif ($v = $query->param($n)) { $v =~ s"([,;:])"$1 "g; print "
$n
\"$v\"\n"; } else { print "
$n
(NO VALUE)\n"; } } print "
\n"; print "
Here are our environment variables:\n"; print "
\n"; for $n (sort keys %ENV) { $v = $ENV{$n}; if ($n eq 'HTTP_ACCEPT') { $v =~ s",", "g; } elsif ($n eq 'PATH') { $v =~ s":" "g; } else { } print "
$n
$v\n"; } print "
\n"; # Look for our list of SMS recipients: print "Find list of SMS recipients ...
\r" if $V>1; $v = $ENV{'SCRIPT_FILENAME'}; print "SCRIPT_FILENAME='$v'
\r" if $V>1; ($scdir = $v) =~ s"[^/]*$""; print "Directory: '$scdir'
\r" if $V>1; chdir $scdir; $rfile = "$scdir/$rcptfile"; print "Recipient file='$rfile' ...
\r" if $V>1; $n = &showSMSrcpts($rfile); print "File '$rfile' has $n recipients.
\r" if $V>1; # Were we passed a message and a recipient? if ($msg = $query->param('msg')) { print "

Message '$msg'
\r" if $V>1; if (@rcpt = $query->param('rcpt')) { for $rcpt (@rcpt) { print "

recipient '$rcpt'
\r" if $V>1; &sendMsg($rcpt,$msg); } } elsif ($rcpt = $query->param('rcpt')) { print "

Recipient '$rcpt'
\r" if $V>1; &sendMsg($rcpt,$msg); } else { print "

No recipient(s).


\r" if $V>1; } } else { print "

No message.


\r" if $V>1; } # Produce box for a message: print "


\n"; print "
\n"; print "\n"; # Show the list of recipients: print "

\n"; print "Check one or more recipients below,\n"; print "then press \n"; print "

\n"; print "\n"; print "\t\n"; print "\t\n"; print "\t\n"; print "\t\n"; print "\t\n"; print "\t\n"; my $id; for $id (sort keys %rcpts) { print "\t\n"; print "\t\t\n"; } print "
SMS recipients
NameIDPhoneCarrierEMail host
\n"; print "\t\t$name{$id}\n"; print "\t\t$id\n"; print "\t\t$phone{$id}\n"; print "\t\t$carrier{$id}\n"; print "\t\t$email{$id}\n"; print "\t
\n"; print "

\n"; # Finish up the HTML: print "
Done.\n"; $r->print("\n"); $r->print("\n"); return 1; } 1; # Gotta let mod_perl know that we loaded OK. __END__