From 5c3a9ab9d77d2d95bec509607b76cc461360fe18 Mon Sep 17 00:00:00 2001 From: Paul Mangan Date: Sun, 20 Apr 2003 08:18:44 +0000 Subject: [PATCH] updated and improved outlook2sylpheed.pl --- ChangeLog.claws | 6 ++ tools/README | 24 +++++- tools/outlook2sylpheed.pl | 176 +++++++++++++++++++++++++++++++------- 3 files changed, 172 insertions(+), 34 deletions(-) diff --git a/ChangeLog.claws b/ChangeLog.claws index 9853e7978..d657ad9ca 100644 --- a/ChangeLog.claws +++ b/ChangeLog.claws @@ -1,3 +1,9 @@ +2003-04-20 [paul] + + * tools/README + tools/outlook2sylpheed.pl + updated and improved by Ricardo Mones Lastra + 2003-04-18 [oliver] 0.8.11claws102 * src/inc.c diff --git a/tools/README b/tools/README index 10b49a4d9..1a0a91dd9 100644 --- a/tools/README +++ b/tools/README @@ -196,6 +196,8 @@ Address book conversion Sylpheed XML address book. HOW TO USE IT + For text files: + -------------- You must export Outlook Express contact list as TXT file, choosing only "Name" and "Address" fields to export. @@ -205,9 +207,27 @@ Address book conversion outlook2sylpheed.pl fullpathname + For csv files: + ------------- + You must export Outlook contact list as CSV file, choosing ALL the + fields available for exporting. + + You must exit Sylpheed before converting the contact list. + + From the command line, execute the following: + + outlook2sylpheed.pl --csv fullpathname + LIMITATIONS - Only works with fields described above. If you have more complex - examples send them to me, and I'll try to enhance the script. + For text files only works with fields described above. If you have + more complex examples send them to me, and I'll try to enhance the + script. + + For csv files you must export all fields (but only non empty fields + are added to the created Sylpheed address book) and the number of + fields expected is harcoded. Look for the $nboffields variable in + the script and change its value if you are sure you exported all + fields and script gives the 'unknown csv file format' error. Contact: Ricardo Mones Lastra diff --git a/tools/outlook2sylpheed.pl b/tools/outlook2sylpheed.pl index fac3b6070..b5817b3a1 100644 --- a/tools/outlook2sylpheed.pl +++ b/tools/outlook2sylpheed.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -w -# * Copyright 2002 Ricardo Mones Lastra +# * Copyright 2002-2003 Ricardo Mones Lastra # * # * This file is free software; you can redistribute it and/or modify it # * under the terms of the GNU General Public License as published by @@ -24,12 +24,23 @@ # kmail2sylpheed.pl by Paul Mangan # # See README file for details and usage. -# +# -# parse required parameter -die "Required filename missing\nSyntax: $0 fullpathname\n" unless (defined($ARGV[0])); -$outl_file = $ARGV[0]; +$nboffields = 28; # change this only if you did read README +# parse parameters +$do_csv = 0; +die "Error: required filename missing\n" unless (defined($ARGV[0])); +$_=$ARGV[0]; +if (/--csv/) { + die "Error: required filename missing\n" unless (defined($ARGV[1])); + $do_csv = 1; + $outl_file = $ARGV[1]; +} +else { + $outl_file = $ARGV[0]; +} +# some init $sylconf = ".sylpheed"; $indexname = "$sylconf/addrbook--index.xml"; @@ -37,7 +48,7 @@ $indexname = "$sylconf/addrbook--index.xml"; $time = time; chdir; -opendir(SYLPHEED, $sylconf) || die("can't open $sylconf directory\n"); +opendir(SYLPHEED, $sylconf) || die("Error: can't open $sylconf directory\n"); push(@cached,(readdir(SYLPHEED))); closedir(SYLPHEED); @@ -54,33 +65,134 @@ $last_one =~ s/.xml$//; $last_one++; $new_book = "/addrbook-"."$last_one".".xml"; +# some subs +# warning: output file is global +sub write_header { + print NEWB "\n"; + print NEWB "\n"; +} + +sub write_footer { + print NEWB "\n"; +} + +sub write_person_h { + my($fn, $ln, $nn, $cn) = @_; + # one of them must be given + if (($fn eq "") and ($ln eq "") and ($nn eq "") and ($cn eq "")) { + $cn = "No name provided"; + # but return may break XML structure + } + print NEWB " \n"; +} + +sub write_person_f { + print NEWB " \n"; +} + +sub write_addrlist_h { + print NEWB " \n"; +} + +sub write_addrlist_f { + print NEWB " \n"; +} + +sub write_address { + my($al, $em, $re) = @_; + if ($em eq "") { + $em = "No e-mail address"; + # email is a must -> no address breaks sylpheed display + # (sylpheed says file is ok but no name is shown) + # maybe this is a bug on sylpheed? + } + print NEWB "
\n"; +} + +sub write_attrlist_h { + print NEWB " \n"; +} + +sub write_attrlist_f { + print NEWB " \n"; +} + +sub write_attribute { + my($aname, $aval) = @_; + if (($aname eq "") or ($aval eq "")) { return; } # both are must + print NEWB " ", $aval, "\n"; +} + +sub process_text { + write_header(); + $count = 0; + while () { + chomp; + if (/\s+[0-9]+\s+(.+)/) { $_ = $1; } + else { $count += 2 and die "Error: wrong format at line $count \n"; } + @field = split(/;/); # first is name, second mail addr + write_person_h("","","",$field[0]); + write_addrlist_h(); + $field[1] =~ s/\r//; # beware, dangerous chars inside ;) + write_address("",$field[1],""); + write_addrlist_f(); + write_person_f(); + ++$count; + } + write_footer(); +} + +sub process_csv { + write_header(); + $count = 0; + while () { + chomp; + # do something useful: quote XML chars + s/\&/&/g; + s/\/>/g; + s/\'/'/g; + s/\"/"/g; + @field = split(/,/); + if ($#field != $nboffields) { $count += 2 and die "Error: wrong format at line $count \n"; } + # First Name, Last Name, Nickname, Name + write_person_h($field[0],$field[1],$field[4],$field[3]); + write_addrlist_h(); + write_address("",$field[5],$field[$nboffields - 1]); + write_addrlist_f(); + write_attrlist_h(); # the remaining values as attributes + foreach $a (2, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27) { + # add only filled fields (should be trimmed?) + if (defined($field[$a]) && $field[$a] ne "") { + write_attribute($headerline[$a],$field[$a]); + } + } + write_attrlist_f(); + write_person_f(); + ++$count; + } + write_footer(); +} + # ok, was enough, do some more bit bashing now open(OUTL, $outl_file) - or die "can't open $outl_file for reading\n"; + or die "Error: can't open $outl_file for reading\n"; +# 1st line: file format checking (csv) or discarding (default) +$_ = ; +chomp; +if ($do_csv) { + @headerline = split(/,/); + # check before creating output file + die "Error: unknown csv file format\n" + unless ($#headerline == $nboffields); +} open(NEWB, '>', "$sylconf/$new_book") - or die "can't open $new_book for writting\n"; - -$_ = ; # skip first line -$count = 0; -# header -print NEWB "\n"; -print NEWB "\n"; -while () { - chomp; - if (/\s+[0-9]+\s+(.+)/) { $_ = $1; } - else { $count += 2 and die "wrong format at line $count \n"; } - @field = split(';',$_); # first is name, second mail addr - print NEWB "\n\n"; - ++$time; - $field[1] =~ s/\r//; # beware, dangerous chars inside ;) - print NEWB "
\n\n\n"; - ++$time; - ++$count; -} -print NEWB "\n"; + or die "Error: can't open $sylconf/$new_book for writting\n"; +if ($do_csv) { process_csv(); } +else { process_text(); } close NEWB; close OUTL; @@ -88,7 +200,7 @@ close OUTL; # update index (more Paul's code :) open(INDX, $indexname) - or die "can't open $indexname for reading\n"; + or die "Error: can't open $indexname for reading\n"; @index_file = ; close INDX; @@ -99,7 +211,7 @@ foreach $index_line (@index_file) { } } open (INDX, '>', $indexname) - or die "can't open $indexname for writting\n"; + or die "Error: can't open $indexname for writting\n"; print INDX "$new_index"; close INDX; -- 2.25.1