Update authors lists
[claws.git] / tools / tb2claws-mail
1 #!/usr/bin/perl
2
3 # Script name : tb2claws-mail
4 # Script version: 1.0.2
5 # Script based on : script kmail2claws-mail.pl
6 # Script purpose : convert The Bat! addressbook into Claws Mail addressbook
7 # Author : Aleksandar Urosevic aka Urke MMI <urke@gmx.net>
8 # Licence : GPL
9 #
10 # Thanks goes to : Paul Mangan <paul@claws-mail.org>
11 #
12 # Usage: Export The Bat! Address Book to CSV file format
13 #        with all fields selected to YES and then start:
14 #        tb2claws-mail --tbfile=/full/path/to/thebat/addressbook.csv
15 #
16 # Change Log:
17 #
18 # 18-12-2003 v 1.0.2
19 #       - bugfix: added missing attribute-list start
20 #
21 # 01-01-2003 v 1.0.1
22 #       - bugfix: no more empty Business Homepage entry
23 #       - bugfix: no more \0D\0A´s in Notes entry
24 #       - bugfix: no more double space in Full Name entry
25 #       - code utilization
26 #       - add info about number of converted addresses
27 #
28 # 15-08-2002 v 1.0.0
29 #       - first public release
30 #
31 # TODO:
32 #
33 # * Add switch for Full Name entry on atrybute part
34 #
35
36 use Getopt::Long;
37
38 GetOptions("tbfile=s" => \$tbfile);
39
40 #$tbfile = '/home/urke/bin/claws-mail/tb2ldif/thebat-addressbook.csv';
41 $total_addresses = 0;
42
43 chdir;
44
45 # check is Claws-Mail instrtalled
46 opendir(CLAWS, ".claws-mail/addrbook") || die("Can't open Claws Mail directory! Conversion aborted\n");
47     push(@cached, (readdir(CLAWS)));
48 closedir(CLAWS);
49
50 # get last existing addressbook filename
51 # to set filename for newest addressbook
52
53 # get all existing addressbook filenames
54 foreach $cached (@cached) {
55     if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) {
56         push(addr, "$cached");
57     }
58 }
59 # sort filenames, get last and set newest filename
60 @sorted = sort {$a cmp $b} @addr;
61 $last_one = pop(@sorted);
62 $last_one =~ s/^addrbook-//;
63 $last_one =~ s/.xml$//;
64 $last_one++;
65 $new_addrbk = "addrbook-"."$last_one".".xml";
66
67 # open thebat file to stack
68 open (TBFILE, "<$tbfile") || die("Specified Address Book file does not exist.\n\033[5m\033[31mYou must specify full path to input file!\033[0m\nConversion aborted.\n");
69     @tblines = <TBFILE>;
70 close TBFILE;
71
72 # shift firs line from file because this is field names
73 $dross = shift(@tblines);
74
75 # set time mark and header of addressbook
76 $time = time;
77 $claws_addr = "<?xml version=\"1.0\" encoding=\"ISO-8859-2\" ?>\n";
78 $claws_addr .= "<address-book name=\"The Bat! Address Book\" >\n";
79
80 # create addressbook entry from The Bat! addressbook
81 foreach $tbline (@tblines) {
82     $total_addresses += 1;
83     (@tbdata) = split(/,/,$tbline); 
84     foreach $tbdata (@tbdata) {
85         # fix nonacceptable characters
86         $tbdata =~ s/^"//;
87         $tbdata =~ s/"$//;
88         $tbdata =~ s/"/&quot;/g;
89         $tbdata =~ s/&/&amp;/g;
90         $tbdata =~ s/'/&apos;/g;
91         $tbdata =~ s/</&lt;/g;
92         $tbdata =~ s/>/&gt;/g;
93         $tbdata =~ s/\\2C\ /, /g;
94         $tbdata =~ s/(\\0D\\0A){1,}/, /g;
95         $tbdata =~ s/\ {2,}/ /g;
96     }
97     # set addressbook field values
98     $claws_addr .= "  <person uid=\"$time\" first-name=\"$tbdata[1]\""
99                 ." last-name=\"$tbdata[2]\" nick-name=\"$tbdata[5]\""
100                 ." cn=\"$tbdata[0]\" >\n"
101                 ."    <address-list>\n";
102     $time++;
103     $claws_addr .= "      <address uid=\"$time\" alias=\"$tbdata[5]\" email=\"$tbdata[4]\""
104                 ." remarks=\"\" />\n"
105                 ."    </address-list>\n";
106
107     # find is need to make entry attributes
108     $check = 0;
109     for($i=6; $i<=31; $i++) {
110         $tbdata[$i] =~ s/^\s+//;
111         $tbdata[$i] =~ s/\s+$//;
112         if ($tbdata[$i] ne "") { $check += 1; }
113     }
114
115     if ($check > 0) {
116     $claws_addr .= "    <attribute-list>\n";
117         if ($tbdata[1] ne "" || $tbdata[2] ne "") {
118             $time++;
119             if($tbdata[29] ne "" && $tbdata[1] ne "") { $full_name = "$tbdata[29] $tbdata[1]"; } else { $full_name = "$tbdata[1]"; }
120             if($tbdata[3] ne "") { $full_name .= " $tbdata[3]"; }
121             if($tbdata[2] ne "") { $full_name .= " $tbdata[2]"; }
122             if($tbdata[28] ne "") { $full_name .= " $tbdata[28]"; }
123                 
124             $claws_addr .= "      <attribute uid=\"$time\" name=\"Full Name\" >"
125                         ."$full_name</attribute>\n";
126         }
127         if ($tbdata[15] ne "") {
128             $time++;
129             $claws_addr .= "      <attribute uid=\"$time\" name=\"Home Street Address\" >"
130                         ."$tbdata[15]</attribute>\n";
131         }
132         if ($tbdata[16] ne "") {
133             $time++;
134             $claws_addr .= "      <attribute uid=\"$time\" name=\"Home City Address\" >"
135                         ."$tbdata[16]</attribute>\n";
136         }
137         if ($tbdata[17] ne "") {
138             $time++;
139             $claws_addr .= "      <attribute uid=\"$time\" name=\"Home State Address\" >"
140                         ."$tbdata[17]</attribute>\n";
141         }
142         if ($tbdata[18] ne "") {
143             $time++;
144             $claws_addr .= "      <attribute uid=\"$time\" name=\"Home Zip Address\" >"
145                         ."$tbdata[18]</attribute>\n";
146         }
147         if ($tbdata[19] ne "") {
148             $time++;
149             $claws_addr .= "      <attribute uid=\"$time\" name=\"Home Country Address\" >"
150                         ."$tbdata[19]</attribute>\n";
151         }
152         if ($tbdata[9] ne "") {
153             $time++;
154             $claws_addr .= "      <attribute uid=\"$time\" name=\"Home Phone\" >"
155                         ."$tbdata[9]</attribute>\n";
156         }
157         if ($tbdata[10] ne "") {
158             $time++;
159             $claws_addr .= "      <attribute uid=\"$time\" name=\"Home Fax\" >"
160                         ."$tbdata[10]</attribute>\n";
161         }
162         if ($tbdata[11] ne "") {
163             $time++;
164             $claws_addr .= "      <attribute uid=\"$time\" name=\"Mobile Phone\" >"
165                         ."$tbdata[11]</attribute>\n";
166         }
167         if ($tbdata[30] ne "") {
168             $time++;
169             $claws_addr .= "      <attribute uid=\"$time\" name=\"Personal Homepage\" >"
170                         ."$tbdata[30]</attribute>\n";
171         }
172         if ($tbdata[14] ne "") {
173             $time++;
174             $claws_addr .= "      <attribute uid=\"$time\" name=\"Pager\" >"
175                         ."$tbdata[14]</attribute>\n";
176         }
177         if ($tbdata[7] ne "") {
178             $time++;
179             $claws_addr .= "      <attribute uid=\"$time\" name=\"Bussines Company\" >"
180                         ."$tbdata[7]</attribute>\n";
181         }
182         if ($tbdata[8] ne "") {
183             $time++;
184             $claws_addr .= "      <attribute uid=\"$time\" name=\"Bussines Job Title\" >"
185                         ."$tbdata[8]</attribute>\n";
186         }
187         if ($tbdata[20] ne "") {
188             $time++;
189             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business Department\" >"
190                         ."$tbdata[20]</attribute>\n";
191         }
192         if ($tbdata[21] ne "") {
193             $time++;
194             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business Office\" >"
195                         ."$tbdata[21]</attribute>\n";
196         }
197         if ($tbdata[22] ne "") {
198             $time++;
199             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business Street Address\" >"
200                         ."$tbdata[22]</attribute>\n";
201         }
202         if ($tbdata[23] ne "") {
203             $time++;
204             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business City Address\" >"
205                         ."$tbdata[23]</attribute>\n";
206         }
207         if ($tbdata[24] ne "") {
208             $time++;
209             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business State Address\" >"
210                         ."$tbdata[24]</attribute>\n";
211         }
212         if ($tbdata[25] ne "") {
213             $time++;
214             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business Zip Address\" >"
215                         ."$tbdata[25]</attribute>\n";
216         }
217         if ($tbdata[26] ne "") {
218             $time++;
219             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business Country Address\" >"
220                         ."$tbdata[26]</attribute>\n";
221         }
222         if ($tbdata[12] ne "") {
223             $time++;
224             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business Phone\" >"
225                         ."$tbdata[12]</attribute>\n";
226         }
227         if ($tbdata[13] ne "") {
228             $time++;
229             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business Fax\" >"
230                         ."$tbdata[13]</attribute>\n";
231         }
232         if ($tbdata[31] ne "") {
233             $time++;
234             $claws_addr .= "      <attribute uid=\"$time\" name=\"Business Homepage\" >"
235                         ."$tbdata[31]</attribute>\n";
236         }
237         if ($tbdata[27] ne "") {
238             $time++;
239             $claws_addr .= "      <attribute uid=\"$time\" name=\"Notes\" >"
240                         ."$tbdata[27]</attribute>\n";
241         }
242         $claws_addr .= "    </attribute-list>\n";
243     }
244 #    if ( $check > 0 ) {
245 #        $claws_addr .= "</attribute-list>\n";
246 #    }
247
248     $claws_addr .=  "  </person>\n";
249     $time++;
250 }
251 $claws_addr .= "</address-book>\n";
252
253 # print new addressbook to file
254 open (NEWADDR, ">.claws-mail/addrbook/$new_addrbk");
255     print NEWADDR $claws_addr;
256 close NEWADDR;
257
258 # add new addressbook to index
259 open (ADDRIN, "<.claws-mail/addrbook/addrbook--index.xml") || die("Can't open addrbook--index.xml");
260     @addrindex_file = <ADDRIN>;
261 close ADDRIN;
262
263 foreach $addrindex_line (@addrindex_file) {
264     if ($addrindex_line =~ m/<\/book_list>/) {
265         $rewrite_addrin .= "    <book name=\"The Bat! Address Book\" file=\"$new_addrbk\" />\n"
266                         ."  </book_list>\n";
267     } else {
268         $rewrite_addrin .= "$addrindex_line";
269     }
270 }
271
272 open (NEWADDRIN, ">.claws-mail/addrbook/addrbook--index.xml");
273     print NEWADDRIN "$rewrite_addrin";
274 close NEWADDRIN;
275 print "You have sucessfully converted your The Bat! addressbook\n";
276 print "New addressbook file name: $new_addrbk\n";
277 print "Total addresses converted: $total_addresses\n";
278 exit;