3 # * Copyright 2007 Jérôme Lelong <jerome.lelong@gmail.com>
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 3 of the License, or
8 # * (at your option) any later version.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 ## script name : mew2claws-mail.pl
21 ## script purpose : convert a Mew addressbook into a Claws Mail addressbook
23 ## This script assumes your Mew addressbook is Latin-1 encoded and not
24 ## unicode. In this latter case, you will have to hack this script a
29 ## Process the command line options
30 ## the program expects one argument: the Mew addressbook file
34 GetOptions
("mew-addressbook=s" => \
$mewfile,
39 print("usage : perl mew2claws-mail.pl [--help] [--mew-addressbook=file] \n");
40 print("\t--help: displays this help\n");
41 print("\t--mew-addressbook=file : file is the filename of your Mew addressbook\n");
44 if ($mewfile ne '' && !-f
$mewfile)
46 print("file $mewfile does not exists\n");
53 $clawsdir=`claws-mail --config-dir`;
55 $clawsdir = $home . '/' . $clawsdir . '/' . 'addrbook/';
57 opendir(CLAWS
, $clawsdir) || die("Can't open $clawsdir directory\n");
58 push(@cached,(readdir(CLAWS
)));
61 ## find the first availabel name for a new addressbook in claws-mail
62 foreach $cached (@cached)
64 if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/)
66 push(@addr, "$cached");
69 @sorted = sort {$a cmp $b} @addr;
70 $last_one = pop(@sorted);
71 $last_one =~ s/^addrbook-//;
72 $last_one =~ s/.xml$//;
74 $new_addrbk = "addrbook-"."$last_one".".xml";
77 open (MEWFILE
, "<$mewfile") || die("Can't find the Mew addressbook file\n");
78 @mewentries = <MEWFILE
>;
81 $claws_addr .= "<?xml version=\"1.0\" encoding=\"ISO8859-1\" ?>\n"
82 . "<address-book name=\"Mew Address Book\" >";
86 foreach $line (@mewentries)
90 (@fields) = split(/ +/,$line);
91 $nickname= shift(@fields);
98 $field = shift(@fields);
102 push(@emails, $field);
105 unshift(@fields, $field);
109 $alias = shift(@fields);
112 print($alias . "\n");
117 $firstname=shift(@fields); $firstname =~ s/"//g;
123 $lastname =~ s/ *$//g;
125 $claws_addr .= " <person uid=\"$time\" first-name=\"$firstname\""
126 ." last-name=\"$lastname\" nick-name=\"$nickname\""
127 ." cn=\"$firstname $lastname\" >\n"
128 ." <address-list>\n";
131 foreach $email (@emails)
133 $claws_addr .= " <address uid=\"$time\" alias=\"$alias\" email=\"$email\""
134 ." remarks=\"\" />\n";
137 $claws_addr .= " </address-list>\n"
138 . " <attribute-list>\n"
139 . " </attribute-list>\n";
140 $claws_addr .= " </person>\n";
143 $claws_addr .= "</address-book>\n";
145 open (NEWADDR
, ">$clawsdir/$new_addrbk") ;
146 print NEWADDR
($claws_addr);
149 open (ADDRIN
, "<$clawsdir/addrbook--index.xml") || die("can't open addrbook--index.xml");
150 @addrindex_file = <ADDRIN
>;
153 foreach $addrindex_line (@addrindex_file)
155 if ($addrindex_line =~ m/<book name=\"Mew Address Book\"/)
157 print("An entry already exists for \"Mew Address Book\", you may duplicate it\n");
158 print("Continuing anyway...\n");
160 if ($addrindex_line =~ m/<\/book_list
>/)
162 $rewrite_addrin .= " <book name=\"Mew Address Book\" file=\"$new_addrbk\" />\n"
166 $rewrite_addrin .= "$addrindex_line";
170 open (NEWADDRIN
, ">$clawsdir/addrbook--index.xml");
171 print NEWADDRIN
"$rewrite_addrin";
174 print "\nYou have sucessfully converted your Mew addressbook\n";