Code formatting.
[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 => 'SMS');
40
41 my $dom = Mojo::DOM->new;
42 $dom->parse($mech->content());
43
44 my $send_url = $dom->find('div[id="top-buttons"] a')->[1]->attrs('href');
45 $mech->get($send_url);
46
47 # Using $mech->submit_form instead is not working, so don't change
48 # code below!
49 $mech->form_name('sendSMS');
50 $mech->set_fields(
51     '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.to' => $number,
52     '/amg/ptk/map/messagebox/formhandlers/MessageFormHandler.body' => $message,
53 );
54 $mech->click();
55
56 $dom->parse($mech->content());
57
58 my $free_sms_left = $dom->find('div[id="syndication"] p span')->[1]->text;
59
60 print "Message has been sent. Free sms'es left: $free_sms_left\n";