#!/usr/local/bin/perl -w # # chkphone # # Check the phone book for consistancy. # This is done as a separate program so that the normal programs # can be done reasonably quickly. # # Check for repeated attributes for a given entry. # Check for unknown attributes # Look for missing (or incomplete) birthdays # Look for missing addresses # # The home for this program is at: # http://mordred.ao.com/phone.list/ # # Dave Regan # 9 May 2000 # regan@mordred.ao.com # # # Future directions: # ------------------ # # The address checking will need to look at the InResidence flag # as well as the address. # use strict; use Getopt::Long; use vars qw(@Attr %Args $DataFile $Version); $Version = 'chkphone v0.10 regan@ao.com'; $DataFile = $ENV{'HOME'} . '/Notes/phone.dat'; # Text data file %Args = ( "birthday" => 0, "address" => 0, ); @Attr = qw(Name E-mail Address1 Address2 Phone WorkPhone CellPhone Pager Modem Fax Reference Comment1 Comment2 Comment3 Comment4 Birthday Anniversary WorksAt ResidenceName InResidence Lists SortAs Savings Savings2); ### ### Main program ### my(%attribute, %attrname, $exit, %inresidence, $line, %residencename, $name, %names, $value); GetOptions(\%Args, "birthday!", "address!"); open(DATA, "<$DataFile") || die "Cannot open $DataFile: $!"; %attrname = map { $_, $_ } @Attr; $line = 0; $exit = 0; while () { chomp; $line++; next if (/^\s*$/ || /^\s*#/); if (/^%%/) { # If we are checking for birthdays, see that it is sane. if ($Args{'birthday'}) { if (!defined($attribute{'Birthday'})) { print "Birthday for $attribute{'Name'} is not known\n"; $exit++; } elsif ($attribute{'Birthday'} =~ /\?/) { print "Birthday for $attribute{'Name'} is not fully known: " . "$attribute{'Birthday'}\n"; $exit++; } } # If we are checking for addresses, see that it is sane. if ($Args{'address'}) { if (!defined($attribute{'Address1'}) && !defined($attribute{'Address2'})) { print "No address for $attribute{'Name'}\n"; $exit++; } } if (defined($attribute{'InResidence'}) && defined($attribute{'Lists'}) && $attribute{'Lists'} =~ /XMAS/) { print "$attribute{'Name'} is in the XMAS list, but " . "is not a residence\n"; $exit++; } if (defined($attribute{'InResidence'}) && defined($attribute{'Address1'})) { print "$attribute{'Name'} has an address, " . "but is not a residence\n"; $exit++; } # See if we already had this person if (defined($names{$attribute{'Name'}})) { print qq|"$attribute{'Name'}" is already in the phone book\n|; $exit++; } $names{$attribute{'Name'}} = 1; $residencename{$attribute{'ResidenceName'}} = $attribute{'Name'} if (defined($attribute{'ResidenceName'})); $inresidence{$attribute{'InResidence'}} = $attribute{'Name'} if (defined($attribute{'InResidence'})); # Clear out the data for the next user undef %attribute; } elsif (/^([^=]*?)\s*=(.*)/) { my($name, $value); ($name, $value) = ($1, $2); $value =~ s/\s+$//; if (defined($attribute{$name})) { print "Line $line: Repeated attribute $name\n"; $exit++; } if (!defined($attrname{$name})) { print "Invalid attribute name $name\n"; $exit++; } $attribute{$name} = $value if ($value ne ""); } else { print "Unexpected line at $line: $_\n"; $exit++; } } # Find out which residences don't match up. for $name (sort keys %inresidence) { if (!defined($residencename{$name})) { print "Cannot find the residence for $name\n"; $exit++; } } exit $exit;