#!/usr/bin/perl # # ldif2addr HP20010415 HP20020213 HP20020522 # # Heiko Purnhagen # # # pilot-ldif, v 0.30 30 Jan 1998 22:00 UT # # pilot-ldif, a script that employs pilot-addresses to transfer # ldif files, such as those generated by the Mozilla address book, # to and from the Palm Pilot. # # Copyright (C) 1999 Bob Kopp r-kopp@uchicago.edu # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. $tempfile=""; $mode=0; $filename = ""; $arguments = ""; $category = "Mozilla"; $clear = 1; $debug = 0; &ParseArgs; if ($tempfile eq "") { &HelpMesg; } else { &ReadFile; } sub ParseArgs { $filename = shift @ARGV; $tempfile = shift @ARGV; } # äöüÄÖÜß æÆåÅøØé # äöüÄÖÜß æÆåÅøØé sub utf8($) { $_ = @_[0]; # s/Heiko/ÖÖÖÖÖ/g; # s/\Ã\¤/\ä/g; s/ä/ä/g; s/ö/ö/g; s/ü/ü/g; s/Ä/Ä/g; s/Ö/Ö/g; s/Ü/Ü/g; s/ß/ß/g; s/æ/æ/g; s/Æ/Æ/g; s/Ã¥/å/g; s/Ã…/Å/g; s/ø/ø/g; s/Ø/Ø/g; s/é/é/g; s/á/á/g; $_; } sub WriteFile { my $buffer = ""; if ($debug) { print "Opening $filename for output\n"; } open (EXP, "> $filename"); if ($debug) { print "Running $commandline\n"; } (system ("$commandline") == 0) or Error ("Error running pilot-addresses"); if ($debug) { print "Opening $tempfile for input\n"; } open (IMP, "$tempfile") or Error ("Error opening $tempfile"); while () { if (/.*\"\n/) { chop; $_ = $buffer . $_; @fields = split /\",\"/; ($fields[0]) = ($fields[0] =~ /\"(.*)/); $dn = encode_base64 (" cn=$fields[1] $fields[0],mail=$fields[8]"); chop $dn; $dn = ": $dn"; $cn = encode_base64 ("$fields[1] $fields[0]"); chop $cn; $cn = ": $cn"; foreach $field (@fields) { $field = encode_base64 ($field); chop ($field); $field = ": $field"; } ($ln,$fn,$ti,$co,$p1,$p2,$p3,$p4,$p5,$ad,$ci,$st,$zi,$ct,$c1,$c2,$c3,$c4,$nt,$ex2) = @fields; print EXP "dn:$dn\n"; print EXP "cn:$cn\n"; print EXP "mail:$p5\n"; print EXP "givenname:$fn\n"; print EXP "sn:$ln\n"; print EXP "description:$nt\n"; print EXP "o:$co\n"; print EXP "streetaddress:$ad\n"; print EXP "postalcode:$zi\n"; print EXP "countryname:$ct\n"; print EXP "locality:$ci\n"; print EXP "st:$st\n"; print EXP "title:$ti\n"; print EXP "telephonenumber:$p1\n"; print EXP "facsimiletelephonenumber:$p3\n"; print EXP "homephone:$p2\n"; print EXP "cellphone:$p3\n"; print EXP "objectclass: top\n"; print EXP "objectclass: person\n"; print EXP "\n"; $buffer = ""; } else { $buffer = $buffer . $_; } } close IMP; close EXP; &Cleanup; exit 0; } sub ReadFile { my $buffer = ""; if ($debug) { print "Opening $filename for input\n"; } open (IMP, $filename) or Error ("$filename not found"); if ($debug) { print "Opening $tempfile for output\n"; } open (EXP, "> $tempfile"); while () { chop; if ( (/:/) or ($_ eq "") ) { $currentline = $_; $_ = $buffer; if ($_ eq "") { if ( $ln or $fn or $co) { print EXP "\"$ln\",\"$fn\",\"$ti\",\"$co\",\"$p1\",\"$p2\",\"$p3\",\"$p4\",\"$p5\",\"$ad\",\"$ci\",\"$st\",\"$zi\",\"$ct\",\"$c1\",\"$c2\",\"$c3\",\"$c4\",\"$nt\",\"0\"\n"; } $ln = ""; $fn = ""; $co = ""; $p1 = ""; $p2 = ""; $p3 = ""; $p4 = ""; $p5 = ""; $ad = ""; $ci = ""; $st = ""; $zi = ""; $ct = ""; $ti = ""; $c1 = ""; $c2 = ""; $c3 = ""; $c4 = ""; $nt = ""; $buffer = ""; } else { ($fieldname, $_) = /(^.*?):(.*)/; if ( /^:/ ) { ($encoded) = /^:\s*(.*)/; $data = decode_base64($encoded); } else { ($data) = /\s*(.*)/; } ($data) = utf8($data); if ($fieldname eq "givenname") { $fn = $data; } elsif ($fieldname eq "sn") { $ln = $data; } elsif ($fieldname eq "o") { $co = $data; } elsif ($fieldname eq "homephone") { $p2 = $data; } elsif ($fieldname eq "telephonenumber") { $p1 = $data; } elsif ($fieldname eq "facsimiletelephonenumber") { $p3 = $data; } elsif ($fieldname eq "cellphone") { $p4 = $data; } elsif ($fieldname eq "mail") { $p5 = $data; } elsif ($fieldname eq "streetaddress") { $ad = $data; } elsif ($fieldname eq "locality") { $ci = $data; } elsif ($fieldname eq "st") { $st = $data; } elsif ($fieldname eq "postalcode") { $zi = $data; } elsif ($fieldname eq "countryname") { $ct = $data; } elsif ($fieldname eq "title") { $ti = $data; } elsif ($fieldname eq "description") { $nt = $data; } } $buffer = $currentline; } else { $buffer = $buffer . $_ }; } close EXP; if ($debug) { print "Closed $tempfile\n"; } close IMP; if ($debug) { print "Closed $filename\n"; } # if ($debug) { print "Running $commandline\n"; } # (system ("$commandline") == 0) # or Error ("Error running pilot-addresses\n"); # &Cleanup; } sub Cleanup { # system ("rm -f $tempfile"); } sub Error { print "Error: @_[0]\n"; &Cleanup; exit 1; } sub HelpMesg { print "usage: ldif2adr xxx.ldif xxx.addr\n"; print " pilot-addresses -d Mozilla -c Mozilla -r xxx.addr\n"; exit 1; } # base 64 routines borrowed from Base64.pm sub encode_base64 ($;$) { my $res = ""; my $eol = $_[1]; $eol = "\n" unless defined $eol; pos($_[0]) = 0; # ensure start at the beginning while ($_[0] =~ /(.{1,45})/gs) { $res .= substr(pack('u', $1), 1); chop($res); } $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs # fix padding at the end my $padding = (3 - length($_[0]) % 3) % 3; $res =~ s/.{$padding}$/'=' x $padding/e if $padding; # break encoded string into lines of no more than 76 characters each if (length $eol) { $res =~ s/(.{1,76})/$1$eol/g; } $res; } sub decode_base64 ($) { local($^W) = 0; # unpack("u",...) gives bogus warning in 5.00[123] my $str = shift; my $res = ""; $str =~ tr|A-Za-z0-9+=/||cd; # remove non-base64 chars if (length($str) % 4 == 1 && substr($str,-1,1) == "=") { chop($str); } if (length($str) % 4) { print "len=",length($str)," str=[",$str,"]\n"; Error("Length of base64 data not a multiple of 4 in $fieldname of $fn $ln $p5") } $str =~ s/=+$//; # remove padding $str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format while ($str =~ /(.{1,60})/gs) { my $len = chr(32 + length($1)*3/4); # compute length byte $res .= unpack("u", $len . $1 ); # uudecode } $res; }