Sync with new Orange.pl website.
[sms.git] / sms.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use WWW::Mechanize;
7 use Mojo::DOM;
8 use Config::General;
9 use Data::Dumper;
10
11 # Usage:
12 #   sms.pl MOBILE_NO "TEXT_MESSAGE"
13 #
14 # You need to set .smsrc file in the HOME directory and fill it like this:
15 #   login    = LOGIN
16 #   password = PASSWORD
17 #
18 # where LOGIN and PASSWORD are your credentials to orange.pl site
19
20 my $conf = new Config::General("$ENV{HOME}/.smsrc");
21 my %config = $conf->getall;
22 die "No login in config file. Stopped" unless $config{login};
23 die "No password in config file. Stopped" unless $config{password};
24
25 my $number = $ARGV[0];
26 my $message = $ARGV[1];
27 die "No recipient number entered. Stopped" unless $number;
28 die "No message entered. Stopped" unless $message;
29
30 my $mech = WWW::Mechanize->new;
31 $mech->get("http://orange.pl");
32 $mech->submit_form(
33     form_id => 'loginForm',
34     fields => {
35       '/tp/core/profile/login/ProfileLoginFormHandler.value.login' => $config{login},
36       '/tp/core/profile/login/ProfileLoginFormHandler.value.password' => $config{password}
37     }
38   );
39 $mech->follow_link( text => 'Multi Box' );
40 $mech->follow_link( text => 'SMS' );
41
42 my $dom = Mojo::DOM->new;
43 $dom->parse($mech->content());
44
45 my $send_url = $dom->find('div[id="top-buttons"] a')->[1]->attr('href');
46 $mech->get($send_url);
47
48 # Using $mech->submit_form instead is not working, so don't change
49 # code below!
50 $mech->form_name('sendSMS');
51 $mech->set_fields(
52     '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.to' => $number,
53     '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.body' => $message,
54 );
55 $mech->click();
56
57 $dom->parse($mech->content());
58
59 my $free_sms_left = $dom->find('div[id="syndication"] p span')->[1]->text;
60
61 print "Message has been sent. Free sms'es left: $free_sms_left\n";