line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::SMS::PChome; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
20034
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
91
|
|
5
|
1
|
|
|
1
|
|
1085
|
use WWW::Mechanize; |
|
1
|
|
|
|
|
188384
|
|
|
1
|
|
|
|
|
42
|
|
6
|
1
|
|
|
1
|
|
1027
|
use HTML::TagParser; |
|
1
|
|
|
|
|
3118
|
|
|
1
|
|
|
|
|
30
|
|
7
|
1
|
|
|
1
|
|
714
|
use Date::Calc qw(check_date check_time Today_and_Now This_Year); |
|
1
|
|
|
|
|
30398
|
|
|
1
|
|
|
|
|
1878
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
10
|
|
|
|
|
|
|
our (@ISA) = qw(Exporter); |
11
|
|
|
|
|
|
|
our (@EXPORT) = qw(send_sms); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
0
|
|
|
0
|
1
|
|
my ($class, %params) = @_; |
15
|
0
|
|
|
|
|
|
my $self = {}; |
16
|
0
|
|
|
|
|
|
bless $self, $class; |
17
|
0
|
0
|
|
|
|
|
$self->_init(%params) or return undef; |
18
|
0
|
|
|
|
|
|
return $self; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub send_sms { |
22
|
0
|
|
|
0
|
0
|
|
return __PACKAGE__->new( |
23
|
|
|
|
|
|
|
username => $_[0], |
24
|
|
|
|
|
|
|
password => $_[1], |
25
|
|
|
|
|
|
|
authcode => $_[2], |
26
|
|
|
|
|
|
|
recipients=> [$_[3]], |
27
|
|
|
|
|
|
|
)->smsSend($_[4]); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub baseurl { |
31
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
32
|
0
|
0
|
|
|
|
|
if (@_) { $self->{"_baseurl"} = shift } |
|
0
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
return $self->{"_baseurl"}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub username { |
37
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
38
|
0
|
0
|
|
|
|
|
if (@_) { $self->{"_username"} = shift } |
|
0
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
return $self->{"_username"}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub password { |
43
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
44
|
0
|
0
|
|
|
|
|
if (@_) { $self->{"_password"} = shift } |
|
0
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
return $self->{"_password"}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub authcode { |
49
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
50
|
0
|
0
|
|
|
|
|
if (@_) { $self->{"_authcode"} = shift } |
|
0
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
return $self->{"_authcode"}; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub login { |
55
|
0
|
|
|
0
|
1
|
|
my ($self, $user, $pass, $auth) = @_; |
56
|
0
|
0
|
|
|
|
|
$self->username($user) if($user); |
57
|
0
|
0
|
|
|
|
|
$self->password($pass) if($pass); |
58
|
0
|
0
|
|
|
|
|
$self->authcode($auth) if($auth); |
59
|
0
|
|
|
|
|
|
return ($self->username, $self->password, $self->authcode); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub smsRecipient { |
63
|
0
|
|
|
0
|
1
|
|
my ($self, $recip) = @_; |
64
|
0
|
0
|
|
|
|
|
push @{$self->{"_recipients"}}, $recip if($recip); |
|
0
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
return $self->{"_recipients"}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub smsMessage { |
69
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
70
|
0
|
0
|
|
|
|
|
if (@_) { $self->{"_message"} = shift } |
|
0
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return $self->{"_message"}; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub smsDeliverydate { |
75
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
76
|
0
|
0
|
|
|
|
|
if (@_) { $self->{"_dlvdatetime"} = shift } |
|
0
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
return $self->{"_dlvdatetime"}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub smsType { |
81
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
82
|
0
|
0
|
|
|
|
|
if (@_) { $self->{"_sendType"} = shift } |
|
0
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
return $self->{"_sendType"}; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub smsEncode { |
87
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
88
|
0
|
0
|
|
|
|
|
if (@_) { $self->{"_encodeType"} = shift } |
|
0
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return $self->{"_encodeType"}; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub is_success { |
93
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
94
|
0
|
|
|
|
|
|
return $self->{"_success"}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub successcount { |
98
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
99
|
0
|
|
|
|
|
|
return $self->{"_successcount"}; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub resultcode { |
103
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
104
|
0
|
|
|
|
|
|
return $self->{"_resultcode"}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub resultmessage { |
108
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
109
|
0
|
|
|
|
|
|
return $self->{"_resultmessage"}; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub smsSend { |
114
|
0
|
|
|
0
|
1
|
|
my ($self, $message) = @_; |
115
|
0
|
0
|
|
|
|
|
$self->smsMessage($message) if($message); |
116
|
0
|
|
|
|
|
|
my $parms = {}; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
#### Check for mandatory input |
119
|
0
|
|
|
|
|
|
foreach(qw/username password authcode recipients message sendType encodeType/) { |
120
|
0
|
0
|
|
|
|
|
$self->_croak("$_ not specified.") unless(defined $self->{"_$_"}); |
121
|
0
|
0
|
|
|
|
|
if($_ eq 'recipients') { |
122
|
0
|
|
|
|
|
|
$parms->{$_} = join(";", @{$self->{"_$_"}}); |
|
0
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
} else { |
124
|
0
|
|
|
|
|
|
$parms->{$_} = $self->{"_$_"}; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Type can be now/dlv |
129
|
0
|
0
|
|
|
|
|
$self->_croak("Invalid type") |
130
|
|
|
|
|
|
|
unless($self->smsType =~ /^[12]$/); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# delivery? We must have a Date that format: YYYYMMDDHHmm (example:200606130830) |
133
|
0
|
0
|
|
|
|
|
if($self->smsType eq '2') { |
134
|
0
|
0
|
|
|
|
|
$self->_croak("No delivery date specified.") unless($self->smsDlvtime); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Encoding can be now/dlv |
138
|
0
|
0
|
|
|
|
|
$self->_croak("Invalid encoding") |
139
|
|
|
|
|
|
|
unless($self->smsEncode =~ /^(BIG5|ASCII)$/); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Append the additional arguments |
142
|
0
|
0
|
|
|
|
|
if(defined $self->{"_dlvdatetime"}) { |
143
|
0
|
0
|
|
|
|
|
if (my ($year,$month,$day,$hour,$min) = $self->{"_dlvdatetime"} =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/) { |
144
|
|
|
|
|
|
|
# Check Date |
145
|
0
|
0
|
|
|
|
|
$self->_croak("Delivery date is incorrect.") |
146
|
|
|
|
|
|
|
unless(check_date($year,$month,$day)); |
147
|
|
|
|
|
|
|
# Check Time |
148
|
0
|
0
|
|
|
|
|
$self->_croak("Delivery time is incorrect.") |
149
|
|
|
|
|
|
|
unless(check_time($hour,$min,undef)); |
150
|
|
|
|
|
|
|
# Check least |
151
|
0
|
|
|
|
|
|
my $now = sprintf("%04d%02d%02d%02d",Today_and_Now()); |
152
|
0
|
0
|
|
|
|
|
$self->_croak("Delivery time must earlier than now.") |
153
|
|
|
|
|
|
|
unless($self->{"_dlvdatetime"} > $now); |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
foreach (qw/year month day hour minute/) { |
156
|
0
|
|
|
|
|
|
$parms->{$_} = ${"$_"} ; |
|
0
|
|
|
|
|
|
|
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"}})); |
|
0
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
|
179
|
0
|
0
|
|
|
|
|
if($self->smsType eq '2') { |
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
|
0
|
|
|
|
|
if($self->{"_ua"}->success()) { |
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
|
0
|
|
|
|
|
if($item->{"success"} eq 'false') { |
202
|
0
|
|
|
|
|
|
$self->{"_successcount"} = 0; |
203
|
0
|
|
|
|
|
|
$self->{"_success"} = 0; |
204
|
|
|
|
|
|
|
} else { |
205
|
0
|
|
|
|
|
|
$self->{"_successcount"} = scalar(@{$self->{"_recipients"}}); |
|
0
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub _init { |
218
|
0
|
|
|
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, # 帳號 |
230
|
|
|
|
|
|
|
password => undef, # 密碼 |
231
|
|
|
|
|
|
|
authcode => undef, # Auth Code |
232
|
|
|
|
|
|
|
recipients => [], # 收訊者 |
233
|
|
|
|
|
|
|
message => undef, # 簡訊內容 |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
dlvdatetime => undef, # 預約時間 delivery date |
236
|
|
|
|
|
|
|
sendType => '1', # 1 =>立即發送, 2 => 預約發送 |
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
|
|
|
|
|
|
|
sub _parse_output { |
250
|
0
|
|
|
0
|
|
|
my $input = shift; |
251
|
0
|
0
|
|
|
|
|
return unless($input); |
252
|
0
|
|
|
|
|
|
my $item = {}; |
253
|
0
|
|
|
|
|
|
my $html = HTML::TagParser->new($input); |
254
|
0
|
|
|
|
|
|
my $list = [$html->getElementsByTagName( "td" )]; |
255
|
|
|
|
|
|
|
|
256
|
0
|
0
|
|
|
|
|
if ($list->[12]->innerText =~ m/恭喜您扣點消費成功\/) { |
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
|
|
|
|
|
|
|
sub _croak { |
276
|
0
|
|
|
0
|
|
|
my ($self, @error) = @_; |
277
|
0
|
|
|
|
|
|
Carp::croak(@error); |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
1; |
280
|
|
|
|
|
|
|
__END__ |