#!/usr/bin/perl # XXX - Don't use this! It's unstable! use Palm::Address; use ColdSync; $MH_ALIASES = "$ENV{HOME}/.mh_aliases"; ConduitMain( "fetch" => \&DoFetch, "dump" => \&DoDump, ); sub DoFetch { my %aliases = (); $PDB = new Palm::PDB; $PDB->Load($HEADERS{"InputDB"} || $HEADERS{"OutputDB"}); open ALIASES, "< $MH_ALIASES" or die "Can't open $MH_ALIASES: $!\n"; alias: while () { chomp; next if /^\s*;/; # Skip comments # Look for lines of the form # alias: address (Full Name) # e.g., # arensb: arensb@ooblick.com (Andrew Arensburger) # # XXX - Note that neither this form, nor the pattern # that follows, are perfect. E-mail addresses, in the # general case, are horribly difficult to do right. next unless /^([-+\w]+):\s+(.*)\s+\((.*)\)\s*$/; my $alias = $1; my $addr = $2; my $fullname = $3; $addr =~ s/<(.*)>/$1/; # Strip brackets # XXX - This is overly simplistic: it assumes that # each name only has one e-mail address. $aliases{$fullname} = $addr; print STDERR "aliases($fullname) == [$aliases{$fullname}]\n"; } close ALIASES; my $name; name: foreach $name (keys %aliases) { $record = &find_person($PDB, $name); next if !defined($record); # No entry in PDB # XXX - Function to extract e-mail address(es) from PDB. my $field; foreach $field ( qw( phone1 phone2 phone3 phone4 phone5 ) ) { next unless $record->{"phoneLabel"}{$field} == 4; # Found an e-mail field # XXX - This is oversimplified: it assumes # that each record in the PDB only has at most # one e-mail field. next name if $aliases{$name} eq $record->{"fields"}{$field}; # The address in the PDB is different from the # one in the alias file. # XXX - Function to add e-mail address to PDB. $record->{"fields"}{$field} = $aliases{$name}; $record->{"attributes"}{"dirty"} = 1; # Mark the record as dirty. next name; # Go on to the next person } # If we get this far, the PDB record doesn't have an # "E-mail" field. Ignore it. # XXX - The Right Thing to do would be to see if # there's a "Main" or "Other" phone field that # contains the requisite address(es). # If the address from .mh_aliases really doesn't # appear anywhere in the record, the Right Thing to do # would be to find an empty field, convert it to an # E-mail field, and add the address. } return 1; # Success } sub DoDump { open ALIASES, "< $MH_ALIASES" or die "502 Can't read $MH_ALIASES: $!\n"; open ALIASES_NEW, "> $MH_ALIASES.new" or die "502 Can't open $MH_ALIASES.new for writing: $!\n"; # Read the existing .mh_aliases file, one entry at a time. The # aliases that appear in the PDB will be updated and written # to ALIASES_NEW. alias: while () { chomp; if (!/^([-+\w]+):\s+(.*)\s+\((.*)\)\s*$/) { # This isn't a line we're interested in. Just write # it to the output file. print ALIASES_NEW $_, "\n"; next alias; } my $alias = $1; my $addr = $2; my $fullname = $3; my $record; $record = &find_person($PDB, $fullname); if (!defined($record)) { # This name appears in the alias file, but not in # the PDB. Leave it untouched. print ALIASES_NEW $_, "\n"; next alias; } # This person appears in both the alias file and in # the PDB. Get the e-mail address from the record and # write the line to the alias file. my $field; my $pdb_addr; # Address in the PDB foreach $field ( qw( phone1 phone2 phone3 phone4 phone5 ) ) { next unless $record->{"phoneLabel"}{$field} == 4; # Found an e-mail field $pdb_addr = $record->{"fields"}{$field}; print ALIASES_NEW "$alias:\t$pdb_addr ($fullname)\n"; next alias; } # If we get this far, then the PDB record doesn't have # an e-mail address. There are two things we can do: # either ignore it altogether, or comment the line # out. It's not clear which one is better. print STDERR "Commenting out entry for $alias ($fullname)\n"; # Comment it out print ALIASES_NEW "; ", $_, "\n"; } close ALIASES_NEW; close ALIASES; rename "$MH_ALIASES.new", $MH_ALIASES or die "Can't rename $MH_ALIASES.new to $MH_ALIASES: $!\n"; return 1; # Success } sub find_person { my $PDB = shift; my $fullname = shift; my $record; #print STDERR "find_person($fullname)\n"; foreach $record (@{$PDB->{"records"}}) { #print STDERR " examining [$record->{fields}{firstName}] [$record->{fields}{name}]\n"; next unless ($record->{"fields"}{"firstName"} . " " . $record->{"fields"}{"name"}) eq $fullname; #print STDERR " Found it!\n"; return $record; } return undef; # Failure }