implemtent RFE 4255, "Switching 'Thread view' on/off discards message selection"
[claws.git] / tools / kmail2claws-mail_v2.pl
blobe0c2d18c5d20588de40f33c90483795d24a1e929
1 #!/usr/bin/perl
3 # * Copyright 2002 Paul Mangan <paul@claws-mail.org>
4 # *
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.
9 # *
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.
14 # *
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
26 use Getopt::Long;
28 $kmailfile = '';
29 $iNeedHelp = '';
31 GetOptions("kmailfile=s" => \$kmailfile,
32 "help" => \$iNeedHelp);
34 if ($kmailfile eq "" || $iNeedHelp) {
35 if (!$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";
40 exit;
43 $claws_dir = ".claws-mail";
44 $addr_index = "$claws_dir/addrbook--index.xml";
45 $new_addressbook = "Kmail address book";
47 $time = time;
49 chdir;
51 opendir(CLAWS, $claws_dir) || die("Can't open $claws_dir directory\n");
52 push(@cached,(readdir(CLAWS)));
53 closedir(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$//;
65 $last_one++;
66 $new_addrbk = "addrbook-"."$last_one".".xml";
68 open (KFILE, "<$kmailfile") || die("Can't open the kmail file [$kmailfile]\n");
69 @kmaillines = <KFILE>;
70 close KFILE;
72 $count = 0;
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) {
84 $kmaildata =~ s/^"//;
85 $kmaildata =~ s/"$//;
86 $kmaildata =~ s/"/&quot;/g;
87 $kmaildata =~ s/&/&amp;/g;
88 $kmaildata =~ s/'/&apos;/g;
89 $kmaildata =~ s/</&lt;/g;
90 $kmaildata =~ s/>/&gt;/g;
91 $kmaildata =~ s/\\n/, /g;
92 chomp $kmaildata;
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"
97 ." <address-list>\n";
98 $time++;
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);
108 if ($def_exist[0]) {
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/'/&apos;/g;
116 $time++;
117 $claws_addr .= " <attribute uid=\"$time\" name=\"$kmaildefs[$def_exist]\" >"
118 ."$kmaildata[$def_exist]</attribute>\n";
119 $attribs = 1;
121 if ($attribs == 1) {
122 $claws_addr .= " </attribute-list>\n";
124 $claws_addr .= " </person>\n";
125 $time++;
126 $count++;
128 $claws_addr .= "</address-book>\n";
130 open (NEWADDR, ">$claws_dir/$new_addrbk");
131 print NEWADDR $claws_addr;
132 close NEWADDR;
134 open (ADDRIN, "<$addr_index")
135 || die("can't open $addr_index for reading");
136 @addrindex_file = <ADDRIN>;
137 close 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"
142 ." </book_list>\n";
143 } else {
144 $rw_addrindex .= "$addrindex_line";
148 open (NEWADDRIN, ">$addr_index")
149 || die("Can't open $addr_index for writing");
150 print NEWADDRIN "$rw_addrindex";
151 close NEWADDRIN;
153 print "Done. $count address(es) converted successfully.\n";
155 exit;