1#!/usr/bin/perl 2 3# * Copyright 2007 J�r�me Lelong <jerome.lelong@gmail.com> 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 19## script name : mew2claws-mail.pl 20 21## script purpose : convert a Mew addressbook into a Claws Mail addressbook 22 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 25## little. 26 27use Getopt::Long; 28 29## Process the command line options 30## the program expects one argument: the Mew addressbook file 31 32my $help=0; 33my $mewfile=''; 34GetOptions("mew-addressbook=s" => \$mewfile, 35 "help" => \$help); 36 37if ($help==1) 38{ 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"); 42 exit 0; 43} 44if ($mewfile ne '' && !-f $mewfile) 45{ 46 print("file $mewfile does not exists\n"); 47 exit 1; 48} 49 50$time=time; 51$claws_addr=''; 52$home = glob("~"); 53$clawsdir=`claws-mail --config-dir`; 54chomp($clawsdir); 55$clawsdir = $home . '/' . $clawsdir . '/' . 'addrbook/'; 56 57opendir(CLAWS, $clawsdir) || die("Can't open $clawsdir directory\n"); 58push(@cached,(readdir(CLAWS))); 59closedir(CLAWS); 60 61## find the first availabel name for a new addressbook in claws-mail 62foreach $cached (@cached) 63{ 64 if ($cached =~ m/^addrbook/ && $cached =~ m/[0-9].xml$/) 65 { 66 push(@addr, "$cached"); 67 } 68} 69@sorted = sort {$a cmp $b} @addr; 70$last_one = pop(@sorted); 71$last_one =~ s/^addrbook-//; 72$last_one =~ s/.xml$//; 73$last_one++; 74$new_addrbk = "addrbook-"."$last_one".".xml"; 75 76 77open (MEWFILE, "<$mewfile") || die("Can't find the Mew addressbook file\n"); 78@mewentries = <MEWFILE>; 79close MEWFILE; 80 81$claws_addr .= "<?xml version=\"1.0\" encoding=\"ISO8859-1\" ?>\n" 82. "<address-book name=\"Mew Address Book\" >"; 83 84 85chomp(@mewentries); 86foreach $line (@mewentries) 87{ 88 $line =~ s/ *\t/ /g; 89 $line =~ s/ *$//g; 90 (@fields) = split(/ +/,$line); 91 $nickname= shift(@fields); 92 @emails=(); 93 $alias=''; 94 $firstname=''; 95 $lastname=''; 96 while (1) 97 { 98 $field = shift(@fields); 99 if ($field =~ m/@/) 100 { 101 $field =~ s/,$//; 102 push(@emails, $field); 103 } else 104 { 105 unshift(@fields, $field); 106 last; 107 } 108 } 109 $alias = shift(@fields); 110 if ($alias eq "\*") 111 { 112 print($alias . "\n"); 113 $alias=''; 114 } 115 116 117 $firstname=shift(@fields); $firstname =~ s/"//g; 118 foreach (@fields) 119 { 120 $lastname .= "$_ "; 121 } 122 $lastname =~ s/"//g; 123 $lastname =~ s/ *$//g; 124 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"; 129 $time++; 130 131 foreach $email (@emails) 132 { 133 $claws_addr .= " <address uid=\"$time\" alias=\"$alias\" email=\"$email\"" 134 ." remarks=\"\" />\n"; 135 $time++; 136 } 137 $claws_addr .= " </address-list>\n" 138 . " <attribute-list>\n" 139 . " </attribute-list>\n"; 140 $claws_addr .= " </person>\n"; 141 $time++; 142} 143$claws_addr .= "</address-book>\n"; 144 145open (NEWADDR, ">$clawsdir/$new_addrbk") ; 146print NEWADDR ($claws_addr); 147close NEWADDR; 148 149open (ADDRIN, "<$clawsdir/addrbook--index.xml") || die("can't open addrbook--index.xml"); 150@addrindex_file = <ADDRIN>; 151close ADDRIN; 152 153foreach $addrindex_line (@addrindex_file) 154{ 155 if ($addrindex_line =~ m/<book name=\"Mew Address Book\"/) 156 { 157 print("An entry already exists for \"Mew Address Book\", you may duplicate it\n"); 158 print("Continuing anyway...\n"); 159 } 160 if ($addrindex_line =~ m/<\/book_list>/) 161 { 162 $rewrite_addrin .= " <book name=\"Mew Address Book\" file=\"$new_addrbk\" />\n" 163 ." </book_list>\n"; 164 } else 165 { 166 $rewrite_addrin .= "$addrindex_line"; 167 } 168} 169 170open (NEWADDRIN, ">$clawsdir/addrbook--index.xml"); 171print NEWADDRIN "$rewrite_addrin"; 172close NEWADDRIN; 173 174print "\nYou have sucessfully converted your Mew addressbook\n"; 175exit; 176