blib/lib/Net/Growl.pm

filter: Statementstmt 39.6% 48 / 121 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 66.6% 14 / 21 Podpod 100% 2 / 2 totaltotal 33.1% 64 / 193 CC 21 SCAR 33.0
CC
21
Cov
30%
new
90.0
register
20.0
notify
20.0
CRAP 27.2
↑ Summary
1 package Net::Growl;
2  
3 1 ▶use vars qw($VERSION); $VERSION = '0.99';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Derived from Rui Carmo's (http://the.taoofmac.com)
11 # netgrowl.php (http://the.taoofmac.com/space/Projects/netgrowl.php)
12 # by Nathan McFarland - (http://nmcfarl.org)
13  
14 1 our @ISA = qw(Exporter);
15 1 our @EXPORT = ( 'register', 'notify' );
16  
17 1 ▶use constant GROWL_UDP_PORT => 9887;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant GROWL_PROTOCOL_VERSION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant GROWL_TYPE_REGISTRATION => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant GROWL_TYPE_NOTIFICATION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 0 ▶sub register {
Subroutine register: not called CC=4 SCAR=30.0
Pod documented
23 0 my %args = @_;
24 my %addr = (
25 0 ▶ PeerAddr => $args{host} || "localhost",
Condition0%0/2
$args{'host'} || 'localhost'
l!l
00
MC/DC0%0/1
$args{'host'} || 'localhost'
$args{'host'}
Truth table0%0/2
A
A= $args{'host'}
Aresult
11
00
26 PeerPort => Net::Growl::GROWL_UDP_PORT,
27 Proto => 'udp' );
28 0 ▶ die "A password is required" if ( !$args{password} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"password"} 0 0 0
29 0 ▶ my $s = IO::Socket::INET->new(%addr) || die "Could not create socket: $!\n";
Condition0%0/2
'IO::Socket::INET'->new(%addr) || die("Could not create socket: $!\n")
l!l
00
MC/DC0%0/1
'IO::Socket::INET'->new(%addr) || die("Could not create socket: $!\n")
'IO::Socket::INET'->new(%addr)
Truth table0%0/2
A
A= 'IO::Socket::INET'->new(%addr)
Aresult
11
00
30 0 my $p = Net::Growl::RegistrationPacket->new(%args);
31 0 $p->addNotification();
32 0 print( $s $p->payload() );
33 0 close($s);
34 }
35  
36 0 ▶sub notify {
Subroutine notify: not called CC=4 SCAR=30.0
Pod documented
37 0 my %args = @_;
38 my %addr = (
39 0 ▶ PeerAddr => $args{host} || "localhost",
Condition0%0/2
$args{'host'} || 'localhost'
l!l
00
MC/DC0%0/1
$args{'host'} || 'localhost'
$args{'host'}
Truth table0%0/2
A
A= $args{'host'}
Aresult
11
00
40 PeerPort => Net::Growl::GROWL_UDP_PORT,
41 Proto => 'udp' );
42 0 ▶ die "A password is required" if ( !$args{password} );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"password"} 0 0 0
43 0 ▶ my $s = IO::Socket::INET->new(%addr) || die "Could not create socket: $!\n";
Condition0%0/2
'IO::Socket::INET'->new(%addr) || die("Could not create socket: $!\n")
l!l
00
MC/DC0%0/1
'IO::Socket::INET'->new(%addr) || die("Could not create socket: $!\n")
'IO::Socket::INET'->new(%addr)
Truth table0%0/2
A
A= 'IO::Socket::INET'->new(%addr)
Aresult
11
00
44 0 my $p = Net::Growl::NotificationPacket->new(%args);
45 0 print( $s $p->payload() );
46  
47 0 close($s);
48 }
49  
50 1 1;
51  
52 package Net::Growl::RegistrationPacket;
53 1 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use base 'Net::Growl';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
57 0 my $class = shift;
58 0 my %args = @_;
59 0 ▶ $args{application} ||= "growl_notify";
Condition0%0/2
$args{"application"} ||= "growl_notify"
l!l
00
MC/DC0%0/1
$args{"application"} ||= "growl_notify"
$args{"application"}
Truth table0%0/2
A
A= $args{"application"}
Aresult
11
00
60 0 my $self = bless \%args, $class;
61 0 $self->{notifications} = {};
62 0 utf8::encode( $self->{application} );
63 0 return $self;
64 }
65  
66 0 ▶sub addNotification {
Subroutine addNotification: not called CC=3 SCAR=24.8
67 0 my ( $self, %args ) = @_;
68 0 ▶ $args{notification} ||= "Command-Line Growl Notification";
Condition0%0/2
$args{"notification"} ||= "Command-Line Growl Notification"
l!l
00
MC/DC0%0/1
$args{"notification"} ||= "Command-Line Growl Notification"
$args{"notification"}
Truth table0%0/2
A
A= $args{"notification"}
Aresult
11
00
69 0 ▶ $args{enabled} = "True" if !defined $args{enabled};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"enabled"} 0 0 0
70 0 $self->{notifications}->{ $args{notification} } = $args{enabled};
71 }
72  
73 0 ▶sub payload {
Subroutine payload: not called CC=4 SCAR=30.0
74 0 my $self = shift;
75 0 my ( $name, $defaults );
76 0 my ( $name_count, $defaults_count );
77 0 for ( keys %{ $self->{notifications} } ) {
78 0 utf8::encode($_);
79 0 $name .= pack( "n", length($_) ) . $_;
80 0 $name_count++;
81 0 ▶ if ( $self->{notifications}->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"notifications"}{$_}) 0 0 0
82 0 $defaults .= pack( "c", $name_count - 1 );
83 0 $defaults_count++;
84 }
85 }
86 $self->{data} = pack( "c2nc2",
87 $self->GROWL_PROTOCOL_VERSION,
88 $self->GROWL_TYPE_REGISTRATION,
89 0 length( $self->{application} ),
90 $name_count,
91 $defaults_count );
92 0 $self->{data} .= $self->{application} . $name . $defaults;
93  
94 0 my $checksum;
95 0 ▶ if ( $self->{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'password'}) { } 0 0 0
96 0 $checksum = pack( "H32", md5_hex( $self->{data} . $self->{password} ) );
97 }
98 else {
99 0 $checksum = pack( "H32", md5_hex( $self->{data} ) );
100 }
101  
102 0 $self->{data} .= $checksum;
103 0 return $self->{data};
104 }
105  
106 1 1;
107  
108 package Net::Growl::NotificationPacket;
109 1 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
110 1 ▶use base 'Net::Growl';
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
113 0 my ( $class, %args ) = @_;
114 0 ▶ $args{application} ||= "growlnotify";
Condition0%0/2
$args{"application"} ||= "growlnotify"
l!l
00
MC/DC0%0/1
$args{"application"} ||= "growlnotify"
$args{"application"}
Truth table0%0/2
A
A= $args{"application"}
Aresult
11
00
115 0 ▶ $args{notification} ||= "Command-Line Growl Notification";
Condition0%0/2
$args{"notification"} ||= "Command-Line Growl Notification"
l!l
00
MC/DC0%0/1
$args{"notification"} ||= "Command-Line Growl Notification"
$args{"notification"}
Truth table0%0/2
A
A= $args{"notification"}
Aresult
11
00
116 0 ▶ $args{title} ||= "Title";
Condition0%0/2
$args{"title"} ||= "Title"
l!l
00
MC/DC0%0/1
$args{"title"} ||= "Title"
$args{"title"}
Truth table0%0/2
A
A= $args{"title"}
Aresult
11
00
117 0 ▶ $args{description} ||= "Description";
Condition0%0/2
$args{"description"} ||= "Description"
l!l
00
MC/DC0%0/1
$args{"description"} ||= "Description"
$args{"description"}
Truth table0%0/2
A
A= $args{"description"}
Aresult
11
00
118 0 ▶ $args{priority} ||= 0;
Condition0%0/2
$args{"priority"} ||= 0
l!l
00
MC/DC0%0/1
$args{"priority"} ||= 0
$args{"priority"}
Truth table0%0/2
A
A= $args{"priority"}
Aresult
11
00
119 0 my $self = bless \%args, $class;
120  
121 0 utf8::encode( $self->{application} );
122 0 utf8::encode( $self->{notification} );
123 0 utf8::encode( $self->{title} );
124 0 utf8::encode( $self->{description} );
125 0 my $flags = ( $args{priority} & 0x07 ) * 2;
126 0 ▶ if ( $args{priority} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"priority"} < 0) 0 0 0
127 0 $flags |= 0x08;
128 }
129 0 ▶ if ( $args{sticky} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"sticky"}) 0 0 0
130 0 $flags = $flags | 0x0001;
131 }
132 $self->{data} = pack( "c2n5",
133 $self->GROWL_PROTOCOL_VERSION,
134 $self->GROWL_TYPE_NOTIFICATION,
135 $flags,
136 length( $self->{notification} ),
137 length( $self->{title} ),
138 length( $self->{description} ),
139 0 length( $self->{application} ) );
140 0 $self->{data} .= $self->{notification};
141 0 $self->{data} .= $self->{title};
142 0 $self->{data} .= $self->{description};
143 0 $self->{data} .= $self->{application};
144  
145 0 ▶ if ( $args{password} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'password'}) { } 0 0 0
146 $self->{checksum} =
147 0 pack( "H32", md5_hex( $self->{data} . $args{password} ) );
148 }
149 else {
150 0 $self->{checksum} = pack( "H32", md5_hex( $self->{data} ) );
151 }
152 0 $self->{data} .= $self->{checksum};
153 0 return $self;
154 }
155  
156 0 ▶sub payload {
Subroutine payload: not called CC=1 SCAR=6.9
157 0 my $self = shift;
158 0 return $self->{data};
159 }
160  
161 1 1;
162  
163 __END__
↑ Summary