blib/lib/Net/PH.pm untested

filter: Statementstmt 0.0% 0 / 261 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 417 CC 37 SCAR 29.9
CC
37
Cov
0%
query
72.0
new
42.0
fields
42.0
CRAP 19.9
1 # Net::PH.pm
2 #
3 # Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com> and
4 # Alex Hristov <hristov@slb.com>. All rights reserved. This program is free
5 # software; you can redistribute it and/or modify it under the same terms
6 # as Perl itself.
7  
8 package Net::PH;
9  
10 require 5.001;
11  
12 use strict;
13 use vars qw(@ISA $VERSION);
14 use Carp;
15  
16 use Socket 1.3;
17 use IO::Socket;
18 use Net::Cmd;
19 eval { local $SIG{__DIE__}; require Net::Config };
20  
21 $VERSION = "2.21"; # $Id: //depot/ph/PH.pm#2 $
22 @ISA = qw(Exporter Net::Cmd IO::Socket::INET);
23  
24 sub new
25 {
26 my $pkg = shift;
27 my $host = shift if @_ % 2;
28 my %arg = @_;
29 my $hosts = defined $host ? [ $host ] : $Net::Config::NetConfig{ph_hosts} || ['ph'];
30 my $ph;
31  
32 my $h;
33 foreach $h (@{$hosts})
34 {
35 $ph = $pkg->SUPER::new(PeerAddr => ($host = $h),
36 PeerPort => $arg{Port} || 'csnet-ns(105)',
37 Proto => 'tcp',
38 Timeout => defined $arg{Timeout}
39 ? $arg{Timeout}
40 : 120
41 ) and last;
42 }
43  
44 return undef
45 unless defined $ph;
46  
47 ${*$ph}{'net_ph_host'} = $host;
48  
49 $ph->autoflush(1);
50  
51 $ph->debug(exists $arg{Debug} ? $arg{Debug} : undef);
52  
53 $ph;
54 }
55  
56 sub status
57 {
58 my $ph = shift;
59  
60 $ph->command('status')->response;
61 $ph->code;
62 }
63  
64 sub login
65 {
66 my $ph = shift;
67 my($user,$pass,$encrypted) = @_;
68 my $resp;
69  
70 $resp = $ph->command("login",$user)->response;
71  
72 if(defined($pass) && $resp == CMD_MORE)
73 {
74 if($encrypted)
75 {
76 my $challenge_str = $ph->message;
77 chomp($challenge_str);
78 Net::PH::crypt::crypt_start($pass);
79 my $cryptstr = Net::PH::crypt::encryptit($challenge_str);
80  
81 $ph->command("answer", $cryptstr);
82 }
83 else
84 {
85 $ph->command("clear", $pass);
86 }
87 $resp = $ph->response;
88 }
89  
90 $resp == CMD_OK;
91 }
92  
93 sub logout
94 {
95 my $ph = shift;
96  
97 $ph->command("logout")->response == CMD_OK;
98 }
99  
100 sub id
101 {
102 my $ph = shift;
103 my $id = @_ ? shift : $<;
104  
105 $ph->command("id",$id)->response == CMD_OK;
106 }
107  
108 sub siteinfo
109 {
110 my $ph = shift;
111  
112 $ph->command("siteinfo");
113  
114 my $ln;
115 my %resp;
116 my $cur_num = 0;
117  
118 while(defined($ln = $ph->getline))
119 {
120 $ph->debug_print(0,$ln)
121 if ($ph->debug & 2);
122 chomp($ln);
123 my($code,$num,$tag,$data);
124  
125 if($ln =~ /^-(\d+):(\d+):(?:\s*([^:]+):)?\s*(.*)/o)
126 {
127 ($code,$num,$tag,$data) = ($1, $2, $3 || "",$4);
128 $resp{$tag} = bless [$code, $num, $tag, $data], "Net::PH::Result";
129 }
130 else
131 {
132 $ph->set_status($ph->parse_response($ln));
133 return \%resp;
134 }
135 }
136  
137 return undef;
138 }
139  
140 sub query
141 {
142 my $ph = shift;
143 my $search = shift;
144  
145 my($k,$v);
146  
147 my @args = ('query', _arg_hash($search));
148  
149 push(@args,'return',_arg_list( shift ))
150 if @_;
151  
152 unless($ph->command(@args)->response == CMD_INFO)
153 {
154 return $ph->code == 501
155 ? []
156 : undef;
157 }
158  
159 my $ln;
160 my @resp;
161 my $cur_num = 0;
162  
163 my($last_tag);
164  
165 while(defined($ln = $ph->getline))
166 {
167 $ph->debug_print(0,$ln)
168 if ($ph->debug & 2);
169 chomp($ln);
170 my($code,$idx,$num,$tag,$data);
171  
172 if($ln =~ /^-(\d+):(\d+):\s*([^:]*):\s*(.*)/o)
173 {
174 ($code,$idx,$tag,$data) = ($1,$2,$3,$4);
175 my $num = $idx - 1;
176  
177 $resp[$num] ||= {};
178  
179 $tag = $last_tag
180 unless(length($tag));
181  
182 $last_tag = $tag;
183  
184 if(exists($resp[$num]->{$tag}))
185 {
186 $resp[$num]->{$tag}->[3] .= "\n" . $data;
187 }
188 else
189 {
190 $resp[$num]->{$tag} = bless [$code, $idx, $tag, $data], "Net::PH::Result";
191 }
192 }
193 else
194 {
195 $ph->set_status($ph->parse_response($ln));
196 return \@resp;
197 }
198 }
199  
200 return undef;
201 }
202  
203 sub change
204 {
205 my $ph = shift;
206 my $search = shift;
207 my $make = shift;
208  
209 $ph->command(
210 "change", _arg_hash($search),
211 "make", _arg_hash($make)
212 )->response == CMD_OK;
213 }
214  
215 sub _arg_hash
216 {
217 my $hash = shift;
218  
219 return $hash
220 unless(ref($hash));
221  
222 my($k,$v);
223 my @r;
224  
225 while(($k,$v) = each %$hash)
226 {
227 my $a = $v;
228 $a =~ s/\n/\\n/sog;
229 $a =~ s/\t/\\t/sog;
230 $a = '"' . $a . '"'
231 if $a =~ /\W/;
232 $a = '""'
233 unless length $a;
234  
235 push(@r, "$k=$a");
236 }
237 join(" ", @r);
238 }
239  
240 sub _arg_list
241 {
242 my $arr = shift;
243  
244 return $arr
245 unless(ref($arr));
246  
247 my $v;
248 my @r;
249  
250 foreach $v (@$arr)
251 {
252 my $a = $v;
253 $a =~ s/\n/\\n/sog;
254 $a =~ s/\t/\\t/sog;
255 $a = '"' . $a . '"'
256 if $a =~ /\W/;
257 push(@r, $a);
258 }
259  
260 join(" ",@r);
261 }
262  
263 sub add
264 {
265 my $ph = shift;
266 my $arg = @_ > 1 ? { @_ } : shift;
267  
268 $ph->command('add', _arg_hash($arg))->response == CMD_OK;
269 }
270  
271 sub delete
272 {
273 my $ph = shift;
274 my $arg = @_ > 1 ? { @_ } : shift;
275  
276 $ph->command('delete', _arg_hash($arg))->response == CMD_OK;
277 }
278  
279 sub force
280 {
281 my $ph = shift;
282 my $search = shift;
283 my $force = shift;
284  
285 $ph->command(
286 "change", _arg_hash($search),
287 "force", _arg_hash($force)
288 )->response == CMD_OK;
289 }
290  
291  
292 sub fields
293 {
294 my $ph = shift;
295  
296 $ph->command("fields", _arg_list(\@_));
297  
298 my $ln;
299 my %resp;
300 my $cur_num = 0;
301 my @tags = ();
302  
303 while(defined($ln = $ph->getline))
304 {
305 $ph->debug_print(0,$ln)
306 if ($ph->debug & 2);
307 chomp($ln);
308  
309 my($code,$num,$tag,$data,$last_tag);
310  
311 if($ln =~ /^-(\d+):(\d+):\s*([^:]*):\s*(.*)/o)
312 {
313 ($code,$num,$tag,$data) = ($1,$2,$3,$4);
314  
315 $tag = $last_tag
316 unless(length($tag));
317  
318 $last_tag = $tag;
319  
320 if(exists $resp{$tag})
321 {
322 $resp{$tag}->[3] .= "\n" . $data;
323 }
324 else
325 {
326 $resp{$tag} = bless [$code, $num, $tag, $data], "Net::PH::Result";
327 push @tags, $tag;
328 }
329 }
330 else
331 {
332 $ph->set_status($ph->parse_response($ln));
333 return wantarray ? (\%resp, \@tags) : \%resp;
334 }
335 }
336  
337 return;
338 }
339  
340 sub quit
341 {
342 my $ph = shift;
343  
344 $ph->close
345 if $ph->command("quit")->response == CMD_OK;
346 }
347  
348 ##
349 ## Net::Cmd overrides
350 ##
351  
352 sub parse_response
353 {
354 return ()
355 unless $_[1] =~ s/^(-?)(\d\d\d):?//o;
356 ($2, $1 eq "-");
357 }
358  
359 sub debug_text { $_[2] =~ /^(clear)/i ? "$1 ....\n" : $_[2]; }
360  
361 package Net::PH::Result;
362  
363 sub code { shift->[0] }
364 sub value { shift->[1] }
365 sub field { shift->[2] }
366 sub text { shift->[3] }
367  
368 package Net::PH::crypt;
369  
370 # The code in this package is based upon 'cryptit.c', Copyright (C) 1988 by
371 # Steven Dorner, and Paul Pomes, and the University of Illinois Board
372 # of Trustees, and by CSNET.
373  
374 use integer;
375 use strict;
376  
377 sub ROTORSZ () { 256 }
378 sub MASK () { 255 }
379  
380 my(@t1,@t2,@t3,$n1,$n2);
381  
382 sub crypt_start {
383 my $pass = shift;
384 $n1 = 0;
385 $n2 = 0;
386 crypt_init($pass);
387 }
388  
389 sub crypt_init {
390 my $pw = shift;
391 my $i;
392  
393 @t2 = @t3 = (0) x ROTORSZ;
394  
395 my $buf = crypt($pw,$pw);
396 return -1 unless length($buf) > 0;
397 $buf = substr($buf . "\0" x 13,0,13);
398 my @buf = map { ord $_ } split(//, $buf);
399  
400  
401 my $seed = 123;
402 for($i = 0 ; $i < 13 ; $i++) {
403 $seed = $seed * $buf[$i] + $i;
404 }
405 @t1 = (0 .. ROTORSZ-1);
406  
407 for($i = 0 ; $i < ROTORSZ ; $i++) {
408 $seed = 5 * $seed + $buf[$i % 13];
409 my $random = $seed % 65521;
410 my $k = ROTORSZ - 1 - $i;
411 my $ic = ($random & MASK) % ($k + 1);
412 $random >>= 8;
413 @t1[$k,$ic] = @t1[$ic,$k];
414 next if $t3[$k] != 0;
415 $ic = ($random & MASK) % $k;
416 while($t3[$ic] != 0) {
417 $ic = ($ic + 1) % $k;
418 }
419 $t3[$k] = $ic;
420 $t3[$ic] = $k;
421 }
422 for($i = 0 ; $i < ROTORSZ ; $i++) {
423 $t2[$t1[$i] & MASK] = $i
424 }
425 }
426  
427 sub encode {
428 my $sp = shift;
429 my $ch;
430 my $n = scalar(@$sp);
431 my @out = ($n);
432 my $i;
433  
434 for($i = 0 ; $i < $n ; ) {
435 my($f0,$f1,$f2) = splice(@$sp,0,3);
436 push(@out,
437 $f0 >> 2,
438 ($f0 << 4) & 060 | ($f1 >> 4) & 017,
439 ($f1 << 2) & 074 | ($f2 >> 6) & 03,
440 $f2 & 077);
441 $i += 3;
442 }
443 join("", map { chr((($_ & 077) + 35) & 0xff) } @out); # ord('#') == 35
444 }
445  
446 sub encryptit {
447 my $from = shift;
448 my @from = map { ord $_ } split(//, $from);
449 my @sp = ();
450 my $ch;
451 while(defined($ch = shift @from)) {
452 push(@sp,
453 $t2[($t3[($t1[($ch + $n1) & MASK] + $n2) & MASK] - $n2) & MASK] - $n1);
454  
455 $n1++;
456 if($n1 == ROTORSZ) {
457 $n1 = 0;
458 $n2++;
459 $n2 = 0 if $n2 == ROTORSZ;
460 }
461 }
462 encode(\@sp);
463 }
464  
465 1;
466  
467 __END__