fix problem with quoted strings in filter_conv.pl
[claws.git] / tools / kmail2sylpheed.pl
1 #!/usr/bin/perl
2
3 #  * Copyright © 2002 Paul Mangan <claws@thewildbeast.co.uk>
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 ## script name : kmail2sylpheed.pl
20
21 ## script purpose : convert a Kmail addressbook into a Sylpheed addressbook
22
23 use Getopt::Long;
24
25 $kmailfile = '';
26
27 GetOptions("kmailfile=s" => \$kmailfile);
28
29 $time = time;
30
31 $sylph_addr = "<?xml version=\"1.0\" encoding=\"US-ASCII\" ?>\n";
32 $sylph_addr .= "<address-book name=\"Kmail Address Book\" >\n";
33
34 chdir;
35 #chdir '.sylpheed' || die("You don't appear to have Sylpheed installed\n");
36
37 opendir(SYLPHEED, ".sylpheed") || die("Can't open .sylpheed directory\n");
38         push(@cached,(readdir(SYLPHEED)));
39 closedir(SYLPHEED);
40
41 foreach $cached (@cached) {
42         if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) {
43                 push(@addr, "$cached");
44         }
45 }
46
47 @sorted = sort {$a cmp $b} @addr;
48 $last_one = pop(@sorted);
49 $last_one =~ s/^addrbook-//;
50 $last_one =~ s/.xml$//;
51 $last_one++;
52 $new_addrbk = "addrbook-"."$last_one".".xml";
53
54 open (KFILE, "<$kmailfile") || die("Can't find the kmail file\n");
55         @kmaillines = <KFILE>;
56 close KFILE;
57
58 $dross = shift(@kmaillines);
59
60 foreach $kmailline (@kmaillines) {
61     (@kmaildata) = split(/,/,$kmailline); 
62         foreach $kmaildata (@kmaildata) {
63                 $kmaildata =~ s/^"//;
64                 $kmaildata =~ s/"$//;
65                 $kmaildata =~ s/"/&quot;/g;
66                 $kmaildata =~ s/&/&amp;/g;
67                 $kmaildata =~ s/'/&apos;/g;
68                 $kmaildata =~ s/</&lt;/g;
69                 $kmaildata =~ s/>/&gt;/g;
70         }
71     $sylph_addr .= "  <person uid=\"$time\" first-name=\"$kmaildata[0]\""
72                  ." last-name=\"$kmaildata[1]\" nick-name=\"$kmaildata[7]\""
73                  ." cn=\"$kmaildata[0] $kmaildata[1]\" >\n"
74                  ."    <address-list>\n";
75     $time++;
76     $sylph_addr .= "      <address uid=\"$time\" alias=\"\" email=\"$kmaildata[6]\""
77                 ." remarks=\"$kmaildata[8]\" />\n"
78                 ."    </address-list>\n";
79     if ($kmaildata[13] ne "" || $kmaildata[9] ne "" || $kmaildata[21] ne "" ||
80         $kmaildata[16] ne "" || $kmaildata[5] ne "" || $kmaildata[24] ne "" ||
81         $kmaildata[19] ne "" || $kmaildata[12] ne "" || $kmaildata[10] ne "" ||
82         $kmaildata[4] ne "" || $kmaildata[2] ne "" || $kmaildata[11] ne "" ||
83         $kmaildata[3] ne "" || $kmaildata[14] ne "" || $kmaildata[22] ne "" ||
84         $kmaildata[17] ne "" || $kmaildata[20] ne "" || $kmaildata[15] ne "" ||
85         $kmaildata[23] ne "" || $kmaildata[18] ne "") {
86         $sylph_addr .= "    <attribute-list>\n";
87
88         if ($kmaildata[3] ne "" || $kmaildata[2] ne "") {
89                 $time++;
90                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Full Name\" >"
91                         ."$kmaildata[3] $kmaildata[0] $kmaildata[2] $kmaildata[1]</attribute>\n";
92         }
93         if ($kmaildata[15] ne "") {
94                 $time++;
95                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Home Street Address\" >"
96                         ."$kmaildata[15]</attribute>\n";
97         }
98         if ($kmaildata[16] ne "") {
99                 $time++;
100                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Home City Address\" >"
101                         ."$kmaildata[16]</attribute>\n";
102         }
103         if ($kmaildata[17] ne "") {
104                 $time++;
105                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Home State Address\" >"
106                         ."$kmaildata[17]</attribute>\n";
107         }
108         if ($kmaildata[18] ne "") {
109                 $time++;
110                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Home Zip Address\" >"
111                         ."$kmaildata[18]</attribute>\n";
112         }
113         if ($kmaildata[19] ne "") {
114                 $time++;
115                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Home Country Address\" >"
116                         ."$kmaildata[19]</attribute>\n";
117         }
118         if ($kmaildata[10] ne "") {
119                 $time++;
120                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Home Phone\" >"
121                         ."$kmaildata[10]</attribute>\n";
122         }
123         if ($kmaildata[12] ne "") {
124                 $time++;
125                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Home Fax\" >"
126                         ."$kmaildata[12]</attribute>\n";
127         }
128         if ($kmaildata[11] ne "") {
129                 $time++;
130                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Mobile Phone\" >"
131                         ."$kmaildata[11]</attribute>\n";
132         }
133         if ($kmaildata[14] ne "") {
134                 $time++;
135                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Pager\" >"
136                         ."$kmaildata[14]</attribute>\n";
137         }
138         if ($kmaildata[5] ne "") {
139                 $time++;
140                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Company\" >"
141                         ."$kmaildata[5]</attribute>\n";
142         }
143         if ($kmaildata[4] ne "") {
144                 $time++;
145                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Job Title\" >"
146                         ."$kmaildata[4]</attribute>\n";
147         }
148         if ($kmaildata[20] ne "") {
149                 $time++;
150                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Business Street Address\" >"
151                         ."$kmaildata[20]</attribute>\n";
152         }
153         if ($kmaildata[21] ne "") {
154                 $time++;
155                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Business City Address\" >"
156                         ."$kmaildata[21]</attribute>\n";
157         }
158         if ($kmaildata[22] ne "") {
159                 $time++;
160                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Business State Address\" >"
161                         ."$kmaildata[22]</attribute>\n";
162         }
163         if ($kmaildata[23] ne "") {
164                 $time++;
165                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Business Zip Address\" >"
166                         ."$kmaildata[23]</attribute>\n";
167         }
168         if ($kmaildata[24] ne "") {
169                 $time++;
170                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Business Country Address\" >"
171                         ."$kmaildata[24]</attribute>\n";
172         }
173         if ($kmaildata[9] ne "") {
174                 $time++;
175                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Business Phone\" >"
176                         ."$kmaildata[9]</attribute>\n";
177         }
178         if ($kmaildata[13] ne "") {
179                 $time++;
180                 $sylph_addr .= "      <attribute uid=\"$time\" name=\"Business Fax\" >"
181                         ."$kmaildata[13]</attribute>\n";
182         }
183         $sylph_addr .= "    </attribute-list>\n";
184     }
185     $sylph_addr .=  "  </person>\n";
186     $time++;
187 }
188 $sylph_addr .= "</address-book>\n";
189
190 open (NEWADDR, ">.sylpheed/$new_addrbk");
191 print NEWADDR $sylph_addr;
192 close NEWADDR;
193
194 open (ADDRIN, "<.sylpheed/addrbook--index.xml") || die("can't open addrbook--index.xml");
195         @addrindex_file = <ADDRIN>;
196 close ADDRIN;
197
198 foreach $addrindex_line (@addrindex_file) {
199         if ($addrindex_line =~ m/<\/book_list>/) {
200                 $rewrite_addrin .= "    <book name=\"Kmail Address Book\" file=\"$new_addrbk\" />\n"
201                         ."  </book_list>\n";
202         } else {
203                 $rewrite_addrin .= "$addrindex_line";
204         }
205 }
206
207 open (NEWADDRIN, ">.sylpheed/addrbook--index.xml");
208 print NEWADDRIN "$rewrite_addrin";
209 close NEWADDRIN;
210
211 print "\nYou have sucessfully converted your Kmail addressbook\n";
212 exit;