| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | # SMS::CPAGateway | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright Eskild Hustvedt 2008, 2010 <zerodogg@cpan.org> | ||||||||||||||||||||||||||||
| 5 | # for Portu Media & Communications | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 8 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | package SMS::CPAGateway; | ||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Any::Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use URI::Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use constant { true => 1, false => undef }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = 0.01; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # - Public attributes - | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | has 'servers' => ( | |||||||||||||||||||||||||||
| 21 | is => 'rw', | ||||||||||||||||||||||||||||
| 22 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 23 | required => true, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | has 'fromNo' => ( | |||||||||||||||||||||||||||
| 27 | isa => 'Int', | ||||||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||||||
| 29 | required => true, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | has 'price' => ( | |||||||||||||||||||||||||||
| 33 | isa => 'Int', | ||||||||||||||||||||||||||||
| 34 | is => 'rw', | ||||||||||||||||||||||||||||
| 35 | required => true, | ||||||||||||||||||||||||||||
| 36 | default => 0, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | has 'key' => ( | |||||||||||||||||||||||||||
| 40 | isa => 'Str', | ||||||||||||||||||||||||||||
| 41 | required => true, | ||||||||||||||||||||||||||||
| 42 | is => 'rw', | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | has 'id' => ( | |||||||||||||||||||||||||||
| 46 | isa => 'Str', | ||||||||||||||||||||||||||||
| 47 | is => 'rw', | ||||||||||||||||||||||||||||
| 48 | builder => '_buildID', | ||||||||||||||||||||||||||||
| 49 | lazy => 1, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | has 'errors'=> ( | ||||||||||||||||||||||||||||
| 53 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 54 | is => 'rw', | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 56 | 1 | writer => '_writeErrors', | |||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | has 'hadError' => ( | |||||||||||||||||||||||||||
| 60 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 61 | is => 'rw', | ||||||||||||||||||||||||||||
| 62 | default => false, | ||||||||||||||||||||||||||||
| 63 | writer => '_setError', | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # - Public methods - | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Purpose: Send an SMS | ||||||||||||||||||||||||||||
| 69 | # Usage: object->send(RECIPIENT,MESSAGE,PRICE?); | ||||||||||||||||||||||||||||
| 70 | # RECIPIENT is the recipient phone number, with +XX direction code. | ||||||||||||||||||||||||||||
| 71 | # MESSAGE is the message. | ||||||||||||||||||||||||||||
| 72 | # PRICE is the price | ||||||||||||||||||||||||||||
| 73 | sub send | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 0 | my $recipient = shift; | |||||||||||||||||||||||||||
| 77 | 0 | my $message = shift; | |||||||||||||||||||||||||||
| 78 | 0 | my $price = shift; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (not $recipient =~ /^\+/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ($recipient =~ /^00/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | { | ||||||||||||||||||||||||||||
| 83 | 0 | $recipient =~ s/^00/+/; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else | ||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 0 | $self->_appendError('Recipient does not start with +'); | |||||||||||||||||||||||||||
| 88 | 0 | return false; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if(not defined $price) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 0 | $price = $self->price; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | elsif(not $price =~ /^\d+$/) | ||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 0 | $self->_appendError('Illegal price value. "'.$price.'"'); | |||||||||||||||||||||||||||
| 98 | 0 | return false; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | $self->_recipient($recipient); | |||||||||||||||||||||||||||
| 101 | 0 | return $self->_SMSSend($message,$price); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # --- INTERNAL METHODS AND ATTRIBUTES --- | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | has '_recipient' => ( | |||||||||||||||||||||||||||
| 107 | isa => 'Str', | ||||||||||||||||||||||||||||
| 108 | is => 'rw', | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | has '_prevID' => ( | |||||||||||||||||||||||||||
| 112 | isa => 'Str', | ||||||||||||||||||||||||||||
| 113 | is => 'rw', | ||||||||||||||||||||||||||||
| 114 | ); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | has '_ua' => ( | ||||||||||||||||||||||||||||
| 117 | isa => 'Object', | ||||||||||||||||||||||||||||
| 118 | is => 'ro', | ||||||||||||||||||||||||||||
| 119 | lazy => true, | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | default => sub { LWP::UserAgent->new() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 121 | 1 | ); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | has '_uriE' => ( | ||||||||||||||||||||||||||||
| 124 | isa => 'Object', | ||||||||||||||||||||||||||||
| 125 | is => 'ro', | ||||||||||||||||||||||||||||
| 126 | lazy => true, | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | default => sub { URI::Encode->new() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 128 | 1 | ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # Purpose: Construct a unique identifier | ||||||||||||||||||||||||||||
| 131 | # Usage: id = this->_buildID(); | ||||||||||||||||||||||||||||
| 132 | sub _buildID | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_buildID: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 134 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 135 | 0 | my $recipient = $self->_recipient; | |||||||||||||||||||||||||||
| 136 | 0 | $recipient =~ s/\+/00/; | |||||||||||||||||||||||||||
| 137 | 0 | my $id; | |||||||||||||||||||||||||||
| 138 | # Should be below 100 chars long, so loop until we get it right | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | while(!defined $id || length($id) > 99) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $id or length $id > 99
MC/DC0%0/2
not defined $id or length $id > 99
not defined $id
length $id > 99
Truth table0%0/3
not(A) or B
A= not defined $idB= length $id > 99
| |||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||
| 141 | 0 | $id = $recipient.'-'.time().int(rand(10_000)).int(rand(100_000)).'-SMS::CPAGateway-'.$VERSION; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 0 | $self->_prevID($id); | |||||||||||||||||||||||||||
| 144 | 0 | return $id; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # Purpose: Get our unique identifier | ||||||||||||||||||||||||||||
| 148 | # Usage: id = self->_getID(); | ||||||||||||||||||||||||||||
| 149 | sub _getID | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_getID: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 151 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | if( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->_prevID and $self->_prevID eq $self->id
MC/DC0%0/2
defined $self->_prevID and $self->_prevID eq $self->id
defined $self->_prevID
$self->_prevID eq $self->id
Truth table0%0/3
A and B
A= defined $self->_prevIDB= $self->_prevID eq $self->id
| |||||||||||||||||||||||||||||
| 153 | (not defined $self->_prevID) | ||||||||||||||||||||||||||||
| 154 | or | ||||||||||||||||||||||||||||
| 155 | (not $self->_prevID eq $self->id) | ||||||||||||||||||||||||||||
| 156 | ) | ||||||||||||||||||||||||||||
| 157 | { | ||||||||||||||||||||||||||||
| 158 | 0 | return $self->id; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 0 | $self->id($self->_buildID()); | |||||||||||||||||||||||||||
| 161 | 0 | return $self->id; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | sub _appendError | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_appendError: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 166 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | 0 | my $error = shift; | |||||||||||||||||||||||||||
| 168 | 0 | my $errorList = $self->errors; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | if (ref($error)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 0 | $error = 'Error reply '.$error->{reply}.' when requesting URL '.$error->{URL}.' at '.time(); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | push(@{$errorList},$error); | |||||||||||||||||||||||||||
| 174 | 0 | $self->_writeErrors($errorList); | |||||||||||||||||||||||||||
| 175 | 0 | $self->_setError(true); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Purpose: Handle errors in requests | ||||||||||||||||||||||||||||
| 179 | # Usage: my $ret = $self->_errHandler(URI,reply); | ||||||||||||||||||||||||||||
| 180 | # | ||||||||||||||||||||||||||||
| 181 | # This method verifies the content of reply and returns true or false. | ||||||||||||||||||||||||||||
| 182 | # false meaning that sending failed and we should retry. Errors are | ||||||||||||||||||||||||||||
| 183 | # added to the error list via $self->_appendError | ||||||||||||||||||||||||||||
| 184 | sub _errHandler | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_errHandler: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 186 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 0 | my $URL = shift; | |||||||||||||||||||||||||||
| 188 | 0 | my $reply = shift; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | my $content = $reply->content; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | if ($content =~ /^\s*ok/i) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 193 | { | ||||||||||||||||||||||||||||
| 194 | # All is well | ||||||||||||||||||||||||||||
| 195 | 0 | return true; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | elsif ($content =~ /^\s*err/i) | ||||||||||||||||||||||||||||
| 198 | { | ||||||||||||||||||||||||||||
| 199 | 0 | $self->_appendError( { URL => $URL, reply => $content } ); | |||||||||||||||||||||||||||
| 200 | 0 | return false; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | else | ||||||||||||||||||||||||||||
| 203 | { | ||||||||||||||||||||||||||||
| 204 | # Okay, this server isn't acting properly | ||||||||||||||||||||||||||||
| 205 | 0 | $self->_appendError('The server returned an unknown reply, so I am assuming failure: '.$content); | |||||||||||||||||||||||||||
| 206 | 0 | return false; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # Purpose: Construct the request URI and attempt to submit it to the servers | ||||||||||||||||||||||||||||
| 211 | # Usage: $ret = $self->_SMSSend(message,$price); | ||||||||||||||||||||||||||||
| 212 | sub _SMSSend | ||||||||||||||||||||||||||||
| 213 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_SMSSend: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 214 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 215 | 0 | my $message = shift; | |||||||||||||||||||||||||||
| 216 | 0 | my $price = shift; | |||||||||||||||||||||||||||
| 217 | # Construct URI | ||||||||||||||||||||||||||||
| 218 | 0 | my $URI = '?auth='.$self->_uriE->encode($self->key). | |||||||||||||||||||||||||||
| 219 | '&id='.$self->_uriE->encode($self->_getID). | ||||||||||||||||||||||||||||
| 220 | '&from='.$self->_uriE->encode($self->fromNo). | ||||||||||||||||||||||||||||
| 221 | '&to='.$self->_uriE->encode($self->_recipient). | ||||||||||||||||||||||||||||
| 222 | '&type=text&data='.$self->_uriE->encode($message). | ||||||||||||||||||||||||||||
| 223 | '&price='.$price; | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | foreach my $host (@{$self->servers}) | |||||||||||||||||||||||||||
| 226 | { | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if ($self->_attemptSend($host,$URI)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | { | ||||||||||||||||||||||||||||
| 229 | # We sent it and all went well | ||||||||||||||||||||||||||||
| 230 | 0 | return true; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | # If not, we didn't send it and we ought to try the next server | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | # If we've gotten this far then all servers failed and we have to give up | ||||||||||||||||||||||||||||
| 235 | 0 | $self->_appendError('All servers failed. Giving up.'); | |||||||||||||||||||||||||||
| 236 | 0 | return false; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # Purpose: Attempt to send an SMS | ||||||||||||||||||||||||||||
| 240 | # Usage: $ret = $self->_attemptSend(host,URI); | ||||||||||||||||||||||||||||
| 241 | sub _attemptSend | ||||||||||||||||||||||||||||
| 242 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_attemptSend: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 243 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 244 | 0 | my $host = shift; | |||||||||||||||||||||||||||
| 245 | 0 | my $URI = shift; | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | my $request = $host.'/'.$URI; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | my $reply = $self->_ua->get($request); | |||||||||||||||||||||||||||
| 250 | 0 | ▶ | if ($reply->is_success()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | { | ||||||||||||||||||||||||||||
| 252 | 0 | return $self->_errHandler($request,$reply); | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | else | ||||||||||||||||||||||||||||
| 255 | { | ||||||||||||||||||||||||||||
| 256 | 0 | $self->_appendError('Server '.$host.' failed'); | |||||||||||||||||||||||||||
| 257 | 0 | return false; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 262 | 1 | 1; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | __END__ | ||||||||||||||||||||||||||||