/usr/share/perl5/SyncBBDB/localBBDB.pm is in syncbbdb 2.3-6.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | #------------------------------------------------------------------------------
# $Date: 2000/12/03 17:19:19 $
# RCS: $Id: localBBDB.pm,v 1.20 2000/12/03 17:19:19 tdeweese Exp $
#------------------------------------------------------------------------------
package localBBDB;
use strict;
use POSIX qw(strftime);
use bbdb::bbdb;
use bbdb::bbdbRecord;
use SyncUtil::localHashDB;
use SyncBBDB::pilotBBDBMap;
@localBBDB::ISA = qw(localHashDB);
## Phone number stuff...
my $parea = '1?\s*[-.\(]?\s*([2-9][0-9][0-9])[-.\)\s]+';
my $pmain = '([1-9][0-9][0-9])[-.\s]*([0-9][0-9][0-9][0-9])';
my $pext = '[-.x\s]+(\d+)';
my $phoneRE1 = "^\\s*" . $parea . $pmain . $pext . "\\s*\$";
my $phoneRE2 = "^\\s*" . $parea . $pmain . "\\s*\$";
my $phoneRE3 = "^\\s*" . $pmain . $pext . "\\s*\$";
my $phoneRE4 = "^\\s*" . $pmain . "\\s*\$";
my $phoneRE5 = "^[-0-9 EeXxtT.()+]*\$";
sub new {
my $type = shift;
my $self = localHashDB->new(shift, shift);
bless ($self, $type);
$self->{'bbdbFile'} = shift;
$self->{'translate'} = shift;
$self->{'no-hash-fields'} = ['timestamp'];
return $self;
}
sub dupRec
{
my $self = shift;
my $lrec = shift;
my $prec = shift;
my $ret = new bbdbRecord($lrec->ePStr());
$ret->{'notes'}{'creation-date'} = strftime("%Y-%m-%d", localtime);
delete $ret->{'notes'}{'pilot-id'}; ## disassociate from any pilot recs
push(@{$self->{'BBDB'}->{'records'}}, $ret);
push(@{$self->{'BBDB'}->{'sync recs'}},$ret);
return $ret;
}
sub finish
{
my $self = shift;
$self->localHashDB::finish();
my $file = new FileHandle(">$self->{'bbdbFile'}");
$self->{'BBDB'}->eprint($file);
undef $file;
}
sub name
{
my $self = shift;
return "Emacs BBDB";
}
sub setup {
my $self = shift;
# This should give us a rough idea of the number of records;
my $nids = $self->{'hashObj'}->getNumIds();
$nids = 100 if ($nids == 0); # Make sure this isn't 0
$self->{'in bbdb read'} = $nids;
$self->{'in bbdb count'} = 0;
$self->{'BBDB'} = new bbdb($self->{'bbdbFile'}, $self);
if ($self->{'in bbdb count'} eq 0) {
if (! -f $self->{'bbdbFile'}) {
### FIXX: should probably ask the user if he wants to proceed...
$self->getHost()->output("BBDB file not found: ".
$self->{'bbdbFile'}."\n");
}
else {
### FIXX: should probably ask the user if he wants to proceed...
$self->getHost()->output("BBDB file was found but is empty: ".
$self->{'bbdbFile'}."\n");
}
}
delete $self->{'in bbdb read'};
delete $self->{'in bbdb count'};
$self->localHashDB::setup(shift, shift);
}
sub getRecords {
my $self = shift;
if (!defined $self->{'sync recs'}) {
$self->{'sync recs'} = [];
foreach my $rec (@{$self->{'BBDB'}->{'records'}}) {
# Skip adding the record if I'm supposed to ignore it.
push(@{$self->{'sync recs'}}, $rec)
unless (defined $rec->{'notes'}{'pilot-ignore'});
}
}
return $self->{'sync recs'};
}
sub getPilotIDs {
my $self = shift;
my $lrec = shift;
my $map = new pilotBBDBMap($lrec, $self->{'translate'});
my @ret;
foreach my $rec (@{$map->{'records'}}) {
push(@ret, $rec->{'id'});
}
return \@ret;
}
sub deleteRec {
my $self = shift;
my $id = shift;
my $rec = $self->{'hash'}{$id};
return if (not defined $rec);
# clear out the hash map entries...
$self->{'hashObj'}->setLocalHash($id, undef);
$self->{'hashObj'}->setPilotHash($id, undef);
my $map = new pilotBBDBMap($rec, $self->{'translate'});
my $mrec = $map->delMapForID($id);
if (scalar(@{$map->{'records'}})) {
# Other mappings still exist so don't delete the record.
$rec->{'notes'}{'pilot-id'} = $map->toString();
return $rec;
}
# really delete the record...
foreach my $r (@{$self->getRecords()}) {
$r = undef if ((defined $r) && ($r == $rec));
}
return $rec;
}
sub syncRec
{
my $self = shift;
my $prec = shift;
my $lrec = shift;
my $pilotdb = $self->{'pilotDB'};
my $is_new_rec = 0;
if (not defined $lrec) {
$lrec = new bbdbRecord();
$is_new_rec = 1;
push(@{$self->{'BBDB'}->{'records'}}, $lrec);
push(@{$self->{'BBDB'}->{'sync recs'}},$lrec);
}
my $map = new pilotBBDBMap($lrec, $self->{'translate'});
my $mapRec = $map->getMapForID($prec->{'id'});
if (!defined $mapRec) {
# new Pilot Record, need a new mapping record...
$mapRec = new pilotBBDBRecMap($prec->{'id'},
$pilotdb->categoryStr($prec),
"Main");
push(@{$map->{'records'}}, $mapRec);
}
$map->updateMappingFromPilot($pilotdb);
# The fields that look like they are new at this point, which means
# they exist in bbdb but don't have any mappings to the palm or
# as 'skipped' fields, are the fields that were deleted on the palm.
my %toDel = %{$map->getNewBBDBFields()};
foreach my $f (keys %toDel) {
$f =~ m/([^:]+):(.*)/;
my $type = $1;
my $name = $2;
# for net and AKA I just skip deleting them since it is
# particularly unclear if the intent is to remove the
# mapping for the palm records.
if ($type eq "addr") {
$lrec->del_addr($name);
} elsif ($type eq "phone") {
$lrec->del_phone($name);
} elsif ($type eq "note") {
delete $lrec->{'notes'}{$name};
}
}
$lrec->{'lname'} = $pilotdb->getField($prec, 'Last');
$lrec->{'fname'} = $pilotdb->getField($prec, 'First');
$lrec->{'org'} = $pilotdb->getField($prec, 'Company');
my $title = $pilotdb->getField($prec, 'Title');
$lrec->{'notes'}{'title'} = $title if ($title);
my $notes = $pilotdb->getField($prec, 'Note');
$lrec->{'notes'}{'notes'} = $notes if ($notes);
foreach $mapRec (@{$map->{'records'}}) {
$prec = $pilotdb->getRec($mapRec->{'id'});
$self->{'hashObj'}->setLocalHash($mapRec->{'id'}, undef);
$mapRec->{'category'} = $pilotdb->categoryStr($prec);
my $addr;
if (defined $mapRec->{'name'}) {
$addr = $lrec->{'addrH'}{$mapRec->{'name'}};
if (not defined $addr) {
$addr = new bbdbAddr("nil");
$addr->{'name'} = $mapRec->{'name'};
}
} else {
$addr = new bbdbAddr("nil");
$addr->{'name'} = "Main";
}
my $paddr = $pilotdb->getField($prec, 'Address');
my @streets = split("\xA", $paddr) if ($paddr);
$addr->{'street'} = \@streets;
$addr->{'city'} = $pilotdb->getField($prec, 'City');
$addr->{'state'} = $pilotdb->getField($prec, 'State');
my $zip = $pilotdb->getField($prec, 'Zip');
my @zips;
if ($zip) {
if ($self->{'BBDB'}{'version'} ge 6) {
# Ok for version 6 and up we can just slam the zip in
# we store it as the first element of an array...
push(@zips, $zip);
} elsif ($zip =~ m/^[-0-9\s]*$/) {
# US Zip???
@zips = split(/[-\s]+/,$zip);
} else {
# intl zip.
@zips = split(/[\s]+/,$zip);
if (scalar(@zips) < 2) {
# BBDB requires at least two fields for string rep of zip.
# So we will push on an empty string.... (UHGG!)
push(@zips,"");
}
}
}
$addr->{'zip'} = \@zips;
$addr->{'country'} = $pilotdb->getField($prec, 'Country');
# if we have any data push it back to BBDB
if (scalar(@{$addr->{'street'}}) ||
scalar(@{$addr->{'zip'}}) ||
$addr->{'city'} || $addr->{'state'} ||$addr->{'country'})
{
$lrec->add_addr($addr);
$mapRec->{'name'} = $addr->{'name'};
} elsif (defined $mapRec->{'name'}) {
# deleted addr case...
$lrec->del_addr($mapRec->{'name'});
# Delete the association with the address info.
# After all they didn't delete the Pilot record.
delete $mapRec->{'name'};
}
foreach my $ent (@{$mapRec->{'mappings'}}) {
my $btype = $ent->{'bbdb-type'};
my $bname = $ent->{'bbdb-name'};
my $pname = $ent->{'pilot'};
# print STDERR "Type: $btype\n";
my $val = $pilotdb->getField($prec, $pname);
if ($btype eq "phone") {
## Phone number.
my $phone;
if ($val =~ m/$phoneRE1/) {
## (415) 555-1212 x123
$phone = new bbdbPhone("\"$bname\" $1 $2 $3 $4");
} elsif ($val =~ m/$phoneRE2/) {
## (415) 555-1212
$phone = new bbdbPhone("\"$bname\" $1 $2 $3");
} elsif ($val =~ m/$phoneRE3/) {
## 555-1212 x123
$phone = new bbdbPhone("\"$bname\" 0 $1 $2 $3");
} elsif ($val =~ m/$phoneRE4/) {
## 555-1212
$phone = new bbdbPhone("\"$bname\" 0 $1 $2");
} else
{ ## Anything else (International)
$phone = new bbdbPhone("\"$bname\" \"$val\"");
}
$lrec->add_phone($phone);
} elsif ($btype eq "note") {
$lrec->{'notes'}{$bname} = $val;
} elsif ($btype eq "net") {
my @email = @{$lrec->{'email'}};
if (!scalar(@email)) {
$lrec->{'email'} = [ $val ];
} elsif ($email[0] ne $val) {
unshift(@{$lrec->{'email'}}, $val);
}
} elsif ($btype eq "aka") {
my @aka = split(/, */,$val);
$lrec->{'aka'} = \@aka;
} else {
print STDERR "Unknown bbdb-type: $btype\n";
}
}
}
$lrec->{'notes'}{'timestamp'} = strftime("%Y-%m-%d", localtime);
$lrec->{'notes'}{'creation-date'} = strftime("%Y-%m-%d", localtime)
if ($is_new_rec);
$lrec->{'notes'}{'pilot-id'} = $map->toString();
# push the changes back to the various pilot records.
# this will also update the hash values.
$pilotdb->syncRec($lrec);
return $lrec;
}
sub toString
{
my $self = shift;
my $rec = shift;
my %hold;
foreach my $f (@{$self->{'no-hash-fields'}}) {
$hold{$f} = $rec->{'notes'}{$f} if (defined $rec->{'notes'}{$f});
delete $rec->{'notes'}{$f};
}
my $str = $rec->ePStr();
foreach my $f (keys %hold) {
$rec->{'notes'}{$f} = $hold{$f};
}
return $str;
}
sub output
{
my $self = shift;
my $rec = shift;
my $fn = $rec->{'fname'};
my $ln = $rec->{'lname'};
my $str;
if ((defined $fn) && $fn)
{
if ((defined $ln) && $ln)
{ $str = "$fn $ln"; }
else
{ $str = $fn; }
}
else
{
if ((defined $ln) && $ln)
{ $str = $ln; }
else
{
$str = "<No Name>";
if (defined $rec->{'notes'}{'pilot-id'}) {
my $map = new pilotBBDBMap($rec, $self->{'translate'});
my @ids;
foreach my $mrec (@{$map->{'records'}}) {
push(@ids, $mrec->{'id'});
}
$str .= " - " . join(", ", @ids);
}
}
}
$self->getHost()->output($str . "\n");
}
sub update {
my $self = shift;
if (!defined $self->{'in bbdb read'}) {
return $self->localHashDB::update();
}
# we are in the bbdb read so call status instead of 'update'.
$self->{'host'}->status("Reading BBDB Records",
int(100*(($self->{'in bbdb count'}++)/
$self->{'in bbdb read'})));
}
1;
|