#!/usr/bin/perl -wT

use strict;
use HTTP::Lite;
use Getopt::Long;
use vars qw($opt_version $opt_help $opt_username $opt_password $opt_api_id $opt_baseurl $opt_text $opt_to $PROGNAME);
use lib utils.pm ;
use utils qw(%ERRORS &print_revision &support &usage);

$PROGNAME = "notify_sms";

sub print_help ();
sub print_usage ();

Getopt::Long::Configure('bundling');

GetOptions
        ("v"   => \$opt_version, "version"    => \$opt_version,
         "h"   => \$opt_help, "help"       => \$opt_help,
         "a=s" => \$opt_api_id, "apiid=s" => \$opt_api_id,
         "b=s" => \$opt_baseurl, "baseurl=s" => \$opt_baseurl,
         "m=s" => \$opt_text, "message=s" => \$opt_text,
         "p=s" => \$opt_password, "password=s" => \$opt_password,
         "t=s" => \$opt_to, "to=s" => \$opt_to,
         "u=s" => \$opt_username, "username=s"  => \$opt_username);

if ($opt_version) {
        print_revision($PROGNAME,'$Revision: 1.1 $');
        exit $ERRORS{'OK'};
}
if ($opt_help) {print_help(); exit $ERRORS{'OK'};}

($opt_api_id) || usage("Gateway api ID must be specified\n");
($opt_text) || usage("Message body must be specified\n");
($opt_password) || usage("Gateway login password must be specified\n");
($opt_to) || usage("Phone to number must be specified\n");
($opt_username) || usage("Gateway login username must be specified\n");

if (!$opt_baseurl) {
    $opt_baseurl = "http://api.clickatell.com";
}

my $login_url = $opt_baseurl."/http/auth?user=".$opt_username."&password=".$opt_password."&api_id=".$opt_api_id;
my $send_url = $opt_baseurl."/http/auth?user=".$opt_username."&password=".$opt_password."&api_id=".$opt_api_id;

#print $text."\n";

my $http = new HTTP::Lite;
my $req = $http->request($login_url);
my $res = $http->body();
my @sres = split(":", $res);

if ($sres[0] eq "OK") {
    my $sess = $sres[1];
    chomp($sess);
    $sess = substr($sess,1);
    my $rhttp = new HTTP::Lite;
    my $ret = $rhttp->request($send_url);
    my $nres = $rhttp->body();
    my @sent = split(":", $nres);
    if ($sent[0] eq "ID") {
	print "Message sent\n";
	exit $ERRORS{'OK'};
    } else {
	print "Gateway authentication failed\n";
	exit $ERRORS{'CRITICAL'};
    }
} else {
    print "Authentication failed\n";
}

sub urlencode {
    my $ask = shift @_;
    my @a2 = unpack "C*", $ask;
    my $s2 = "";
    while (@a2) {
	$s2 .= sprintf "%%%X", shift @a2;
    }
    return $s2;
}

sub print_usage () {
    print "Usage: $PROGNAME -u <gw_username> -p <gw_password> -a <gw_api_id> -b <gw_baseurl> -m <message_text> -t <to_msisdn>\n";
}

sub print_help () {
        print_revision($PROGNAME,'$Revision: 1.1 $');
        print "
Copyright (c) 2005 Serhan D. KIYMAZ <skiymaz\@netpia.com>
Netpia Internet Hizmetleri Pazarlama A.S. - TURKEY

This plugin sends SMS notification messages to contacts from a SMS Gateway.
It has been tested with Clickatell <www.clickatell.com>

";
        print_usage();
        print "
-a, --apiid=API_ID
   Api ID for gateway login. It is specified by gateway company.
-b, --baseurl=BASE_URL
   Base URL for login and send SMS actions of gateway. Default: http://api.clickatell.com.
-m, --message=MESSAGE_TEXT
   Message text that will be sent to TO number. Max: 160 chars.
-p, --password=PASSWORD
   Login password for gateway login.
-t, --to=TO_NUMBER
   GSM phone number that the message will be delivered to.
-u, --username=USERNAME
   Login username for gateway login.

";
        support();
    }
