File Coverage

blib/lib/sBNC/User/Summary.pm
Criterion Covered Total %
statement 14 69 20.2
branch 0 12 0.0
condition n/a
subroutine 5 13 38.4
pod n/a
total 19 94 20.2


line stmt bran cond sub pod time code
1             package sBNC::User::Summary;
2              
3 1     1   75496 use 5.006;
  1         5  
4              
5 1     1   11 use Carp;
  1         3  
  1         72  
6 1     1   536 use Moose;
  1         545566  
  1         9  
7              
8 1     1   7867 use vars qw/$VERSION/;
  1         4  
  1         846  
9              
10             $VERSION = 1.02;
11              
12             ################################################################################
13              
14             has users => (is => 'ro', isa => 'ArrayRef', required => 1);
15             has dir => (is => 'ro', isa => 'Str', required => 1);
16              
17             has conf_files => (is => 'ro', isa => 'HashRef', lazy_build => 1);
18             has chan_files => (is => 'ro', isa => 'HashRef', lazy_build => 1);
19             has config => (is => 'ro', isa => 'HashRef', lazy_build => 1);
20              
21             ################################################################################
22              
23             sub _file_contents {
24 0     0     my $self = shift;
25 0           my $filename = shift;
26 0           my $type = shift; # conf, chan
27              
28 0 0         my $content = $type eq 'conf'
29             ? $self->_conf_file($filename)
30             : $self->_chan_file($filename);
31              
32 0           return $content;
33             }
34              
35             sub _conf_file {
36 0     0     my $self = shift;
37 0           my $filename = shift;
38              
39 0           my %content;
40              
41 0 0         if (open (my $fh, '<', $filename)) {
42 0           while (my $row = <$fh>) {
43 0           chomp $row;
44              
45 0           my ($handle, $key, $value) = $row =~ /^(\w+)\.(\w+)=(.+)/;
46              
47 0 0         $content{$key} = $value if $key;
48             }
49              
50 0           close $fh;
51             } else {
52 0           warn sprintf("No such channel file %s\n", $filename);
53             }
54              
55 0           return \%content;
56             }
57              
58             sub _chan_file {
59 0     0     my $self = shift;
60 0           my $filename = shift;
61              
62 0           my %content;
63              
64 0 0         if (open (my $fh, '<', $filename)) {
65 0           while (my $row = <$fh>) {
66 0           chomp $row;
67              
68 0           my ($name, $modes) = $row =~ /^channel add (#\w+) \{\ (.+)\ \}$/;
69 0           my %options;
70              
71 0 0         if ($modes) {
72 0           while ($modes =~ /(\w+) (\w+)+/g) {
73 0           $options{$1} = $2;
74             }
75             }
76              
77 0 0         $content{$name} = \%options if $name;
78             }
79              
80 0           close $fh;
81             } else {
82 0           warn sprintf("No such channel file %s\n", $filename);
83             }
84              
85 0           return \%content;
86             }
87              
88             sub _filename {
89 0     0     my $self = shift;
90 0           my $user = shift;
91 0           my $type = shift; # conf, chan
92              
93 0           return sprintf("%s/%s.%s", $self->dir, $user, $type);
94             }
95              
96             sub _file_builder {
97 0     0     my $self = shift;
98 0           my $type = shift;
99              
100 0           my %files;
101              
102 0           foreach my $user (@{$self->users}) {
  0            
103 0           my $filename = $self->_filename($user, $type);
104              
105 0           $files{$user} = $self->_file_contents($filename, $type);
106             }
107              
108 0           return \%files;
109             }
110              
111             ################################################################################
112              
113             sub _build_config {
114 0     0     my $self = shift;
115              
116 0           my %config;
117              
118 0           foreach my $user (@{$self->users}) {
  0            
119 0           my $out = $self->conf_files->{$user};
120              
121 0           $out->{channels} = $self->chan_files->{$user};
122 0           $config{$user} = $out;
123             }
124              
125 0           return \%config;
126             }
127              
128             sub _build_conf_files {
129 0     0     my $self = shift;
130              
131 0           return $self->_file_builder('conf');
132             }
133              
134             sub _build_chan_files {
135 0     0     my $self = shift;
136              
137 0           return $self->_file_builder('chan');
138             }
139              
140             ################################################################################
141              
142 1     1   8 no Moose;
  1         2  
  1         5  
143             1;
144             __END__
145              
146             =head1 NAME
147              
148             sBNC::User::Summary - Translate sBNC user files into usable objects.
149              
150             =head1 DESCRIPTION
151              
152             Takes input of sBNC's config flatfiles (.conf, .chan) for users and translates
153             them into a Perl-friendly format.
154              
155             =head1 VERSION
156              
157             Version 1.01
158              
159             =head2 USAGE
160              
161             my $users = sBNC::User::Summary->new({
162             users => [ 'Mike', 'Dave' ],
163             dir => '/absolute/path/to/sBNC/users/',
164             });
165              
166             my $output = $users->config;
167              
168             =head1 AUTHOR
169              
170             Mike Jones L<email:mike@netsplit.org.uk>
171              
172             =head1 LICENSE
173              
174             Copyright (c) Mike Jones 2016
175              
176