#!/usr/bin/perl
#
# Sends a SMS using the telcel web service
#
# Ivan Chavero <ichavero@uach.mx>
#
# The recipient must have the internet SMS service activated on her phone
#
#
# You need to have the module WWW::Mechanize installed on your system
#
# you can check for the module with this perl command line:
#
# # perl -MWWW::Mechanize
#
# if you get an error, you don't have the module installed
#
# if you don't have it install it with the following this instructions:
#
# # perl -MCPAN -e shell
# cpan> force install WWW::Mechanize
#


use strict;
use WWW::Mechanize;
use Getopt::Std;


my $SMS_URL = "http://www.ideastelcel.com/home.do";
my $FORM_NAME = "centro_mensajes";
my $DEBUG = 0;

my $TELEPHONE = "6141231212";
my $MESSAGE = "PRUEBA DESDE PERL";
my $SENDER = ":: Perl SMS";



if($#ARGV < 1){
	print "Use: $0 -t telephone -m message -s sender -d (debug messages)\n";
	exit;
}

my %options = ();

getopts('dt:m:s:', \%options);

$TELEPHONE = check_option(\%options, 't');
$MESSAGE = check_option(\%options, 'm');
$SENDER = check_option(\%options, 's');
$DEBUG = $options{'d'};



print "Sending: $MESSAGE to $TELEPHONE\n" if($DEBUG);

my $mech = WWW::Mechanize->new();
do {
	eval { $mech->get($SMS_URL) };
	if($@){
		print "VOLVERA A TRATAR: ERROR $@\n" if($DEBUG);
	}
} while($@);

print $mech->content if($DEBUG);
my $resp = eval { $mech->submit_form(
	form_name => $FORM_NAME,
	fields    => {
			tel1    => $TELEPHONE,
			mensaje => $MESSAGE,
			#nomtel  => $SENDER,
				
		},
	);
};
print $mech->content if($DEBUG);


#Checks te option and returns the value
#Exits if there is no value on the option

sub check_option {
	my $options = shift;
	my $option = shift;
	if($options->{$option} ne ""){
		return($options->{$option});
	}
	else {
		print "Missing option (-$option)\n";
		exit;
	}
}

