| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #--------------------------------------------------------------------- | 
| 2 |  |  |  |  |  |  | package WebService::Google::Voice::SendSMS; | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # Copyright 2013 Christopher J. Madsen | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | # Author: Christopher J. Madsen | 
| 7 |  |  |  |  |  |  | # Created: 29 Jan 2013 | 
| 8 |  |  |  |  |  |  | # | 
| 9 |  |  |  |  |  |  | # This program is free software; you can redistribute it and/or modify | 
| 10 |  |  |  |  |  |  | # it under the same terms as Perl itself. | 
| 11 |  |  |  |  |  |  | # | 
| 12 |  |  |  |  |  |  | # This program is distributed in the hope that it will be useful, | 
| 13 |  |  |  |  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 14 |  |  |  |  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either the | 
| 15 |  |  |  |  |  |  | # GNU General Public License or the Artistic License for more details. | 
| 16 |  |  |  |  |  |  | # | 
| 17 |  |  |  |  |  |  | # ABSTRACT: Send a SMS using Google Voice | 
| 18 |  |  |  |  |  |  | #--------------------------------------------------------------------- | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 1 |  |  | 1 |  | 22672 | use 5.008; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 59 |  | 
| 21 | 1 |  |  | 1 |  | 7 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 46 |  | 
| 22 | 1 |  |  | 1 |  | 40 | use warnings; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 52 |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 1 |  |  | 1 |  | 8 | use Carp (); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 54 |  | 
| 25 | 1 |  |  | 1 |  | 906 | use LWP::UserAgent 6 (); | 
|  | 1 |  |  |  |  | 58364 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 26 | 1 |  |  | 1 |  | 946 | use HTTP::Request::Common (); | 
|  | 1 |  |  |  |  | 2599 |  | 
|  | 1 |  |  |  |  | 726 |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | #use Smart::Comments '###'; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | our $VERSION = '1.001'; | 
| 31 |  |  |  |  |  |  | # This file is part of WebService-Google-Voice-SendSMS 1.001 (November 1, 2014) | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | #===================================================================== | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 0 |  |  | 0 | 0 |  | sub inboxURL { 'https://www.google.com/voice/m/' } | 
| 36 | 0 |  |  | 0 | 0 |  | sub loginURL { 'https://www.google.com/accounts/ClientLogin' } | 
| 37 | 0 |  |  | 0 | 0 |  | sub smsURL   { 'https://www.google.com/voice/m/sendsms' } | 
| 38 |  |  |  |  |  |  | #--------------------------------------------------------------------- | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub new | 
| 42 |  |  |  |  |  |  | { | 
| 43 | 0 |  |  | 0 | 1 |  | my ($class, $username, $password) = @_; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 |  |  |  |  |  | bless { | 
| 46 |  |  |  |  |  |  | username => $username, | 
| 47 |  |  |  |  |  |  | password => $password, | 
| 48 |  |  |  |  |  |  | ua       => LWP::UserAgent->new( | 
| 49 |  |  |  |  |  |  | agent => "Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2_1 like Mac OS X;". | 
| 50 |  |  |  |  |  |  | " en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) ". | 
| 51 |  |  |  |  |  |  | "Version/3.1.1 Mobile/5H11 Safari/525.20", | 
| 52 |  |  |  |  |  |  | cookie_jar => {}, | 
| 53 |  |  |  |  |  |  | ), | 
| 54 |  |  |  |  |  |  | }, $class; | 
| 55 |  |  |  |  |  |  | } # end new | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | #--------------------------------------------------------------------- | 
| 58 |  |  |  |  |  |  | # Get and return the Google authorization credential | 
| 59 |  |  |  |  |  |  | # | 
| 60 |  |  |  |  |  |  | # Called automatically by _make_request when needed | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub _login | 
| 63 |  |  |  |  |  |  | { | 
| 64 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | my $rsp = $self->_make_request( | 
| 67 |  |  |  |  |  |  | HTTP::Request::Common::POST($self->loginURL, [ | 
| 68 |  |  |  |  |  |  | accountType => 'GOOGLE', | 
| 69 |  |  |  |  |  |  | Email       => $self->{username}, | 
| 70 |  |  |  |  |  |  | Passwd      => $self->{password}, | 
| 71 |  |  |  |  |  |  | service     => 'grandcentral', | 
| 72 |  |  |  |  |  |  | source      => 'org.cpan.WebService.Google.Voice.SendSMS', | 
| 73 |  |  |  |  |  |  | ]), | 
| 74 |  |  |  |  |  |  | { no_headers => 1 }    # Can't add authorization, we're logging in | 
| 75 |  |  |  |  |  |  | ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 |  |  |  |  |  | my $cref = $rsp->decoded_content(ref => 1); | 
| 78 | 0 | 0 |  |  |  |  | $$cref =~ /Auth=([A-z0-9_-]+)/ | 
| 79 |  |  |  |  |  |  | or Carp::croak("SendSMS: Unable to find Auth in response"); | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 |  |  |  |  |  | return $1; | 
| 82 |  |  |  |  |  |  | } # end _login | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | #--------------------------------------------------------------------- | 
| 85 |  |  |  |  |  |  | # Send a request via our UA and return the response: | 
| 86 |  |  |  |  |  |  | # | 
| 87 |  |  |  |  |  |  | # Options may be passed in $args hashref: | 
| 88 |  |  |  |  |  |  | #   allow_failure:  if true, do not die if request is unsuccessful | 
| 89 |  |  |  |  |  |  | #   no_headers:     if true, omit Authorization & Referer headers | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub _make_request | 
| 92 |  |  |  |  |  |  | { | 
| 93 | 0 |  |  | 0 |  |  | my ($self, $req, $args) = @_; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 0 | 0 |  |  |  |  | unless ($args->{no_headers}) { | 
| 96 | 0 |  | 0 |  |  |  | $req->header(Authorization => | 
| 97 |  |  |  |  |  |  | 'GoogleLogin auth=' . ($self->{login_auth} ||= $self->_login)); | 
| 98 | 0 |  |  |  |  |  | $req->referer($self->{lastURL}); | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | ### Request : $req->as_string | 
| 102 | 0 |  |  |  |  |  | my $rsp = $self->{ua}->request($req); | 
| 103 |  |  |  |  |  |  | ### Response : $rsp->as_string | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 0 | 0 |  |  |  |  | if ($rsp->is_success) { | 
| 106 | 0 |  |  |  |  |  | $self->{lastURL} = $rsp->request->uri; | 
| 107 |  |  |  |  |  |  | } else { | 
| 108 | 0 | 0 |  |  |  |  | Carp::croak("SendSMS: HTTP request failed: " . $rsp->status_line) | 
| 109 |  |  |  |  |  |  | unless $args->{allow_failure}; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  |  |  |  | $rsp; | 
| 113 |  |  |  |  |  |  | } # end _make_request | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | #--------------------------------------------------------------------- | 
| 117 |  |  |  |  |  |  | sub _get_rnr_se | 
| 118 |  |  |  |  |  |  | { | 
| 119 | 0 |  |  | 0 |  |  | my $self = shift; | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 0 |  |  |  |  |  | my $rsp = $self->_make_request(HTTP::Request::Common::GET($self->inboxURL)); | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 0 |  |  |  |  |  | my $cref = $rsp->decoded_content(ref => 1); | 
| 124 | 0 | 0 |  |  |  |  | $$cref =~ /]*?name="_rnr_se"[^>]*?value="([^"]*)"/s | 
| 125 |  |  |  |  |  |  | or Carp::croak("SendSMS: Unable to find _rnr_se in response"); | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 0 |  |  |  |  |  | return $1; | 
| 128 |  |  |  |  |  |  | } # end _get_rnr_se | 
| 129 |  |  |  |  |  |  | #--------------------------------------------------------------------- | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | sub send_sms | 
| 133 |  |  |  |  |  |  | { | 
| 134 | 0 |  |  | 0 | 1 |  | my ($self, $number, $message) = @_; | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 0 |  |  |  |  |  | my $req = HTTP::Request::Common::POST($self->smsURL, [ | 
| 137 |  |  |  |  |  |  | id      => '', | 
| 138 |  |  |  |  |  |  | c       => '', | 
| 139 |  |  |  |  |  |  | number  => $number, | 
| 140 |  |  |  |  |  |  | smstext => $message, | 
| 141 |  |  |  |  |  |  | _rnr_se => $self->_get_rnr_se, | 
| 142 |  |  |  |  |  |  | ]); | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 0 |  |  |  |  |  | return $self->_make_request($req, { allow_failure => 1 })->is_success; | 
| 145 |  |  |  |  |  |  | } # end send_sms | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | #===================================================================== | 
| 148 |  |  |  |  |  |  | # Package Return Value: | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | 1; | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | __END__ |