add Keyword Warner plugin details
[claws.git] / tools / mew2claws-mail.pl
blobe626282c4aff361eabb56cb609d2ed5cbe99130f
1 #!/usr/bin/perl
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.
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
25 ## little.
27 use Getopt::Long;
29 ## Process the command line options
30 ## the program expects one argument: the Mew addressbook file
32 my $help=0;
33 my $mewfile='';
34 GetOptions("mew-addressbook=s" => \$mewfile,
35 "help" => \$help);
37 if ($help==1)
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;
44 if ($mewfile ne '' && !-f $mewfile)
46 print("file $mewfile does not exists\n");
47 exit 1;
50 $time=time;
51 $claws_addr='';
52 $home = glob("~");
53 $clawsdir=`claws-mail --config-dir`;
54 chomp($clawsdir);
55 $clawsdir = $home . '/' . $clawsdir . '/' . 'addrbook/';
57 opendir(CLAWS, $clawsdir) || die("Can't open $clawsdir directory\n");
58 push(@cached,(readdir(CLAWS)));
59 closedir(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$//;
73 $last_one++;
74 $new_addrbk = "addrbook-"."$last_one".".xml";
77 open (MEWFILE, "<$mewfile") || die("Can't find the Mew addressbook file\n");
78 @mewentries = <MEWFILE>;
79 close MEWFILE;
81 $claws_addr .= "<?xml version=\"1.0\" encoding=\"ISO8859-1\" ?>\n"
82 . "<address-book name=\"Mew Address Book\" >";
85 chomp(@mewentries);
86 foreach $line (@mewentries)
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)
98 $field = shift(@fields);
99 if ($field =~ m/@/)
101 $field =~ s/,$//;
102 push(@emails, $field);
103 } else
105 unshift(@fields, $field);
106 last;
109 $alias = shift(@fields);
110 if ($alias eq "\*")
112 print($alias . "\n");
113 $alias='';
117 $firstname=shift(@fields); $firstname =~ s/"//g;
118 foreach (@fields)
120 $lastname .= "$_ ";
122 $lastname =~ 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";
129 $time++;
131 foreach $email (@emails)
133 $claws_addr .= " <address uid=\"$time\" alias=\"$alias\" email=\"$email\""
134 ." remarks=\"\" />\n";
135 $time++;
137 $claws_addr .= " </address-list>\n"
138 . " <attribute-list>\n"
139 . " </attribute-list>\n";
140 $claws_addr .= " </person>\n";
141 $time++;
143 $claws_addr .= "</address-book>\n";
145 open (NEWADDR, ">$clawsdir/$new_addrbk") ;
146 print NEWADDR ($claws_addr);
147 close NEWADDR;
149 open (ADDRIN, "<$clawsdir/addrbook--index.xml") || die("can't open addrbook--index.xml");
150 @addrindex_file = <ADDRIN>;
151 close 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"
163 ." </book_list>\n";
164 } else
166 $rewrite_addrin .= "$addrindex_line";
170 open (NEWADDRIN, ">$clawsdir/addrbook--index.xml");
171 print NEWADDRIN "$rewrite_addrin";
172 close NEWADDRIN;
174 print "\nYou have sucessfully converted your Mew addressbook\n";
175 exit;