]> git.deb.at Git - pkg/abook.git/blob - contrib/vcard2abook.pl
Merge remote-tracking branch 'upstream/master' into upstream
[pkg/abook.git] / contrib / vcard2abook.pl
1 #!/usr/bin/perl -w
2 my $timestamp= "Time-stamp: \"vcard2abook.pl was last updated on Sun, 17 Dec 2000 10:34am\"";
3
4 #==============================================================================*
5 #   vcard2abook.pl by jeff covey <jeff.covey@pobox.com>                        *
6 #                                                                              *
7 #   this script has two main features:                                         *
8 #                                                                              *
9 #   1. it converts a file containing addressbook entries in vcard format to    *
10 #      one containing entries in abook format.                                 *
11 #   2. it almost has more comments than code.                                  *
12 #                                                                              *
13 #   This program is free software; you can redistribute it and/or modify       *
14 #   it under the terms of the GNU General Public License as published by       *
15 #   the Free Software Foundation; either version 2 of the License, or          *
16 #   (at your option) any later version.                                        *
17 #                                                                              *
18 #   This program is distributed in the hope that it will be useful,            *
19 #   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
20 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
21 #   GNU General Public License for more details.                               *
22 #                                                                              *
23 #   You should have received a copy of the GNU General Public License          *
24 #   along with this program; if not, write to the Free Software                *
25 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                  *
26 #==============================================================================*
27
28 use strict;
29
30 ($#ARGV >= 1) or die
31   "usage: vcard2abook.pl <vcard input file> <abook output file>\noutput file will be overwritten!\n";
32
33 my $vcards="$ARGV[0]";
34 my $abook ="$ARGV[1]";
35 my $key;
36
37 my %conversions = (
38                    "FN"         => "name=",
39                    "NICKNAME"   => "nick=",
40                    "EMAIL"      => "email=",
41                    "ORG"        => "notes=",
42                    "NOTE"       => "notes=",
43                    "URL"        => "url=",
44                    
45                    "TEL;HOME"   => "phone=",
46                    "TEL;PREF"   => "phone=",
47                    "TEL;VOICE"  => "phone=",
48                    "TEL;MSG"    => "phone=",
49                    "TEL;VIDEO"  => "phone=",
50                    "TEL;MODEM"  => "phone=",
51                    "TEL;ISDN"   => "phone=",
52                    "TEL;WORK"   => "workphone=",
53                    "TEL;CELL"   => "mobile=",
54                    "TEL;PAGER"  => "mobile=",
55                    "TEL;CAR"    => "mobile=",
56                    "TEL;FAX"    => "fax=",
57                    );
58
59 open (VCARDS,"$vcards") or quit("couldn't open $vcards");
60 open (ABOOK,">$abook")  or quit("couldn't open $abook for writing");
61
62 while (<VCARDS>) {
63   if    (/^\s*$/)         { }
64   elsif (/^BEGIN:VCARD/i) { print ABOOK "[]\n"; }
65   elsif (/^END:VCARD/i)   { print ABOOK   "\n"; }
66   else {
67     chomp; my @sections=split /:/, $_, 2;
68     if ($sections[0] =~ /^ADR/i) {
69       my @fields=split /;/, $sections[1];
70       if ($fields[2]) {print ABOOK "address=$fields[2]\n";}
71       if ($fields[3]) {print ABOOK "city=$fields[3]\n";   }
72       if ($fields[4]) {print ABOOK "state=$fields[4]\n";  }
73       if ($fields[5]) {print ABOOK "zip=$fields[5]\n";    }
74       if ($fields[6]) {print ABOOK "country=$fields[6]\n";}
75     }
76     else {
77       foreach $key (keys %conversions) {
78         if ($sections[0] =~ /^$key/i) {
79           print ABOOK "$conversions{$key}$sections[1]\n";
80         }
81       }
82     }
83   }
84 }
85
86 close (VCARDS) or quit("couldn't close $vcards");
87 close (ABOOK)  or quit("couldn't close $abook");
88
89 sub quit {
90   print "whoops!  $_[0]:\n $!\n"; die;
91 }