webtext.pl

Send Vodafone ireland text messages from the command line. Uses the keychain info from safari on the mac. There’s an annoying sleep as it looks to detect pump-and-dump html requests (i.e. scripts like this).


#!/usr/bin/perl -w

use strict;
use HTML::Form;
use HTTP::Request;
use LWP;
use Data::Dumper;
my $username;
my $password;
my $message;
my @recipients = ();
if ($^O eq “darwin”) {
# Log in once to webtext under safari and this will work
# you need to allow the security program to access the keychain
open(IN, “security find-internet-password -g -s www.vodafone.ie -r htps 2>&1 |”) || die “$!”;
while(<IN>) {
if (m/”acct”<blob>=”(.*)”/) { $username = $1; }
if (m/password: “(.*)”/) { $password = $1; }
}
close(IN);
} else {
# use something else, or program the passwords here
$username=”0876666666″;
$password=”password”;
}
my $webtext_url=”https://www.vodafone.ie/myv/messaging/webtext/index.jsp”;
sub usage {
print “$0 <addressee1> […<addressee5>] \”Message\”\n”;
exit 0
}
usage() if ((scalar(@ARGV) < 2) || (scalar(@ARGV) > 6));
while (scalar(@ARGV) > 1) {
push(@recipients, shift @ARGV);
}
$message = pop(@ARGV);
if (length($message) > 160) {
die “Message too long.\n Shorten to: \”” . substr($message, 0, 160) . “\””;
}
my $ua = LWP::UserAgent->new;
push @{ $ua->requests_redirectable }, ‘POST’;
$ua->cookie_jar({ });
$ua->agent(‘Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10’);
my $response = $ua->get($webtext_url);
#print Dumper($response);
my @forms = HTML::Form->parse($response->content, $response->base);
my $form = undef;
foreach (@forms) {
if (defined($_->{‘attr’}) &&
lc($_->{‘attr’}{‘name’}) eq “login”) {
$form=$_;
}
}
die “Could not get Login Form” unless $form;
$form->value(‘username’, $username);
$form->value(‘password’, $password);
$response = $ua->request($form->click);
die “Login Failed” if (!$response->is_success || $response->base =~ /Login.shtml/);
$response->as_string() =~ /emaining messages this month:.*>(\d+)</s
or die “Unexpected response: ” . $response->as_string() . ” Couldn’t determine number of remaining texts”;
my $remaining = $1;
@forms = HTML::Form->parse($response->content, $response->base);
$form = undef;
foreach (@forms) {
if (defined($_->{‘attr’}) &&
lc($_->{‘attr’}{‘name’}) eq “webtext”) {
$form=$_;
}
}
die “Could not get Webtext Form” unless $form;
# walk the label for=… -> form id=… matches
my %chk_labels;
my %address_book;
%chk_labels = $response->content =~ m/label for=”chk_box_(\d*)”>([^<]*)</g;
#print Dumper($form);
foreach (keys(%chk_labels)) {
my $iv = $form->find_input(“chk_box_$_”);
$address_book{$chk_labels{$_}} = $iv->{menu}[1]{‘value’};
}
$form->value(‘message’, $message);
my $i = 0;
print “Sending: $message to “;
foreach (@recipients) {
my $phone = $_;
if (!($phone =~ /^[0-9]/)) {
$phone = $address_book{$phone};
}
$form->value(“recipients[$i]”, $phone);
print $phone . ” “;
$i++;
}
print “\n”;
sleep(5);
$response = $ua->request($form->click);
die “Message was not sent correctly” . Dumper($response) if (!($response->as_string() =~ /Your message was sent to:/));
$response = $ua->get($webtext_url);
$response->as_string() =~ /emaining messages this month:.*>(\d+)</s
or die “Unexpected response: ” . $response->as_string() . ” Couldn’t determine number of remaining texts”;
if ($remaining == $1) {
die “Even though the message was sent, the number of available messages didn’t reduce”;
}
print “Remaining Messages: $1\n”;
exit 0

Download Here