aboutsummaryrefslogtreecommitdiffstats
path: root/tools/csv2vcard
blob: b968fbd9c38c42432dbe8f8b2fac2c2c7dad9f5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/perl -w
#
# cvs2vcard - Script to convert Outlook CSV files into VCard files
# suitable to be imported into Evolution.
#
# Copyright (C) 2001 Ximian, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Author: Michael MacDonald <mjmac@ximian.com>
#

use strict;
use diagnostics;
use Text::ParseWords;

sub usage
{
  print STDERR << "--EndOfUsage";

Takes a CSV-formatted list of contacts from Outlook and attempts to
convert it into a list of VCards suitable for import into Evolution.

Usage: $0 [infile outfile]

--EndOfUsage

  exit;
}

sub is_recognized_format
{
  my $line = shift;

  # Making some assumptions here...  Prolly OK.
  return $line =~ /(First Name|Middle Name|Last Name)/; 
}

sub map_columns
{
  my $line = shift;

  my @names = parse_line(',', 0, $line);

  my $ctr = 0;
  my %fieldmap = map { $_ => $ctr++ } @names;

  return %fieldmap;
}

sub build_vcard_attr_from_def
{
  my ($def, $fields, $map) = @_;

  # Valid chars for lookup (from Outlook CSV) are
  # A-Za-z0-9_-'/
  # Valid chars for formatting of attr are
  # \s,|
  my @lookup = map { s/=0A$//; s/[^\w\s\-'\/]//; $_; } split /[\s,]*\|[\s,]*/, $def;

  foreach my $el (@lookup) {
    unless (defined($map->{ $el })) {
      print STDERR "$el is undefined\n";
      next;
    }
    if (defined($fields->[$map->{ $el }])) {
      unless ($fields->[$map->{ $el }] =~ /(^$|0\/0\/00)/) {
        $def =~ s/$el/$fields->[$map->{ $el }]/;
      } else {
        $def =~ s/((?<=\|)\s*)?$el(\s*?(?=\|))?(=0A)?,?//;
      }
    } else {
      $def =~ s/((?<=\|)\s*)?$el(\s*?(?=\|))?(=0A)?,?//;
    }
  }
  # Get rid of field delimiters
  $def =~ s/\|//g;
  # Snip off any trailing semicolons or whitespace
  $def =~ s/[\s;]*$//;

  return $def;
}

sub build_vcard_from_line {
  my ($line, %map) = @_;
  my %vcard;

  my @fields = parse_line(',', 0, $line);

  my %vcard_def = ( FN => 'Title |First Name |Middle Name |Last Name |Suffix',
            N => 'Last Name| Suffix|;First Name|;Middle Name|;Title',
           'ADR;WORK'  => 'PO Box|;Business Street 2|;Business Street|;Business City|;Business State|;Business Postal Code|;Business Country',
           'LABEL;QUOTED-PRINTABLE;WORK' => 'PO Box |Business Street=0A|Business Street 2=0A|Business City,| Business State| Business Postal Code=0A|Business Country',
           'TEL;WORK;VOICE' => 'Business Phone',
           'TEL;WORK;VOICE2' => 'Business Phone 2',
           'TEL;WORK;FAX' => 'Business Fax',
           'TEL;WORK;COMPANY' => 'Company Main Phone',
           'ADR;HOME'  => ';Home Street 2|;Home Street|;Home City|;Home State|;Home Postal Code|;Home Country',
           'LABEL;QUOTED-PRINTABLE;HOME' => 'Home Street=0A|Home Street 2=0A|Home City,| Home State| Home Postal Code=0A|Home Country',
           'TEL;HOME;VOICE' => 'Home Phone',
           'TEL;HOME;VOICE2' => 'Home Phone 2',
           'TEL;HOME;FAX' => 'Home Fax',
           'ADR;POSTAL'  => ';Other Street 2|;Other Street|;Other City|;Other State|;Other Postal Code|;Other Country',
           'LABEL;QUOTED-PRINTABLE;POSTAL' => 'Other Street=0A|Other Street 2=0A|Other City,| Other State| Other Postal Code=0A|Other Country',
           'TEL;VOICE' => 'Other Phone',
           'TEL;FAX' => 'Other Fax',
           'TEL;CELL' => 'Mobile Phone',
           'TEL;CAR' => 'Car Phone',
           'TEL;PAGER' => 'Pager',
           'TEL;PREF' => 'Primary Phone',
           'TEL;ISDN' => 'ISDN',
           'TEL;X-EVOLUTION-CALLBACK' => 'Callback',
           'TEL;X-EVOLUTION-TTYTDD' => 'TTY/TDD Phone',
           'TEL;X-EVOLUTION-TELEX' => 'Telex',
           'TEL;X-EVOLUTION-RADIO' => 'Radio Phone',
           'EMAIL;INTERNET' => 'E-mail Address',
           'EMAIL;INTERNET2' => 'E-mail 2 Address',
           'EMAIL;INTERNET3' => 'E-mail 3 Address',
            ORG => 'Company|;Department',
            TITLE => 'Job Title',
            ROLE => 'Profession',
                   'X-EVOLUTION-ASSISTANT' => "Assistant's Name",
           'TEL;X-EVOLUTION-ASSISTANT' => "Assistant's Phone",
           'X-EVOLUTION-SPOUSE' => 'Spouse',
                   'X-EVOLUTION-ANNIVERSARY' => 'Anniversary',
                   'X-EVOLUTION-MANAGER' => "Manager's Name",
                   'X-EVOLUTION-OFFICE' => 'Office Location',
            BDAY => 'Birthday',
            NOTE => 'Notes',
            FBURL => 'Internet Free Busy',
            URL => 'Web Page',
                   );

  foreach my $key (keys(%vcard_def)) {
    my $attr = build_vcard_attr_from_def($vcard_def{ $key }, \@fields, \%map);
    if (defined($attr)) {
      $vcard{ $key } = $attr unless ($attr =~ /^$/);
    }
  } 

  return %vcard;
}

sub print_vcard_to_fh
{
  my ($fh, %vcard) = @_;
 
  print $fh "BEGIN:VCARD\n";
  foreach my $key (keys(%vcard)) {
    # Dirty hack because Evolution's vcard stores multiple email addrs
    # with same sttribute, hence key collision.  Bleah.
    # Ugh!  Same deal for multiple phones... (eg. bus. phone)
    #
    # And finally, while we're special-casing...  Outlook exports dates
    # differently, so munge 'em if we find 'em.
    if ($key =~ /EMAIL;INTERNET/o) {
      (my $temp = $key) =~ s/\d$//;
      print $fh "$temp:$vcard{ $key }\n";
    } elsif ($key =~ /TEL;(HOME|WORK)/o) {
      (my $temp = $key) =~ s/\d$//;
      print $fh "$temp:$vcard{ $key }\n";
    } elsif ($key =~ /(BDAY|X\-EVOLUTION\-ANNIVERSARY)/o) {
      my $temp = $vcard{ $key };
      if ($temp =~ /(\d\d)\/(\d\d)\/(\d\d)/) {
        # Y2k !!  MS Didn't learn anything.
        # Hope no one was born before 1915
        if ((1900 + $3) < 1915) {
          print $fh "$key:20$3-$1-$2\n";
        } else {
          print $fh "$key:19$3-$1-$2\n";
        }
      } else {
        # Something's funky...  Just delete the attribute
        print STDERR "Couldn't figure out what to do with $key:$vcard{ $key }\n";
        delete($vcard{ $key });
      } 
    } else {
      print $fh "$key:$vcard{ $key }\n";
    }
  }
  print $fh "END:VCARD\n\n";
}

my $in  = $ARGV[0];
my $out = $ARGV[1];

usage() unless(defined($in) && defined($out));

open (IN, $in)
  or die "Can't open($in): $!\n";

open (OUT, ">$out")
  or die "Can't open($out): $!\n";

my $linectr = 0;
my %map;

while (my $line = <IN>) {
  $line =~ s/\r//g;
  $line =~ s/\n$//;
  if ($linectr == 0) {
    $linectr++;
    usage() unless is_recognized_format($line);
    %map = map_columns($line);
    #if ($line =~ /\r\n$/) {
    #  print STDERR "Apparenlty found DOS-style EOL indicators...\n";
      $/ = "\r\n";
    #}
  } else {
    $linectr++;
    while ($line =~ /^(("([^"]|\n|"")*")?,)*"([^"]|\n|"")*$/) {
      my $temp = $line;
      $line = <IN>;
      $line =~ s/\r//g;
      $line =~ s/\n$//;
      $line = "$temp $line";
    }
    my %vcard = build_vcard_from_line($line, %map);
    print_vcard_to_fh(\*OUT, %vcard);
  }
}  

close(IN);
close(OUT);