| 1 | package Net::APNS::Notification; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use Any::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use Net::SSLeay qw/die_now die_if_ssl_error/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use Socket; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use Encode qw(decode encode); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use JSON::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | our $VERSION = '0.0202'; | |||||||||
| 9 | |||||||||||
| 10 | 3 | has message => ( | |||||||||
| 11 | is => 'rw', | ||||||||||
| 12 | isa => 'Str', | ||||||||||
| 13 | default => '', | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | 3 | has badge => ( | |||||||||
| 17 | is => 'rw', | ||||||||||
| 18 | isa => 'Int', | ||||||||||
| 19 | default => 0, | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | 3 | has sound => ( | |||||||||
| 23 | is => 'rw', | ||||||||||
| 24 | isa => 'Str', | ||||||||||
| 25 | default => '' | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | has custom => ( | ||||||||||
| 29 | is => 'rw', | ||||||||||
| 30 | isa => 'HashRef', | ||||||||||
| 31 | 3 | ▶ | default => sub { {} } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 3 | ); | |||||||||
| 33 | |||||||||||
| 34 | has devicetoken => ( | ||||||||||
| 35 | is => 'rw', | ||||||||||
| 36 | isa => 'Str', | ||||||||||
| 37 | 2 | ▶ | trigger => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 38 | 2 | ▶ | if (@_ >= 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 2 | my $dt = $_[1]; | |||||||||
| 40 | 2 | $dt =~ s/\s//g; | |||||||||
| 41 | 2 | $_[0]->{devicetoken} = $dt; | |||||||||
| 42 | } | ||||||||||
| 43 | } | ||||||||||
| 44 | 3 | ); | |||||||||
| 45 | |||||||||||
| 46 | 3 | has cert => ( | |||||||||
| 47 | is => 'rw', | ||||||||||
| 48 | isa => 'Str', | ||||||||||
| 49 | required => 1, | ||||||||||
| 50 | ); | ||||||||||
| 51 | |||||||||||
| 52 | 3 | has key => ( | |||||||||
| 53 | is => 'rw', | ||||||||||
| 54 | isa => 'Str', | ||||||||||
| 55 | required => 1, | ||||||||||
| 56 | ); | ||||||||||
| 57 | |||||||||||
| 58 | 3 | has passwd => ( | |||||||||
| 59 | is => 'rw', | ||||||||||
| 60 | ); | ||||||||||
| 61 | |||||||||||
| 62 | 1 | ▶ | sub type_pem { &Net::SSLeay::FILETYPE_PEM } | ||||||||
|
Subroutine
type_pem: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub _apple_serv_params { | ||||||||
|
Subroutine
_apple_serv_params: not called CC=1 SCAR=6.9
| |||||||||||
| 65 | 0 | my $self = $_[0]; | |||||||||
| 66 | 0 | return sockaddr_in( $self->port, inet_aton( $self->host ) ); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub host { | ||||||||
|
Subroutine
host: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 70 | 0 | my $self = $_[0]; | |||||||||
| 71 | 0 | ▶ | return 'gateway.' . | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | ($self->sandbox ? 'sandbox.' : '') . | ||||||||||
| 73 | 'push.apple.com'; | ||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 3 | has port => ( | |||||||||
| 77 | is => 'rw', | ||||||||||
| 78 | isa => 'Int', | ||||||||||
| 79 | default => 2195, | ||||||||||
| 80 | ); | ||||||||||
| 81 | |||||||||||
| 82 | 3 | has sandbox => ( | |||||||||
| 83 | is => 'rw', | ||||||||||
| 84 | isa => 'Bool', | ||||||||||
| 85 | default => 0, | ||||||||||
| 86 | ); | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub _message_encode { | ||||||||
|
Subroutine
_message_encode: not called CC=1 SCAR=6.9
| |||||||||||
| 89 | 0 | my $self = shift; | |||||||||
| 90 | 0 | return encode( 'unicode', decode( 'utf8', $self->message ) ); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub _pack_payload { | ||||||||
|
Subroutine
_pack_payload: not called CC=3 SCAR=24.8
| |||||||||||
| 94 | 0 | my $self = shift; | |||||||||
| 95 | |||||||||||
| 96 | 0 | my $data = { | |||||||||
| 97 | aps => { | ||||||||||
| 98 | alert => $self->_message_encode, | ||||||||||
| 99 | badge => $self->badge | ||||||||||
| 100 | } | ||||||||||
| 101 | }; | ||||||||||
| 102 | |||||||||||
| 103 | 0 | ▶ | if (length $self->sound) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | $data->{aps}->{sound} = $self->sound; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | if (scalar keys %{$self->custom} > 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 0 | $data->{custom} = $self->custom; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 0 | my $jsonxs = JSON::XS->new->utf8(1)->encode($data); | |||||||||
| 112 | 0 | $jsonxs =~ s/("badge":)"([^"]+)"/$1$2/; | |||||||||
| 113 | return | ||||||||||
| 114 | 0 | chr(0) | |||||||||
| 115 | . pack( 'n', 32 ) | ||||||||||
| 116 | . pack( 'H*', $self->devicetoken ) | ||||||||||
| 117 | . pack( 'n', length($jsonxs) ) | ||||||||||
| 118 | . $jsonxs; | ||||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 0 | ▶ | sub write { | ||||||||
|
Subroutine
write: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||
| 122 | 0 | my ( $self, $args ) = @_; | |||||||||
| 123 | |||||||||||
| 124 | 0 | ▶ | if ( $args->{devicetoken} ) { $self->devicetoken( $args->{devicetoken} ); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 0 | ▶ | if ( $args->{message} ) { $self->message( $args->{message} ); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | ▶ | if ( $args->{badge} ) { $self->badge( $args->{badge} ); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | 0 | ▶ | if ( $args->{sound} ) { $self->sound( $args->{sound} ); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | ▶ | if ( $args->{custom} ) { $self->custom( $args->{custom} ); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | |||||||||||
| 130 | 0 | $Net::SSLeay::trace = 4; | |||||||||
| 131 | 0 | $Net::SSLeay::ssl_version = 10; | |||||||||
| 132 | |||||||||||
| 133 | 0 | Net::SSLeay::load_error_strings(); | |||||||||
| 134 | 0 | Net::SSLeay::SSLeay_add_ssl_algorithms(); | |||||||||
| 135 | 0 | Net::SSLeay::randomize(); | |||||||||
| 136 | |||||||||||
| 137 | 0 | my $socket; | |||||||||
| 138 | 0 | ▶ | socket( $socket, PF_INET, SOCK_STREAM, getprotobyname('tcp') ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | or die "socket: $!"; | ||||||||||
| 140 | 0 | ▶ | connect( $socket, $self->_apple_serv_params ) or die "Connect: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | my $ctx = Net::SSLeay::CTX_new() or die_now("Failed to create SSL_CTX $!."); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 0 | Net::SSLeay::CTX_set_options( $ctx, &Net::SSLeay::OP_ALL ); | |||||||||
| 144 | 0 | die_if_ssl_error("ssl ctx set options"); | |||||||||
| 145 | |||||||||||
| 146 | 0 | ▶ | Net::SSLeay::CTX_set_default_passwd_cb( $ctx, sub { $self->passwd } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 147 | 0 | Net::SSLeay::CTX_use_RSAPrivateKey_file( $ctx, $self->key, $self->type_pem ); | |||||||||
| 148 | 0 | die_if_ssl_error("private key"); | |||||||||
| 149 | |||||||||||
| 150 | 0 | Net::SSLeay::CTX_use_certificate_file( $ctx, $self->cert, $self->type_pem ); | |||||||||
| 151 | 0 | die_if_ssl_error("certificate"); | |||||||||
| 152 | |||||||||||
| 153 | 0 | my $ssl = Net::SSLeay::new($ctx); | |||||||||
| 154 | 0 | Net::SSLeay::set_fd( $ssl, fileno($socket) ); | |||||||||
| 155 | 0 | ▶ | Net::SSLeay::connect($ssl) or die_now("Failed SSL connect ($!)"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | 0 | Net::SSLeay::write( $ssl, $self->_pack_payload ); | |||||||||
| 157 | 0 | CORE::shutdown( $socket, 1 ); | |||||||||
| 158 | 0 | Net::SSLeay::free($ssl); | |||||||||
| 159 | 0 | Net::SSLeay::CTX_free($ctx); | |||||||||
| 160 | 0 | close($socket); | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||
| 164 | |||||||||||
| 165 | __END__ | ||||||||||