# SMSfcts.pm module # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # #NAME # SMSfcts.pm - First test for sanity of mod_perl inside apache # #SYNOPSIS # http://host.dom.ain/hello/world # #DESCRIPTION # This is a mod_perl module that contains the common routines for the SMSmsg # CGI script and the SMSmsg.pm mod_perl module. This separation was done so # that we can send SMS messages whether or not we have mod_perl installed. # #EXAMPLES # #FILES # #BUGS # #SEE ALSO # #AUTHOR # John Chambers # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # package SMSfcts; use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; use diagnostics; my $query = new CGI; my @names = $query->param(); my %ids = (); # SMS recipient ids my %name = (); # SMS recipient names my %phnum = (); # SMS recipient phone numbers my %smsid = (); # SMS recipient ids (digits of phone numbers) my %carrier = (); # SMS recipient carrier names my %where = (); # Where to send the message my %type = (); # How to send the message my $rcptcnt = 0; # Number of SMS recipients my $V = $main::V || 2; my $self; # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Create an instance of this module: sub new { $self = {}; bless $self; $self->initialize (); return $self; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Do we have any initialization? sub initialize { return 1; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub cgi { my $id = shift; my $F = "$main::P/cgi" || ''; my $Fr = $query->param('CGIfrom') || ''; my $RT = $query->param('CGIreplyto') || ''; my $CB = $query->param('CGIcallback') || ''; my $uri = $where{$id}; my $url = "http://$uri"; my($cmd,$en,$ln,$rsp); print "$F: From: $Fr
\n" if $V>1; print "$F: Reply-To: $RT
\n" if $V>1; print "$F: Callback: $CB
\n" if $V>1; if ($uri =~ "^vtext.com/.*messaging_lo.jsp") { print "$F: Recognized Verizon's SMS message forwarder.
\n" if $V>1; $cmd = "w3post $url"; if (open(POST,"| $cmd")) { print "$F: Opened pipe to '$cmd' command.
\n" if $V>1; print POST "?min=$smsid{$id}"; if ($Fr) { print POST "&trackResponses=No&showgroup=n&Send.x=Yes&DOMAIN_NAME=\@vtext.com"; print POST "&subject=$Fr"; } if ($RT) { print POST "&sender=$RT"; } if ($CB) { print POST "&callback=$CB"; } print POST "&text="; for $ln (@_) { print "ln: \"$ln\"
\n" if $V>1; ($en = $ln) =~ s/(\W)/'%'.ord($1)/ge; print "en: \"$en\"
\n" if $V>1; print POST $en; } print POST "\n\n"; close POST; } else { print "$F: Can't open 'w3post $url' command.
\n" if $V>0; } } else { print "$F: Don't recognize CGI '$uri'
\n" if $V>0; } print "$F: NOT FULLY IMPLEMENTED YET
\n" if $V>0; return 0; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Send one message. The first arg is the recipient; # the rest of the args are lines of the message. sub email { my $id = shift; my $F = $main::P . '/email'; my $P = "$main::P"; my($ad,$cm,$ln,$pn,$rcpt,$st); print "$F: id='$id' V=$V
\r" if $V>0; $rcpt = "$id ($name{$id} $smsid{$id} $where{$id})"; print "$F: rcpt='$rcpt'
\r" if $V>1; ($pn = $smsid{$id}); print "$F: pn='$pn'
\r" if $V>1; $ad = $pn . '@' . $where{$id}; print "$F: ad='$ad'
\r" if $V>1; $cm = "SendMsg to $ad"; print "$F: cm='$cm'
\r" if $V>1; if (open(MAIL,"| $cm")) { print "$F: Opened '| $cm'
\r" if $V>1; for $ln (@_) { print "Line: \"$ln\"
\n" if $V>1; print MAIL "$ln\n"; } $st = close MAIL; if ($st) { print "$P: Message to $rcpt accepted.
\n" if $V>0; } else { print "$P: Message to $rcpt failed (status $st)
\n" if $V>0; } } else { print "### Can't run '$cm' ($!)
\n" if $V>0; } } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # sub getrcpts { my $rfile = shift; my $F = $main::P . '/getrcpts'; my($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; line: 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') { if ($id = $v) { # Remember this recipient's id print "+++ Warning: Duplicate id '$id' ($ids{$id} earlier)
\n" if $ids{$id}; $ids{$id}++; # Count the occurrences of the ids print "--- $id
\n" if $V>1; $rcptcnt++; } next line; } next line unless $id; if ($n eq 'name') { $name{$id} = $v; print "--- $id name='$v'
\n" if $V>1; } elsif ($n eq 'phone') { $phnum{$id} = $v; ($smsid{$id} = $v) =~ s/\D+//g; print "--- $id phnum='$phnum{id}'
\n" if $V>1; print "--- $id smsid='$smsid{$id}'
\n" if $V>1; } elsif ($n eq 'carrier') { $carrier{$id} = $v; print "--- $id carrier='$v'
\n" if $V>1; } elsif ($n eq 'method') { if ($v =~ s/^([@!])//) { $type{$id} = $1; # Default method is email $where{$id} = $v; # Remainder is address or command print "--- $id Type='$type{$id}' method='$where{$id}'
\n" if $V>1; } else { $type{$id} = '@'; # Default method is email $where{$id} = $v; print "--- $id type='\@' where='$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 to handle one SMSmsg request sub form { my $r = shift; my $F = "$main::P/form"; 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; my $cwd = `pwd`; $cwd =~ s"[\r\s]+$""; my $host = `hostname`; $host =~ s"[\r\s]+$""; $V = $main::V || 2; print " Client: $host
\n"; print " Universal time: $udt.
\n"; print " Our local time: $ldt.
\n"; print " Current dir: $cwd
\n"; print " Verbosity: $V
\n"; print "

\n"; $V = $query->param('V') || $V; if ($V>1) { print "


Here are our form elements:\n"; 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"; } if ($V>1) { 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 = &getrcpts($rfile); print "File '$rfile' has $n entries.
\r" if $V>1; # Were we passed a message and a recipient? if ($msg = $query->param('msg')) { print "$F: Message '$msg'
\r" if $V>1; if (@rcpt = $query->param('rcpt')) { # Multiple recipients $n = int(@rcpt); print "$F: We have $n recipients:
\r" if $V>1; for $rcpt (@rcpt) { print "$F: recipient '$rcpt' type '$type{$rcpt}'
\r" if $V>1; if ($type{$rcpt} eq '!') { print "$F: recipient '$rcpt' is type '$type{$rcpt}'
\r" if $V>1; &cgi($rcpt,$msg); } else { print "$F: recipient '$rcpt' is $smsid{$rcpt}\@$where{$rcpt}
\r" if $V>1; &email($rcpt,$msg); } } } elsif ($rcpt = $query->param('rcpt')) { # Single recipient print "$F: Recipient '$rcpt'
\r" if $V>1; &email($rcpt,$msg); } else { print "$F: No recipient(s).


\r" if $V>1; } } else { print "$F: No message.


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


"; print "
\n"; print "
\n"; # Parameters for SMS via CGI: print "Data for Verizon CGI script:
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
From:
Reply-To:
Callback:
\n"; # Show the list of recipients: print "

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

\n"; &rcptlist(); print "Verbosity:
\n"; print "

\n"; # Finish up the HTML: print "
Done.\n" if $V>1; print "\n"; print "\n"; return 1; } # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # Produce the HTML table that shows the recipient list. sub rcptlist() { my $id; print "\n"; print "\t\n"; print "\t\n"; print "\t\n"; print "\t\n"; print "\t\n"; print "\t\n"; for $id (sort keys %ids) { print "\t\n"; print "\t\t\n"; } print "
SMS recipients
nameidphonecarriermethod
\n"; print "\t\t$name{$id}\n"; print "\t\t$id\n"; print "\t\t$phnum{$id}\n"; print "\t\t$carrier{$id}\n"; print "\t\t$type{$id}$where{$id}\n"; print "\t

\n"; } 1; # Gotta let mod_perl know that we loaded OK. __END__