3 # * Copyright 2002 Paul Mangan <paul@claws-mail.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 ## script name : kmail2claws-mail_v2.pl
21 ## script purpose : convert an exported Kmail addressbook csv file
22 ## into a Claws Mail addressbook
24 ## tested with Kmail 1.4.7 and KAddressBook 3.1beta1
31 GetOptions
("kmailfile=s" => \
$kmailfile,
32 "help" => \
$iNeedHelp);
34 if ($kmailfile eq "" || $iNeedHelp) {
36 print "No filename given\n";
38 print "Use the following format:\n";
39 print "\tkmail2claws-mail_v2.pl --kmailfile=/path/to/addressbook.csv\n";
43 $claws_dir = ".claws-mail";
44 $addr_index = "$claws_dir/addrbook--index.xml";
45 $new_addressbook = "Kmail address book";
51 opendir(CLAWS
, $claws_dir) || die("Can't open $claws_dir 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_addrbk = "addrbook-"."$last_one".".xml";
68 open (KFILE
, "<$kmailfile") || die("Can't open the kmail file [$kmailfile]\n");
69 @kmaillines = <KFILE
>;
73 $defs = shift(@kmaillines);
74 @extra_def = (3,4,5,7 ... 27,29 ... 32,34 ... 42);
76 (@kmaildefs) = split(/,/,$defs);
78 $claws_addr = "<?xml version=\"1.0\" encoding=\"US-ASCII\" ?>\n";
79 $claws_addr .= "<address-book name=\"Kmail address book\" >\n";
81 foreach $kmailline (@kmaillines) {
82 (@kmaildata) = split(/,/,$kmailline);
83 foreach $kmaildata (@kmaildata) {
86 $kmaildata =~ s/"/"/g;
87 $kmaildata =~ s/&/&/g;
88 $kmaildata =~ s/'/'/g;
89 $kmaildata =~ s/</</g;
90 $kmaildata =~ s/>/>/g;
91 $kmaildata =~ s/\\n/, /g;
94 $claws_addr .= " <person uid=\"$time\" first-name=\"$kmaildata[2]\""
95 ." last-name=\"$kmaildata[1]\" nick-name=\"$kmaildata[6]\""
96 ." cn=\"$kmaildata[2] $kmaildata[1]\" >\n"
99 $claws_addr .= " <address uid=\"$time\" alias=\"\" email=\"$kmaildata[28]\""
100 ." remarks=\"$kmaildata[33]\" />\n"
101 ." </address-list>\n";
103 foreach $extra_def (@extra_def) {
104 if ($kmaildata[$extra_def] ne "") {
105 push (@def_exist, $extra_def);
109 $claws_addr .= " <attribute-list>\n";
111 foreach $def_exist (@def_exist) {
112 $kmaildefs[$def_exist] =~ s/^"//;
113 $kmaildefs[$def_exist] =~ s/"$//;
114 $kmaildefs[$def_exist] =~ s/'/'/g;
117 $claws_addr .= " <attribute uid=\"$time\" name=\"$kmaildefs[$def_exist]\" >"
118 ."$kmaildata[$def_exist]</attribute>\n";
122 $claws_addr .= " </attribute-list>\n";
124 $claws_addr .= " </person>\n";
128 $claws_addr .= "</address-book>\n";
130 open (NEWADDR
, ">$claws_dir/$new_addrbk");
131 print NEWADDR
$claws_addr;
134 open (ADDRIN
, "<$addr_index")
135 || die("can't open $addr_index for reading");
136 @addrindex_file = <ADDRIN
>;
139 foreach $addrindex_line (@addrindex_file) {
140 if ($addrindex_line =~ m/<\/book_list
>/) {
141 $rw_addrindex .= " <book name=\"$new_addressbook\" file=\"$new_addrbk\" />\n"
144 $rw_addrindex .= "$addrindex_line";
148 open (NEWADDRIN
, ">$addr_index")
149 || die("Can't open $addr_index for writing");
150 print NEWADDRIN
"$rw_addrindex";
153 print "Done. $count address(es) converted successfully.\n";