#!/usr/bin/perl
#
# sendsms.pl - send SMS 
#
#
# Copyright (c) 2007, DreamTime.net Inc.
# All rights reserved.
# Email: sales@diamondcard.us
# Phone: 1-303-997-0900
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the DreamTime.net Inc. nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY DreamTime.net Inc. ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL DreamTime.net Inc. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#

                                        # -- Local constants --
my $SERVER = 'http://sms.diamondcard.us/';
my $URI = 'SMSapi';
my $PROXY = $SERVER;
my ($USAGE)=<<EOS;
Usage: sendsms.pl [OPTIONS]

Options:
  --accountid=accId     AccountId
  --pin=pinCode    	PIN code
  --from=sendFrom       SMS From field
  --to=listOfNumbers    List of destination numbers separated by coma
  --msg=message		Message to sent
  --verbose		Verbose mode
  --help                Print this help and exit
EOS
                                        # -- External routines --
use SOAP::Lite;				
use Getopt::Long;
                                        # -- Local variables --
my $help;
my $accId;
my $pinCode;
my $from;
my $to;
my $verbose;

                                        # -- START HERE --
GetOptions("help" => \$help,
	   "accountid=s" => \$accId,
	   "pin=s" => \$pin,
	   "from=s" => \$from,
	   "to=s" => \$to,
           "msg=s" => \$msg,
	   "verbose" => \$verbose) or die $USAGE;

my $err .= "AccountId is missing\n" if (!$accId);
$err = "PIN code is missing\n" if (!$err && !$pin);
$err = "SMS From field is missing\n" if (!$err && !$from);
$err = "Destination number is missing\n" if (!$err && !$to);
$err = "Message to sent is missing\n" if (!$err && !$msg);

print "$err\n" if $err;
if ($help || $err) {
  print $USAGE;
  exit 1;
}

my @destList = split(',',$to);
%params = ('AccId' => $accId,		# Account Id
           'PinCode' => $pin,		# Pin code
           'MsgTxt' => $msg,		# Message to sent
           'SendFrom' => $from,		# Send from
	   'Destination' => \@destList, # List of destinations
          );
$res = &sendRequest('send', \%params);

if ($res->{ErrCode}) {                 	# Fail
  print "Error: $res->{ErrMsg}\n" if $verbose;	# Error message
  if ($res->{ErrCode} eq 'INVDEST') {	# Load list of invalid destinations if available
    if (ref($res->{InvalidDestinations}) eq 'ARRAY') {
      print "List of invalid phone numbers:\n".
    	    join(', ',@{$res->{InvalidDestinations}})."\n" if $verbose;
    }
    elsif ($res->{InvalidDestinations}) {
      print "Invalid phone number: $res->{InvalidDestinations}\n" if $verbose;
    }
  }
  exit 1;
}
                                        # Success
print "Message Sent.\n" if $verbose;	
exit;

#
# sendRequest - send request using SOAP::Lite
# Call:		$result = sendRequest($func, $params)
# 
sub sendRequest {
  my ($func, $params) = @_;
  my $soap = SOAP::Lite
                -> uri($URI)
                -> proxy($PROXY)
  		-> on_fault(sub { return {
	                           'ErrMsg' => ref $_[1] ? $_[1]->faultstring : $_[0]->transport->status,
				   'ErrCode' => 'ERR'}
                                })
  		-> $func($params);

  if (!$soap) {
    return {'ErrMsg' => 'SOAP creation error', 
	    'ErrCode' => 'ERR'}; 
  }

  if ($soap->{ErrMsg}) {
    return {'ErrMsg' => $soap->{ErrMsg}, 
	    'ErrCode' => 'ERR'};
  }
  return $soap->result();
}
