#!/usr/bin/perl -w
#----------------------------------------------------#
# A perl script to use xitae sms service in Spain.   #
#----------------------------------------------------#
# tips to: pancake@phreaker.net                      #
# note: do not abuse, you only can send 7 sms daily. #
#       is nice to protect this kind of services.    #
# remember: DO NOT ABUSE!                            #
#----------------------------------------------------#
# 
$user="";
$pass="";
#	
# Some fast documentation:
#
# 216.127.76.70:80 (vhost for www.xitae.com)
#
# LegalTerms::
# GET /usuarios/terminos.php HTTP/1.1
# Host: www.xitae.com
#
# Auth::
# GET /xsms/sys/xsmsv.php?1=username&2=password&3=297 HTTP/1.1
# Host: www.xitae.com
#
# CreateUser::
# GET /xsms/sys/adduser.php?1=username&3=em@il&
#       4=name&5=cognom1+cognom2&6=street+num&
#       7=postal&8=comunity&a=phone&b=01011987& (borndate)
#       c=0&d=1&e=2&f=1&g=110000110000100000 (interests)
# Host: www.xitae.com
#
# OK: return 1 o 0
#
# SendSMS::
# GET /xsms.php?msg=msg+word+word&num=666333111&user=username&pass=password
# HTTP/1.1
# Host: www.xitae.com
#
# OK returns 1 1 0
# ERR returns 1 0 0

use IO::Socket;

$s=0;

sub doconnect
{
	$s=IO::Socket::INET->new(
			"PeerAddr"=>"216.127.76.70", # www.xitae.com
		        "PeerPort"=>80) or
	die ("Cannot connect to remote host");
}

sub dofork
{
	if (fork())
	{
		exit 0;
	}
}

if ($ARGV[0] eq "-l") {
	doconnect();
	print $s "GET /usuarios/terminos.php HTTP/1.1\r\nHost: www.xitae.com\r\n\r\n";
	print "Use enter few times...\n";
	$@=0;
	while(<$s>) { print; <STDIN>; }
	goto fin;
} elsif ($#ARGV != 2 )
	{
	print "perl xsms client.- by pancake\@phreaker.net\n";
	print "Usage ./xxx \"options\" \"arg0\" \"arg1\".\n";
	print "     -s : send ('num' 'msg')\n";
	print "     -l : read xsms license\n";
	print "     -r : register ('username' 'email')\n";
	exit 1;
	}

if ($ARGV[0] eq "-s")
{
	if ( ($user eq "") || ($pass eq "") )
	{
		print "Login and password not defined.\nRegister First.\n";
		print "And define the \$user and \$pass vars on top of script.\n";
		exit;
	}

	doconnect();
	$num=$ARGV[1];
	$msg=$ARGV[2];
	$msg=~tr/ /+/;

	if ( length($num) != 9  )
		{
		print "Invalid phone number: '$num'.\nMust be 9digit number.\n";
		goto fin;
		}

	print "Connected.\n";
	print $s "GET /xsms.php?user=$user&pass=$pass&msg=$msg&num=$num HTTP/1.1\r\nHost: www.xitae.com\r\n\r\n";

	$flag=0;
	while(<$s>)
	{
	if ((/^1/)||(/^0/)) 
		{ 
		if ($flag == 1)
			{
			if (/^1/)
				{
				print "Message sent OK :D\n";
				dofork();
				}
			elsif (/^0/)
				{
				print "Message not sent properly :(\n";
				dofork();
				} else {
				print "Internal server error :/\n";
				dofork();
				}
			}
		$flag++;
		}	
	}
} elsif ($ARGV[0] eq "-r") {
	$user=$ARGV[1];
	$mail=$ARGV[2];
	$mail=~s/\@/%40/;
	print "Your mail is: $mail\n";
	print "Name: (aa): ";    $name=<STDIN>;
	print "Surname: (bb+cc):"; $surn=<STDIN>;
	print "Street+n: (str+3):";$stre=<STDIN>;
	print "PostCode: (08015):";$post=<STDIN>;
	print "Comunity: (madrid):";$comm=<STDIN>;
	print "Phone: (913332221):";   $phon=<STDIN>;
	print "Borndate(ddmmyyyy): ";$born=<STDIN>;
	$get="GET /xsms/sys/adduser.php?1=$user&3=$mail&";
	$get.="4=$name&5=$surn&6=$stre&7=$post&8=$comm&";
	$get.="a=$phon&b=$born&c=0&d=1&e=2&f=1&";
	$get=~s/\n//g;
	$get.="g=110000110000100000 HTTP/1.1\r\n";
	$get.="Host: www.xitae.com\r\n\r\n";
	doconnect();
	print $s "$get";
	$flag=0;
	while(<$s>)
	{
	if ((/^1/)||(/^0/)||(/^5/)) 
		{ 
		if ($flag == 1)
			{
			if (/^o/)
				{
				print "User created OK, Now read your mail and take your password.\n";
				dofork();
				}
			elsif (/^n/)
				{
				print "Cannot create user: Invalid given data?\n";
				dofork();
				}
			elsif (/^5/)
				{
				print "Server database too busy xD. Message probably arrives ok :P\n";
				goto fin;
				} else {
				print "Internal server error :/\n";
				dofork();
				}
			}
		$flag++;
		}	
	}
	goto fin;
} else {
	print "Invalid option.\n";
}

	fin:
	$s->close();


