blib/lib/Mail/Alias.pm

filter: Statementstmt 60.2% 156 / 259 Branchbran 42.5% 46 / 108 Conditioncond 59.0% 13 / 22 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 68.9% 20 / 29 Podpod 71.4% 10 / 14 totaltotal 56.2% 251 / 446 CC 64 SCAR 29.6
CC
64
Cov
55%
update
72.0
write
56.0
write
42.0
CRAP 19.3
↑ Summary
1 # Mail::Alias.pm
2 #
3 # Version 1.14 Date: 26 February 2022
4 #
5 # Copyright (c) 2022 Jonathan Kamens <jik@kamens.us>. All rights reserved.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the same terms as Perl itself.
8 #
9 # Portions of earlier versions of this program were copyrighted by Tom
10 # Zeltwanger and Graham Barr. The current copyright holder extends full
11 # authorship rights to both of the previous authors.
12  
13 # PERLDOC documentation is found at the end of this file
14  
15  
16  
17 ##################################
18 package Mail::Alias; #
19 ##################################
20  
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 $VERSION = 1.14;
25 0 ▶sub Version { $VERSION }
Subroutine Version: not called CC=1 SCAR=6.9
Pod undocumented
26  
27  
28 # Global variable initialization
29 1 my $alias_error = ""; # String used for returning error messages
30 1 my $aliases_file_default = "/etc/mail/aliases"; # The default aliases file name
31 1 my $max_alias_length = "40"; # The max number of characters in aliases
32 1 my $alias_nochar = "@[]"; # Characters not allowed in aliases
33  
34  
35 #-------------#
36 # new() method#
37 #-------------#
38  
39 sub new {
40  
41 2 ▶ my ($class, $object, $filename);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
42 2 $class = shift; # Get the class name
43  
44 2 $filename = $aliases_file_default; # Use the default filenname
45 2 ▶ if (defined($_[0])) { # Unless a new name was passed as 1st argument
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 2 0 2
46 2 $filename = $_[0];
47 }
48
49 2 $object = { _filename => $filename,
50 _errormsg => "no error reported",
51 _usemem => "0",
52 _usefile=> "1"
53 };
54
55 2 my $self = bless ($object, $class);
56
57 2 $self->_init($filename); # Execute the _init method for the calling class
58  
59
60 2 return $object;
61  
62 }
63  
64  
65 #----------#
66 # _init() #
67 #----------#
68 sub _init {
69 1 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
70 1 $self->usefile; # If Alias object, default to file access
71
72 }
73  
74  
75  
76 #----------#
77 # format() #
78 #----------#
79 sub format {
80 0 ▶ my $me = shift;
Subroutine format: not called CC=2 SCAR=17.9
Pod documented
81 0 my $fmt = shift;
82 0 my $pkg = "Mail::Alias::" . $fmt;
83  
84 croak "Unknown format '$fmt'"
85 0 ▶ unless @{$pkg . "::ISA"};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$pkg . '::ISA';} 0 0 0
86  
87 0 bless $me, $pkg;
88 }
89  
90  
91 #----------#
92 # usemem() #
93 #----------#
94  
95 sub usemem {
96 1 ▶ my $self = shift;
Subroutine usemem: called CC=1 SCAR=0.0
Pod documented
97 1 $self->{_usemem} = "1";
98 1 $self->{_usefile} = "0";
99 1 return;
100 }
101  
102  
103 #----------#
104 # usefile()#
105 #----------#
106  
107 sub usefile {
108 1 ▶ my $self = shift;
Subroutine usefile: called CC=1 SCAR=0.0
Pod documented
109 1 $self->{_usefile} = "1";
110 1 $self->{_usemem} = "0";
111 1 return;
112 }
113  
114  
115  
116 #----------#
117 # exists() #
118 #----------#
119 sub exists {
120 3 ▶ my ($self, $alias) = @_;
Subroutine exists: called CC=5 SCAR=16.1
Pod documented
121  
122
123 3 ▶ if ($self->{_usemem}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_usemem'}) { } 1 2 3
124 1 return defined $self->{$alias};
125  
126 }
127 else {
128
129 2 my ($self, $alias) = @_;
130 2 my ($text_line) = undef; # Temp storage of the line from the alias file
131  
132 2 $aliases_file = $self->{_filename};
133  
134 2 ▶ open (ALIASES_FILE , $aliases_file) || die "ERROR: Can't open $aliases_file\n";
Branch: 1 branch
BranchTrueFalseTotal
open ALIASES_FILE, $aliases_file or die "ERROR: Can't open $aliases_file\n" 0 2 2
135  
136 # search till alias is found or EOF
137 2 ▶ while (<ALIASES_FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ALIASES_FILE)) 25 1 26
138 25 ▶ if (/^$alias:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^$alias:/i) 1 24 25
139 1 $text_line = $_;
140 1 chomp($text_line);
141 1 close ALIASES_FILE;
142 1 return $text_line;
143 }
144  
145 }
146  
147 # If you got here, the EOF was hit - returns undefined
148 1 $self->{_errormsg} = "ERROR: There is no alias $alias in $aliases_file";
149 1 close ALIASES_FILE;
150 1 return undef;
151  
152 }
153 }
154  
155  
156 #----------#
157 # expand() #
158 #----------#
159 sub expand {
160 1 ▶ my $me = shift;
Subroutine expand: called CC=5 SCAR=16.4
Pod documented
161 1 my @result = ();
162 1 my %done = ();
163 1 my $alias;
164 1 my @todo = @_;
165  
166 1 ▶ while($alias = shift(@todo)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias = shift @todo) 1 1 2
167 1 ▶ next if(defined $done{$alias});
Branch: 1 branch
BranchTrueFalseTotal
if defined $done{$alias} 0 1 1
168 1 $done{$alias} = 1;
169 1 ▶ if(defined $me->{$alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{$alias}) { } 1 0 1
170 1 push(@todo,@{$me->{$alias}});
171 }
172 else {
173 0 push(@result,$alias);
174 }
175 }
176 1 ▶ wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
177 }
178  
179  
180  
181 #---------------------------------#
182 # Alias::append() Method #
183 # Version 1.0 8/19/00 #
184 #---------------------------------#
185  
186 sub append {
187  
188 1 ▶ my $return_string;
Subroutine append: called CC=4 SCAR=15.2
Pod documented
189 1 my ($self, $alias, $address_string) = @_;
190  
191 # Die if no alias was passed
192 1 ▶ unless ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alias) 0 1 1
193 0 die "ERROR: Alias::append requires an Alias argument\n";
194 }
195
196 1 $aliases_file = $self->{_filename};
197
198 1 ▶ if ($self->exists($alias)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exists($alias)) { } 0 1 1
199 0 $self->{_errormsg} = "ERROR: $alias is already in the file $aliases_file\n";
200 0 undef ($return_string);
201 }
202
203 else {
204  
205 1 ▶ open (ALIASES_FILE ,">>$aliases_file") || die "ERROR: Can't open $alias_file\n";
Branch: 1 branch
BranchTrueFalseTotal
open ALIASES_FILE, ">>$aliases_file" or die "ERROR: Can't open $alias_file\n" 0 1 1
206 1 print ALIASES_FILE "$alias: $address_string\n";
207 1 close ALIASES_FILE;
208 1 $return_string = "1"; # Successfully added the alias
209  
210 } # ELSE
211  
212 }
213  
214  
215 #------------------------------#
216 # Alias::delete() Method #
217 # Version 1.0 8/13/00#
218 #------------------------------#
219  
220 sub delete {
221
222 1 ▶ my ($self, @alias_list) = @_;
Subroutine delete: called CC=7 SCAR=19.5
Pod documented
223 1 $filename = $self->{_filename};
224 1 my $deleted = undef;
225
226  
227
228
229 1 my $working_file = ($filename . ".tmp");
230 1 rename ("$filename", "$working_file");
231
232
233 1 ▶ open (NEW_FILE ,">$filename")
Branch: 1 branch
BranchTrueFalseTotal
open NEW_FILE, ">$filename" or die "ERROR: Can't open $filename\n" 0 1 1
234 || die "ERROR: Can't open $filename\n";
235
236
237 1 ▶ open (EXISTING_FILE , "$working_file")
Branch: 1 branch
BranchTrueFalseTotal
open EXISTING_FILE, "$working_file" or die "ERROR: Can't open $working_file\n" 0 1 1
238 || die "ERROR: Can't open $working_file\n";
239  
240
241
242  
243 1 ▶ while (defined ($textline = <EXISTING_FILE>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($textline = readline EXISTING_FILE)) 13 1 14
244 13 chomp ($textline);
245
246
247 13 ▶ if (($textline =~ /^\s*$/) || ($textline =~ /^#/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($textline =~ /^\s*$/ or $textline =~ /^#/) { } 7 6 13
Condition100%3/3
$textline =~ /^\s*$/ or $textline =~ /^#/
l!l&&r!l&&!r
436
MC/DC100%2/2
$textline =~ /^\s*$/ or $textline =~ /^#/
$textline =~ /^\s*$/ $textline =~ /^#/
Truth table100%3/3
A or B
A= $textline =~ /^\s*$/B= $textline =~ /^#/
ABresult
1X1
011
000
248 7 print NEW_FILE "$textline\n";
249 }
250  
251 else {
252
253
254
255 6 ▶ if (!alias_check ($textline , \@alias_list)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not alias_check($textline, \@alias_list)) { } 5 1 6
256 5 print NEW_FILE "$textline\n";
257 }
258
259 else {
260 1 print "DELETING: $textline\n";
261 1 $deleted = "1";
262 }
263
264
265 }
266
267 }
268  
269 # Close the files
270 1 close EXISTING_FILE;
271 1 close NEW_FILE;
272 1 return $deleted;
273
274 } # end delete
275  
276  
277 #------------------------------#
278 # Alias::update() Method #
279 # Version 1.0 8/13/00#
280 #------------------------------#
281  
282 sub update {
283  
284 0 ▶ my ($self, $alias, $address_string) = @_;
Subroutine update: not called CC=8 SCAR=42.8
Pod documented
285 0 my ($found_it, $alias_line);
286  
287 0 undef $found_it;
288  
289 # Form the alias line from the passed arguments
290 0 ▶ if ($address_string) { # If there is a second argument passed
Branch: 1 branch
BranchTrueFalseTotal
if ($address_string) { } 0 0 0
291 0 $alias_line = "$alias" . ": " . " $address_string";
292 }
293 else {
294 0 $alias_line = $alias; # The whole alias line is in $alias
295 0 $alias_line =~ /^(\S+)\s*:\s*(\S*)$/; # Extract the alias from the alias_line
296 0 $alias = $1;
297 }
298
299
300 0 $filename = $self->{_filename}; # Get the name of the aliases_file to be updated
301  
302
303
304 0 my $working_file = ($filename . ".tmp");
305 0 rename ("$filename", "$working_file");
306
307
308 0 ▶ open (NEW_FILE ,">$filename")
Branch: 1 branch
BranchTrueFalseTotal
open NEW_FILE, ">$filename" or die "ERROR: Can't open $filename\n" 0 0 0
309 || die "ERROR: Can't open $filename\n";
310
311
312 0 ▶ open (EXISTING_FILE , "$working_file")
Branch: 1 branch
BranchTrueFalseTotal
open EXISTING_FILE, "$working_file" or die "ERROR: Can't open $working_file\n" 0 0 0
313 || die "ERROR: Can't open $working_file\n";
314  
315
316
317 0 ▶ while (defined ($textline = <EXISTING_FILE>)) { # For every line
Branch: 1 branch
BranchTrueFalseTotal
if (defined($textline = readline EXISTING_FILE)) 0 0 0
318
319 # If line is blank or comment, just write it out
320 0 chomp ($textline);
321
322 0 ▶ if (($textline =~ /^\s+$/) || ($textline =~ /^#/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($textline =~ /^\s+$/ or $textline =~ /^#/) { } 0 0 0
Condition0%0/3
$textline =~ /^\s+$/ or $textline =~ /^#/
l!l&&r!l&&!r
000
MC/DC0%0/2
$textline =~ /^\s+$/ or $textline =~ /^#/
$textline =~ /^\s+$/ $textline =~ /^#/
Truth table0%0/3
A or B
A= $textline =~ /^\s+$/B= $textline =~ /^#/
ABresult
1X1
011
000
323 0 print NEW_FILE "$textline\n";
324 }
325  
326 else { # Process alias lines here
327
328
329 0 ▶ if ($textline =~ /^$alias:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($textline =~ /^$alias:/i) { } 0 0 0
330 0 print NEW_FILE "$alias_line\n";
331 0 $found_it = "1";
332 }
333
334 else {
335
336 0 print NEW_FILE "$textline\n";
337
338 }
339  
340  
341 }
342
343 }
344  
345
346 0 close EXISTING_FILE;
347 0 close NEW_FILE;
348  
349 0 return $found_it;
350  
351 } # end update
352  
353  
354 #-------------------#
355 # valid_alias Method#
356 #-------------------#
357 # valid_alias performs validation of the alias passed as an argument.
358 # Return 1 if success and UNDEF if the test fails
359  
360 sub valid_alias {
361  
362 0 ▶ my ($self, $alias) = @_; # Get the alias
Subroutine valid_alias: not called CC=3 SCAR=24.8
Pod undocumented
363 0 my $return_string = 1; # Set return for success
364  
365 0 ▶ if (($alias =~ /[$alias_nochar]/) || (length($alias) > $max_alias_length))
Branch: 1 branch
BranchTrueFalseTotal
if ($alias =~ /[$alias_nochar]/ or length $alias > $max_alias_length) 0 0 0
Condition0%0/3
$alias =~ /[$alias_nochar]/ or length $alias > $max_alias_length
l!l&&r!l&&!r
000
MC/DC0%0/2
$alias =~ /[$alias_nochar]/ or length $alias > $max_alias_length
$alias =~ /[$alias_nochar]/ length $alias > $max_alias_length
Truth table0%0/3
A or B
A= $alias =~ /[$alias_nochar]/B= length $alias > $max_alias_length
ABresult
1X1
011
000
366 0 { undef($return_string)
367 }
368
369 0 return $return_string;
370  
371 }
372  
373  
374 #------------------#
375 # alias_file Method#
376 #------------------#
377 # alias_file returns the complete path to the alias file that is being operated upon
378 # by the Mail::Alias methods.
379 # If a filename is passed as an argument, it is set to be the new filename for
380 # all future operations. The file must exist or nothing is done.
381  
382 sub alias_file {
383  
384 1 ▶ my ($self, $newname) = @_; # Get the new name if one was passed
Subroutine alias_file: called CC=3 SCAR=17.3
Pod documented
385  
386 # If an argument was passed, make it the new $aliases_file value and return
387 1 ▶ if ($newname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newname) { } 0 1 1
388  
389
390 0 ▶ if (-e $newname) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $newname) { } 0 0 0
391 0 $self->{_filename} = $newname;
392 0 return "$newname";
393 }
394  
395 else {
396
397 0 $self->{_errormsg} = "ERROR: $newname does not exist\n";
398 0 return undef;
399 }
400
401 }
402
403  
404 # If no argument, just return the current working aliases file pathname
405 else {
406  
407 1 return $self->{_filename};
408 }
409
410 }
411  
412  
413 #------------#
414 # error_check#
415 #------------#
416 # Returns the last error message in a text string
417 # This method can be used after any method failed (i.e. returned UNDEF)
418  
419 sub error_check {
420  
421 0 ▶ my $self = shift;
Subroutine error_check: not called CC=1 SCAR=6.9
Pod undocumented
422 0 my $return_string;
423
424
425 0 $return_string = $self->{_errormsg};
426
427
428 0 $self->{_errormsg} = "No error found";
429
430 0 return $return_string;
431  
432 }
433  
434  
435 #------------#
436 # alias_check#
437 #------------#
438 # Check a line of text to see if it begins with any alias in the alias_list
439 # Return the matching alias if found or UNDEF if no match exists
440 # Alias matching is not case sensitive
441  
442 sub alias_check {
443 # Define variables and get arguments
444 6 ▶ my ($list_length, $list_index, $text);
Subroutine alias_check: called CC=3 SCAR=11.0
Pod undocumented
445 6 $text = $_[0]; # 1st argument is the line of text
446 6 $list = $_[1]; # 2nd argument is an array reference
447  
448 # Extract the first non-whitespace from the text_line
449
450 6 $text =~ /^\s*(\S+)\s+/;
451 6 $text = $1;
452 6 $text =~ s/://; # Get rid of trailing :
453  
454 # Search for the string
455 6 $list_length = @$list;
456  
457 6 ▶ for ($list_index = 0; $list_index < $list_length; $list_index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_index < $list_length) 6 5 11
458  
459 # Check each alias for a match with the beginning of the text line
460 # to get a match, the alias must be:
461 # the first non-whitespace on the line
462 # followed by whitespace or a : character
463 6 ▶ if ($text =~ /^\s*$$list[$list_index]:?\s*$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^\s*$list->[$list_index]:?\s*$/i) 1 5 6
464 1 return $$list[$list_index]; # Return the matching string from the list
465 }
466
467 }
468
469
470 5 return undef;
471
472 }
473  
474  
475 #############################################################
476 package Mail::Alias::Sendmail; #
477 # Defines the Sendmail alias class read() and write() #
478 #############################################################
479  
480 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
481 #use Mail::Address;
482  
483 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
484  
485 1 @ISA = qw(Mail::Alias);
486  
487  
488 #----------#
489 # _init() #
490 #----------#
491 sub _init {
492 1 ▶ my ($self, $filename) = @_;
Subroutine _init: called CC=2 SCAR=7.1
493  
494 1 ▶ $self->read($filename) if($filename);
Branch: 1 branch
BranchTrueFalseTotal
if $filename 1 0 1
495 1 $self->usemem; # If Alias::Sendmail object, default to memory access
496  
497 }
498  
499  
500 #---------#
501 # write() #
502 #---------#
503  
504 sub write {
505 0 ▶ my $me = shift;
Subroutine write: not called CC=6 SCAR=37.4
506 0 my $file = shift;
507 0 my $alias;
508 my $fd;
509 0 local *ALIAS;
510  
511 0 ▶ if(ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 0 0 0
512 0 $fd = $file;
513 }
514 else {
515 0 ▶ open(ALIAS,$file) || croak "Cannot open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ALIAS, $file or croak("Cannot open ${file}: $!\n") 0 0 0
516 0 $fd = \*ALIAS;
517 }
518  
519 0 foreach $alias (sort keys %$me) {
520 0 ▶ unless ($alias =~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alias =~ /^_/) 0 0 0
521 0 my $ln = $alias . ": " . join(", ",@{$me->{$alias}});
522 0 $ln =~ s/(.{55,78},)/$1\n\t/g;
523 0 print $fd $ln,"\n";
524 }
525 }
526  
527 0 ▶ close(ALIAS) if($fd == \*ALIAS);
Branch: 1 branch
BranchTrueFalseTotal
if $fd == \*ALIAS 0 0 0
528 }
529  
530 #-----------------------------------------------------------#
531 # _include_file Local sub for expanding :include: files #
532 #-----------------------------------------------------------#
533 sub _include_file {
534 1 ▶ my $file = shift;
Subroutine _include_file: called CC=3 SCAR=13.6
535 1 local *INCLUDE;
536 1 my @ln;
537 1 local $_;
538 1 ▶ open(INCLUDE,$file) or carp "Cannot open file '$file'" and return "";
Branch: 1 branch
BranchTrueFalseTotal
open INCLUDE, $file or carp("Cannot open file '${file}'") and return "" 1 0 1
Condition50%2/4
open(INCLUDE, $file) || carp("Cannot open file '${file}'") && (return '')
l!l
01
carp("Cannot open file '${file}'") and return ""
!ll
01
MC/DC0%0/2
open(INCLUDE, $file) || carp("Cannot open file '${file}'") && (return '')
open(INCLUDE, $file) carp("Cannot open file '${file}'")
Truth table33%1/3
A || B
A= open(INCLUDE, $file)B= carp("Cannot open file '${file}'")
ABresult
1X1
011
000
539 0 @ln = grep(/^[^#]/,<INCLUDE>);
540 0 close(INCLUDE);
541 0 chomp(@ln);
542 0 join(",",@ln);
543 }
544  
545 #--------#
546 # read() #
547 #--------#
548 sub read {
549 1 ▶ my $me = shift;
Subroutine read: called CC=13 SCAR=25.7
550 1 my $file = shift;
551  
552 1 local *ALIAS;
553 1 local $_;
554 1 ▶ open(ALIAS,$file) || croak "Cannot open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ALIAS, $file or croak("Cannot open ${file}: $!\n") 0 1 1
555  
556 1 my $group = undef;
557 1 my $line = undef;
558  
559 1 ▶ while(defined($_ = <ALIAS>) or defined($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ALIAS) or defined $line) 13 0 13
Condition66%2/3
defined($_ = readline ALIAS) or defined $line
l!l&&r!l&&!r
1210
MC/DC0%0/2
defined($_ = readline ALIAS) or defined $line
defined($_ = readline ALIAS) defined $line
Truth table66%2/3
A or B
A= defined($_ = readline ALIAS)B= defined $line
ABresult
1X1
011
000
560 13 ▶ if(defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 12 1 13
561 12 chomp;
562 12 ▶ if(defined $line && /^\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and /^\s/) 1 11 12
Condition100%3/3
defined $line and /^\s/
!ll&&!rl&&r
831
MC/DC100%2/2
defined $line and /^\s/
defined $line /^\s/
Truth table100%3/3
A and B
A= defined $lineB= /^\s/
ABresult
0X0
100
111
563 1 $line .= $_;
564 1 next;
565 }
566 }
567 12 ▶ if(defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 4 8 12
568 4 ▶ if($line =~ s/^([^:]+)://) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^([^:]+)://) 4 0 4
569 4 my @resp;
570 4 $group = $1;
571 4 $group =~ s/(\A\s+|\s+\Z)//g;
572 4 $line =~ s/\"?:include:\s*(\S+)\"?/_include_file($1)/eg;
573 4 $line =~ s/(\A[\s,]+|[\s,]+\Z)//g;
574  
575 4 ▶ while(length($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line) 4 4 8
576 4 $line =~ s/\A([^\"][^ \t,]+|\"[^\"]+\")(\s*,\s*)*//;
577 4 push(@resp,$1);
578 }
579  
580 4 $me->{$group} = \@resp;
581 }
582 4 undef $line;
583 }
584 12 ▶ last if (! defined $_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 1 11 12
585 11 ▶ next if (/^#/ || /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ or /^\s*$/ 7 4 11
Condition100%3/3
/^#/ or /^\s*$/
l!l&&r!l&&!r
344
MC/DC100%2/2
/^#/ or /^\s*$/
/^#/ /^\s*$/
Truth table100%3/3
A or B
A= /^#/B= /^\s*$/
ABresult
1X1
011
000
586 4 $line = $_;
587 }
588 1 close(ALIAS);
589 }
590  
591 ###############################
592 package Mail::Alias::Ucbmail; #
593 ###############################
594  
595 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
596  
597 1 @ISA = qw(Mail::Alias::Binmail);
598  
599 ###############################
600 package Mail::Alias::Binmail; #
601 ###############################
602  
603 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
604 #use Mail::Address;
605  
606 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
607  
608 1 @ISA = qw(Mail::Alias);
609  
610 #----------#
611 # _init() #
612 #----------#
613 sub _init {
614 0 ▶ my ($self, $filename) = @_;
Subroutine _init: not called CC=2 SCAR=17.9
615  
616 0 ▶ $self->read($filename) if($filename);
Branch: 1 branch
BranchTrueFalseTotal
if $filename 0 0 0
617 0 $self->usemem; # If Alias::Binmail object, default to memory access
618 }
619  
620  
621 #--------#
622 # read() #
623 #--------#
624 sub read {
625 0 ▶ my $me = shift;
Subroutine read: not called CC=5 SCAR=34.0
626 0 my $file = shift;
627  
628 0 local *ALIAS;
629 0 local $_;
630 0 ▶ open(ALIAS,$file) || croak "Cannot open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ALIAS, $file or croak("Cannot open ${file}: $!\n") 0 0 0
631  
632 0 ▶ while(<ALIAS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ALIAS)) 0 0 0
633 0 ▶ next unless(/^\s*(alias|group)\s+(\S+)\s+(.*)/);
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s*(alias|group)\s+(\S+)\s+(.*)/ 0 0 0
634 0 my($group,$who) = ($2,$3);
635  
636 0 $who =~ s/(\A[\s,]+|[\s,]+\Z)//g;
637  
638 0 my @resp = ();
639  
640 0 ▶ while(length($who)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $who) 0 0 0
641 # $who =~ s/\A([^\"]\S*|\"[^\"]*\")\s*//;
642 # my $ln = $1;
643 # $ln =~ s/\A\s*\"|\"\s*\Z//g;
644 0 $who =~ s/\A\s*(\"?)([^\"]*)\1\s*//;
645 0 push(@resp,$2);
646 # push(@resp,$ln);
647 }
648 0 $me->{$group} = [ @resp ];
649 }
650 0 close(ALIAS);
651 }
652  
653 #---------#
654 # write() #
655 #---------#
656 sub write {
657 0 ▶ my $me = shift;
Subroutine write: not called CC=7 SCAR=40.3
658 0 my $file = shift;
659 0 my $alias;
660 my $fd;
661 0 local *ALIAS;
662  
663 0 ▶ if(ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 0 0 0
664 0 $fd = $file;
665 }
666 else {
667 0 ▶ open(ALIAS,$file) || croak "Cannot open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ALIAS, $file or croak("Cannot open ${file}: $!\n") 0 0 0
668 0 $fd = \*ALIAS;
669 }
670  
671 0 foreach $alias (sort keys %$me) {
672 0 my @a = @{$me->{$alias}};
673 0 ▶ map { $_ = '"' . $_ . '"' if /\s/ } @a;
Branch: 1 branch
BranchTrueFalseTotal
if /\s/ 0 0 0
674 0 ▶ unless ($alias =~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($alias =~ /^_/) 0 0 0
675 0 print $fd "alias $alias ",join(" ",@a),"\n";
676 }
677 }
678  
679 0 ▶ close(ALIAS) if($fd == \*ALIAS);
Branch: 1 branch
BranchTrueFalseTotal
if $fd == \*ALIAS 0 0 0
680 }
681  
682  
683 #############################
684 # Documentation starts here #
685 #############################
686  
687 =head1 NAME
688  
689 Mail::Alias - Maniulates mail alias files of various formats. Works on files directly or loads files into memory and works on the buffer.
690  
691 =head1 SYNOPSIS
692  
693 use Mail::Alias;
694  
695 =head1 DESCRIPTION
696  
697 C<Mail::Alias> can read various formats of mail alias. Once an object has been created it can be used to expand aliases and output in another format.
698  
699  
700 =head1 CONSTRUCTOR
701  
702 =over 4
703  
704 =item B<new ()>
705 Alias objects can be created in two ways;
706 With a format specified- Mail::Alias::Sendmail->new([filename])
707 Without a format specified- Mail::Alias->new([filename]}. Format defaults to
708 SENDMAIL
709 In either case, the filename is optional and, if supplied, it will be read in
710 when the object is created. Available formats are Sendmail, Ucbmail, and
711 Binmail.
712  
713 =back
714  
715 =head1 METHODS
716  
717 =over 4
718  
719 =item B<read ()>
720 Reads an alias file of the specified format into memory. Comments or blank
721 lines are lost upon reading. Due to storage in a hash, ordering of the alias
722 lines is also lost.
723  
724 =item B<write ()>
725 The current set of aliases contained in the object memory are written to a
726 file using the current format.
727 If a filehandle is passed, data is written to the already opened file. If a
728 filename is passed, it is opened and the memory is written to the file.
729 Note: if passing a filename, include the mode (i.e. to write to a file named
730 aliases pass >aliases). Before writing, the alias lines are sorted
731 alphabetically.
732  
733 =item B<format ()>
734 Set the current alias file format.
735  
736 =item B<exists ()>
737 Indicates the presence of the passed alias within the object (if using memory
738 access), or the current aliases file (if using direct file access). For
739 direct file access, the return value is the address string for the alias.
740
741 =item B<expand ()>
742 Expands the passed alias into a list of addresses. Expansion properly handles
743 :include: files, recursion, and continuation lines.Only works when memory
744 access is being used. If the alias is not found in the object, you get back
745 what you sent.
746  
747 =item B<alias_file ()>
748 Sets or gets the name of the current alias filename for direct access.
749  
750 =item B<append () *-Sendmail only-*>
751 Adds an alias to an existing Sendmail alias file. The alias and addresses can
752 be passed as two separate arguments (alias, addresses) or as a single line of
753 text (alias: addresses)
754  
755 =item B<delete () *-Sendmail only-*>
756 Deletes the entry for an alias from the current alias file.
757  
758 =item B<update () *-Sendmail only-*>
759 Replaces the address string entry for an alias in the current alias file.
760  
761 =item B<usemem ()>
762 Sets the working mode to use memory (indirect access). Use read(), write() and
763 format() methods.
764  
765 =item B<usefile ()>
766 Sets the working mode to use files (direct access). Use append() and delete()
767 methods.
768  
769  
770 =back
771  
772 =head1 MAINTAINER
773  
774 Jonathan Kamens <jik@kamens.us> (CPAN author ID: JIK)
775  
776 =head1 COPYRIGHT
777  
778 Copyright (c) 2022 Jonathan Kamens <jik@kamens.us>. All rights reserved. This
779 program is free software; you can redistribute it and/or modify it under the
780 same terms as Perl itself.
781  
782 Portions of earlier versions of this program were copyrighted by Tom Zeltwanger
783 and Graham Barr. The current copyright holder extends full authorship rights to
784 both of the previous authors.
785  
786 =cut
787  
788 1 1;
↑ Summary