Article View: es.comp.os.linux.programacion
Article #16371Script para enviar mensajes SMS a teléfonos móviles.
From: batsman
Date: Tue, 25 Apr 2000 00:00
Date: Tue, 25 Apr 2000 00:00
241 lines
8618 bytes
8618 bytes
-----BEGIN PGP SIGNED MESSAGE-----
Hace no mucho publiqu� aqu� un script que permit�a enviar mensajes a m�viles.
Hab�a alguien interesado en el tema, pero ya no s� qui�n; como no tengo tiempo
para repasar dejanews y enviarselo a �l en persona, aqu� va una nueva versi�n
actualizada del script. Son s�lo 6Kb as� que creo que tampoco molestar� a
nadie. Aqu� va:
- -------------------------------- esms -----------------------------------
#!/usr/bin/perl
# esms, env�o de mensajes a m�viles de Espa�a
# Copyright (C) 2000 Mauricio Julio Fern�ndez Pradier
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
use CGI;
use Text::Wrap qw(wrap $columns);
use LWP::UserAgent;
use HTTP::Request::Common qw(POST GET);
local $debug = 1;
sub ajustar_fecha()
{
##my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = @_;
## 0 1 2 3 4 5 6 7 8
$_[5] = $_[5]+1900;
$_[4] = $_[4]+1;
$_[4] = "0" . "$_[4]" if ($_[4]<10);
$_[3] = "0" . "$_[3]" if ($_[3]<10);
$_[2] = "0" . "$_[2]" if ($_[2]<10);
$_[1] = "0" . "$_[1]" if ($_[1]<10);
$_[0] = "0" . "$_[0]" if ($_[0]<10);
}
sub obtener_telefono
{
my $alias = pop @_;
my $fichero;
$fichero = $ENV{"HOME"} . "/.esmsalias";
print "Abriendo fichero de alias $fichero.\n";
print "Buscando tel�fono de $alias.\n";
open(INPUT, $fichero);
while (<INPUT>) {
chop($_);
## print "Le�da l�nea $_.\n";
if ( /$alias/ ) {
s/^$alias //g;
s/ //g;
s/\t//g;
$ok = 1;
close(INPUT);
return $_;
}
}
close(INPUT);
return 0;
}
sub enviar_mensaje
{
my ($linea, $telefono,$repeat) = @_;
my $remote;
$linea =~ s/\�/\n/g;
if ($repeat == 0) {
print "Error al enviar el mensaje: $linea\n" if ($debug);
return;
}
my $ua = new LWP::UserAgent;
$ua->agent("Mozilla/5.0 " . $ua->agent);
# $ua->proxy("http", "http://localhost:8080/");
# Create a request
my $seconds = time();
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($seconds);
&ajustar_fecha ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
#my $fecha = "{ts '$year-$mon-$mday $hour:$min:$sec'}";
my $cookie_monster = GET 'http://www.terra.es/sms/';
my $res;
print "Obteniendo c�digo...\n" if ($debug);
my $i = 0;
do {
$i++;
$res = $ua->request($cookie_monster);
} while (!$res->is_success && $i<10);
my @parts = split /\n/, $res->headers_as_string();
my @cookies;
foreach( @parts ) {
## print "Analizando l�nea $_\n";
if ( /^Set-Cookie:/ ) {
s/^Set-Cookie: //;
my @tmp = split /\;/ , $_;
## print "Insertando l�nea $tmp[0]\n";
push @cookies, $tmp[0];
}
}
print "Protecci�n desactivada.\n" if ($debug);
my $req = POST 'http://www.terra.es/sms/envio.cfm', [ telefono => $telefono,
fecha => "CHANGEME",
mensaje => $linea];
my $cfecha = "%7Bts+%27$year%2D$mon%2D$mday+$hour%3A$min%3A$sec%27%7C";
$req->header ( Referer => "http://www.terra.es/sms/" );
$req->header ( Cookie => "$cookies[0]; $cookies[1]");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($seconds+36000);
&ajustar_fecha ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
$cfecha = "%7Bts+%27$year%2D$mon%2D$mday+$hour%3A$min%3A$sec%27%7D";
${$req->content_ref} =~ s/CHANGEME/$cfecha/g;
# Pass request to the user agent and get a response back
# print "<br><pre>Enviando:" . $req->as_string() . "</pre><br>";
$res = $ua->request($req);
# Check the outcome of the response
if ($res->content =~ /correctamente/) {
print "Mensaje enviado correctamente\n" if ($debug);
return 1;
} elsif ($repeat > 1) {
print "Error al enviar mensaje. Reintentando\n" if ($debug);
my $i;
for ($i=0; $i<3; $i++) {
return 1 if (&enviar_mensaje($linea,$telefono,1) == 1);
print "Reintentando env�o\n" if ($debug);
}
# sleep(1);
return &enviar_mensaje($linea,$telefono,$repeat-1);
}
print ( "Error al enviar el mensaje: $linea\n" ) if ($debug);
return 0;
}
if ($ARGV[0] eq "") {
print "esms 0.5, Copyright � 2000 Mauricio Julio Fern�ndez Pradier\n";
print "esms comes with ABSOLUTELY NO WARRANTY. This is free software,\n";
print "and you are welcome to redistribute it under certain conditions.\n";
print "\n";
print "Uso: esms <telefono> [columnas]\n";
print "Ejemplo:\n";
print " fortune -s | esms 123456789 120\n";
print " uname -a | esms Linus \n";
print "\nEste �ltimo buscar� 'Linus' en el fichero de alias\n";
print "\t\t" . $ENV{"HOME"} . "/.esmsalias\n";
print "Ejemplo de .esmsalias:\n";
print "yo 123456789\n";
print "Linus 1111111111\n";
exit(0);
}
my $telefono = $ARGV[0];
my $name = $telefono;
if ( $telefono !~ /[0-9]+/ ) {
$telefono = &obtener_telefono($telefono);
}
print "Obtenido tel�fono: $telefono.\n";
if ($telefono == 0)
{
print "No se pudo encontrar el telefono asociado a $name.\n";
exit(1);
}
my $col = $ARGV[1];
if ($col) { $columns = $col; } else { $columns = 115; }
my @in = <STDIN>;
my $mensaje = join ('', @in);
$mensaje =~ s/\n/\�/g;
my $linea = wrap ("","",$mensaje);
my @lineas = split(/\n/,$linea);
my $numlineas = @lineas;
$mensaje =~ s/\�/\n/g;
$| = 1; ## turn auto-flush on
print "Env�o de mensaje a m�vil\n";
print "Tel�fono: $telefono\n";
print "Mensaje:\n $mensaje\n";
print "Enviando mensaje:\n";
print "El mensaje ha sido fragmentado en $numlineas paquetes.\n"
if ($numlineas > 1);
print "El mensaje no ha sido fragmentado.\n"
if ($numlineas == 1);
sleep(1);
sub espera
{
my $tiempo=1;
print "Esperando $tiempo segundo...\n" if ($debug);
sleep($tiempo); ## wait after each message part
}
for ($i=1; $i<$numlineas+1; $i++) {
&espera() if ($i>1);
&enviar_mensaje("$lineas[$i-1]\n$i/$numlineas MFP",
$telefono,10);
}
if ($debug) {
print "\n";
close(STDOUT);
};
- -------------------------------- esms -----------------------------------
- --
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Real email: batsman dot geo at yahoo dot com
-----BEGIN PGP SIGNATURE-----
Version: 2.6.3ia
Charset: noconv
Comment: Public key via email: send mail with Subject: [PGPKEY]
iQEVAwUBOQYnhb0Y27yr0iefAQF8Vgf+OhNLhryDmKxRGCQgFSiC0rbrUXMzrvjv
ZGtujepl+zIreNYbgWoEmlNgv3Zv8UemwP2LTkYBJ+dEbYWQIvs5rkCNGHRTOVpB
JHrhsXDvesj0/SZiS+jvGxI7F4ELqtco+WodsOLMWpRcee0lQ7IKSNMtNwwBT6yY
2R/t1goBHblJHKYbX3UjT9rJ8Lp+1VBbuSDMkEZmodeNNNs8QnH8o6r0yEn/YGGI
kGt4lr/BlpD7PvuyHzFOQlQsbpSTsO2Fpxu5AFNL5NBf9TWIvum+IZhTYmAftv/3
lIllHCAKr0EZTrmHPR8mR03kLZ7jAZ3AkAlEWL6O8SSdaPYCxVpYag==
=r6aI
-----END PGP SIGNATURE-----
Technical Details
Message-ID:
<a295e8.2j1.ln@simpsons4ever>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: f86c8,38c4313933f906ec,start X-Google-Attributes: gidf86c8,public From: batsman@simpsons.for.ever (batsman) Subject: Script para enviar mensajes SMS a teléfonos móviles. Date: 2000/04/25 Message-ID: <a295e8.2j1.ln@simpsons4ever> X-Deja-AN: 615631969 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=iso-8859-1 X-Complaints-To: abuse@news.wanadoo.es X-Trace: m2newsread.uni2.es 956697524 62.36.41.95 (Tue, 25 Apr 2000 23:18:44 MET DST) Organization: Wanadoo Usenet News - Visita http://www.wanadoo.es/ Keywords: SMS mensaje script envío perl Mime-Version: 1.0 User-Agent: slrn/0.9.5.3 (UNIX) Reply-To: see_signature-mira_firma NNTP-Posting-Date: Tue, 25 Apr 2000 23:18:44 MET DST Newsgroups: es.comp.os.linux.programacion