BitTorrent.pm

filter: Statementstmt 16.4% 22 / 134 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 36.3% 4 / 11 Podpod 0.0% 0 / 7 totaltotal 13.9% 26 / 187 CC 18 SCAR 41.3
CC
18
Cov
13%
benc_parse_hash
132.0
getTrackerInfo
42.0
bdecode
6.0
CRAP 62.1
↑ Summary install_contrib.pl »
1 package BitTorrent;
2  
3 #use 5.008007;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Digest::SHA1 qw(sha1);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 our @ISA = qw(Exporter);
12  
13 1 our %EXPORT_TAGS = ( 'all' => [ qw(
14
15 ) ] );
16  
17 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18  
19 1 our @EXPORT = qw(
20 getHealth
21 getTrackerInfo
22 );
23  
24 1 our $VERSION = '0.10';
25 1 our $TorrentScrape = "/var/lib/perl/torrent-checker.php";
26  
27  
28 0 ▶sub new(){
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
29
30 0 my $self = bless {}, shift;
31 0 return $self;
32
33 }; # new()
34  
35  
36 0 ▶sub getTrackerInfo(){
Subroutine getTrackerInfo: not called CC=6 SCAR=37.4
Pod undocumented
37  
38 0 my $self = shift;
39 0 my $file = shift;
40 0 my $content;
41  
42 0 ▶ if ( $file =~ /^http/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /^http/i) { } 0 0 0
43 0 $content = get($file);
44 } else {
45 0 ▶ open(RH,"<$file") or warn;
Branch: 1 branch
BranchTrueFalseTotal
open RH, "<$file" or warn 0 0 0
46 0 binmode(RH);
47 0 $content = do { local( $/ ) ; <RH> } ;
48 0 close RH;
49 };
50  
51 0 my %result;
52  
53 0 my $t = &bdecode(\$content);
54  
55 0 my $info = $t->{'info'};
56 0 my $s = substr($content, $t->{'_info_start'}, $t->{'_info_length'});
57 0 my $hash = bin2hex(sha1($s));
58 0 my $announce = $t->{'announce'};
59  
60 0 $result{'hash'} = $hash;
61 0 $result{'announce'} = $announce;
62 0 $result{'files'} = [];
63 0 my $tsize = 0;
64 0 ▶ if(defined($info->{'files'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{'files'}) { } 0 0 0
65 0 foreach my $f (@{$info->{'files'}}) {
66 0 my %file_record = ( 'size' => $f->{'length'});
67  
68 0 $tsize += $f->{'length'};
69 0 my $path = $f->{'path'};
70  
71 0 ▶ if(ref($path) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path eq 'ARRAY') { } 0 0 0
72 0 $file_record{'name'} = $info->{'name'}.'/'.$path->[0];
73 } else {
74 0 $file_record{'name'} = $info->{'name'}.'/'.$path;
75 }
76 0 push @{$result{'files'}}, \%file_record;
77 }
78  
79 } else {
80 $tsize += $info->{'length'},
81  
82 0 push @{$result{'files'}},
83 {
84 'size' => $info->{'length'},
85 0 'name' => $info->{'name'},
86 };
87  
88 }
89 0 $result{'total_size'} = $tsize;
90  
91 0 return \%result;
92  
93 }; # sub getTrackerInfo(){
94  
95  
96 0 ▶sub getHealth(){
Subroutine getHealth: not called CC=1 SCAR=6.9
Pod undocumented
97  
98 0 my $self = shift;
99 0 my $torrent = shift;
100  
101 # init
102 0 my $Hash = ();
103 0 my %Hash = ();
104  
105 # get torrent
106 0 my $random = int(rand(100000)+1);
107 0 my $TorrentStore = "/tmp/$random.torrent";
108 0 getstore($torrent, $TorrentStore);
109
110 # scrape torrent
111 0 my $returnVal = `php $TorrentScrape $TorrentStore`;
112
113 # extract infos
114 0 my @SeederLeecher = split('#', $returnVal);
115 0 my $Seeder = $SeederLeecher[0];
116 0 my $Leecher = $SeederLeecher[1];
117
118 0 eval {
119 0 $Seeder =~ s/^\s+//;
120 0 $Seeder =~ s/\s+$//;
121 0 $Leecher =~ s/^\s+//;
122 0 $Leecher =~ s/\s+$//;
123 };
124  
125 0 $Hash->{seeder} = $Seeder;
126 0 $Hash->{leecher} = $Leecher;
127  
128  
129 0 return $Hash;
130  
131 }; # sub sub getHealth(){
132  
133  
134 0 ▶sub bin2hex() {
Subroutine bin2hex: not called CC=1 SCAR=6.9
Pod undocumented
135
136 0 my ($d) = @_;
137 0 $d =~ s/(.)/sprintf("%02x",ord($1))/egs;
138 0 $d = lc($d);
139
140 0 return $d;
141  
142 }; # sub bin2hex() {
143  
144 0 ▶sub bdecode {
Subroutine bdecode: not called CC=2 SCAR=17.9
Pod undocumented
145 0 my ($dataref) = @_;
146 0 ▶ unless(ref($dataref) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $dataref eq "SCALAR") 0 0 0
147 0 die('Function bdecode takes a scalar ref!');
148 } # unless
149 0 my $p = 0;
150 0 return benc_parse_hash($dataref,\$p);
151 } # sub bdecode
152  
153 0 ▶sub benc_parse_hash {
Subroutine benc_parse_hash: not called CC=11 SCAR=48.8
Pod undocumented
154 0 my ($data, $p) = @_;
155 0 my $c = substr($$data,$$p,1);
156 0 my $r = undef;
157 0 ▶ if($c eq 'd') { # hash
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq 'd') { } 0 0 0
elsif ($c eq 'l') { } 0 0 0
elsif ($c eq 'i') { } 0 0 0
elsif ($c =~ /\d/) { } 0 0 0
158 # print "Found a hash\n";
159 0 %{$r} = ();
160 0 ++$$p;
161 0 ▶ while(($$p < length($$data)) && (substr($$data, $$p, 1) ne 'e')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$p < length $$data and substr($$data, $$p, 1) ne "e") 0 0 0
Condition0%0/3
$$p < length $$data and substr($$data, $$p, 1) ne "e"
!ll&&!rl&&r
000
MC/DC0%0/2
$$p < length $$data and substr($$data, $$p, 1) ne "e"
$$p < length $$data substr($$data, $$p, 1) ne "e"
Truth table0%0/3
A and B
A= $$p < length $$dataB= substr($$data, $$p, 1) ne "e"
ABresult
0X0
100
111
162 0 my $k = benc_parse_string($data, $p);
163 0 my $start = $$p;
164 0 ▶ $r->{'_' . $k . '_start'} = $$p if($k eq 'info');
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "info" 0 0 0
165 0 my $v = benc_parse_hash($data, $p);
166 0 ▶ $r->{'_' . $k . '_length'} = ($$p - $start) if($k eq 'info');
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "info" 0 0 0
167 # print "\t{$k} => $v\n";
168 0 $r->{$k} = $v;
169 } # while
170 0 ++$$p;
171 # print "End of Hash\n";
172 } elsif($c eq 'l') { # list
173 0 @{$r} = \();
174 0 ++$$p;
175 # print "Found a list\n";
176 0 ▶ while(substr($$data, $$p, 1) ne 'e') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$data, $$p, 1) ne "e") 0 0 0
177 0 push(@{$r},benc_parse_hash($data, $p));
178 # print "\t[@{$r}] = $$r[-1]\n";
179 } # while
180 0 ++$$p;
181 } elsif($c eq 'i') { # number
182 0 $r = 0;
183 0 my $c;
184 0 ++$$p;
185 0 ▶ while(($c = substr($$data,$$p,1)) ne 'e') {
Branch: 1 branch
BranchTrueFalseTotal
if (($c = substr($$data, $$p, 1)) ne "e") 0 0 0
186 0 $r *= 10;
187 0 $r += int($c);
188 0 ++$$p;
189 } # while
190 0 ++$$p;
191 # print "Found an int: $r\n";
192 } elsif($c =~ /\d/) { # string
193 0 $r = benc_parse_string($data, $p);
194 # print "Found a string: ", length($r), "\n";
195 } else {
196 0 die("Unknown token '$c' at $p!");
197 } # case
198 0 return $r;
199 } # benc_parse
200  
201 0 ▶sub benc_parse_string {
Subroutine benc_parse_string: not called CC=2 SCAR=17.9
Pod undocumented
202 0 my ($data, $p) = @_;
203 0 my $l = 0;
204 0 my $c = undef;
205 0 my $s;
206 0 ▶ while(($c = substr($$data,$$p,1)) ne ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (($c = substr($$data, $$p, 1)) ne ":") 0 0 0
207 # print "Char: $c, ", int($c), "\n";
208 0 $l *= 10;
209 0 $l += int($c);
210 0 ++$$p;
211 } # while
212 0 ++$$p;
213 # print "Length: $l\n";
214 0 $s = substr($$data,$$p,$l);
215 0 $$p += $l;
216 # print "Returning length $l = ", length($s), " ($s)\n";
217 0 return $s;
218 } # benc_parse_string
219  
220  
221 1 1;
222  
223  
224  
225 # Preloaded methods go here.
226  
227 1 1;
228 __END__
↑ Summary install_contrib.pl »