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