speed up of marking messages as read
[claws.git] / tools / outlook2sylpheed.pl
1 #!/usr/bin/perl -w
2
3 #  * Copyright 2002 Ricardo Mones Lastra <mones@aic.uniovi.es>
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 # outlook2sylpheed.pl -- perl script to convert an Outlook generated 
20 #                        contact list into a Sylpheed XML address book.
21
22 # This script is based on:
23 #       out2syl.sh by Rafael Lossurdo <mugas@via-rs.net>
24 #       kmail2sylpheed.pl by Paul Mangan <claws@thewildbeast.co.uk>
25 #
26 # See README file for details and usage.
27 #  
28
29 # parse required parameter
30 die "Required filename missing\nSyntax: $0 fullpathname\n" unless (defined($ARGV[0]));
31 $outl_file = $ARGV[0];
32
33 $sylconf = ".sylpheed";
34 $indexname = "$sylconf/addrbook--index.xml";
35
36 # the next is mostly Paul's code
37 $time = time;
38
39 chdir;
40 opendir(SYLPHEED, $sylconf) || die("can't open $sylconf directory\n");
41         push(@cached,(readdir(SYLPHEED)));
42 closedir(SYLPHEED);
43
44 foreach $cached (@cached) {
45         if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) {
46                 push(@addr, "$cached");
47         }
48 }
49
50 @sorted = sort {$a cmp $b} @addr;
51 $last_one = pop(@sorted);
52 $last_one =~ s/^addrbook-//;
53 $last_one =~ s/.xml$//;
54 $last_one++;
55 $new_book = "/addrbook-"."$last_one".".xml";
56
57 # ok, was enough, do some more bit bashing now
58 open(OUTL, $outl_file) 
59         or die "can't open $outl_file for reading\n";
60 open(NEWB, '>', "$sylconf/$new_book") 
61         or die "can't open $new_book for writting\n";
62
63 $_ = <OUTL>; # skip first line
64 $count = 0;
65 # header
66 print NEWB "<?xml version=\"1.0\" encoding=\"US-ASCII\" ?>\n";
67 print NEWB "<address-book name=\"Outlook Address Book\" >\n"; 
68 while (<OUTL>) {
69         chomp;
70         if (/\s+[0-9]+\s+(.+)/) { $_ = $1; } 
71         else { $count += 2 and die "wrong format at line $count \n"; }
72         @field = split(';',$_); # first is name, second mail addr
73         print NEWB "<person uid=\"", $time, "\" first-name=\"\"";
74         print NEWB "last-name=\"\" nick-name=\"\" cn=\"", $field[0];
75         print NEWB "\" >\n<address-list>\n";
76         ++$time;
77         $field[1] =~ s/\r//; # beware, dangerous chars inside ;)
78         print NEWB "<address uid=\"", $time, "\" alias=\"\" email=\"", $field[1];
79         print NEWB "\" /> \n</address-list>\n</person>\n";
80         ++$time;
81         ++$count;
82 }
83 print NEWB "</address-book>\n";
84
85 close NEWB;
86 close OUTL;
87
88 # update index (more Paul's code :)
89
90 open(INDX, $indexname) 
91         or die "can't open $indexname for reading\n";
92 @index_file = <INDX>;
93 close INDX;
94
95 foreach $index_line (@index_file) {
96         if ($index_line =~ m/<\/book_list>/) {
97                 $new_index .= "    <book name=\"Outlook Address Book\" file=\"$new_book\" />\n"."  </book_list>\n";                                                     } else {
98                 $new_index .= "$index_line";
99         }
100 }
101 open (INDX, '>', $indexname)
102         or die "can't open $indexname for writting\n";
103 print INDX "$new_index";
104 close INDX;
105
106 print "Done. $count address(es) converted successfully.\n";
107