#!/usr/bin/perl
# Envios gratis de SMS, esta vez gracias a google :)
# GNU by KaR]V[aN 2007
# Dedicado, como no, a mi nena, que la quiero mucho.
# Made on a Mac
# Version 0.1
#

use IO::Socket;

$debug = 0; # Dont need to touch this

print "\nEnvios de SMS gratis by KaR]V[aN\n";
print "\tGNU Licensed\n\n";
print "\n$0 654321987 We are in the lol\n\n" if (!$ARGV[0]);

# Lets collect some data, sms and tlf
foreach my $arg (@ARGV) {
	if(!$on) { $tlf = $arg; $on = 1; next; }
	$sms .= "$arg ";
}

if(!$tlf) { print "Telefono?: "; $tlf=<STDIN>; chop($tlf); }
&salir("Telefono incorrecto") unless length $tlf == 9;
&salir("\nNo hay Telefono") unless $tlf;
if(!$sms) { print "SMS?: "; $sms=<STDIN>; chop($sms); }
&salir("\nNo hay mensaje") unless $sms;

@sms = split(//, $sms);
@smss = &separa_sms(@sms);

print "Smss a mandar: " . @smss . "\n";

# Here begins the sends
foreach my $sms (@smss) {
	my $success = 0;
	my $query = "/sendtophone";
	my $kk = "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
	my $data = "gl=ES&hl=es&client=mobile&text=date&c=1&ec=on&mobile_user_id=$tlf&from=$sms&send_button=Enviar+SMS";
	my $londata = length $data;
	$data = $kk.$data;
	my $peticion = &peticion(post,query,$query,hoster,"www.google.es",lon,$londata,data,$data);
	my @resp = &conecta($peticion);
	foreach my $linea (@resp) { 
		if ($linea =~ /Enhorabuena/) {
			print "SMS Enviado correctamente.\n"; 
			$success = 1;
		} 
	}
	if($success == 0) { print "No se ha podido mandar el sms, quizas hayas enviado ya 4?\n"; }
}

# Some subfunctions
sub conecta {
	my $socket = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "66.249.93.104", PeerPort => 80) || &salir("No me puedo conectar a la web!!");
	print $socket $_[0];
	my @resp = <$socket>;
	if ($debug == 1) {
		print $_[0];
		foreach my $linea (@resp) {
			print $linea;
		}
	}
	return @resp;
}

sub peticion {
        # $_[x] funcion, $_[x+1] parametro
	
	my $tmp;

	if($_[0] eq "get") {
		$tmp = "GET";
	}
	elsif($_[0] eq "post") {
		$tmp = "POST";
	} else {
		print "No has puesto lo que quieres.\n";    
	}
	
	for($i=0;$_[$i];$i++) {
		if($_[$i] eq "query") {
			$tmp .= " $_[$i+1] HTTP/1.1\r\n";
		}
		if($_[$i] eq "hoster") {
			$tmp .= "Host: $_[$i+1]\r\n";
		}
		if($_[$i] eq "cookie") {
			$tmp .= "Cookie: $_[$i+1]\r\n";
		}
		if($_[$i] eq "referer") {
			$tmp .= "Referer: $_[$i+1]\r\n";
		}
	}
	
	if($_[0] eq "get") {
		$tmp .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\nAccept: */*\r\n";
	}
	elsif($_[0] eq "post") {
		$tmp .= "User-Agent: libwww-perl/5.805\r\n";
	}
	$tmp .= "Keep-Alive: 300\r\n";
	$tmp .= "Connection: Close\r\n";

	if($_[0] eq "post") {
		for($i=0;$_[$i];$i++) {
			if($_[$i] eq "lon") {
				$tmp .= "Content-Length: $_[$i+1]\r\n";
			}
			if($_[$i] eq "data") {
				$tmp .= "$_[$i+1]\r\n";
			}
		}
	}
	
	elsif($_[0] eq "get") {
		$tmp .= "\n";
	}
	return $tmp
}

sub salir {
	print $_[0] . "\n";
	print "Pulsa una tecla para salir..\n";
	exit if <STDIN>;
}

sub separa_sms {
        my @tmp;
	my $tmp = 1;
	my $tmp2;
	foreach my $pos (@sms) {
		if ($tmp <= 130) { $tmp2 .= $pos; }
		if ($tmp == 130) { $tmp = 0; push(@tmp,$tmp2); $tmp2 = ""; }
		$tmp++;
	}
	push(@tmp, $tmp2);
	return @tmp;
}

