blib/lib/Net/SMS/PChome.pm

filter: Statementstmt 11.9% 19 / 159 Branchbran 0.0% 0 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 20.0% 5 / 25 Podpod 88.2% 15 / 17 totaltotal 14.9% 39 / 261 CC 34 SCAR 47.1
CC
34
Cov
9%
smsSend
342.0
login
20.0
_parse_output
12.0
CRAP 110.7
↑ Summary
1 package Net::SMS::PChome;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTML::TagParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Date::Calc qw(check_date check_time Today_and_Now This_Year);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.11';
10 1 our (@ISA) = qw(Exporter);
11 1 our (@EXPORT) = qw(send_sms);
12  
13 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
14 0 my ($class, %params) = @_;
15 0 my $self = {};
16 0 bless $self, $class;
17 0 ▶ $self->_init(%params) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_init(%params) or return undef 0 0 0
18 0 return $self;
19 }
20  
21 0 ▶sub send_sms {
Subroutine send_sms: not called CC=1 SCAR=6.9
Pod undocumented
22 0 return __PACKAGE__->new(
23 username => $_[0],
24 password => $_[1],
25 authcode => $_[2],
26 recipients=> [$_[3]],
27 )->smsSend($_[4]);
28 }
29  
30 0 ▶sub baseurl {
Subroutine baseurl: not called CC=2 SCAR=17.9
Pod documented
31 0 my $self = shift;
32 0 ▶ if (@_) { $self->{"_baseurl"} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
33 0 return $self->{"_baseurl"};
34 }
35  
36 0 ▶sub username {
Subroutine username: not called CC=2 SCAR=17.9
Pod documented
37 0 my $self = shift;
38 0 ▶ if (@_) { $self->{"_username"} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
39 0 return $self->{"_username"};
40 }
41  
42 0 ▶sub password {
Subroutine password: not called CC=2 SCAR=17.9
Pod documented
43 0 my $self = shift;
44 0 ▶ if (@_) { $self->{"_password"} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
45 0 return $self->{"_password"};
46 }
47  
48 0 ▶sub authcode {
Subroutine authcode: not called CC=2 SCAR=17.9
Pod documented
49 0 my $self = shift;
50 0 ▶ if (@_) { $self->{"_authcode"} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
51 0 return $self->{"_authcode"};
52 }
53  
54 0 ▶sub login {
Subroutine login: not called CC=4 SCAR=30.0
Pod documented
55 0 my ($self, $user, $pass, $auth) = @_;
56 0 ▶ $self->username($user) if($user);
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
57 0 ▶ $self->password($pass) if($pass);
Branch: 1 branch
BranchTrueFalseTotal
if $pass 0 0 0
58 0 ▶ $self->authcode($auth) if($auth);
Branch: 1 branch
BranchTrueFalseTotal
if $auth 0 0 0
59 0 return ($self->username, $self->password, $self->authcode);
60 }
61  
62 0 ▶sub smsRecipient {
Subroutine smsRecipient: not called CC=2 SCAR=17.9
Pod documented
63 0 my ($self, $recip) = @_;
64 0 ▶ push @{$self->{"_recipients"}}, $recip if($recip);
Branch: 1 branch
BranchTrueFalseTotal
if $recip 0 0 0
65 0 return $self->{"_recipients"};
66 }
67  
68 0 ▶sub smsMessage {
Subroutine smsMessage: not called CC=2 SCAR=17.9
Pod documented
69 0 my $self = shift;
70 0 ▶ if (@_) { $self->{"_message"} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
71 0 return $self->{"_message"};
72 }
73  
74 0 ▶sub smsDeliverydate {
Subroutine smsDeliverydate: not called CC=2 SCAR=17.9
Pod documented
75 0 my $self = shift;
76 0 ▶ if (@_) { $self->{"_dlvdatetime"} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
77 0 return $self->{"_dlvdatetime"};
78 }
79  
80 0 ▶sub smsType {
Subroutine smsType: not called CC=2 SCAR=17.9
Pod documented
81 0 my $self = shift;
82 0 ▶ if (@_) { $self->{"_sendType"} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
83 0 return $self->{"_sendType"};
84 }
85  
86 0 ▶sub smsEncode {
Subroutine smsEncode: not called CC=2 SCAR=17.9
Pod documented
87 0 my $self = shift;
88 0 ▶ if (@_) { $self->{"_encodeType"} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
89 0 return $self->{"_encodeType"};
90 }
91  
92 0 ▶sub is_success {
Subroutine is_success: not called CC=1 SCAR=6.9
Pod documented
93 0 my $self = shift;
94 0 return $self->{"_success"};
95 }
96  
97 0 ▶sub successcount {
Subroutine successcount: not called CC=1 SCAR=6.9
Pod undocumented
98 0 my $self = shift;
99 0 return $self->{"_successcount"};
100 }
101  
102 0 ▶sub resultcode {
Subroutine resultcode: not called CC=1 SCAR=6.9
Pod documented
103 0 my $self = shift;
104 0 return $self->{"_resultcode"};
105 }
106  
107 0 ▶sub resultmessage {
Subroutine resultmessage: not called CC=1 SCAR=6.9
Pod documented
108 0 my $self = shift;
109 0 return $self->{"_resultmessage"};
110
111 }
112  
113 0 ▶sub smsSend {
Subroutine smsSend: not called CC=18 SCAR=58.3
Pod documented
114 0 my ($self, $message) = @_;
115 0 ▶ $self->smsMessage($message) if($message);
Branch: 1 branch
BranchTrueFalseTotal
if $message 0 0 0
116 0 my $parms = {};
117
118 #### Check for mandatory input
119 0 foreach(qw/username password authcode recipients message sendType encodeType/) {
120 0 ▶ $self->_croak("$_ not specified.") unless(defined $self->{"_$_"});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self{"_$_"} 0 0 0
121 0 ▶ if($_ eq 'recipients') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'recipients') { } 0 0 0
122 0 $parms->{$_} = join(";", @{$self->{"_$_"}});
123 } else {
124 0 $parms->{$_} = $self->{"_$_"};
125 }
126 }
127  
128 # Type can be now/dlv
129 0 ▶ $self->_croak("Invalid type")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->smsType =~ /^[12]$/ 0 0 0
130 unless($self->smsType =~ /^[12]$/);
131  
132 # delivery? We must have a Date that format: YYYYMMDDHHmm (example:200606130830)
133 0 ▶ if($self->smsType eq '2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->smsType eq 2) 0 0 0
134 0 ▶ $self->_croak("No delivery date specified.") unless($self->smsDlvtime);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->smsDlvtime 0 0 0
135 }
136  
137 # Encoding can be now/dlv
138 0 ▶ $self->_croak("Invalid encoding")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->smsEncode =~ /^(BIG5|ASCII)$/ 0 0 0
139 unless($self->smsEncode =~ /^(BIG5|ASCII)$/);
140  
141 # Append the additional arguments
142 0 ▶ if(defined $self->{"_dlvdatetime"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_dlvdatetime"}) 0 0 0
143 0 ▶ if (my ($year,$month,$day,$hour,$min) = $self->{"_dlvdatetime"} =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->{'_dlvdatetime'} =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/) { } 0 0 0
144 # Check Date
145 0 ▶ $self->_croak("Delivery date is incorrect.")
Branch: 1 branch
BranchTrueFalseTotal
unless check_date($year, $month, $day) 0 0 0
146 unless(check_date($year,$month,$day));
147 # Check Time
148 0 ▶ $self->_croak("Delivery time is incorrect.")
Branch: 1 branch
BranchTrueFalseTotal
unless check_time($hour, $min, undef) 0 0 0
149 unless(check_time($hour,$min,undef));
150 # Check least
151 0 my $now = sprintf("%04d%02d%02d%02d",Today_and_Now());
152 $self->_croak("Delivery time must earlier than now.")
153 0 ▶ unless($self->{"_dlvdatetime"} > $now);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_dlvdatetime"} > $now 0 0 0
154
155 0 foreach (qw/year month day hour minute/) {
156 0 $parms->{$_} = ${"$_"} ;
157 }
158 } else {
159 0 $self->_croak("Format of Delivery date is incorrect.");
160 }
161 }
162  
163 # Should be ok now, right? Let's send it!
164 # Login
165 0 $self->{"_ua"}->agent_alias('Windows IE 6');
166 0 $self->{"_ua"}->get($self->baseurl);
167 0 $self->{"_ua"}->form_number(1);
168 0 $self->{"_ua"}->field('smsid', $parms->{username});
169 0 $self->{"_ua"}->field('pwd', $parms->{password});
170 0 $self->{"_ua"}->submit();
171
172 # Input SMS_Message, Recipients
173 0 $self->{"_ua"}->form_number(2);
174 0 $self->{"_ua"}->field('InputMsg', $parms->{message});
175 0 $self->{"_ua"}->field('mobiles', $parms->{recipients});
176 0 $self->{"_ua"}->field('sendType', $parms->{sendType});
177 0 $self->{"_ua"}->field('longCount', scalar(@{$self->{"_recipients"}}));
178  
179 0 ▶ if($self->smsType eq '2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->smsType eq 2) 0 0 0
180 0 $self->{"_ua"}->select('year', ($parms->{year} - This_Year()));
181 0 $self->{"_ua"}->select('month', $parms->{month});
182 0 $self->{"_ua"}->select('day', $parms->{day});
183 0 $self->{"_ua"}->select('hour', $parms->{hour});
184 0 $self->{"_ua"}->select('minute', $parms->{minute});
185 }
186 0 $self->{"_ua"}->submit();
187  
188 # Input Authcode
189 0 $self->{"_ua"}->field('auth_code', $parms->{authcode});
190 0 $self->{"_ua"}->current_form()->action('https://ezpay.pchome.com.tw/auth_form_do');
191 0 $self->{"_ua"}->submit();
192  
193 0 ▶ if($self->{"_ua"}->success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_ua'}->success) { } 0 0 0
194 0 my $item = _parse_output($self->{"_ua"}->content);
195  
196 # Set the return info
197 0 $self->{"_resultcode"} = $item->{"resultcode"};
198 0 $self->{"_resultmessage"} = $item->{"resultmessage"};
199  
200 # Successful?
201 0 ▶ if($item->{"success"} eq 'false') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{'success'} eq 'false') { } 0 0 0
202 0 $self->{"_successcount"} = 0;
203 0 $self->{"_success"} = 0;
204 } else {
205 0 $self->{"_successcount"} = scalar(@{$self->{"_recipients"}});
206 0 $self->{"_success"} = 1;
207 }
208 } else {
209 0 $self->{"_resultcode"} = -999;
210 0 $self->{"_resultmessage"} = $self->{"_ua"}->status;
211 }
212 0 return $self->is_success;
213 }
214  
215  
216 ####################################################################
217 0 ▶sub _init {
Subroutine _init: not called CC=2 SCAR=17.9
218 0 my $self = shift;
219 0 my %params = @_;
220  
221 0 my $ua = WWW::Mechanize->new(
222 agent => __PACKAGE__." v. $VERSION",
223 );
224  
225 # Set/override defaults
226 0 my %options = (
227 ua => $ua,
228 baseurl => 'http://sms.pchome.com.tw/jsp/smslong.jsp',
229 username => undef, # ±b¸¹
230 password => undef, # ±K½X
231 authcode => undef, # Auth Code
232 recipients => [], # ¦¬°TªÌ
233 message => undef, # ²°T¤º®e
234  
235 dlvdatetime => undef, # ¹w¬ù®É¶¡ delivery date
236 sendType => '1', # 1 =>¥ß§Yµo°e, 2 => ¹w¬ùµo°e
237 encodeType => 'BIG5', # BIG5, ASCII
238  
239 success => undef, #
240 successcount => undef, #
241 resultcode => undef, #
242 resultmessage => undef, #
243 %params,
244 );
245 0 $self->{"_$_"} = $options{$_} foreach(keys %options);
246 0 return $self;
247 }
248  
249 0 ▶sub _parse_output {
Subroutine _parse_output: not called CC=3 SCAR=24.8
250 0 my $input = shift;
251 0 ▶ return unless($input);
Branch: 1 branch
BranchTrueFalseTotal
unless $input 0 0 0
252 0 my $item = {};
253 0 my $html = HTML::TagParser->new($input);
254 0 my $list = [$html->getElementsByTagName( "td" )];
255
256 0 ▶ if ($list->[12]->innerText =~ m/®¥³ß±z¦©ÂI®ø¶O¦¨¥\\/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->[12]->innerText =~ /\256\245\263\337\261z\246\251\302I\256\370\266O\246\250\245\\/) { } 0 0 0
257 # success
258 0 $item->{"order_sn"} = $list->[17];#
259 0 $item->{"Consume_summary"} = $list->[19];#
260 0 $item->{"Trade_time"} = $list->[21];#
261 0 $item->{"Quota_originally"} = $list->[23];#
262 0 $item->{"Quota_consume"} = $list->[25];#
263 0 $item->{"Quota_surplus"} = $list->[27];#
264 0 $item->{"success"} = 'true';
265 0 $item->{"resultcode"} = 1;
266 0 $item->{"resultmessage"} = 'Send SMS from PChome is success';
267 } else {
268 0 $item->{"success"} = 'false';
269 0 $item->{"resultcode"} = -1;
270 0 $item->{"resultmessage"} = 'Username or Password or Auth Code is incorrect.';
271 }
272 0 return $item;
273 }
274  
275 0 ▶sub _croak {
Subroutine _croak: not called CC=1 SCAR=6.9
276 0 my ($self, @error) = @_;
277 0 Carp::croak(@error);
278 }
279 1 1;
280 __END__
↑ Summary