PSYC/Storage.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 145 CC 15 SCAR 33.5
CC
15
Cov
0%
nick
72.0
readpass
20.0
readUNI
12.0
CRAP 28.6
1 package Net::PSYC::Storage;
2 #
3 # this provides access to information in ~/.psyc files -lynX
4 # and PSYC environment variables according to http://psyc.pages.de/storage
5 #
6 # ... and maybe one day implements also _request_retrieve and _request_store
7 # but then i'd have to do the linking as well.. and that doesn't belong here
8 #
9 our $VERSION = '0.1';
10  
11 use strict;
12 use Carp;
13 use Net::PSYC;
14 use base 'Exporter';
15  
16 our @EXPORT = qw(UNI nick pass);
17  
18 my $UNL = $ENV{PSYCUNL};
19 my $UNI = $ENV{PSYC};
20 my $pass = $ENV{PSYCPASS};
21 my $nick = undef;
22  
23 sub path () { "/.psyc/" }
24  
25 sub readUNI () {
26 my $I;
27 unless (open($I, $ENV{HOME}.path.'me')) {
28 warn "Consider putting your <UNI> into ~/.psyc/me\n...";
29 return 0;
30 }
31 while(<$I>) {
32 # last if ($nick, $UNI, $pass) = /^(\S+)\s+(psyc:\S+)\s*(\S*)$/;
33 last if ($UNI) = /^(psyc:\S+)$/;
34 }
35 close $I;
36 return $UNI;
37 }
38  
39 sub readUNL () {
40 my $I;
41 unless (open($I, $ENV{HOME}.path.'unl')) {
42 warn "Consider putting your <UNL> (containing your dyndns.host) into ~/.psyc/unl\n...";
43 return 0;
44 }
45 while(<$I>) {
46 # last if ($nick, $UNI, $pass) = /^(\S+)\s+(psyc:\S+)\s*(\S*)$/;
47 last if ($UNL) = /^(psyc:\S+)$/;
48 }
49 close $I;
50 return $UNL;
51 }
52  
53 sub readpass {
54 my $me = lc(shift);
55 my ($I, $UNI);
56  
57 return 0 unless ($me);
58 unless (open($I, $ENV{HOME}.path.'auth')) {
59 warn "Consider putting '<UNI> <pass>' into ~/.psyc/auth\n...";
60 return 0;
61 }
62 while(<$I>) {
63 ($UNI, $pass) = /^(psyc:\S+)\s+(\S+)$/;
64 if (lc($UNI) eq $me) {
65 close($I);
66 return $pass;
67 }
68 }
69 close $I;
70 return 0;
71 }
72  
73 sub UNL () { $UNL || readUNL; }
74 sub UNI () { $UNI || readUNI; }
75 sub pass { $pass || readpass($_[0]||$UNI); }
76 sub nick () {
77 return $nick if $nick;
78 return $nick if $nick = $ENV{PSYCNICK} || $ENV{NICK};
79 if (UNI) {
80 my ($user, $host, $port, $type, $object) = parse_uniform(UNI);
81 return $nick = $user if $user;
82 return $nick = $1 if $object =~ m#~(\S+)/?#;
83 }
84 return $nick if $nick = $ENV{IRCNICK};
85 return $nick if $nick = $ENV{USER}
86 and $nick ne 'root' && $nick ne 'daemon';
87 return $nick = $ENV{HOST} || 'someone';
88 }
89  
90 1;