blib/lib/Growl/GNTP.pm

filter: Statementstmt 15.9% 47 / 295 Branchbran 0.6% 1 / 154 Conditioncond 13.2% 11 / 83 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 32.1% 9 / 28 Podpod 100% 5 / 5 totaltotal 12.0% 73 / 607 CC 124 SCAR 65.5
CC
124
Cov
11%
notify
1722.0
subscribe
756.0
wait
380.0
CRAP 696.8
↑ Summary
1 package Growl::GNTP;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Crypt::CBC;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Digest::SHA qw/sha1_hex sha256_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our $VERSION = '0.21';
11  
12 1 ▶sub new {
Subroutine new: called CC=11 SCAR=26.3
Pod documented
13 1 my $class = shift;
14 1 my %args = @_;
15 1 ▶ $args{Proto} ||= 'tcp';
Condition50%1/2
$args{"Proto"} ||= "tcp"
l!l
01
MC/DC0%0/1
$args{"Proto"} ||= "tcp"
$args{"Proto"}
Truth table50%1/2
A
A= $args{"Proto"}
Aresult
11
00
16 1 ▶ $args{PeerHost} ||= 'localhost';
Condition50%1/2
$args{"PeerHost"} ||= "localhost"
l!l
01
MC/DC0%0/1
$args{"PeerHost"} ||= "localhost"
$args{"PeerHost"}
Truth table50%1/2
A
A= $args{"PeerHost"}
Aresult
11
00
17 1 ▶ $args{PeerPort} ||= 23053;
Condition50%1/2
$args{"PeerPort"} ||= 23053
l!l
01
MC/DC0%0/1
$args{"PeerPort"} ||= 23053
$args{"PeerPort"}
Truth table50%1/2
A
A= $args{"PeerPort"}
Aresult
11
00
18 1 ▶ $args{Timeout} ||= 5;
Condition50%1/2
$args{"Timeout"} ||= 5
l!l
01
MC/DC0%0/1
$args{"Timeout"} ||= 5
$args{"Timeout"}
Truth table50%1/2
A
A= $args{"Timeout"}
Aresult
11
00
19 1 ▶ $args{AppName} ||= 'Growl::GNTP';
Condition50%1/2
$args{"AppName"} ||= "Growl::GNTP"
l!l
10
MC/DC0%0/1
$args{"AppName"} ||= "Growl::GNTP"
$args{"AppName"}
Truth table50%1/2
A
A= $args{"AppName"}
Aresult
11
00
20 1 ▶ $args{AppIcon} ||= '';
Condition50%1/2
$args{"AppIcon"} ||= ""
l!l
01
MC/DC0%0/1
$args{"AppIcon"} ||= ""
$args{"AppIcon"}
Truth table50%1/2
A
A= $args{"AppIcon"}
Aresult
11
00
21 1 ▶ $args{Password} ||= '';
Condition50%1/2
$args{"Password"} ||= ""
l!l
01
MC/DC0%0/1
$args{"Password"} ||= ""
$args{"Password"}
Truth table50%1/2
A
A= $args{"Password"}
Aresult
11
00
22 1 ▶ $args{PasswordHashAlgorithm} ||= 'MD5';
Condition50%1/2
$args{"PasswordHashAlgorithm"} ||= "MD5"
l!l
01
MC/DC0%0/1
$args{"PasswordHashAlgorithm"} ||= "MD5"
$args{"PasswordHashAlgorithm"}
Truth table50%1/2
A
A= $args{"PasswordHashAlgorithm"}
Aresult
11
00
23 1 ▶ $args{EncryptAlgorithm} ||= 'NONE';
Condition50%1/2
$args{"EncryptAlgorithm"} ||= "NONE"
l!l
01
MC/DC0%0/1
$args{"EncryptAlgorithm"} ||= "NONE"
$args{"EncryptAlgorithm"}
Truth table50%1/2
A
A= $args{"EncryptAlgorithm"}
Aresult
11
00
24 1 ▶ $args{Debug} ||= 0;
Condition50%1/2
$args{"Debug"} ||= 0
l!l
01
MC/DC0%0/1
$args{"Debug"} ||= 0
$args{"Debug"}
Truth table50%1/2
A
A= $args{"Debug"}
Aresult
11
00
25 1 $args{Callbacks} = [];
26 1 srand();
27 1 bless {%args}, $class;
28 }
29  
30 1 ▶sub register {
Subroutine register: called CC=21 SCAR=57.6
Pod documented
31 1 my $self = shift;
32 1 ▶ my $notifications = shift || [];
Condition50%1/2
shift() || []
l!l
10
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
33  
34 1 my $AppName = $self->{AppName};
35 1 $AppName =~ s!\r\n!\n!;
36 1 my $AppIcon = $self->{AppIcon};
37 1 $AppIcon =~ s!\r\n!\n!;
38 1 my $count = scalar @$notifications;
39  
40 my $sock = IO::Socket::INET->new(
41 PeerAddr => $self->{PeerHost},
42 PeerPort => $self->{PeerPort},
43 Proto => $self->{Proto},
44 Timeout => $self->{Timeout},
45 1 );
46 1 ▶ die $@ unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 1 0 1
47  
48 0 my $identifier;
49 0 ▶ if (-f $AppIcon) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $AppIcon) 0 0 0
50 0 open my $f, "<:raw", $AppIcon;
51 0 $identifier = do { local $/; <$f> };
52 0 close $f;
53 0 $AppIcon = "x-growl-resource://" . Digest::MD5::md5_hex(Digest::MD5->new->add($identifier)->digest);
54 }
55  
56 0 my $form = <<EOF;
57 Application-Name: $AppName
58 Application-Icon: $AppIcon
59 Notifications-Count: $count
60  
61 EOF
62 0 $form =~ s!\n!\r\n!g;
63  
64 0 $count = 0;
65 0 for my $notification ( @{$notifications} ) {
66 0 $count++;
67 my %data = (
68 Name => $notification->{Name} || "Growl::GNTP Notify$count",
69 DisplayName => $notification->{DisplayName}
70 || $notification->{Name} || "Growl::GNTP Notify$count",
71 Enabled => _translate_bool($notification->{Enabled} || 'True'),
72 0 ▶ Icon => $notification->{Icon} || '', # will default to Application-Icon if not specified.
Condition0%0/11
$notification->{'Name'} || "Growl::GNTP Notify$count"
l!l
00
$notification->{'DisplayName'} || $notification->{'Name'} || "Growl::GNTP Notify$count"
l!l
00
$notification->{"DisplayName"} or $notification->{"Name"}
l!l&&r!l&&!r
000
$notification->{'Enabled'} || 'True'
l!l
00
$notification->{'Icon'} || ''
l!l
00
MC/DC0%0/1
$notification->{'Name'} || "Growl::GNTP Notify$count"
$notification->{'Name'}
MC/DC0%0/2
$notification->{'DisplayName'} || $notification->{'Name'} || "Growl::GNTP Notify$count"
$notification->{"DisplayName"} $notification->{"Name"}
MC/DC0%0/1
$notification->{'Enabled'} || 'True'
$notification->{'Enabled'}
MC/DC0%0/1
$notification->{'Icon'} || ''
$notification->{'Icon'}
Truth table0%0/2
A
A= $notification->{'Name'}
Aresult
11
00
Truth table0%0/3
A or B
A= $notification->{"DisplayName"}B= $notification->{"Name"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $notification->{'Enabled'}
Aresult
11
00
Truth table0%0/2
A
A= $notification->{'Icon'}
Aresult
11
00
73 );
74 0 $data{$_} =~ s!\r\n!\n! for ( keys %data );
75  
76 0 my $subform .= <<EOF;
77 Notification-Name: \$(Name)
78 Notification-Display-Name: \$(DisplayName)
79 Notification-Enabled: \$(Enabled)
80 Notification-Icon: \$(Icon)
81  
82 EOF
83 0 $subform =~ s!\n!\r\n!g;
84 0 $subform =~ s/\$\((\w+)\)/$data{$1}/ge;
85 0 $form .= $subform;
86 }
87 0 ▶ if ($identifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($identifier) 0 0 0
88 0 $form.=sprintf("Identifier: %s\r\r\n",substr($AppIcon, 19));
89 0 $form.=sprintf("Length: %d\r\r\n\r\r\n",length $identifier);
90 0 $form =~ s!\r\r\n!\r\n!g;
91 0 $form .= $identifier;
92 0 $form .= "\r\n\r\n";
93 }
94  
95 0 ▶ print $form if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
96 0 $form = _gen_header($self, 'REGISTER', $form);
97 0 $sock->send($form);
98  
99 0 my $ret = <$sock>;
100 0 ▶ $ret = $1 if $ret =~ /^GNTP\/1\.0 -?(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ m[^GNTP/1\.0 -?(\w+)] 0 0 0
101 0 ▶ print "$_\n" if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
102  
103 0 my $description = 'failed to register';
104 0 ▶ if ($ret ne 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret ne "OK") 0 0 0
105 0 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 0 0 0
106 0 $_ =~ s!\r\n!!g;
107 0 ▶ print "$_\n" if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
108 0 ▶ $description = $1 if $_ =~ /^Error-Description:\s*(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Error-Description:\s*(.*)$/ 0 0 0
109 0 ▶ last if length($_) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ == 0 0 0 0
110 }
111 }
112 0 close $sock;
113  
114 0 ▶ die $description if $ret ne 'OK';
Branch: 1 branch
BranchTrueFalseTotal
if $ret ne "OK" 0 0 0
115 }
116  
117 0 ▶sub notify {
Subroutine notify: not called CC=41 SCAR=74.5
Pod documented
118 0 my ( $self, %args ) = @_;
119 my %data = (
120 AppName => $self->{AppName},
121 Name => $args{Name} || $args{Event} || '',
122 Title => $args{Title} || '',
123 Message => $args{Message} || '',#optional
124 Icon => $args{Icon} || '', #optional
125 ID => $args{ID} || '', # optional
126 CoalescingID => $args{CoalescingID} || '', # optional
127 Priority => _translate_int($args{Priority} || 0), #optional
128 Sticky => _translate_bool($args{Sticky} || 'False'), #optional
129 CallbackContext => $args{CallbackContext} || '',#optional
130 CallbackContextType => $args{CallbackContextType} || '',#optional, required if CallbackContext
131 CallbackTarget => $args{CallbackTarget} || '', #optional exclusive of CallbackContext[-Type] #!# for now, needs Context pair until GfW v2.0.0.20
132 CallbackFunction => $args{CallbackFunction} || {}, #optional
133 0 ▶ Custom => $args{Custom} || '', # optional
Condition0%0/29
$args{'Name'} || $args{'Event'} || ''
l!l
00
$args{"Name"} or $args{"Event"}
l!l&&r!l&&!r
000
$args{'Title'} || ''
l!l
00
$args{'Message'} || ''
l!l
00
$args{'Icon'} || ''
l!l
00
$args{'ID'} || ''
l!l
00
$args{'CoalescingID'} || ''
l!l
00
$args{'Priority'} || 0
l!l
00
$args{'Sticky'} || 'False'
l!l
00
$args{'CallbackContext'} || ''
l!l
00
$args{'CallbackContextType'} || ''
l!l
00
$args{'CallbackTarget'} || ''
l!l
00
$args{'CallbackFunction'} || {}
l!l
00
$args{'Custom'} || ''
l!l
00
MC/DC0%0/2
$args{'Name'} || $args{'Event'} || ''
$args{"Name"} $args{"Event"}
MC/DC0%0/1
$args{'Title'} || ''
$args{'Title'}
MC/DC0%0/1
$args{'Message'} || ''
$args{'Message'}
MC/DC0%0/1
$args{'Icon'} || ''
$args{'Icon'}
MC/DC0%0/1
$args{'ID'} || ''
$args{'ID'}
MC/DC0%0/1
$args{'CoalescingID'} || ''
$args{'CoalescingID'}
MC/DC0%0/1
$args{'Priority'} || 0
$args{'Priority'}
MC/DC0%0/1
$args{'Sticky'} || 'False'
$args{'Sticky'}
MC/DC0%0/1
$args{'CallbackContext'} || ''
$args{'CallbackContext'}
MC/DC0%0/1
$args{'CallbackContextType'} || ''
$args{'CallbackContextType'}
MC/DC0%0/1
$args{'CallbackTarget'} || ''
$args{'CallbackTarget'}
MC/DC0%0/1
$args{'CallbackFunction'} || {}
$args{'CallbackFunction'}
MC/DC0%0/1
$args{'Custom'} || ''
$args{'Custom'}
Truth table0%0/3
A or B
A= $args{"Name"}B= $args{"Event"}
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{'Title'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'Message'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'Icon'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'ID'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'CoalescingID'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'Priority'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'Sticky'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'CallbackContext'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'CallbackContextType'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'CallbackTarget'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'CallbackFunction'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'Custom'}
Aresult
11
00
134 );
135 0 $data{$_} =~ s!\r\n!\n! for ( keys %data );
136  
137 0 my $identifier;
138 0 ▶ if (-f $data{Icon}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $data{"Icon"}) 0 0 0
139 0 open my $f, "<:raw", $data{Icon};
140 0 $identifier = do { local $/; <$f> };
141 0 close $f;
142 0 $data{Icon} = "x-growl-resource://" . Digest::MD5::md5_hex(Digest::MD5->new->add($identifier)->digest);
143 }
144  
145 # once GfW v2.0.0.20, this CallbackTarget can be removed.
146 0 ▶ if ($data{CallbackTarget}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{"CallbackTarget"}) 0 0 0
147 0 ▶ $data{CallbackContext} = $data{CallbackContext} || 'TARGET';
Condition0%0/2
$data{'CallbackContext'} || 'TARGET'
l!l
00
MC/DC0%0/1
$data{'CallbackContext'} || 'TARGET'
$data{'CallbackContext'}
Truth table0%0/2
A
A= $data{'CallbackContext'}
Aresult
11
00
148 0 ▶ $data{CallbackContextType} = $data{CallbackContextType} || 'TARGET';
Condition0%0/2
$data{'CallbackContextType'} || 'TARGET'
l!l
00
MC/DC0%0/1
$data{'CallbackContextType'} || 'TARGET'
$data{'CallbackContextType'}
Truth table0%0/2
A
A= $data{'CallbackContextType'}
Aresult
11
00
149 }
150  
151 my $sock = IO::Socket::INET->new(
152 PeerAddr => $self->{PeerHost},
153 PeerPort => $self->{PeerPort},
154 Proto => $self->{Proto},
155 Timeout => $self->{Timeout},
156 0 );
157 0 ▶ die $@ unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
158  
159 0 my $form;
160 0 $form.=sprintf("Application-Name: %s\r\r\n",$data{AppName});
161 0 $form.=sprintf("Notification-Name: %s\r\r\n",$data{Name});
162 0 $form.=sprintf("Notification-Title: %s\r\r\n",$data{Title});
163 0 ▶ $form.=sprintf("Notification-ID: %s\r\r\n",$data{ID}) if $data{ID};
Branch: 1 branch
BranchTrueFalseTotal
if $data{"ID"} 0 0 0
164 0 ▶ $form.=sprintf("Notification-Priority: %s\r\r\n",$data{Priority}) if $data{Priority};
Branch: 1 branch
BranchTrueFalseTotal
if $data{"Priority"} 0 0 0
165 0 ▶ $form.=sprintf("Notification-Text: %s\r\r\n",$data{Message}) if $data{Message};
Branch: 1 branch
BranchTrueFalseTotal
if $data{"Message"} 0 0 0
166 0 ▶ $form.=sprintf("Notification-Sticky: %s\r\r\n",$data{Sticky}) if $data{Sticky};
Branch: 1 branch
BranchTrueFalseTotal
if $data{"Sticky"} 0 0 0
167 0 ▶ $form.=sprintf("Notification-Icon: %s\r\r\n",$data{Icon}) if $data{Icon};
Branch: 1 branch
BranchTrueFalseTotal
if $data{"Icon"} 0 0 0
168 0 ▶ $form.=sprintf("Notification-Coalescing-ID: %s\r\r\n",$data{CoalescingID}) if $data{CoalescingID};
Branch: 1 branch
BranchTrueFalseTotal
if $data{"CoalescingID"} 0 0 0
169 0 ▶ if ($data{CallbackContext}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{"CallbackContext"}) 0 0 0
170 0 $form.=sprintf("Notification-Callback-Context: %s\r\r\n",$data{CallbackContext});
171 0 $form.=sprintf("Notification-Callback-Context-Type: %s\r\r\n",$data{CallbackContextType});
172 }
173 0 ▶ if ($data{CallbackTarget}) { # BOTH method are provided here for GfW compatability.
Branch: 1 branch
BranchTrueFalseTotal
if ($data{"CallbackTarget"}) 0 0 0
174 0 $form.=sprintf("Notification-Callback-Context-Target: %s\r\r\n",$data{CallbackTarget});
175 0 $form.=sprintf("Notification-Callback-Target: %s\r\r\n",$data{CallbackTarget});
176 }
177 0 ▶ if (ref($data{Custom}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data{"Custom"} eq "HASH") 0 0 0
178 0 foreach my $header (sort keys %{$data{Custom}}){
179 0 $form.=sprintf("X-%s: %s\r\r\n",$header,$data{Custom}{$header});
180 }
181 }
182  
183 0 ▶ if ($identifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($identifier) { } 0 0 0
184 0 $form .= "\r\r\n";
185 0 $form.=sprintf("Identifier: %s\r\r\n",substr($data{Icon}, 19));
186 0 $form.=sprintf("Length: %d\r\r\n\r\r\n",length $identifier);
187 0 $form =~ s!\r\r\n!\r\n!g;
188 0 $form .= $identifier;
189 0 $form .= "\r\n";
190 } else {
191 0 $form =~ s!\r\r\n!\r\n!g;
192 }
193 0 $form .= "\r\n";
194 0 ▶ print $form if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
195  
196 0 $form = _gen_header($self, 'NOTIFY', $form);
197 0 $sock->send($form);
198  
199 0 my $ret = <$sock>;
200 0 ▶ $ret = $1 if $ret =~ /^GNTP\/1\.0 -?(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ m[^GNTP/1\.0 -?(\w+)] 0 0 0
201 0 ▶ print "$_\n" if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
202  
203 0 my $description = 'failed to notify';
204 0 ▶ if ($ret ne 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret ne "OK") 0 0 0
205 0 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 0 0 0
206 0 $_ =~ s!\r\n!!g;
207 0 ▶ print "$_\n" if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
208 0 ▶ $description = $1 if $_ =~ /^Error-Description:\s*(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Error-Description:\s*(.*)$/ 0 0 0
209 0 ▶ last if length($_) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ == 0 0 0 0
210 }
211 }
212 0 close $sock;
213  
214 0 ▶ die $description if $ret ne 'OK';
Branch: 1 branch
BranchTrueFalseTotal
if $ret ne "OK" 0 0 0
215 }
216  
217 0 ▶sub subscribe {
Subroutine subscribe: not called CC=27 SCAR=66.3
Pod documented
218 0 my ( $self, %args ) = @_;
219 0 chomp(my $hostname = `hostname`);
220 my %data = (
221 ID => $args{ID} || Data::UUID->new->create_str,
222 Name => $args{Name} || $hostname,
223 0 ▶ Port => $args{Port} || 23053,
Condition0%0/8
$args{'ID'} || 'Data::UUID'->new->create_str
l!l&&r!l&&!r
000
$args{'Name'} || $hostname
l!l&&r!l&&!r
000
$args{'Port'} || 23053
l!l
00
MC/DC0%0/2
$args{'ID'} || 'Data::UUID'->new->create_str
$args{'ID'} 'Data::UUID'->new->create_str
MC/DC0%0/2
$args{'Name'} || $hostname
$args{'Name'} $hostname
MC/DC0%0/1
$args{'Port'} || 23053
$args{'Port'}
Truth table0%0/3
A || B
A= $args{'ID'}B= 'Data::UUID'->new->create_str
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $args{'Name'}B= $hostname
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{'Port'}
Aresult
11
00
224 );
225 0 $data{$_} =~ s!\r\n!\n! for ( keys %data );
226 0 ▶ my $password = $args{Password} || '';
Condition0%0/2
$args{'Password'} || ''
l!l
00
MC/DC0%0/1
$args{'Password'} || ''
$args{'Password'}
Truth table0%0/2
A
A= $args{'Password'}
Aresult
11
00
227 0 ▶ my $callback = $args{CallbackFunction} || '';
Condition0%0/2
$args{'CallbackFunction'} || ''
l!l
00
MC/DC0%0/1
$args{'CallbackFunction'} || ''
$args{'CallbackFunction'}
Truth table0%0/2
A
A= $args{'CallbackFunction'}
Aresult
11
00
228  
229 my $sock = IO::Socket::INET->new(
230 PeerAddr => $self->{PeerHost},
231 PeerPort => $self->{PeerPort},
232 Proto => $self->{Proto},
233 Timeout => $self->{Timeout},
234 0 );
235 0 ▶ die $@ unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
236  
237 0 my $form = <<EOF;
238 Subscriber-ID: \$(ID)
239 Subscriber-Name: \$(Name)
240 Subscriber-Port: \$(Port)
241  
242 EOF
243 0 $form =~ s!\r?\n!\r\n!g;
244 0 $form =~ s/\$\((\w+)\)/$data{$1}/ge;
245  
246 0 $form = _gen_header($self, 'SUBSCRIBE', $form);
247 0 $sock->send($form);
248  
249 0 my $ret = <$sock>;
250 0 ▶ $ret = $1 if $ret =~ /^GNTP\/1\.0 -?(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ m[^GNTP/1\.0 -?(\w+)] 0 0 0
251 0 ▶ print "$_\n" if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
252  
253 0 my $description = 'failed to register';
254 0 ▶ if ($ret ne 'OK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret ne "OK") 0 0 0
255 0 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 0 0 0
256 0 $_ =~ s!\r\n!!g;
257 0 ▶ print "$_\n" if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
258 0 ▶ $description = $1 if $_ =~ /^Error-Description:\s*(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Error-Description:\s*(.*)$/ 0 0 0
259 0 ▶ last if length($_) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ == 0 0 0 0
260 }
261 0 ▶ die $description if $ret ne 'OK';
Branch: 1 branch
BranchTrueFalseTotal
if $ret ne "OK" 0 0 0
262 }
263  
264 $sock = IO::Socket::INET->new(
265 LocalPort => $data{Port},
266 Proto => 'tcp',
267 Listen => 10,
268 Timeout => $self->{Timeout},
269 0 );
270 0 ▶ die $@ unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
271  
272 0 $description = 'failed to subscribe';
273 0 while (1) {
274 0 my $client = $sock->accept();
275 0 my ($Title, $Message) = ('', '');
276 0 ▶ while (<$client>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $client)) 0 0 0
277 0 $_ =~ s!\r\n!!g;
278 0 ▶ print "$_\n" if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
279 0 ▶ $ret = $1 if $_ =~ /^GNTP\/1\.0 -?(\w+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ m[^GNTP/1\.0 -?(\w+)] 0 0 0
280 0 ▶ $description = $1 if $_ =~ /^Error-Description:\s*(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Error-Description:\s*(.*)$/ 0 0 0
281 0 ▶ $Title = $1 if $_ =~ /^Notification-Title: (.*)\r\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Notification-Title: (.*)\r\n/ 0 0 0
282 0 ▶ $Message = $1 if $_ =~ /^Notification-Text: (.*)\r\n/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Notification-Text: (.*)\r\n/ 0 0 0
283 # TODO
284 # handling more GNTP protocols.
285 # currently, can't treat multiline header which include LF.
286 ## hrmmm...
287 0 ▶ last if length($_) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ == 0 0 0 0
288 }
289 0 $client->close();
290  
291 0 ▶ if ($Title && ref($callback) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($Title and ref $callback eq "CODE") 0 0 0
Condition0%0/3
$Title and ref $callback eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
$Title and ref $callback eq "CODE"
$Title ref $callback eq "CODE"
Truth table0%0/3
A and B
A= $TitleB= ref $callback eq "CODE"
ABresult
0X0
100
111
292 0 $callback->($Title, $Message);
293 }
294 }
295  
296 0 ▶ die $description if $ret ne 'OK';
Branch: 1 branch
BranchTrueFalseTotal
if $ret ne "OK" 0 0 0
297 }
298  
299 0 ▶sub wait {
Subroutine wait: not called CC=19 SCAR=59.4
Pod documented
300 0 my $self = shift;
301 0 ▶ my $waitall = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
302  
303 0 my @callbacks = @{$self->{Callbacks}};
304 0 my @old = @callbacks;
305 0 my $bits = "";
306 0 ▶ while (@callbacks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@callbacks) 0 0 0
307 0 vec($bits, fileno($_->{Socket}), 1) = 1 for @callbacks;
308 0 ▶ next unless select($bits, undef, undef, 0.1);
Branch: 1 branch
BranchTrueFalseTotal
unless select $bits, undef, undef, "0.1" 0 0 0
309 0 ▶ for (my $i = 0; $i < @callbacks; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @callbacks) 0 0 0
310 0 my $callback = $callbacks[$i];
311 0 my $sock = $callback->{Socket};
312 0 ▶ if (vec($bits, fileno($sock), 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $bits, fileno $sock, 1) 0 0 0
313 0 my ($result, $type, $context, $id, $timestamp) = ('', '', '','','');
314 0 ▶ while (<$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $sock)) 0 0 0
315 0 $_ =~ s!\r\n!!g;
316 0 ▶ print "$_\n" if $self->{Debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Debug"} 0 0 0
317 0 ▶ $id = $1 if $_ =~ /^Notification-ID: (.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Notification-ID: (.*)$/ 0 0 0
318 0 ▶ $timestamp = $1 if $_ =~ /^Notification-Callback-Timestamp: (.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Notification-Callback-Timestamp: (.*)$/ 0 0 0
319 0 ▶ $result = $1 if $_ =~ /^Notification-Callback-Result: (.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Notification-Callback-Result: (.*)$/ 0 0 0
320 0 ▶ $context = $1 if $_ =~ /^Notification-Callback-Context: (.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Notification-Callback-Context: (.*)$/ 0 0 0
321 0 ▶ $type = $1 if $_ =~ /^Notification-Callback-Context-Type: (.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /^Notification-Callback-Context-Type: (.*)$/ 0 0 0
322 0 ▶ last if length($_) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ == 0 0 0 0
323 }
324 0 ▶ if (ref($callback->{Function}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $callback->{"Function"} eq "CODE") 0 0 0
325 0 $callback->{Function}->($result, $type, $context,$id,$timestamp);
326 }
327 0 splice(@callbacks, $i, 1);
328 }
329 }
330 0 ▶ last unless $waitall;
Branch: 1 branch
BranchTrueFalseTotal
unless $waitall 0 0 0
331 };
332  
333 0 ▶ for (my $i = 0; $i < @{$self->{Callbacks}}; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"Callbacks"};}) 0 0 0
334 0 ▶ if (grep { $_->{Socket} eq $self->{Callbacks}[$i]->{Socket} } @old) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->{"Socket"} eq $self->{"Callbacks"}[$i]{"Socket"};} @old) 0 0 0
335 0 splice(@{$self->{Callbacks}}, $i--, 1);
336 }
337 }
338 0 1;
339 }
340  
341 0 ▶sub _translate_int {
Subroutine _translate_int: not called CC=1 SCAR=6.9
342 0 return 0 + shift;
343 }
344  
345 0 ▶sub _translate_bool {
Subroutine _translate_bool: not called CC=4 SCAR=30.0
346 0 my $value = shift;
347 0 ▶ return 'True' if $value =~ /^([Tt]rue|[Yy]es)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^([Tt]rue|[Yy]es)$/ 0 0 0
348 0 ▶ return 'False' if $value =~ /^([Ff]alse|[Nn]o)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^([Ff]alse|[Nn]o)$/ 0 0 0
349 0 ▶ return 'True' if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
350 0 return 'False';
351 }
352  
353 0 ▶sub _gen_header {
Subroutine _gen_header: not called CC=3 SCAR=24.8
354 0 my ($ctx, $method, $form) = @_;
355  
356 0 ▶ if ($ctx->{Password}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{'Password'}) { } 0 0 0
357 0 my ($hash, $salt) = _gen_hash($ctx);
358 0 my $crypt = _gen_encrypt($ctx, $salt, \$form);
359 0 ▶ if ($crypt eq 'NONE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($crypt eq 'NONE') { } 0 0 0
360 0 $form = "GNTP/1.0 $method NONE $hash\r\n$form\r\n";
361 } else {
362 0 $form = "GNTP/1.0 $method $crypt $hash\r\n$form\r\n\r\n";
363 }
364 } else {
365 0 $form = "GNTP/1.0 $method NONE\r\n$form\r\n";
366 }
367 0 return $form;
368 }
369  
370 0 ▶sub _gen_salt {
Subroutine _gen_salt: not called CC=2 SCAR=17.9
371 0 my $count = shift;
372 0 my @salt = ( '.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z' );
373 0 my $salt;
374 0 $salt .= (@salt)[rand @salt] for 1..$count;
375 0 return $salt;
376 }
377  
378 0 ▶sub _gen_hash {
Subroutine _gen_hash: not called CC=2 SCAR=17.9
379 0 my $ctx = shift;
380 0 my $hash_algorithm = $ctx->{PasswordHashAlgorithm};
381 0 my $password = $ctx->{Password};
382 0 ▶ return 'NONE' if $hash_algorithm eq 'NONE';
Branch: 1 branch
BranchTrueFalseTotal
if $hash_algorithm eq "NONE" 0 0 0
383  
384 0 my $salt = _gen_salt(8);
385 0 my $salthex = uc unpack("H*", $salt);
386  
387 my %hashroll = (
388 0 ▶ 'MD5' => sub { my ($password, $salt) = @_; return uc Digest::MD5::md5_hex(Digest::MD5->new->add($password)->add($salt)->digest); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
389 0 ▶ 'SHA1' => sub { my ($password, $salt) = @_; return uc Digest::SHA::sha1_hex(Digest::SHA->new(1)->add($password)->add($salt)->digest); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
390 0 ▶ 'SHA256' => sub { my ($password, $salt) = @_; return uc Digest::SHA::sha256_hex(Digest::SHA->new(256)->add($password)->add($salt)->digest); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
391 0 );
392 0 my $key = $hashroll{$hash_algorithm}->($password, $salt);
393 0 return "$hash_algorithm:$key.$salthex", $salt;
394 }
395  
396 0 ▶sub _gen_encrypt {
Subroutine _gen_encrypt: not called CC=2 SCAR=17.9
397 0 my ($ctx, $salt, $data) = @_;
398 0 my $hash_algorithm = $ctx->{PasswordHashAlgorithm};
399 0 my $crypt_algorithm = $ctx->{EncryptAlgorithm};
400 0 my $password = $ctx->{Password};
401 0 ▶ return 'NONE' if $crypt_algorithm eq 'NONE';
Branch: 1 branch
BranchTrueFalseTotal
if $crypt_algorithm eq "NONE" 0 0 0
402  
403 my %hashroll = (
404 0 ▶ 'MD5' => sub { my ($password, $salt) = @_; return Digest::MD5->new->add($password)->add($salt)->digest },
Subroutine __ANON__: not called CC=1 SCAR=6.9
405 0 ▶ 'SHA1' => sub { my ($password, $salt) = @_; return Digest::SHA->new(1)->add($password)->add($salt)->digest },
Subroutine __ANON__: not called CC=1 SCAR=6.9
406 0 ▶ 'SHA256' => sub { my ($password, $salt) = @_; return Digest::SHA->new(256)->add($password)->add($salt)->digest },
Subroutine __ANON__: not called CC=1 SCAR=6.9
407 0 );
408 0 my $key = $hashroll{$hash_algorithm}->($password, $salt);
409  
410 my %cryptroll = (
411 0 ▶ 'AES' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
412 0 my ($data, $key) = @_;
413 0 my $iv = Crypt::CBC->random_bytes(16);
414 0 my $cbc = Crypt::CBC->new(
415 -key => substr($key, 0, 24),
416 -iv => $iv,
417 -keysize => 24,
418 -header => 'none',
419 -literal_key => 1,
420 -padding => 'standard',
421 -cipher => 'Crypt::OpenSSL::AES',
422 );
423 0 return $cbc->encrypt($data), uc unpack("H*", $iv);
424 },
425 0 ▶ 'DES' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
426 0 my ($data, $key) = @_;
427 0 my $iv = Crypt::CBC->random_bytes(8);
428 0 my $cbc = Crypt::CBC->new(
429 -key => substr($key, 0, 8),
430 -iv => $iv,
431 -header => 'none',
432 -literal_key => 1,
433 -padding => 'standard',
434 -cipher => 'DES',
435 );
436 0 return $cbc->encrypt($data), uc unpack("H*", $iv);
437 },
438 0 ▶ '3DES' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
439 0 my ($data, $key) = @_;
440 0 my $iv = Crypt::CBC->random_bytes(8);
441 0 ▶ $key = $key.substr($key,0,24-length($key)) if length($key) < 24;
Branch: 1 branch
BranchTrueFalseTotal
if length $key < 24 0 0 0
442 0 my $cbc = Crypt::CBC->new(
443 -key => substr($key, 0, 24),
444 -iv => $iv,
445 -header => 'none',
446 -literal_key => 1,
447 -padding => 'standard',
448 -cipher => 'DES_EDE3',
449 );
450 0 return $cbc->encrypt($data), uc unpack("H*", $iv);
451 },
452 0 );
453 0 ($$data, my $hash) = $cryptroll{$crypt_algorithm}->($$data, $key);
454 0 return "$crypt_algorithm:$hash";
455 }
456  
457 0 ▶sub _debug {
Subroutine _debug: not called CC=1 SCAR=6.9
458 0 my ($name, $data) = @_;
459 0 open my $f, ">", $name;
460 0 binmode $f;
461 0 print $f $data;
462 0 close $f;
463 }
464  
465 2 1;
466 __END__
↑ Summary