Support 9210 CSV contacts format.
[PerlMail.git] / contacts-n9k2mutt
1 #! /usr/bin/perl
2
3 #       $Id$
4 # Copyright (C) 2002-2003 Jan Kratochvil <project-PerlMail@jankratochvil.net>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21 use vars qw($VERSION);
22 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
23 use strict;
24 use warnings;
25
26 require Getopt::Long;
27 require PerlMail::Contacts::9000;
28 require Mail::Address;
29
30
31 Getopt::Long::Configure(
32                 "no_ignorecase",
33                 "bundling",
34                 );
35 my $opt_9000;
36 die if !Getopt::Long::GetOptions(
37                 "9000"=>\$opt_9000,
38                 );
39
40 my $type="9210";
41 $type="9000" if $opt_9000;
42 my $class="PerlMail::Contacts::$type";
43 eval "require $class;1;" or die "Cannot load loader $class: $!";
44
45 undef $/;
46 while (<>) {
47         my @data=map({
48                         my @record=@$_;
49                         my %record;
50                         map({
51                                         my %item=%$_;
52                                         $record{$item{"FIELD"}}=$item{"FIELD-DATA"};
53                                         ($item{"FIELD"} ne "Mail" || !$item{"FIELD-DATA"} ? () : ({
54                                                         "name"=>$record{"Name"},
55                                                         "mail"=>$item{"FIELD-DATA"},
56                                                         "attr"=>$item{"FIELD-NAME"},
57                                                         "iscompany"=>($record{"Name"} && $record{"Company"} && $record{"Name"} eq $record{"Company"}),
58                                                         }));
59                                         } @record);
60                         } $class->in($_));
61         my @nicked=map({
62                         my $data=$_;
63                         my($name,$mail,$attr,$iscompany)=map(($data->{$_}),qw(name mail attr iscompany));
64                         $name=~s/,.*$//s;
65                         my @nicks=();
66                         my $name_force;
67                         while ($name=~s/"([^"]*)"([!])?//s) {
68                                 push @nicks,$1;
69                                 $name_force=$1 if $2;
70                                 }
71                         $name=~s/\s*$//s;
72                         $name=~s/^(\w+)\s+(\w+)$/$2 $1/s if !$iscompany;
73                         push @nicks,$name if !$name_force;
74                         for (@nicks) {
75                                 $_.=".$attr" if $attr;
76                                 s/\s+/./gs;
77                                 }
78                         map({
79                                         "nick"=>$_,
80                                         "obj"=>Mail::Address->new(($name_force || $name),$mail),
81                                         },@nicks);
82                         } @data);
83         for (@nicked) {
84                 print "alias ".$_->{"nick"}." ".$_->{"obj"}->format()."\n";
85                 }
86         }