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