#!/usr/bin/perl -w # Written by Hugo Haas 2003-12-24 # Based on pdb2gcrd (though there's not much left of it): # Written (or modified) by Christian Feldbauer # in August 14, 2001. # Partially taken from Don Harper # Originally by Daniel Smith # This software is freely distributable # as long as the author's Name and Email # address is maintained in the source code. use strict; use Palm::PDB; use Palm::Address; use Getopt::Long; my $VERSION = "0.8"; my $PDBFILE = $ENV{"HOME"}."/.jpilot/AddressDB.pdb"; my $gammu = 0; my $t616hack = 0; my @PL = qw( phone1 phone2 phone3 phone4 phone5 ); print STDERR "This is pdb2gnokii $VERSION\nIt dumps address records from $PDBFILE\nin Gnokii format to stdout.\n\n"; print STDERR "You probably want to pipe the output to:\n'/usr/lib/gnokii/gnokii --writephonebook -o'\n\n"; print STDERR "With the --gammu switch, you probably want to feed the output to:\n'gamma --addnew '\n\n"; print STDERR "With the --t616hack switch, you probably want to call:\n'python ContactUpload.py'\n\n"; my $PDB = new Palm::Address; $PDB->Load($PDBFILE); my @categories = @{$PDB->{appinfo}{categories}}; my @ok_cat; my %opts; GetOptions('gammu' => \$gammu, 't616hack' => \$t616hack); if ($#ARGV >= 0) { print STDERR "Restricting categories:"; for (my $i = 0; $i <= $#categories; $i++) { foreach my $wc (@ARGV) { if ($categories[$i]->{name} eq $wc) { print STDERR " $wc"; push(@ok_cat, $i); last; } } } print STDERR ".\n\n"; } else { print STDERR "Using all categories.\n"; print STDERR "You can specify categories to restrict processing as parameters, e.g.:\n$0 Business\n\n"; for (my $i = 0; $i <= $#categories; $i++) { push(@ok_cat, $i); } } my $count = 0; if ($gammu) { print "[Backup]\nCreator = \"pdb2gnokii $VERSION\"\nFormat = 1.03"; } #my $converter; if ($t616hack) { # require Text::Unaccent; print '['; # require Text::Iconv; # $converter = Text::Iconv->new("iso-8859-1", "ucs-2"); } foreach my $record (@{$PDB->{records}}) { my $ok = 0; foreach my $c (@ok_cat) { if ($record->{category} == $c) { $ok = 1; last; } } next unless $ok; my $name; $name = $record->{fields}{firstName}; if ($name) { chomp($name); $name .= ' '; } else { $name = ''; } if ($record->{fields}{name}) { $name .= $record->{fields}{name}; } if ($record->{fields}{company}) { if ($name eq '') { $name = $record->{fields}{company}; } else { $name .= ' ' . $record->{fields}{company}; } } chomp($name); # Strip name to first 30 characters as the T68i (or Gnokii) seems to have # a limitation my $shorter_name = substr($name, 0, 30); if ($shorter_name ne $name) { print STDERR "Warning: Truncating \"$name\" into \"$shorter_name\"\n"; $name = $shorter_name; } my $done = ''; foreach my $field (@PL) { my $q; my $field_data = $record->{fields}{$field}; next unless ($field_data); $field_data =~ s/[^0-9+]//g; next unless ($field_data); chomp($field_data); my $field_labl = $record->{phoneLabel}{$field}; # 0=Work 1=Home 2=Fax 3=Other 4=E-Mail 5=Main 6=Pager 7=Mobile if($field_labl == 0) { $q = "W"; } if($field_labl == 1) { $q = "H"; } if($field_labl == 2) { $q = "F"; } if($field_labl == 3) { $q = "O"; } if($field_labl == 4) { $q = "E"; # Can't handle emails yet next; } if($field_labl == 5) { $q = "H"; } if($field_labl == 6) { $q = "M"; } if($field_labl == 7) { $q = "M"; } if ($done =~ m/$q/) { # Try to use other if ($done =~ m/O/) { print STDERR "Skipping: "; printf STDERR "$name / $field_data / $q"; next; } else { $q = 'O'; } } $count++; if ($gammu) { printf "\n[PhonePBK%03.0f]\nLocation = %03.0f\nEntry00Type = NumberGeneral\nEntry00Text = \"%s\"\nEntry01Type = Name\nEntry01Text = \"%s/%s\"", $count, $count, $field_data, $name, $q; } elsif ($t616hack) { if ($count > 1) { printf ', '; } printf "(\"%s\", \"%s\", '145', \"%s\", '%s')", $count, $field_data, $name, $q; # $count, $field_data, Text::Unaccent::unac_string('iso-8859-1', $name), $q; # $count, $field_data, $converter->convert($name), $q; } else { printf "%s/%s;%s;ME;0;0\n", $name, $q, $field_data; } unless ($field_data =~ m/^[+]/) { print STDERR "Warning, not international #: "; printf STDERR "$name / $field_data / $q"; } $done .= $q; } } if ($t616hack) { print "]\n"; } print STDERR "\nDone: $count records\n";