| 1 | package SMS::Send::TW::chtsns; | ||||||||||||
| 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 IO::Socket; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Switch; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use Text::Iconv; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use base 'SMS::Send::Driver'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use vars qw{$VERSION}; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | $VERSION = '0.03'; | |||||||||||
| 14 | } | ||||||||||||
| 15 | |||||||||||||
| 16 | 1 | ▶ | use constant { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 1 | SENDBUF_SIZE => 217, | |||||||||||
| 18 | RECVBUF_SIZE => 189, | ||||||||||||
| 19 | TYPE_SERV_CHECK => 0, | ||||||||||||
| 20 | TYPE_SERV_EDIT_PASSWD => 1, | ||||||||||||
| 21 | TYPE_SERV_SEND => 2, | ||||||||||||
| 22 | TYPE_SERV_QUERY => 3, | ||||||||||||
| 23 | TYPE_SERV_GET => 4, | ||||||||||||
| 24 | TYPE_SERV_SEND_WITH_UDHI => 6, | ||||||||||||
| 25 | TRAN_SEND_NOW => 100, | ||||||||||||
| 26 | TRAN_SEND_ORDER => 101, | ||||||||||||
| 27 | # Uncodumented coding | ||||||||||||
| 28 | CODING_ASCII => 0x00, | ||||||||||||
| 29 | CODING_BIG5 => 0x01, | ||||||||||||
| 30 | CODING_BINARY => 0xf5, | ||||||||||||
| 31 | CODING_UNICODE => 0x08, | ||||||||||||
| 32 | SMS_SERVER_IP => '203.66.172.133', | ||||||||||||
| 33 | SMS_SERVER_PORT => '8001', | ||||||||||||
| 34 | 1 | }; | |||||||||||
| 35 | |||||||||||||
| 36 | # Preloaded methods go here. | ||||||||||||
| 37 | |||||||||||||
| 38 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||
| 39 | 0 | my ($class, %params) = @_; | |||||||||||
| 40 | 0 | my $agent; | |||||||||||
| 41 | my $conn; | ||||||||||||
| 42 | |||||||||||||
| 43 | 0 | foreach(qw/username password/) { | |||||||||||
| 44 | 0 | ▶ | Carp::croak("No $_ specified") unless(defined $params{"_$_"}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | } | ||||||||||||
| 46 | 0 | ▶ | $params{'host_ip'} = $params{'_host_ip'} || SMS_SERVER_IP; | ||||||||||
|
Condition0%0/2
$params{'_host_ip'} || '203.66.172.133'
MC/DC0%0/1
$params{'_host_ip'} || '203.66.172.133'
$params{'_host_ip'}
Truth table0%0/2
A
A= $params{'_host_ip'}
| |||||||||||||
| 47 | 0 | ▶ | $params{'host_port'} = $params{'_host_port'} || SMS_SERVER_PORT; | ||||||||||
|
Condition0%0/2
$params{'_host_port'} || '8001'
MC/DC0%0/1
$params{'_host_port'} || '8001'
$params{'_host_port'}
Truth table0%0/2
A
A= $params{'_host_port'}
| |||||||||||||
| 48 | 0 | ▶ | $params{'proxy_host'} = $params{'_proxy_host'} if (defined($params{'_proxy_host'})); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 0 | ▶ | $params{'proxy_port'} = $params{'_proxy_port'} if (defined($params{'_proxy_port'})); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 0 | ▶ | $params{'proxy_type'} = $params{'_proxy_type'} if (defined($params{'_proxy_type'})); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | |||||||||||||
| 52 | 0 | ▶ | if (defined($params{'proxy_type'})) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 0 | foreach(qw/proxy_host proxy_port/) { | |||||||||||
| 54 | 0 | ▶ | Carp::croak("No $_ specified") unless(defined $params{"$_"}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | } | ||||||||||||
| 56 | 0 | switch($params{'proxy_type'}) { | |||||||||||
| 57 | 0 | ▶ | case 'http' { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | $agent = LWP::UserAgent->new(keep_alive => 1); | |||||||||||
| 59 | $agent->proxy( | ||||||||||||
| 60 | 0 | http => "http://" . $params{'proxy_host'}. ':' . $params{'proxy_port'} . "/"); | |||||||||||
| 61 | my $req = HTTP::Request->new( | ||||||||||||
| 62 | 0 | CONNECT => "http://" . $params{'host_ip'} . ':' . $params{'host_port'}. "/"); | |||||||||||
| 63 | 0 | my $res = $agent->request($req); | |||||||||||
| 64 | |||||||||||||
| 65 | 0 | ▶ | Carp::croak($res->status_line()) unless $res->is_success(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 0 | $conn = $res->{client_socket}; | |||||||||||
| 67 | 0 | } | |||||||||||
| 68 | 0 | } | |||||||||||
| 69 | } else { | ||||||||||||
| 70 | 0 | $conn = IO::Socket::INET->new($params{'host_ip'} . ':' . $params{'host_port'}); | |||||||||||
| 71 | |||||||||||||
| 72 | 0 | ▶ | Carp::croak('Not able to connect to CHT SMS Server') unless $conn->connected; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | 0 | $params{'conn'} = $conn; | |||||||||||
| 76 | 0 | ▶ | $params{'agent'} = $agent if (defined($agent)); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 0 | $params{'auth'} = 0; | |||||||||||
| 78 | |||||||||||||
| 79 | 0 | my $self = bless { %params }, $class; | |||||||||||
| 80 | |||||||||||||
| 81 | 0 | return $self; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 0 | ▶ | sub _send_packet { | ||||||||||
|
Subroutine
_send_packet: not called CC=7 SCAR=40.3
| |||||||||||||
| 85 | 0 | my $self = shift; | |||||||||||
| 86 | 0 | my %params = @_; | |||||||||||
| 87 | |||||||||||||
| 88 | 0 | my $send_buf = ''; | |||||||||||
| 89 | 0 | my $tmp; | |||||||||||
| 90 | 0 | my $zero_buf = "\0"x230; | |||||||||||
| 91 | |||||||||||||
| 92 | 0 | my $conn = $self->{'conn'}; | |||||||||||
| 93 | |||||||||||||
| 94 | 0 | foreach (qw/type/) { | |||||||||||
| 95 | 0 | ▶ | Carp::croak("No $_ specified") unless(defined $params{"$_"}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | 0 | foreach (qw/coding length tran_type/) { | |||||||||||
| 99 | 0 | ▶ | $params{"_$_"} = 0 unless (defined $params{"$_"}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 0 | foreach (qw/pchID pchPasswd pchMsisdn pchMessageID pchMessage pchSendTime/) { | |||||||||||
| 103 | 0 | ▶ | $params{"_$_"} = '' unless (defined $params{"$_"}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | $send_buf = pack("CCCCZ9Z9Z13Z9a160Z13", | ||||||||||||
| 107 | $params{'type'}, | ||||||||||||
| 108 | $params{'coding'}, | ||||||||||||
| 109 | $params{'length'}, | ||||||||||||
| 110 | $params{'tran_type'}, | ||||||||||||
| 111 | $params{'pchID'}, | ||||||||||||
| 112 | $params{'pchPasswd'}, | ||||||||||||
| 113 | $params{'pchMsisdn'}, | ||||||||||||
| 114 | $params{'pchMessageID'}, | ||||||||||||
| 115 | $params{'pchMessage'}, | ||||||||||||
| 116 | 0 | $params{'pchSendTime'} | |||||||||||
| 117 | ); | ||||||||||||
| 118 | |||||||||||||
| 119 | # Fill 0... | ||||||||||||
| 120 | 0 | $send_buf = pack("a" . SENDBUF_SIZE, $send_buf); | |||||||||||
| 121 | |||||||||||||
| 122 | # Send to server | ||||||||||||
| 123 | 0 | $conn->syswrite($send_buf, SENDBUF_SIZE); | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | 0 | ▶ | sub _recv_packet { | ||||||||||
|
Subroutine
_recv_packet: not called CC=5 SCAR=34.0
| |||||||||||||
| 127 | 0 | my $self = shift; | |||||||||||
| 128 | |||||||||||||
| 129 | 0 | my $recv_buf = ''; | |||||||||||
| 130 | 0 | my $tmp; | |||||||||||
| 131 | my %ret; | ||||||||||||
| 132 | |||||||||||||
| 133 | 0 | my $conn = $self->{'conn'}; | |||||||||||
| 134 | |||||||||||||
| 135 | 0 | $conn->sysread($recv_buf, RECVBUF_SIZE); | |||||||||||
| 136 | |||||||||||||
| 137 | # unpack | ||||||||||||
| 138 | ($ret{'code'}, $ret{'coding'}, $ret{'length'}, $ret{'send_msisdn'}, $ret{'recv_msisdn'}, | ||||||||||||
| 139 | 0 | $ret{'buffer'}) = unpack("cccZ13Z13a160", $recv_buf); | |||||||||||
| 140 | |||||||||||||
| 141 | # Convert Message to UTF-8 | ||||||||||||
| 142 | 0 | my $from_code; | |||||||||||
| 143 | 0 | switch($ret{'coding'}) { | |||||||||||
| 144 | 0 | ▶ | case CODING_ASCII { $ret{'msg'} = $ret{'buffer'}; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | 0 | ▶ | case CODING_BIG5 { $from_code = 'big5'; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 146 | 0 | ▶ | case CODING_UNICODE { $from_code = 'ucs-2'; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 147 | 0 | } | |||||||||||
| 148 | 0 | ▶ | if (defined($from_code)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | 0 | my $converter = Text::Iconv->new($from_code, "utf-8"); | |||||||||||
| 150 | 0 | $ret{'msg'} = $converter->convert($ret{'buffer'}); | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | 0 | return %ret; | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | 0 | ▶ | sub send_sms { | ||||||||||
|
Subroutine
send_sms: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 157 | 0 | my $self = shift; | |||||||||||
| 158 | 0 | my %params = @_; | |||||||||||
| 159 | |||||||||||||
| 160 | 0 | my %ret; | |||||||||||
| 161 | |||||||||||||
| 162 | # Get the message and destination | ||||||||||||
| 163 | 0 | my $message = $self->_MESSAGE( $params{text} ); | |||||||||||
| 164 | 0 | my $recipient = $self->_TO( delete $params{to} ); | |||||||||||
| 165 | 0 | my $conn = $self->{'conn'}; | |||||||||||
| 166 | |||||||||||||
| 167 | # Login | ||||||||||||
| 168 | 0 | ▶ | if (not $self->{'auth'}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 169 | $self->_send_packet( | ||||||||||||
| 170 | 'type' => TYPE_SERV_CHECK, | ||||||||||||
| 171 | 'pchID' => $self->{'_username'}, | ||||||||||||
| 172 | 0 | 'pchPasswd' => $self->{'_password'} | |||||||||||
| 173 | ); | ||||||||||||
| 174 | 0 | %ret = $self->_recv_packet(); | |||||||||||
| 175 | 0 | ▶ | Carp::croak($ret{'buffer'}) unless(0 == $ret{'code'}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 176 | 0 | $self->{'auth'} = 1; | |||||||||||
| 177 | } | ||||||||||||
| 178 | |||||||||||||
| 179 | $self->_send_packet( | ||||||||||||
| 180 | 0 | 'type' => TYPE_SERV_SEND, | |||||||||||
| 181 | 'coding' => CODING_UNICODE, | ||||||||||||
| 182 | 'pchMsisdn' => $recipient, | ||||||||||||
| 183 | 'pchMessage' => $message, | ||||||||||||
| 184 | 'length' => length($message), | ||||||||||||
| 185 | 'tran_type' => TRAN_SEND_NOW | ||||||||||||
| 186 | ); | ||||||||||||
| 187 | 0 | %ret = $self->_recv_packet(); | |||||||||||
| 188 | |||||||||||||
| 189 | 0 | return %ret; | |||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | 0 | ▶ | sub _MESSAGE { | ||||||||||
|
Subroutine
_MESSAGE: not called CC=3 SCAR=24.8
| |||||||||||||
| 193 | |||||||||||||
| 194 | 0 | ▶ | my $class = ref $_[0] ? ref shift : shift; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 195 | 0 | my $message = shift; | |||||||||||
| 196 | 0 | my $converter = Text::Iconv->new("utf-8", "ucs-2"); | |||||||||||
| 197 | 0 | ▶ | unless ( length($message) <= 160 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 198 | 0 | Carp::croak("Message length limit is 160 characters"); | |||||||||||
| 199 | } | ||||||||||||
| 200 | |||||||||||||
| 201 | 0 | return $converter->convert($message); | |||||||||||
| 202 | } | ||||||||||||
| 203 | |||||||||||||
| 204 | 0 | ▶ | sub _TO { | ||||||||||
|
Subroutine
_TO: not called CC=2 SCAR=17.9
| |||||||||||||
| 205 | 0 | ▶ | my $class = ref $_[0] ? ref shift : shift; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 206 | 0 | my $to = shift; | |||||||||||
| 207 | |||||||||||||
| 208 | # International numbers need their + removed | ||||||||||||
| 209 | 0 | $to =~ y/0123456789//cd; | |||||||||||
| 210 | |||||||||||||
| 211 | 0 | return $to; | |||||||||||
| 212 | } | ||||||||||||
| 213 | 1 | 1; | |||||||||||
| 214 | =head1 NAME | ||||||||||||
| 215 | |||||||||||||
| 216 | SMS::Send::TW::chtsns - SMS::Send driver for SNS Service of CHT | ||||||||||||
| 217 | |||||||||||||
| 218 | =head1 SYNOPSIS | ||||||||||||
| 219 | |||||||||||||
| 220 | use SMS::send; | ||||||||||||
| 221 | |||||||||||||
| 222 | my $sender = SMS::Send->new('TW::chtsns', | ||||||||||||
| 223 | _username => 'UserName', | ||||||||||||
| 224 | _password => 'Password', | ||||||||||||
| 225 | _proxy_host => 'proxy.to.specified', | ||||||||||||
| 226 | _proxy_port => 3128, | ||||||||||||
| 227 | _proxy_type => 'http', | ||||||||||||
| 228 | ); | ||||||||||||
| 229 | |||||||||||||
| 230 | my $sent = $sender->send_sms( | ||||||||||||
| 231 | text => 'My very urgent message', | ||||||||||||
| 232 | to => '0912345678', | ||||||||||||
| 233 | ); | ||||||||||||
| 234 | |||||||||||||
| 235 | =head1 DESCRIPTION | ||||||||||||
| 236 | |||||||||||||
| 237 | SMS::Send::TW::chtsns is a SMS::Send driver which allows you to send messages through L<http://203.66.172.129/sns/sns_help.htm/>. | ||||||||||||
| 238 | |||||||||||||
| 239 | =head1 METHODS | ||||||||||||
| 240 | |||||||||||||
| 241 | =head2 new | ||||||||||||
| 242 | |||||||||||||
| 243 | The C<new> method takes a few parameters. C<_username> and C<_password> > | ||||||||||||
| 244 | are mandatory. | ||||||||||||
| 245 | |||||||||||||
| 246 | =head2 send_sms | ||||||||||||
| 247 | |||||||||||||
| 248 | Takes C<to> as recipient phonenumber, and C<text> as the utf-8 encoded text | ||||||||||||
| 249 | that's supposed to be delivered. | ||||||||||||
| 250 | |||||||||||||
| 251 | =head1 SEE ALSO | ||||||||||||
| 252 | |||||||||||||
| 253 | =over 5 | ||||||||||||
| 254 | |||||||||||||
| 255 | =item * L<Send::SMS> | ||||||||||||
| 256 | |||||||||||||
| 257 | =item * L<IO::Socket> | ||||||||||||
| 258 | |||||||||||||
| 259 | =item * L<LWP::UserAgent> | ||||||||||||
| 260 | |||||||||||||
| 261 | =head1 AUTHOR | ||||||||||||
| 262 | |||||||||||||
| 263 | Jui-Nan Lin, E<lt>jnlin@csie.nctu.edu.twE<gt> | ||||||||||||
| 264 | |||||||||||||
| 265 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 266 | |||||||||||||
| 267 | Copyright (C) 2010 by Jui-Nan Lin | ||||||||||||
| 268 | |||||||||||||
| 269 | This library is free software; you can redistribute it and/or modify | ||||||||||||
| 270 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||
| 271 | at your option, any later version of Perl 5 you may have available. | ||||||||||||
| 272 | |||||||||||||
| 273 | |||||||||||||
| 274 | =cut | ||||||||||||