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>
10 # Thanks goes to : Paul Mangan <claws@thewildbeast.co.uk>
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
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
23 # - add info about number of converted addresses
26 # - first public release
30 # * Add switch for Full Name entry on atrybute part
35 GetOptions("tbfile=s" => \$tbfile);
37 #$tbfile = '/home/urke/bin/sylpheed/tb2ldif/thebat-addressbook.csv';
42 # check is Sylpheed instrtalled
43 opendir(SYLPHEED, ".sylpheed") || die("Can't open Sylpheed directory! Conversion abroted\n");
44 push(@cached, (readdir(SYLPHEED)));
47 # get last existing addressbook filename
48 # to set filename for newest addressbook
50 # get all existing addressbook filenames
51 foreach $cached (@cached) {
52 if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) {
53 push(addr, "$cached");
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$//;
62 $new_addrbk = "addrbook-"."$last_one".".xml";
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");
69 # shift firs line from file because this is field names
70 $dross = shift(@tblines);
72 # set time mark and header of addressbook
74 $sylph_addr = "<?xml version=\"1.0\" encoding=\"ISO-8859-2\" ?>\n";
75 $sylph_addr .= "<address-book name=\"The Bat! Address Book\" >\n";
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
85 $tbdata =~ s/"/"/g;
86 $tbdata =~ s/&/&/g;
87 $tbdata =~ s/'/'/g;
88 $tbdata =~ s/</</g;
89 $tbdata =~ s/>/>/g;
90 $tbdata =~ s/\\2C\ /, /g;
91 $tbdata =~ s/(\\0D\\0A){1,}/, /g;
92 $tbdata =~ s/\ {2,}/ /g;
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"
100 $sylph_addr .= " <address uid=\"$time\" alias=\"$tbdata[5]\" email=\"$tbdata[4]\""
101 ." remarks=\"\" />\n"
102 ." </address-list>\n";
104 # find is need to make entry attributes
106 for($i=6; $i<=31; $i++) {
107 $tbdata[$i] =~ s/^\s+//;
108 $tbdata[$i] =~ s/\s+$//;
109 if ($tbdata[$i] ne "") { $check += 1; }
113 if ($tbdata[1] ne "" || $tbdata[2] ne "") {
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]"; }
120 $sylph_addr .= " <attribute uid=\"$time\" name=\"Full Name\" >"
121 ."$full_name</attribute>\n";
123 if ($tbdata[15] ne "") {
125 $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Street Address\" >"
126 ."$tbdata[15]</attribute>\n";
128 if ($tbdata[16] ne "") {
130 $sylph_addr .= " <attribute uid=\"$time\" name=\"Home City Address\" >"
131 ."$tbdata[16]</attribute>\n";
133 if ($tbdata[17] ne "") {
135 $sylph_addr .= " <attribute uid=\"$time\" name=\"Home State Address\" >"
136 ."$tbdata[17]</attribute>\n";
138 if ($tbdata[18] ne "") {
140 $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Zip Address\" >"
141 ."$tbdata[18]</attribute>\n";
143 if ($tbdata[19] ne "") {
145 $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Country Address\" >"
146 ."$tbdata[19]</attribute>\n";
148 if ($tbdata[9] ne "") {
150 $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Phone\" >"
151 ."$tbdata[9]</attribute>\n";
153 if ($tbdata[10] ne "") {
155 $sylph_addr .= " <attribute uid=\"$time\" name=\"Home Fax\" >"
156 ."$tbdata[10]</attribute>\n";
158 if ($tbdata[11] ne "") {
160 $sylph_addr .= " <attribute uid=\"$time\" name=\"Mobile Phone\" >"
161 ."$tbdata[11]</attribute>\n";
163 if ($tbdata[30] ne "") {
165 $sylph_addr .= " <attribute uid=\"$time\" name=\"Personal Homepage\" >"
166 ."$tbdata[30]</attribute>\n";
168 if ($tbdata[14] ne "") {
170 $sylph_addr .= " <attribute uid=\"$time\" name=\"Pager\" >"
171 ."$tbdata[14]</attribute>\n";
173 if ($tbdata[7] ne "") {
175 $sylph_addr .= " <attribute uid=\"$time\" name=\"Bussines Company\" >"
176 ."$tbdata[7]</attribute>\n";
178 if ($tbdata[8] ne "") {
180 $sylph_addr .= " <attribute uid=\"$time\" name=\"Bussines Job Title\" >"
181 ."$tbdata[8]</attribute>\n";
183 if ($tbdata[20] ne "") {
185 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Department\" >"
186 ."$tbdata[20]</attribute>\n";
188 if ($tbdata[21] ne "") {
190 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Office\" >"
191 ."$tbdata[21]</attribute>\n";
193 if ($tbdata[22] ne "") {
195 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Street Address\" >"
196 ."$tbdata[22]</attribute>\n";
198 if ($tbdata[23] ne "") {
200 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business City Address\" >"
201 ."$tbdata[23]</attribute>\n";
203 if ($tbdata[24] ne "") {
205 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business State Address\" >"
206 ."$tbdata[24]</attribute>\n";
208 if ($tbdata[25] ne "") {
210 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Zip Address\" >"
211 ."$tbdata[25]</attribute>\n";
213 if ($tbdata[26] ne "") {
215 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Country Address\" >"
216 ."$tbdata[26]</attribute>\n";
218 if ($tbdata[12] ne "") {
220 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Phone\" >"
221 ."$tbdata[12]</attribute>\n";
223 if ($tbdata[13] ne "") {
225 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Fax\" >"
226 ."$tbdata[13]</attribute>\n";
228 if ($tbdata[31] ne "") {
230 $sylph_addr .= " <attribute uid=\"$time\" name=\"Business Homepage\" >"
231 ."$tbdata[31]</attribute>\n";
233 if ($tbdata[27] ne "") {
235 $sylph_addr .= " <attribute uid=\"$time\" name=\"Notes\" >"
236 ."$tbdata[27]</attribute>\n";
238 $sylph_addr .= " </attribute-list>\n";
240 $sylph_addr .= " </person>\n";
243 $sylph_addr .= "</address-book>\n";
245 # print new addressbook to file
246 open (NEWADDR, ">.sylpheed/$new_addrbk");
247 print NEWADDR $sylph_addr;
250 # add new addressbook to index
251 open (ADDRIN, "<.sylpheed/addrbook--index.xml") || die("Can't open addrbook--index.xml");
252 @addrindex_file = <ADDRIN>;
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"
260 $rewrite_addrin .= "$addrindex_line";
264 open (NEWADDRIN, ">.sylpheed/addrbook--index.xml");
265 print NEWADDRIN "$rewrite_addrin";
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";