#!/usr/bin/perl -w # # Copyright (C) 2004 Domenico Delle Side (dds@gnulinux.it) # # TimSMS: Interfaccia testuale per il servizio di sms gratuiti di # Telecom Italia Mobile (solo per clienti) # # Versione 0.7.6 # # ###################################################################### # NOVITA` # # - 23/10/04 # Aggiunta la possibilità di salvare username e password in un # file di configurazione (~.timsms.rc). la sintassi è: # # username=33899... # password=42984... # sender=... (opzionale) # # E` possibile inserire commenti utilizzando il carattere #. # # - 18/10/04 # Il programma ora ha un primo rudimentale supporto per i messaggi # superiori a 160 caratteri e per la gestione dei caratteri speciali # (lettere accentate, simboli, ...). # # - 16/10/04 # Inoltre, il programma ora gestisce in maniera più furba il # meccanismo di autenticazione, riuscendo, al contrario di prima, a # determinare se questa sia avvenuta con successo o meno. # ###################################################################### # USO # Dopo aver popolato correttamente creato il file di configurazione # ~/.timsms.rc secondo la sintassi: # # username=3334455666 # password=123456 # sender= # # l'uso è poi semplicissimo: # timsms.pl -d numero_destinatario -t 'testo del messaggio' # # Per funzionare correttamente, inoltre, lo script necessita dei # moduli LWP::UserAgent, URI::Escape e Crypt::SSLeay (necessario ad # LWP per effettuare la connessione https), facilmente reperibili per # ogni sistema operativo che supporti perl. Gli altri moduli necessari # dovrebbero essere tutti presenti nelle distribuzioni standard di # perl. # ###################################################################### # BUG # Il programma ha ancora qualche problema nel gestire le accentate ed # altri caratteri speciali. # ###################################################################### # TODO # Implementare una sorta di rubrica per mandare gli sms utlizzando # direttamente il nome del destinatario. # ###################################################################### # # 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., 59 Temple Place - Suite 330, Boston, MA # 02111-1307, USA. use strict; use utf8; use encoding 'utf-8'; use Encode qw(encode); use LWP::UserAgent; use Getopt::Long; use URI::Escape; my $username = ""; # numero di telefono my $password = ""; # password numerica my $agent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040928 Firefox/0.9.3'; # sostituibile my $cookie_file = ".cookies"; my ($dest_number, $sms_text, $response, $ua); my ($success, $failure) = (0, 1); my ($help, $verbose) = ('', ''); my %service_values = ( CODSMS => '', SENDER => '', # inserire qui il proprio nome o altro testo breve (opzionale) NOME_LISTA => ' ', OTPWD => ' ', contatoreSms => '', chr => '', DEST => '', SHORT_MESSAGE => '', SHORT_MESSAGE2 => '' ); #################################################################### sub help { # Stampa a schermo una breve descrizione del programma e delle sue # opzioni print < ) { chomp; s/^[\#].*$//; # Kill comments next if /^\s*$/; # ignore empty lines ($var, $val) = split('=', $_); $username = $val if ( $var eq 'username' ); $password = $val if ( $var eq 'password' ); $service_values{'SENDER'} = $val if ( $var eq 'sender' ); } close(CONF); } sub my_uri_escape { # Funzione wrapper per evitare di ripetere continuamente lo stesso # testo. Si è dovuto ricorrere a questa soluzione poiché il # modulo URI::Escape poteva far sorgere noie maneggiando stringhe # codificate con UTF-8, perciò si è deciso di forzare la # codifica ad iso-8859-1 che è sicuramente ben supportato. my $text = shift; return uri_escape(encode("iso-8859-1", $text)); } sub set_request_field { # Riempie gli elementi dell'hash che contiene i valori da passare alla # form per l'invio degli sms, eseguendo alcuni calcoli (giusti?) per # determinare se l'sms è singolo o multiplo. my ($dest, $text) = @_; my $single_sms_head_len = 21; my $first_long_sms_head_len = 27; my $other_long_sms_head_len = 6; my $text_len = length($text); my $num = 1; my $total_sms_len = -$single_sms_head_len; $num = int(($text_len - 133)/154) + 2 if ( $text_len > 141 ); if ( $num > 1 ) { $total_sms_len = -$first_long_sms_head_len; $total_sms_len -= ($num - 1) * $other_long_sms_head_len; } $total_sms_len += 640 - $text_len; $service_values{'DEST'} = $dest; $service_values{'SHORT_MESSAGE'} = my_uri_escape($text); $service_values{'SHORT_MESSAGE2'} = $text; $service_values{'chr'} = $total_sms_len; $service_values{'contatoreSms'} = $num.'/4'; } sub connect_error { # Stampa un messaggio di errore generico e termina lo script. print 'Problemi durante la connessione a http://www.tim.it', "\n"; exit($failure); } sub tim_login { # Effettua il login, preoccupandosi di verificare se questo è # avvenuto con successo o meno. In caso affermativo, ritorna l'oggetto # LWP::UserAgent creato. my ($user, $pass) = @_; my $login_url = 'https://www.tim.it/itims_cas_auth/1,,,00.html'; my $res; $login_url .= '?user=' . $user . '&pwd=' . $pass; $ua = LWP::UserAgent->new(); $ua->cookie_jar({ file => $cookie_file }); $ua->agent($agent); $res = $ua->get($login_url); if ( $res->is_success ) { return $ua if ($res->content =~ /timn_post_autenticazione/); print "Impossibile autenticarsi\n"; print "\n". $res->content if $verbose; exit($failure); } connect_error(); } sub choose_free_sms { # my $free_sms_url = 'http://webmail.posta.tim.it/login?servizio=EWSf'; my $free_sms_url = sprintf("http://webmail.posta.tim.it/ews/jsp/it_IT-TIM-UM/jsp/SMS/composerSMS.jsp?msisdn=%s&locale=it_IT-TIM-UM", $username); my $res; $res = $ua->get($free_sms_url); if ( !$res->is_success ) { print $res->content if $verbose; print 'Errore nel selezionare il servizio di sms gratuiti', "\n"; connect_error(); } } sub send_message { my ($dest, $text) = @_; # my $service_url = 'http://webmail.posta.tim.it/ewsms/jsp/it_IT-TIM-UM/inviaSms.jsp'; my $service_url = 'http://webmail.posta.tim.it/ews/jsp/it_IT-TIM-UM/jsp/SMS/sendSMS2.jsp'; my $res; my $return_value = $failure; set_request_field($dest_number, $sms_text); $res = $ua->post($service_url, \%service_values); if ( $res->is_success && ($res->content =~ /effettuato/) ) { print 'Messaggio inviato', "\n"; $return_value = $success; goto end; } print 'Messaggio non inviato', "\n"; print $res->content if $verbose; end: unlink($cookie_file); exit($return_value); } #################################################################### sub main { parse_config(); Getopt::Long::Configure('auto_abbrev'); GetOptions('destinatario=s' => \$dest_number, 'testo=s' => \$sms_text, 'username=s' => \$username, 'password=s' => \$password, 'help' => \$help, 'verbose' => \$verbose); help() if ( !($username && $password && $dest_number && $sms_text) || $help ); $ua = tim_login($username, $password); choose_free_sms(); send_message($dest_number, $sms_text); } main();