-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYouMail.pm
194 lines (169 loc) · 7.54 KB
/
YouMail.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env perl
package YouMail;
use Moose;
use LWP::UserAgent;
has 'phone_number' => (
isa => 'Int',
is => 'rw',
default => '',
);
has 'pin' => (
isa => 'Int',
is => 'rw',
default => '',
);
has 'cookie_jar' => (
# isa => 'String',
is => 'rw',
default => "$ENV{'HOME'}/.cookies.txt",
);
has 'user_agent' => (
is => 'rw',
);
has 'server_mailbox_data' => (
is => 'rw',
);
has 'folders' => (
is => 'rw',
);
has 'current_folder' => (
is => 'rw',
default => 'Inbox',
);
sub login {
my $self = shift;
croak("You must provide a phone number.") unless $self->phone_number;
croak("You must provide a pin number.") unless $self->pin;
$self->user_agent(LWP::UserAgent->new);
$self->user_agent->cookie_jar(
{
'file' => "$ENV{HOME}/.cookies.txt"
}
);
$self->user_agent->timeout(10);
$self->user_agent->env_proxy;
# Allow posts to redirect, as YouMail does this.
push @{ $self->user_agent->requests_redirectable }, 'POST';
# This is the most lightweight way I know of to get a cookie from YouMail.
my $response = $self->user_agent->post('http://m.youmail.com/mobile/signin.do?m=300', {
'userIdentifier' => $self->phone_number,
'password' => $self->pin
});
if ($response->is_success) {
# Todo: read the total messages comment in this response.
#print $response->decoded_content; # or whatever
}
else {
croak($response->status_line);
}
}
# Until I can figure out how to either get the full messageBox json dump from a
# direct call, this has to be used in order to know what folders we have to work
# with. There has to be a better way...
sub get_initial_server_data {
my $self = shift;
my $response = $self->user_agent->post('http://www.youmail.com/youmail/vm/inbox/inbox.do');
if ($response->is_success) {
# Parse out the message list from the javascript embedded in the page.
# print $response->decoded_content; # or whatever
use XML::Twig;
my $twig = XML::Twig->new(
pretty_print => 'indented',
discard_spaces => 1,
empty_tags=> 'normal',
comments=> 'keep',
twig_handlers => {
div => sub {
if(($_->{'att'}->{'id'}) && ($_->{'att'}->{'id'} eq 'mainContentPane')){
# Get the JavaScript
#$_->print;
my $script = $_->first_child('script');
$script = $script->trimmed_text;
# Regex out YouMail's embedded JSON.
$script =~ /Youmail.vm.data = ({.*?})\;/;
croak("Failed to extract JSON from message listing!") unless $1;
my $messages_in_json = $1;
# Once we get here, we really should parse out the json, store the folders in
# $self->folders, store the messages in $self->messages.
# Only JSON::DWIW handles YouMail's json properly.
# They take some liberties with quoting some of the name/value pairs,
# and even setting relaxed doesn't fix it. :(
use JSON::DWIW;
my $json = new JSON::DWIW;
my $data = $json->from_json($messages_in_json);
# Just cleaning up the folders and messages to have an index rather than
# just be anonymous hashes.
my $folders;
my $current_folder;
foreach my $folder( @{ $data->{'messageBox'}->{'folders'} } ){
# Make a note of which folder we're looking at, so we put our
# messages in the right one.
if($folder->{'selected'}){
$current_folder = $folder->{'folderKey'};
}
foreach my $key(keys %$folder){
$folders->{ $folder->{'folderKey'} }->{$key} = $folder->{$key};
}
}
my $messages;
foreach my $message( @{ $data->{'messageBox'}->{'messages'} } ){
foreach my $key(keys %$message){
$messages->{ $message->{'XID'} }->{$key} = $message->{$key};
}
}
$folders->{$current_folder}->{'messages'} = $messages;
$self->folders( $folders );
# $self->messages( $messages );
#$self->server_mailbox_data( $messages_in_json );
#croak("Failed to interpret server mailbox data!") unless $self->server_mailbox_data;
croak("Failed to interpret server mailbox data!") unless $self->folders;
}
else{
#print("Nope, not the div I want - id: ".$_->{'att'}->{'id'}."\n") if $_->{'att'}->{'id'};
}
}
},
);
# In case of debug, use soft tabs.
$twig->set_indent(" ");
$twig->safe_parse_html($response->decoded_content);
}
else {
croak($response->status_line);
}
}
sub update_message_list {
# ONLY use this routine after having already done login and get_initial_server_data!
# Must also realize to set the current folder, otherwise you'll always get the Inbox.
my $self = shift;
my $response = $self->user_agent->get('http://www.youmail.com/youmail/vm/inbox/inboxData.do?f='.$self->current_folder);
if ($response->is_success) {
use JSON::DWIW;
my $json = JSON::DWIW->new;
my $data = $json->from_json($response->decoded_content);
return unless scalar( @{ $data->{'messages'} } );
# Flush the existing messages hash for this folder, then
# populate the hash with what we just pulled.
$self->folders->{ $self->current_folder }->{'messages'} = undef;
my $messages;
foreach my $server_message( @{ $data->{'messages'} } ){
foreach my $key(keys %$server_message){
$messages->{ $server_message->{'XID'} }->{$key} = $server_message->{$key};
}
}
$self->folders->{ $self->current_folder }->{'messages'} = $messages;
}
else{
croak("update_message_list(): Failed to get updated listing for ".$self->current_folder.", the error was: ".$response->status_line);
}
}
sub fetch_message_as_mp3 {
my $self = shift;
my $xid = shift;
# Use the following URL to grab the mp3 data.
# http://www.youmail.com/youmail/voicemail/getDataStream.do?id=14977312
# Where 14977312 is the XID of the voicemail.
# You'll have to use LWP::UserAgent to store the file to a temp directory.
# We'll eventually want to store the message in a SQLite Database.
}
1