[ILUG] Vodafone Webtext
[ILUG] Vodafone Webtext
Greg McRandal
mcrandal at maths.tcd.ie
Wed Jan 21 11:45:27 GMT 2004
Hi,
Decided to give this a whirl. Seems I can log on but I get this output afterwards.
[ logging in to 0876997286 at vodafone.ie ... ]
[ login successful ]
[ message sending failed; unknown response from server ]
Retry ? y/n [n]: q
[ okay, I'm outta here. ]
Now sadly not knowing Perl I cannot debug this myself, suggestions as to what I'm doing wrong are most welcome.
I've attached the script I'm using.
Thanks,
Greg
On Mon, Jan 19, 2004 at 11:47:24AM +0000, Conall O'Brien wrote:
> On Mon, Jan 19, 2004 at 11:45:06AM GMT, Paul Flynn
> <fl1979ie at utvinternet.com > incoherently babbled:
>
> > Hi,
> >
> > I have just noticed that vodafone has incressed there webtext to 300 and
> > allows the ability to text any network.
> >
> > I was wondering what would the best way to script it to post the form in
> > a script ? I know lynx's has -cmd_log option and all but was wondering
> > if there was a easyer way.
> >
> > To make it a bit clear I would look to have a script that will log in
> > and post the txt from the cmd line?
>
> There's already at least one script to do this. It's called vodasms and it
> based on o2sms writtem by mackers at netsoc.tcd.ie. See attached for the
> script. Comments indicate the perl modules needed from CPAN.
>
> --
>
> Conall O'Brien
>
> +353 (0)87 9194139 | http://www.conall.net
>
> GPG Key: http://www.conall.net/gpg/
>
> How C++ is like teenage sex:
>
> 1. It is on everyone's mind all the time.
> 2. Everyone talks about it all the time.
> 3. Everyone thinks everyone else is doing it.
> 4. Almost no one is really doing it.
> 5. The few who are doing it are: A. Doing it poorly. B. Sure it will
> be better next time. C. Not practising it safely.
> #!/usr/bin/perl
>
> # $Id: o2sms,v 2.29 2003/11/05 11:11:00 mackers Exp $
> #
> # o2sms
> # a script to send sms's using o2.ie (needs an account)
> # http://www.mackers.com/projects/o2sms/
> #
> # required modules:
> # HTML::Parser
> # IO::Socket::SSL
> # Net::SSLeay
> # Crypt::SSLeay
> # libwww-perl
> # URI
>
> # -- perl modules
> use strict;
> use LWP::UserAgent;
> use HTTP::Request::Common;
> use HTTP::Cookies;
> use URI::Escape;
> use POSIX qw(strftime);
>
> # -- global vars
> my $cvsid = '$Id: o2sms,v 2.29 2003/11/05 11:11:00 mackers Exp $';
> my $version = (split(/ /,$cvsid))[2];
> my $doreadconf = 0;
> my $showhelp = 0;
> my $debug = 0;
> my $dontsend = 0;
> my $loginbutdontsend = 0;
> my $dontfill = 1;
> my $dontsplit = 0;
> my $dontreuse = 0;
> my $dontuc = 1;
> my $dol_login_url = 'https://apps.o2.ie/NASApp/Portal/Login';
> my $dol_smsform_url = 'http://webtext.o2.ie/NASApp/TM/O2/proc/sendMessage.jsp';
> my $dol_login_referrer = 'http://www1.o2.ie/home';
> my $dol_smsform_referrer = 'https://apps.o2.ie/NASApp/redirects/grouptext/webtext.jsp';
> my $voda_login_url = 'https://www.vodafone.ie/servlet/ie.vodafone.servlets.LoginServlet';
> my $voda_login_referrer = 'http://www.vodafone.ie/';
> my $voda_smsform_referrer = 'http://www.vodafone.ie/myvodafone/textandwap/index.jsp';
> my $voda_smsform_url = 'https://www.vodafone.ie/myvodafone/textandwap/webtext/index.jsp';
> my $single_max_length = 160;
> my $sms_max_length = 500;
> my $expiresecs = 1800;
> my $man_delim = "\\\\\\\\";
> my $username = ( getpwuid $< ) [0];
> my $password = "";
> my $sig = "";
> my @recips;
> my @numbers;
> my @message_parts;
> my $message;
> my %aliases;
> my $ua = LWP::UserAgent->new;
> my $proxy = $ENV{HTTP_PROXY};
> my %useragentstrs = ("Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)" => "26",
> "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" => "68",
> "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0" => "72",
> "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)" => "80",
> "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)" => "85",
> "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" => "93",
> "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4.1) Gecko/20020314 Netscape6/6.2.2" => "95",
> "Opera/6.05 (Windows 2000; U) [en]" => "96",
> "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)" => "97",
> "Opera/7.0 (Windows 2000; U)" => "98",
> "Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.1) Gecko/20020827" => "99",
> "Mozilla/4.0 (compatible; MSIE 5.21; Mac_PowerPC)" => "100"
> );
> my $is_o2 = 0;
> my $is_voda = 0;
> my $is_meteor = 0;
> my $cookiefile;
> my $msgfile;
> my $conffile;
>
> # -- get command name
> if ($0 =~ /voda(fone)?sms$/i) {
> $is_voda = 1;
> $cookiefile = $ENV{HOME} . "/.vodasmscookie";
> $msgfile = $ENV{HOME} . "/.vodasmsmsg";
> $conffile = $ENV{HOME} . "/.vodasmsrc";
> } elsif ($0 =~ /met(eor)?sms$/i) {
> $is_meteor = 1;
> $cookiefile = $ENV{HOME} . "/.meteorsmscookie";
> $msgfile = $ENV{HOME} . "/.meteorsmsmsg";
> $conffile = $ENV{HOME} . "/.meteorsmsrc";
> } else {
> #if ($0 =~ /o2sms$/i) {
> $is_o2 = 1;
> $cookiefile = $ENV{HOME} . "/.o2smscookie";
> $msgfile = $ENV{HOME} . "/.o2smsmsg";
> $conffile = $ENV{HOME} . "/.o2smsrc";
> }
>
> # -- read user conf file
> read_config_file($conffile);
>
> # -- parse command line
> while ($_ = shift) {
> if ($_ eq "-u") {
> $username = shift;
> $dontreuse = 1;
> } elsif ($_ eq "-p") {
> $password = shift;
> } elsif ($_ eq "-d") {
> $debug = 1;
> } elsif ($_ eq "-f") {
> $dontfill = 1;
> } elsif ($_ eq "-s") {
> $dontsplit = 1;
> } elsif ($_ eq "-r") {
> $dontreuse = 1;
> } elsif ($_ eq "-C") {
> $dontuc = 0;
> } elsif ($_ eq "-c") {
> $conffile = shift;
> $doreadconf = 1;
> $dontreuse = 1;
> } elsif ($_ eq "-P") {
> $proxy = shift;
> } elsif ($_ eq "-v") {
> print "o2sms version $version\n";
> exit(0);
> } elsif ($_ eq "--dont-send") {
> $dontsend = 1;
> } elsif (/^\-\-?\w+/) {
> $showhelp = 1;
> } else {
> push (@recips, $_);
> }
> }
>
> # -- display help message
> if ((scalar(@recips) == 0) || ($showhelp == 1)) {
> print STDERR <<EOF;
> Usage: $0 [options] <number|alias|group> [<number|alias|group> ...]
>
> $0 is a script to send SMSs via the command line. It works by sending
> requests to your phone operator's website (ie. simulating a browser). The
> script requires that you have a valid web account, which is available to o2,
> Vodaphone and Metor phone owners. At the time of writing, o2 give 250 free
> web texts per month, Vodaphone 300 and Meteor 30.
>
> Options:
> -u <username> use this username (defaults to unix username or config file
> username) (implies -r)
> -p <password> use this password (defaults to config file password or
> prompts for password)
> -c <conffile> use this config file (defaults to ~/.o2smsrc) (implies -r)
> -s don't split message over multiple SMSs
> -f don't fill the end of the message with spaces (to clear ad)
> -r don't reuse cookies when logging in
> -C capitalise first letter in every sentence
> -P <proxy> use this http proxy
> -v print version
> -h prints this help message
> -d debug mode - prints the entire http dialog
>
> Config File Format:
> username <username>
> password <password>
> alias <name> <number> [number|alias ...]
> sig <text-to-place-at-end-of-every-sms>
> proxy <http://host:port/ >
> nosplit
> nofill
> noreuse
> capitalise
>
> EOF
> exit (0);
> }
>
> # -- read config file again
> if ($doreadconf == 1) {
> unless (read_config_file($conffile)) {
> print STDERR "Couldn't open $conffile.\n";
> exit(1);
> }
> }
>
> # -- explode groups
> my @recips2;
> foreach my $recip (@recips) {
> if(ref($aliases{$recip}) eq 'ARRAY'){
> # is group
> push (@recips2, @{$aliases{$recip}});
> } else {
> push (@recips2, $recip);
> }
> }
> @recips = @recips2;
>
> # -- check for aliases
> for (my $i=0; $i<scalar(@recips); $i++) {
> my $recip = $recips[$i];
> if ($recip =~ /[^\d]/) {
> # has non-numeric character - might be alias
> if (exists($aliases{$recip})) {
> $numbers[$i] = $aliases{$recip};
> } else {
> print STDERR "Not a valid alias: $recip.\n";
> exit (1);
> }
> } else {
> $numbers[$i] = $recip;
> }
> }
>
> foreach my $number (@numbers) {
> # -- check number
> if ($number =~ /^0(8[567])(\d*)/) {
> # is an irish mobile number - check length
> if (length($number) != 10) {
> print STDERR "Number is the wrong length for an Irish mobile number.\n";
> exit (1);
> }
> # length ok - make international
> $number = "00353$1$2";
> } elsif ($number =~ /^00(\d*)/) {
> # is an international number
> } else {
> # ?
> print STDERR "Not a valid number: $number. Please use 08[567]xxxxxxx or international number.\n";
> exit(1);
> }
> if (($is_voda) && ($number !~ /^00353/)) {
> print STDERR "Vodafone webtexts can only be sent to Irish mobile numbers.\n";
> exit(1);
> }
> }
>
> # -- print welcome message
> print "[ recipient(s) : ";
> for (my $i=0; $i<scalar(@recips); $i++) {
> my $recip = $recips[$i];
> if ($recip =~ /[^\d]/) {
> # is alias, print both
> print "$recip (" . $numbers[$i] . ") ";
> } else {
> # just number
> print $numbers[$i] . " ";
> }
> }
> print "]\n";
>
> # -- set the adjusted max length (with sig)
> $single_max_length = $single_max_length - length($sig);
>
> # -- get the text
> while (<STDIN>) {
> last if (/^\./);
> $message .= $_;
> }
>
> # -- prepare the message
> chomp($message); # kill last new line
> $message =~ s/\n/ /gsm; # make other newlines spaces
> unless ($dontuc) { # captialise first letter of every sentence
> $message =~ s/(^\w)/uc($1)/gsme;
> $message =~ s/([\.\?\!:\\]\s*)(\w)/$1 . uc($2)/gsme;
> }
> #$message =~ s/^$man_delim//; # remove unneeded splitters
> #$message =~ s/$man_delim$//; # remove unneeded splitters
>
> # -- split up the message
> if (length($message) > $sms_max_length) {
> print "[ warning : very long message, truncating to $sms_max_length chars ]\n";
> $message = substr($message,0,$sms_max_length);
> print $message;
> }
> if ((length($message) > $single_max_length) || ($message =~ /$man_delim/)) {
> if ($dontsplit && ($message !~ /$man_delim/)) {
> print "Message is too long (" . length($message) . "/$single_max_length), exitting.\n";
> exit(1);
> }
> my $restmsg = $message;
> my $partmsg;
> while ((length($restmsg) > $single_max_length) || ($restmsg =~ /$man_delim/)) {
> # if manual split, and message part can fit, then add the first bit and loop
> if ($restmsg =~ /^(.*?)$man_delim(.*)/) {
> if (length($1) < $single_max_length) {
> $partmsg = $1;
> $restmsg = $2;
> push (@message_parts, $partmsg) if ($partmsg =~ /\S/);
> next;
> }
> }
> # if we don't want to split..
> if ($dontsplit) {
> print "Message part " . (scalar(@message_parts) + 1) . " is too long (" . length($restmsg) . "/$single_max_length), exitting.\n";
> exit(1);
> }
> # message too long, split at the most natural place
> ($partmsg, $restmsg) = &split_message($single_max_length, $restmsg);
> push (@message_parts, $partmsg) if ($partmsg =~ /\S/);
> }
> push (@message_parts, $restmsg . $sig) if ($restmsg =~ /\S/);
> print "[ warning : long or split message, splitting into " . scalar(@message_parts) . " parts ]\n";
> } else {
> push (@message_parts, $message . $sig);
> }
> if (scalar(@message_parts) == 0) {
> print "Nothing to do, exitting.\n";
> exit (2);
> }
>
> # -- fill the end of each message part with white spaces to clear the "free web text" ad
> if (!$dontfill) {
> foreach my $message_part (@message_parts) {
> my $msg_fill = " " x ($single_max_length - length($message_part) + length($sig));
> $message_part .= $msg_fill;
> }
> }
>
> # -- set the useragent to some random browser (to avoid suspicion!)
> $ua->agent(&get_weighted_rand_elem_from_hash(%useragentstrs));
> print "sms: using user agent: " . $ua->agent . "\n" if ($debug);
>
> # -- set the proxy
> if (($proxy) && ($proxy ne "")) {
> $ua->proxy('http',$proxy);
> $ua->proxy('https',$proxy);
> print "sms: using proxy: $proxy\n" if ($debug);
> }
>
> # -- get a cookie jar from file or login
> my $cj;
> # check for existing cookie jar and use if hasn't expired
> my @fstat = stat($cookiefile);
> if ((@fstat) && ($debug)) {
> print "sms: found cookiejar in $cookiefile with mtime of " . strftime("%a %b %e %H:%M:%S %Y", gmtime($fstat[9])) . "\n";
> }
> if (($dontreuse == 0) && (@fstat) && ($fstat[9] + $expiresecs > time())) {
> # reuse the last cookie
>
> # -- print status
> print "[ reusing last login for $username\@" . &get_sp_domain . " ... ]\n";
>
> # -- load the cookie file
> $cj = HTTP::Cookies->new(ignore_discard => 1);
> $cj->load($cookiefile);
>
> # -- touch the cookie jar to keep it up to date
> utime (time, time, $cookiefile);
> } else {
> # cookie jar non existant or expired, login instead
>
> # -- print status
> print "[ logging in to $username\@" . &get_sp_domain . " ... ]\n";
>
> # -- get the password
> if ($password eq "") {
> system "stty -echo"; # Echo off
> print "Password: "; # Prompt for password
> chomp($password = <STDIN>); # Remove newline
> system "stty echo"; # Echo on
> print "\n";
> }
>
> # -- login
> if ($dontsend > 0) {
> $cj = &test_login ($username, $password);
> } elsif ($is_o2) {
> $cj = &o2_login ($username, $password);
> } elsif ($is_voda) {
> $cj = &voda_login ($username, $password);
> } elsif ($is_meteor) {
> $cj = &meteor_login ($username, $password);
> }
> if (!defined($cj)) {
> print STDERR "[ login failed; your username or password is incorrect ]\n";
> exit(1);
> } else {
> print "[ login successful ]\n";
> }
> }
>
> # -- copy cookies from apps.o2.ie to webtext.o2.ie
> #sub cookie_copy {
> # my $version = $_[0];
> # my $key = $_[1];
> # my $val = $_[2];
> # my $path = "/NASApp/TM";
> # my $domain = "webtext.o2.ie";
> # $cj->set_cookie($version,$key,$val,$path,$domain);
> #}
> #$cj->scan(\&cookie_copy);
> #$cj->save($cookiefile);
>
> # -- pick up the cookies from the other server
> if (($dontsend == 0) && ($is_o2)) {
> print "sms: picking up cookies from $dol_smsform_referrer\n" if ($debug);
> my $req = HTTP::Request->new();
> $req->uri($dol_smsform_referrer);
> $req->method("GET");
> $req->protocol("HTTP/1.0");
> $cj->add_cookie_header($req);
> print $req->as_string() if ($debug);
> my $resp = $ua->send_request($req);
> $cj->extract_cookies($resp);
> print $resp->as_string() if ($debug);
> $req->uri($resp->header("Location"));
> print $req->as_string() if ($debug);
> $resp = $ua->send_request($req);
> $cj->extract_cookies($resp);
> print $resp->as_string() if ($debug);
> print $cj->as_string() if ($debug);
> }
>
> # -- send off the message
> foreach my $number (@numbers) {
> #foreach my $message (@message_parts) {
> for (my $i=0; $i<scalar(@message_parts); $i++) {
>
> my $retry;
>
> do {
>
> my $message = $message_parts[$i];
> my $failed = 0;
> $retry = 0;
>
> my $retval;
> if (($dontsend > 0) || ($loginbutdontsend > 0)) {
> $retval = &test_send_sms($cj,$message,$number);
> } elsif ($is_o2) {
> $retval = &o2_send_sms($cj,$message,$number);
> } elsif ($is_voda) {
> $retval = &voda_send_sms($cj,$message,$number);
> } elsif ($is_meteor) {
> $retval = &meteor_send_sms($cj,$message,$number);
> }
>
> # get a nice message
> my $partmsg = "";
> if (scalar(@message_parts) > 1) {
> $partmsg = " (part " . ($i+1) . " of " . scalar(@message_parts) . ")";
> }
>
> # -- check return values
> if (defined($retval) && ($retval == -1)) {
> # unknown repsonse.
> print STDERR "[ message sending failed; unknown response from server ]\n";
> $failed = 1;
> } elsif ($retval == -2) {
> # message sent, but we don't know how many are left
> print "[ message$partmsg sent to $number, ? remaining this month ]\n";
> } elsif ($retval == 0) {
> # 0 messages left - probably failed sending
> print STDERR "[ message sending failed; possibly a malformed message ]\n";
> $failed = 1;
> } elsif ($retval > 0) {
> # message send successful
> print "[ message$partmsg sent to $number, $retval remaining this month ]\n";
> } else {
> # unknown negative response
> print STDERR "[ message sending failed ]\n";
> $failed = 1;
> }
>
> if ($failed == 1) {
> # ask for a retry
> print "Retry ? y/n [n]: ";
> my $resp = <STDIN>;
> if ($resp =~ /^y/i) {
> $retry = 1;
> } else {
> print "[ okay, I'm outta here. ]\n";
> unlink ($cookiefile);
> exit(1);
> }
> }
> } while ($retry == 1);
> }
> }
>
> # -- write message to message file
> open (SMSMSG, "> $msgfile") || die ("Can't write to message file!");
> print SMSMSG "$message\n";
> close (SMSMSG);
>
> # -- quit with success
> exit(0);
>
> sub o2_login {
> my ($username, $password) = @_;
> print "Logging in... \n" if ($debug);
>
> # my $req = HTTP::Request->new('POST',$dol_login_url,
> # [ referrer => $dol_login_referrer,
> # username => $username,
> # password => $password
> # ]);
> #$req->protocol("HTTP/1.0");
> #my $res = $ua->request($req);
>
> my $res = $ua->post($dol_login_url,
> [ referrer => $dol_login_referrer,
> username => $username,
> password => $password
> ]);
>
> my $lresp = $res->as_string();
> print $lresp if ($debug);
>
> # check for a broken server
> if ($res->code == 500) {
> print STDERR "[ login failed; server returned error 500 (Internal Server Error) ]\n";
> exit(1);
> }
>
> # check for invalid username
> #if ($lresp =~ /your username or password is incorrect/) {
> #if (($res->is_redirect) && ($lresp =~ /error_login_page/)) {
> if ($lresp =~ /Sorry, your username or password is incorrect./) {
> return undef;
> #} elsif ($lresp =~ /Welcome.*back.*$username/i) {
> } else {
> my $cookie_jar = HTTP::Cookies->new(ignore_discard => 1);
> $cookie_jar->extract_cookies($res);
>
> # save the cookies so we can reuse them
> if (defined($cookie_jar) && ($dontreuse == 0)) {
> $cookie_jar->save($cookiefile);
> }
> return $cookie_jar;
> }
> }
>
> sub o2_send_sms {
> my ($cookie_jar, $message, $number) = @_;
>
> # remove whitespace at beginning of message
> $message =~ s/^\s*//;
>
> # encode the message for sending
> $message = uri_escape($message);
>
> # get what to send to server
> #my $msgsize = 132 - length($message);
> my $msgsize = length($message);
> my $postmsg = "msisdn=$number&Msg=$message&recipients=1&grpSTR=&ConSTR=&command=send&NumMessages=1";
>
> # construct the request for sending the message
> my $req = HTTP::Request->new();
> $req->push_header("Referer" => $dol_smsform_url);
> $req->push_header("Referrer" => $dol_smsform_url);
> $req->push_header("Host" => "webtext.o2.ie");
> $req->push_header("Cookie2" => "\$Version=\"1\"");
> $req->push_header("Content-Type" => "application/x-www-form-urlencoded");
> $req->push_header("Content-Length" => length($postmsg));
> $req->uri($dol_smsform_url);
> $req->method("POST");
> $req->protocol("HTTP/1.0");
>
> #$req = HTTP::Request->new();
> #$req->uri($dol_smsform_url . "?" . $postmsg);
> #$req->method("GET");
> #$req->protocol("HTTP/1.0");
>
> $cookie_jar->add_cookie_header($req);
> $req->content($postmsg);
> print $req->as_string() if ($debug);
>
> my $resp = $ua->request($req);
>
> # print if debug
> print $resp->as_string() if ($debug);
>
> # if we don't get redirected to "sent.jsp" then there's some problem
> if ($resp->header("Location") !~ /sent\.jsp/i) {
> return -1;
> }
>
> # make redirect absolute
> my $redirh = $resp->header("Location");
> $redirh =~ s#\.\.\/#http://webtext.o2.ie/NASApp/TM/O2/#;
>
> # success - follow the redirect
> $req->method("GET");
> $req->remove_header("Content-Type");
> $req->remove_header("Content-Length");
> $req->content("");
> $req->uri($redirh);
> print $req->as_string() if ($debug);
> $resp = $ua->request($req);
> print $resp->as_string() if ($debug);
>
> # get the html
> my $resp_html = $resp->as_string();
>
> # if number > 0, then message was sent
> #if (($resp_html =~ /Your message has been sent/i) &&
> #($resp_html =~ /You have (\d*) messages remaining this month/i)) {
> if (($resp_html =~ /<td class="StoryTitle ">Sent <\/td>/i) &&
> ($resp_html =~ /You now have (\d*) Free Messages remaining this month/i)) {
> #print "Message sent!\n";
> #print "(You have $1 messages left this month)\n";
> return $1;
> } else {
> # the message was sent, but we don't know how many messages are left
> return -2;
> }
>
> }
>
> sub voda_login {
> my ($username, $password) = @_;
> print "Logging in... \n" if ($debug);
>
> my $res = $ua->post($voda_login_url,
> [ referrer => $voda_login_referrer,
> username => $username,
> password => $password
> ]);
>
> my $lresp = $res->as_string();
> print $lresp if ($debug);
>
> # check for a broken server
> if ($res->code == 500) {
> print STDERR "[ login failed; server returned error 500 (Internal Server Error) ]\n";
> exit(1);
> }
>
> # check for invalid username
> if (($res->is_redirect) && ($res->header("Location") =~ m#myvodafone/services/logon_failed.jsp#i)) {
> return undef;
> #} elsif ($lresp =~ /Welcome.*back.*$username/i) {
> } else {
> my $cookie_jar = HTTP::Cookies->new(ignore_discard => 1);
> $cookie_jar->extract_cookies($res);
>
> # save the cookies so we can reuse them
> if (defined($cookie_jar) && ($dontreuse == 0)) {
> $cookie_jar->save($cookiefile);
> }
> return $cookie_jar;
> }
> }
>
> sub voda_send_sms {
> my ($cookie_jar, $message, $number) = @_;
>
> # remove whitespace at beginning of message
> $message =~ s/^\s*//;
>
> # encode the message for sending
> $message = uri_escape($message);
>
> # make number national
> $number =~ s/^00353/0/;
>
> # get what to send to server
> my $msgsize = length($message);
> my $charleft = $single_max_length - length($message);
> my $postmsg = "phoneid1=$number&MESSAGE=$message&num=$charleft&posted=yes";
>
> # construct the request for sending the message
> my $req = HTTP::Request->new();
> $req->push_header("Referer" => $voda_smsform_referrer);
> $req->push_header("Referrer" => $voda_smsform_referrer);
> $req->push_header("Host" => "www.vodafone.ie");
> $req->push_header("Cookie2" => "\$Version=\"1\"");
> $req->push_header("Content-Type" => "application/x-www-form-urlencoded");
> $req->push_header("Content-Length" => length($postmsg));
> $req->uri($voda_smsform_url);
> $req->method("POST");
> $req->protocol("HTTP/1.0");
>
> $cookie_jar->add_cookie_header($req);
> $req->content($postmsg);
> print $req->as_string() if ($debug);
>
> my $resp = $ua->request($req);
>
> # get the html
> my $resp_html = $resp->as_string();
>
> # print if debug
> print $resp_html if ($debug);
>
> # this text means successful send
> if ($resp->header("Location") !~ /message_sent/i) {
> return -1;
> }
>
> # go get the page again to see how many messages are left this month
> my $req2 = HTTP::Request->new();
> $req2->uri($voda_smsform_url);
> $req2->method("GET");
> $req2->protocol("HTTP/1.0");
> $cookie_jar->add_cookie_header($req2);
> $resp = $ua->request($req2);
>
> $resp_html = $resp->as_string();
>
> print $resp_html if ($debug);
>
> if ($resp_html =~ /<td height="16" width="200"><b>(\d{1,3})<\/b><\/td>/i) {
> return $1;
> } else {
> return -2;
> }
> }
>
> sub meteor_login {
> my ($username, $password) = @_;
> print STDERR "Meteor not supported yet.\n";
> exit -1;
> }
>
> sub meteor_send_sms {
> }
>
> sub test_login {
> my ($username, $password) = @_;
> print "sms: logging in with username $username\n";
> return "x";
> }
>
> sub test_send_sms {
> my ($cookie_jar, $message, $number) = @_;
> $message =~ s/^\s*//;
> $message = uri_escape($message);
> print "sms: message = $message\n";
> print "sms: number = $number\n";
> return 666;
> }
>
> sub split_message {
> my ($len, $message) = @_;
>
> # split the message on a break between words..
> # find the position of the last ' ' in the sentance
> my $pos = index(reverse(substr($message,0, $len-3)), ' ');
> $pos = $len - 3 - $pos;
>
> # the part the message to send
> my $partmessage = substr($message, 0, $pos-1);
> # the remaining part of the message
> my $restofmessage = substr($message, $pos);
>
> return ($partmessage, $restofmessage);
> }
>
> sub read_config_file {
> open (SMSCONF, $_[0]) && do {
> while (<SMSCONF>) {
> chomp();
> next if (/^#/);
> if (/^alias\s*(\w*)\s*(\d*)$/i) {
> $aliases{$1} = $2;
> print "sms: added alias for $1\n" if ($debug);
> } elsif (/^alias\s*(\w*)\s*([\w\d\s]*)/i) {
> my @group = split(/\s+/,$2);
> @{$aliases{$1}} = @group;
> print "sms: added group for $1 (" . join(", ",@{$aliases{$1}}) . ")\n" if ($debug);
> } elsif (/^username\s*(.*)/i) {
> $username = $1;
> } elsif (/^password\s*(.*)/i) {
> $password = $1;
> } elsif (/^sig\s*(.*)/i) {
> $sig = " $1";
> chomp($sig);
> } elsif (/^proxy\s*(.*)/i) {
> $proxy = $1;
> chomp($proxy);
> } elsif (/^nofill/i) {
> $dontfill = 1;
> } elsif (/^noreuse/i) {
> $dontreuse = 1;
> } elsif (/^nosplit/i) {
> $dontsplit = 1;
> } elsif (/^capitali[sz]e/i) {
> $dontuc = 0;
> } elsif (/^debug\s*on/i) {
> $debug = 1;
> }
> }
> close (SMSCONF);
> return 1;
> };
> return 0;
> }
>
> sub get_rand_elem_from_array {
> my $elem = int(rand(scalar(@_)));
> return $_[$elem];
> }
>
> sub get_weighted_rand_elem_from_hash {
> my %thehash = @_;
> my @uas = sort { $thehash{$b} <=> $thehash{$a} } keys %thehash;
> my $rand = int(rand(100)) + 1;
> foreach my $uastr (@uas) {
> return $uastr if ($rand > $thehash{$uastr});
> }
> return $_[0];
> }
>
> sub get_sp_domain {
> return "o2.ie" if ($is_o2);
> return "vodafone.ie" if ($is_voda);
> return "meteor.ie" if ($is_meteor);
> }
> --
> Irish Linux Users' Group
> http://www.linux.ie/mailman/listinfo/ilug/
>
-------------- next part --------------
#!/usr/bin/perl
# $Id: o2sms,v 2.29 2003/11/05 11:11:00 mackers Exp $
#
# o2sms
# a script to send sms's using o2.ie (needs an account)
# http://www.mackers.com/projects/o2sms/
#
# required modules:
# HTML::Parser
# IO::Socket::SSL
# Net::SSLeay
# Crypt::SSLeay
# libwww-perl
# URI
# -- perl modules
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::Cookies;
use URI::Escape;
use POSIX qw(strftime);
# -- global vars
my $cvsid = '$Id: o2sms,v 2.29 2003/11/05 11:11:00 mackers Exp $';
my $version = (split(/ /,$cvsid))[2];
my $doreadconf = 0;
my $showhelp = 0;
my $debug = 0;
my $dontsend = 0;
my $loginbutdontsend = 0;
my $dontfill = 1;
my $dontsplit = 0;
my $dontreuse = 0;
my $dontuc = 1;
my $dol_login_url = 'https://apps.o2.ie/NASApp/Portal/Login';
my $dol_smsform_url = 'http://webtext.o2.ie/NASApp/TM/O2/proc/sendMessage.jsp';
my $dol_login_referrer = 'http://www1.o2.ie/home';
my $dol_smsform_referrer = 'https://apps.o2.ie/NASApp/redirects/grouptext/webtext.jsp';
my $voda_login_url = 'https://www.vodafone.ie/servlet/ie.vodafone.servlets.LoginServlet';
my $voda_login_referrer = 'http://www.vodafone.ie/';
my $voda_smsform_referrer = 'http://www.vodafone.ie/myvodafone/textandwap/index.jsp';
my $voda_smsform_url = 'https://www.vodafone.ie/myvodafone/textandwap/webtext/index.jsp';
my $single_max_length = 160;
my $sms_max_length = 500;
my $expiresecs = 1800;
my $man_delim = "\\\\\\\\";
my $username = ( getpwuid $< ) [0];
my $password = "";
my $sig = "";
my @recips;
my @numbers;
my @message_parts;
my $message;
my %aliases;
my $ua = LWP::UserAgent->new;
my $proxy = $ENV{HTTP_PROXY};
my %useragentstrs = ("Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)" => "26",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" => "68",
"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0" => "72",
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.1)" => "80",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)" => "85",
"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" => "93",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4.1) Gecko/20020314 Netscape6/6.2.2" => "95",
"Opera/6.05 (Windows 2000; U) [en]" => "96",
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)" => "97",
"Opera/7.0 (Windows 2000; U)" => "98",
"Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.1) Gecko/20020827" => "99",
"Mozilla/4.0 (compatible; MSIE 5.21; Mac_PowerPC)" => "100"
);
my $is_o2 = 0;
my $is_voda = 0;
my $is_meteor = 0;
my $cookiefile;
my $msgfile;
my $conffile;
# -- get command name
if ($0 =~ /voda(fone)?sms$/i) {
$is_voda = 1;
$cookiefile = $ENV{HOME} . "/.vodasmscookie";
$msgfile = $ENV{HOME} . "/.vodasmsmsg";
$conffile = $ENV{HOME} . "/.vodasmsrc";
} elsif ($0 =~ /met(eor)?sms$/i) {
$is_meteor = 1;
$cookiefile = $ENV{HOME} . "/.meteorsmscookie";
$msgfile = $ENV{HOME} . "/.meteorsmsmsg";
$conffile = $ENV{HOME} . "/.meteorsmsrc";
} else {
#if ($0 =~ /o2sms$/i) {
$is_o2 = 1;
$cookiefile = $ENV{HOME} . "/.o2smscookie";
$msgfile = $ENV{HOME} . "/.o2smsmsg";
$conffile = $ENV{HOME} . "/.o2smsrc";
}
# -- read user conf file
read_config_file($conffile);
# -- parse command line
while ($_ = shift) {
if ($_ eq "-u") {
$username = shift;
$dontreuse = 1;
} elsif ($_ eq "-p") {
$password = shift;
} elsif ($_ eq "-d") {
$debug = 1;
} elsif ($_ eq "-f") {
$dontfill = 1;
} elsif ($_ eq "-s") {
$dontsplit = 1;
} elsif ($_ eq "-r") {
$dontreuse = 1;
} elsif ($_ eq "-C") {
$dontuc = 0;
} elsif ($_ eq "-c") {
$conffile = shift;
$doreadconf = 1;
$dontreuse = 1;
} elsif ($_ eq "-P") {
$proxy = shift;
} elsif ($_ eq "-v") {
print "o2sms version $version\n";
exit(0);
} elsif ($_ eq "--dont-send") {
$dontsend = 1;
} elsif (/^\-\-?\w+/) {
$showhelp = 1;
} else {
push (@recips, $_);
}
}
# -- display help message
if ((scalar(@recips) == 0) || ($showhelp == 1)) {
print STDERR <<EOF;
Usage: $0 [options] <number|alias|group> [<number|alias|group> ...]
$0 is a script to send SMSs via the command line. It works by sending
requests to your phone operator's website (ie. simulating a browser). The
script requires that you have a valid web account, which is available to o2,
Vodaphone and Metor phone owners. At the time of writing, o2 give 250 free
web texts per month, Vodaphone 300 and Meteor 30.
Options:
-u <username> use this username (defaults to unix username or config file
username) (implies -r)
-p <password> use this password (defaults to config file password or
prompts for password)
-c <conffile> use this config file (defaults to ~/.o2smsrc) (implies -r)
-s don't split message over multiple SMSs
-f don't fill the end of the message with spaces (to clear ad)
-r don't reuse cookies when logging in
-C capitalise first letter in every sentence
-P <proxy> use this http proxy
-v print version
-h prints this help message
-d debug mode - prints the entire http dialog
Config File Format:
username <username>
password <password>
alias <name> <number> [number|alias ...]
sig <text-to-place-at-end-of-every-sms>
proxy <http://host:port/ >
nosplit
nofill
noreuse
capitalise
EOF
exit (0);
}
# -- read config file again
if ($doreadconf == 1) {
unless (read_config_file($conffile)) {
print STDERR "Couldn't open $conffile.\n";
exit(1);
}
}
# -- explode groups
my @recips2;
foreach my $recip (@recips) {
if(ref($aliases{$recip}) eq 'ARRAY'){
# is group
push (@recips2, @{$aliases{$recip}});
} else {
push (@recips2, $recip);
}
}
@recips = @recips2;
# -- check for aliases
for (my $i=0; $i<scalar(@recips); $i++) {
my $recip = $recips[$i];
if ($recip =~ /[^\d]/) {
# has non-numeric character - might be alias
if (exists($aliases{$recip})) {
$numbers[$i] = $aliases{$recip};
} else {
print STDERR "Not a valid alias: $recip.\n";
exit (1);
}
} else {
$numbers[$i] = $recip;
}
}
foreach my $number (@numbers) {
# -- check number
if ($number =~ /^0(8[567])(\d*)/) {
# is an irish mobile number - check length
if (length($number) != 10) {
print STDERR "Number is the wrong length for an Irish mobile number.\n";
exit (1);
}
# length ok - make international
$number = "00353$1$2";
} elsif ($number =~ /^00(\d*)/) {
# is an international number
} else {
# ?
print STDERR "Not a valid number: $number. Please use 08[567]xxxxxxx or international number.\n";
exit(1);
}
if (($is_voda) && ($number !~ /^00353/)) {
print STDERR "Vodafone webtexts can only be sent to Irish mobile numbers.\n";
exit(1);
}
}
# -- print welcome message
print "[ recipient(s) : ";
for (my $i=0; $i<scalar(@recips); $i++) {
my $recip = $recips[$i];
if ($recip =~ /[^\d]/) {
# is alias, print both
print "$recip (" . $numbers[$i] . ") ";
} else {
# just number
print $numbers[$i] . " ";
}
}
print "]\n";
# -- set the adjusted max length (with sig)
$single_max_length = $single_max_length - length($sig);
# -- get the text
while (<STDIN>) {
last if (/^\./);
$message .= $_;
}
# -- prepare the message
chomp($message); # kill last new line
$message =~ s/\n/ /gsm; # make other newlines spaces
unless ($dontuc) { # captialise first letter of every sentence
$message =~ s/(^\w)/uc($1)/gsme;
$message =~ s/([\.\?\!:\\]\s*)(\w)/$1 . uc($2)/gsme;
}
#$message =~ s/^$man_delim//; # remove unneeded splitters
#$message =~ s/$man_delim$//; # remove unneeded splitters
# -- split up the message
if (length($message) > $sms_max_length) {
print "[ warning : very long message, truncating to $sms_max_length chars ]\n";
$message = substr($message,0,$sms_max_length);
print $message;
}
if ((length($message) > $single_max_length) || ($message =~ /$man_delim/)) {
if ($dontsplit && ($message !~ /$man_delim/)) {
print "Message is too long (" . length($message) . "/$single_max_length), exitting.\n";
exit(1);
}
my $restmsg = $message;
my $partmsg;
while ((length($restmsg) > $single_max_length) || ($restmsg =~ /$man_delim/)) {
# if manual split, and message part can fit, then add the first bit and loop
if ($restmsg =~ /^(.*?)$man_delim(.*)/) {
if (length($1) < $single_max_length) {
$partmsg = $1;
$restmsg = $2;
push (@message_parts, $partmsg) if ($partmsg =~ /\S/);
next;
}
}
# if we don't want to split..
if ($dontsplit) {
print "Message part " . (scalar(@message_parts) + 1) . " is too long (" . length($restmsg) . "/$single_max_length), exitting.\n";
exit(1);
}
# message too long, split at the most natural place
($partmsg, $restmsg) = &split_message($single_max_length, $restmsg);
push (@message_parts, $partmsg) if ($partmsg =~ /\S/);
}
push (@message_parts, $restmsg . $sig) if ($restmsg =~ /\S/);
print "[ warning : long or split message, splitting into " . scalar(@message_parts) . " parts ]\n";
} else {
push (@message_parts, $message . $sig);
}
if (scalar(@message_parts) == 0) {
print "Nothing to do, exitting.\n";
exit (2);
}
# -- fill the end of each message part with white spaces to clear the "free web text" ad
if (!$dontfill) {
foreach my $message_part (@message_parts) {
my $msg_fill = " " x ($single_max_length - length($message_part) + length($sig));
$message_part .= $msg_fill;
}
}
# -- set the useragent to some random browser (to avoid suspicion!)
$ua->agent(&get_weighted_rand_elem_from_hash(%useragentstrs));
print "sms: using user agent: " . $ua->agent . "\n" if ($debug);
# -- set the proxy
if (($proxy) && ($proxy ne "")) {
$ua->proxy('http',$proxy);
$ua->proxy('https',$proxy);
print "sms: using proxy: $proxy\n" if ($debug);
}
# -- get a cookie jar from file or login
my $cj;
# check for existing cookie jar and use if hasn't expired
my @fstat = stat($cookiefile);
if ((@fstat) && ($debug)) {
print "sms: found cookiejar in $cookiefile with mtime of " . strftime("%a %b %e %H:%M:%S %Y", gmtime($fstat[9])) . "\n";
}
if (($dontreuse == 0) && (@fstat) && ($fstat[9] + $expiresecs > time())) {
# reuse the last cookie
# -- print status
print "[ reusing last login for $username\@" . &get_sp_domain . " ... ]\n";
# -- load the cookie file
$cj = HTTP::Cookies->new(ignore_discard => 1);
$cj->load($cookiefile);
# -- touch the cookie jar to keep it up to date
utime (time, time, $cookiefile);
} else {
# cookie jar non existant or expired, login instead
# -- print status
print "[ logging in to $username\@" . &get_sp_domain . " ... ]\n";
# -- get the password
if ($password eq "") {
system "stty -echo"; # Echo off
print "Password: "; # Prompt for password
chomp($password = <STDIN>); # Remove newline
system "stty echo"; # Echo on
print "\n";
}
# -- login
if ($dontsend > 0) {
$cj = &test_login ($username, $password);
} elsif ($is_o2) {
$cj = &o2_login ($username, $password);
} elsif ($is_voda) {
$cj = &voda_login ($username, $password);
} elsif ($is_meteor) {
$cj = &meteor_login ($username, $password);
}
if (!defined($cj)) {
print STDERR "[ login failed; your username or password is incorrect ]\n";
exit(1);
} else {
print "[ login successful ]\n";
}
}
# -- copy cookies from apps.o2.ie to webtext.o2.ie
#sub cookie_copy {
# my $version = $_[0];
# my $key = $_[1];
# my $val = $_[2];
# my $path = "/NASApp/TM";
# my $domain = "webtext.o2.ie";
# $cj->set_cookie($version,$key,$val,$path,$domain);
#}
#$cj->scan(\&cookie_copy);
#$cj->save($cookiefile);
# -- pick up the cookies from the other server
if (($dontsend == 0) && ($is_o2)) {
print "sms: picking up cookies from $dol_smsform_referrer\n" if ($debug);
my $req = HTTP::Request->new();
$req->uri($dol_smsform_referrer);
$req->method("GET");
$req->protocol("HTTP/1.0");
$cj->add_cookie_header($req);
print $req->as_string() if ($debug);
my $resp = $ua->send_request($req);
$cj->extract_cookies($resp);
print $resp->as_string() if ($debug);
$req->uri($resp->header("Location"));
print $req->as_string() if ($debug);
$resp = $ua->send_request($req);
$cj->extract_cookies($resp);
print $resp->as_string() if ($debug);
print $cj->as_string() if ($debug);
}
# -- send off the message
foreach my $number (@numbers) {
#foreach my $message (@message_parts) {
for (my $i=0; $i<scalar(@message_parts); $i++) {
my $retry;
do {
my $message = $message_parts[$i];
my $failed = 0;
$retry = 0;
my $retval;
if (($dontsend > 0) || ($loginbutdontsend > 0)) {
$retval = &test_send_sms($cj,$message,$number);
} elsif ($is_o2) {
$retval = &o2_send_sms($cj,$message,$number);
} elsif ($is_voda) {
$retval = &voda_send_sms($cj,$message,$number);
} elsif ($is_meteor) {
$retval = &meteor_send_sms($cj,$message,$number);
}
# get a nice message
my $partmsg = "";
if (scalar(@message_parts) > 1) {
$partmsg = " (part " . ($i+1) . " of " . scalar(@message_parts) . ")";
}
# -- check return values
if (defined($retval) && ($retval == -1)) {
# unknown repsonse.
print STDERR "[ message sending failed; unknown response from server ]\n";
$failed = 1;
} elsif ($retval == -2) {
# message sent, but we don't know how many are left
print "[ message$partmsg sent to $number, ? remaining this month ]\n";
} elsif ($retval == 0) {
# 0 messages left - probably failed sending
print STDERR "[ message sending failed; possibly a malformed message ]\n";
$failed = 1;
} elsif ($retval > 0) {
# message send successful
print "[ message$partmsg sent to $number, $retval remaining this month ]\n";
} else {
# unknown negative response
print STDERR "[ message sending failed ]\n";
$failed = 1;
}
if ($failed == 1) {
# ask for a retry
print "Retry ? y/n [n]: ";
my $resp = <STDIN>;
if ($resp =~ /^y/i) {
$retry = 1;
} else {
print "[ okay, I'm outta here. ]\n";
unlink ($cookiefile);
exit(1);
}
}
} while ($retry == 1);
}
}
# -- write message to message file
open (SMSMSG, "> $msgfile") || die ("Can't write to message file!");
print SMSMSG "$message\n";
close (SMSMSG);
# -- quit with success
exit(0);
sub o2_login {
my ($username, $password) = @_;
print "Logging in... \n" if ($debug);
# my $req = HTTP::Request->new('POST',$dol_login_url,
# [ referrer => $dol_login_referrer,
# username => $username,
# password => $password
# ]);
#$req->protocol("HTTP/1.0");
#my $res = $ua->request($req);
my $res = $ua->post($dol_login_url,
[ referrer => $dol_login_referrer,
username => $username,
password => $password
]);
my $lresp = $res->as_string();
print $lresp if ($debug);
# check for a broken server
if ($res->code == 500) {
print STDERR "[ login failed; server returned error 500 (Internal Server Error) ]\n";
exit(1);
}
# check for invalid username
#if ($lresp =~ /your username or password is incorrect/) {
#if (($res->is_redirect) && ($lresp =~ /error_login_page/)) {
if ($lresp =~ /Sorry, your username or password is incorrect./) {
return undef;
#} elsif ($lresp =~ /Welcome.*back.*$username/i) {
} else {
my $cookie_jar = HTTP::Cookies->new(ignore_discard => 1);
$cookie_jar->extract_cookies($res);
# save the cookies so we can reuse them
if (defined($cookie_jar) && ($dontreuse == 0)) {
$cookie_jar->save($cookiefile);
}
return $cookie_jar;
}
}
sub o2_send_sms {
my ($cookie_jar, $message, $number) = @_;
# remove whitespace at beginning of message
$message =~ s/^\s*//;
# encode the message for sending
$message = uri_escape($message);
# get what to send to server
#my $msgsize = 132 - length($message);
my $msgsize = length($message);
my $postmsg = "msisdn=$number&Msg=$message&recipients=1&grpSTR=&ConSTR=&command=send&NumMessages=1";
# construct the request for sending the message
my $req = HTTP::Request->new();
$req->push_header("Referer" => $dol_smsform_url);
$req->push_header("Referrer" => $dol_smsform_url);
$req->push_header("Host" => "webtext.o2.ie");
$req->push_header("Cookie2" => "\$Version=\"1\"");
$req->push_header("Content-Type" => "application/x-www-form-urlencoded");
$req->push_header("Content-Length" => length($postmsg));
$req->uri($dol_smsform_url);
$req->method("POST");
$req->protocol("HTTP/1.0");
#$req = HTTP::Request->new();
#$req->uri($dol_smsform_url . "?" . $postmsg);
#$req->method("GET");
#$req->protocol("HTTP/1.0");
$cookie_jar->add_cookie_header($req);
$req->content($postmsg);
print $req->as_string() if ($debug);
my $resp = $ua->request($req);
# print if debug
print $resp->as_string() if ($debug);
# if we don't get redirected to "sent.jsp" then there's some problem
if ($resp->header("Location") !~ /sent\.jsp/i) {
return -1;
}
# make redirect absolute
my $redirh = $resp->header("Location");
$redirh =~ s#\.\.\/#http://webtext.o2.ie/NASApp/TM/O2/#;
# success - follow the redirect
$req->method("GET");
$req->remove_header("Content-Type");
$req->remove_header("Content-Length");
$req->content("");
$req->uri($redirh);
print $req->as_string() if ($debug);
$resp = $ua->request($req);
print $resp->as_string() if ($debug);
# get the html
my $resp_html = $resp->as_string();
# if number > 0, then message was sent
#if (($resp_html =~ /Your message has been sent/i) &&
#($resp_html =~ /You have (\d*) messages remaining this month/i)) {
if (($resp_html =~ /<td class="StoryTitle ">Sent <\/td>/i) &&
($resp_html =~ /You now have (\d*) Free Messages remaining this month/i)) {
#print "Message sent!\n";
#print "(You have $1 messages left this month)\n";
return $1;
} else {
# the message was sent, but we don't know how many messages are left
return -2;
}
}
sub voda_login {
my ($username, $password) = @_;
print "Logging in... \n" if ($debug);
my $res = $ua->post($voda_login_url,
[ referrer => $voda_login_referrer,
username => $username,
password => $password
]);
my $lresp = $res->as_string();
print $lresp if ($debug);
# check for a broken server
if ($res->code == 500) {
print STDERR "[ login failed; server returned error 500 (Internal Server Error) ]\n";
exit(1);
}
# check for invalid username
if (($res->is_redirect) && ($res->header("Location") =~ m#myvodafone/services/logon_failed.jsp#i)) {
return undef;
#} elsif ($lresp =~ /Welcome.*back.*$username/i) {
} else {
my $cookie_jar = HTTP::Cookies->new(ignore_discard => 1);
$cookie_jar->extract_cookies($res);
# save the cookies so we can reuse them
if (defined($cookie_jar) && ($dontreuse == 0)) {
$cookie_jar->save($cookiefile);
}
return $cookie_jar;
}
}
sub voda_send_sms {
my ($cookie_jar, $message, $number) = @_;
# remove whitespace at beginning of message
$message =~ s/^\s*//;
# encode the message for sending
$message = uri_escape($message);
# make number national
$number =~ s/^00353/0/;
# get what to send to server
my $msgsize = length($message);
my $charleft = $single_max_length - length($message);
my $postmsg = "phoneid1=$number&MESSAGE=$message&num=$charleft&posted=yes";
# construct the request for sending the message
my $req = HTTP::Request->new();
$req->push_header("Referer" => $voda_smsform_referrer);
$req->push_header("Referrer" => $voda_smsform_referrer);
$req->push_header("Host" => "www.vodafone.ie");
$req->push_header("Cookie2" => "\$Version=\"1\"");
$req->push_header("Content-Type" => "application/x-www-form-urlencoded");
$req->push_header("Content-Length" => length($postmsg));
$req->uri($voda_smsform_url);
$req->method("POST");
$req->protocol("HTTP/1.0");
$cookie_jar->add_cookie_header($req);
$req->content($postmsg);
print $req->as_string() if ($debug);
my $resp = $ua->request($req);
# get the html
my $resp_html = $resp->as_string();
# print if debug
print $resp_html if ($debug);
# this text means successful send
if ($resp->header("Location") !~ /message_sent/i) {
return -1;
}
# go get the page again to see how many messages are left this month
my $req2 = HTTP::Request->new();
$req2->uri($voda_smsform_url);
$req2->method("GET");
$req2->protocol("HTTP/1.0");
$cookie_jar->add_cookie_header($req2);
$resp = $ua->request($req2);
$resp_html = $resp->as_string();
print $resp_html if ($debug);
if ($resp_html =~ /<td height="16" width="200"><b>(\d{1,3})<\/b><\/td>/i) {
return $1;
} else {
return -2;
}
}
sub meteor_login {
my ($username, $password) = @_;
print STDERR "Meteor not supported yet.\n";
exit -1;
}
sub meteor_send_sms {
}
sub test_login {
my ($username, $password) = @_;
print "sms: logging in with username $username\n";
return "x";
}
sub test_send_sms {
my ($cookie_jar, $message, $number) = @_;
$message =~ s/^\s*//;
$message = uri_escape($message);
print "sms: message = $message\n";
print "sms: number = $number\n";
return 666;
}
sub split_message {
my ($len, $message) = @_;
# split the message on a break between words..
# find the position of the last ' ' in the sentance
my $pos = index(reverse(substr($message,0, $len-3)), ' ');
$pos = $len - 3 - $pos;
# the part the message to send
my $partmessage = substr($message, 0, $pos-1);
# the remaining part of the message
my $restofmessage = substr($message, $pos);
return ($partmessage, $restofmessage);
}
sub read_config_file {
open (SMSCONF, $_[0]) && do {
while (<SMSCONF>) {
chomp();
next if (/^#/);
if (/^alias\s*(\w*)\s*(\d*)$/i) {
$aliases{$1} = $2;
print "sms: added alias for $1\n" if ($debug);
} elsif (/^alias\s*(\w*)\s*([\w\d\s]*)/i) {
my @group = split(/\s+/,$2);
@{$aliases{$1}} = @group;
print "sms: added group for $1 (" . join(", ",@{$aliases{$1}}) . ")\n" if ($debug);
} elsif (/^username\s*(.*)/i) {
$username = $1;
} elsif (/^password\s*(.*)/i) {
$password = $1;
} elsif (/^sig\s*(.*)/i) {
$sig = " $1";
chomp($sig);
} elsif (/^proxy\s*(.*)/i) {
$proxy = $1;
chomp($proxy);
} elsif (/^nofill/i) {
$dontfill = 1;
} elsif (/^noreuse/i) {
$dontreuse = 1;
} elsif (/^nosplit/i) {
$dontsplit = 1;
} elsif (/^capitali[sz]e/i) {
$dontuc = 0;
} elsif (/^debug\s*on/i) {
$debug = 1;
}
}
close (SMSCONF);
return 1;
};
return 0;
}
sub get_rand_elem_from_array {
my $elem = int(rand(scalar(@_)));
return $_[$elem];
}
sub get_weighted_rand_elem_from_hash {
my %thehash = @_;
my @uas = sort { $thehash{$b} <=> $thehash{$a} } keys %thehash;
my $rand = int(rand(100)) + 1;
foreach my $uastr (@uas) {
return $uastr if ($rand > $thehash{$uastr});
}
return $_[0];
}
sub get_sp_domain {
return "o2.ie" if ($is_o2);
return "vodafone.ie" if ($is_voda);
return "meteor.ie" if ($is_meteor);
}
More information about the ILUG
mailing list
Read this without the formatting .