3 # Copyright 2002-2003 Ricardo Mones <ricardo@mones.org>
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # outlook2claws-mail.pl -- perl script to convert an Outlook generated
20 # contact list into a Claws Mail XML address book.
22 # This script is based on:
23 # out2syl.sh by Rafael Lossurdo <mugas@via-rs.net>
24 # kmail2claws-mail.pl by Paul Mangan <paul@claws-mail.org>
26 # See README file for details and usage.
29 $nboffields = 28; # change this only if you did read README
33 die "Error: required filename missing\n" unless (defined($ARGV[0]));
36 die "Error: required filename missing\n" unless (defined($ARGV[1]));
38 $outl_file = $ARGV[1];
41 $outl_file = $ARGV[0];
44 $clawsconf = ".claws-mail/addrbook";
45 $indexname = "$clawsconf/addrbook--index.xml";
47 # the next is mostly Paul's code
51 opendir(CLAWS
, $clawsconf) || die("Error: can't open $clawsconf directory\n");
52 push(@cached,(readdir(CLAWS
)));
55 foreach $cached (@cached) {
56 if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) {
57 push(@addr, "$cached");
61 @sorted = sort {$a cmp $b} @addr;
62 $last_one = pop(@sorted);
63 $last_one =~ s/^addrbook-//;
64 $last_one =~ s/.xml$//;
66 $new_book = "/addrbook-"."$last_one".".xml";
69 # warning: output file is global
71 print NEWB
"<?xml version=\"1.0\" encoding=\"US-ASCII\" ?>\n";
72 print NEWB
"<address-book name=\"Outlook Address Book\" >\n";
76 print NEWB
"</address-book>\n";
80 my($fn, $ln, $nn, $cn) = @_;
81 # one of them must be given
82 if (($fn eq "") and ($ln eq "") and ($nn eq "") and ($cn eq "")) {
83 $cn = "No name provided";
84 # but return may break XML structure
86 print NEWB
" <person uid=\"", $time++, "\" first-name=\"", $fn, "\" ";
87 print NEWB
"last-name=\"", $ln, "\" nick-name=\"", $nn, "\" cn=\"", $cn, "\" >\n";
91 print NEWB
" </person>\n";
94 sub write_addrlist_h
{
95 print NEWB
" <address-list>\n";
98 sub write_addrlist_f
{
99 print NEWB
" </address-list>\n";
103 my($al, $em, $re) = @_;
105 $em = "No e-mail address";
106 # email is a must -> no address breaks claws-mail display
107 # (claws-mail says file is ok but no name is shown)
108 # maybe this is a bug on claws-mail?
110 print NEWB
" <address uid=\"", $time++, "\" ";
111 print NEWB
"alias=\"", $al, "\" email=\"", $em, "\" remarks=\"", $re, "\" />\n";
114 sub write_attrlist_h
{
115 print NEWB
" <attribute-list>\n";
118 sub write_attrlist_f
{
119 print NEWB
" </attribute-list>\n";
122 sub write_attribute
{
123 my($aname, $aval) = @_;
124 if (($aname eq "") or ($aval eq "")) { return; } # both are must
125 print NEWB
" <attribute uid=\"", $time++, "\" ";
126 print NEWB
"name=\"", $aname, "\" >", $aval, "</attribute>\n";
134 if (/\s+[0-9]+\s+(.+)/) { $_ = $1; }
135 else { $count += 2 and die "Error: wrong format at line $count \n"; }
136 @field = split(/;/); # first is name, second mail addr
137 write_person_h
("","","",$field[0]);
139 $field[1] =~ s/\r//; # beware, dangerous chars inside ;)
140 write_address
("",$field[1],"");
153 # do something useful: quote XML chars
160 if ($#field != $nboffields) { $count += 2 and die "Error: wrong format at line $count \n"; }
161 # First Name, Last Name, Nickname, Name
162 write_person_h
($field[0],$field[1],$field[4],$field[3]);
164 write_address
("",$field[5],$field[$nboffields - 1]);
166 write_attrlist_h
(); # the remaining values as attributes
167 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) {
168 # add only filled fields (should be trimmed?)
169 if (defined($field[$a]) && $field[$a] ne "") {
170 write_attribute
($headerline[$a],$field[$a]);
180 # ok, was enough, do some more bit bashing now
181 open(OUTL
, $outl_file)
182 or die "Error: can't open $outl_file for reading\n";
183 # 1st line: file format checking (csv) or discarding (default)
187 @headerline = split(/,/);
188 # check before creating output file
189 die "Error: unknown csv file format\n"
190 unless ($#headerline == $nboffields);
192 open(NEWB
, '>', "$clawsconf/$new_book")
193 or die "Error: can't open $clawsconf/$new_book for writing\n";
194 if ($do_csv) { process_csv
(); }
195 else { process_text
(); }
200 # update index (more Paul's code :)
202 open(INDX
, $indexname)
203 or die "Error: can't open $indexname for reading\n";
204 @index_file = <INDX
>;
207 foreach $index_line (@index_file) {
208 if ($index_line =~ m/<\/book_list
>/) {
209 $new_index .= " <book name=\"Outlook Address Book\" file=\"$new_book\" />\n"." </book_list>\n"; } else {
210 $new_index .= "$index_line";
213 open (INDX
, '>', $indexname)
214 or die "Error: can't open $indexname for writing\n";
215 print INDX
"$new_index";
218 print "Done. $count address(es) converted successfully.\n";