Debian Codegraph @

src/2vcard

raw ◀ ▶
1 Debian patch touch this file
•1 #!/usr/bin/perl -w
2 use strict;
3 # Hint from http://www.seligma.com/download/palm-ldif2csv
4 use MIME::Base64;
5 use MIME::QuotedPrint;
6
7 ###############################################################################
8 #
9 # $Author: Jan Schaumann <[email protected]> $
10 #
11 # Copyright (c) 2001,2002,2003,2005 Jan Schaumann <[email protected]>
12 # All rights reserved.
13 #
14 # Redistribution and use in source and binary forms, with or without
15 # modification, are permitted provided that the following conditions are met:
16 #
17 # * Redistributions of source code must retain the above copyright notice,
18 # this list of conditions and the following disclaimer.
19 # * Redistributions in binary form must reproduce the above copyright
20 # notice, this list of conditions and the following disclaimer in the
21 # documentation and/or other materials provided with the distribution.
22 # * The name of the author may not be used to endorse or promote products
23 # derived from this software without specific prior written permission.
24 #
25 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 # POSSIBILITY OF SUCH DAMAGE.
36 #
37 ###############################################################################
38
39 use Getopt::Std;
40
41 my $NAME = "2vcard";
42 my $VERSION = "0.6";
43 my %SUPPORTED = ("abook" => 0, "mh" => 0, "mutt" => 1,
44 "pine" => 0, "juno" => 0, "ldif" => 0,
45 "eudora" => 0);
46
47 init();
48 main();
49 done();
50
51 #we're done - bye, bye
52 exit 0;
53
54
55 ######### #########
56 ######### Functions #########
57 ######### #########
58
59 ###
60 # parses command-line options etc.
61 ###
62 sub init
63 {
64 my %Options;
65 my $ok = getopts('Ff:hi:o:v', \%Options);
66 if (!$ok) {
67 my $i;
68 my @values = keys(%Options);
69 foreach $i (@values) {
70 if (!$Options{$i}) {
71 print STDERR "Option '$i' requires an argument.\n";
72 print STDERR "Try $NAME -h for details.\n";
73 exit(1);
74 }
75 }
76 usage();
77 exit(1);
78 }
79
80 open (READ, "<&STDIN") ||
81 die "Can't read from STDIN!\n";
82
83 open (WRITE, ">&STDOUT") ||
84 die "Can't write to STDOUT!\n";
85
86 if ($Options{'f'}) {
87 setFormat("$Options{'f'}");
88 }
89 if ($Options{'F'}) {
90 formats();
91 exit 0;
92 }
93 if ($Options{'h'}) {
94 usage();
95 exit 0;
96 }
97 if ($Options{'v'}) {
98 print "$NAME Version $VERSION\n";
99 exit 0;
100 }
101 if ($Options{'i'}) {
102 open(READ, "$Options{'i'}") ||
103 die "Can't open \"$Options{'i'}\" for reading!\n";
104 }
105 if ($Options{'o'}) {
106 open(WRITE, ">$Options{'o'}") ||
107 die "Can't open \"$Options{'o'}\" for writing!\n";
108 }
109
110 if ($#ARGV > -1) {
111 usage();
112 exit(1);
113 }
114 }
115
116 ###
117 # sets the current format
118 ###
119 sub setFormat
120 {
121 my ($which) = @_;
122 my $key;
123 foreach $key (keys %SUPPORTED) {
124 $SUPPORTED{"$key"} = 0;
125 }
126 if ($which =~ m/(abook)|(eudora)|(juno)|(ldif)|(mh)|(mutt)|(pine)/) {
127 $SUPPORTED{"$which"} = 1;
128 } else {
129 print STDERR "$NAME: ERROR:\n";
130 print STDERR "Format \"$which\" not supported.\n";
131 print STDERR "Try \"-F\" and/or \"-h\".\n";
132 exit(1);
133 }
134 }
135
136 ###
137 # check which format we want to read and dispatch to the proper function
138 ###
139 sub main
140 {
141 parseAbook() if ($SUPPORTED{"abook"});
142 parseEudora() if ($SUPPORTED{"eudora"});
143 parseJuno() if ($SUPPORTED{"juno"});
144 parseLdif() if ($SUPPORTED{"ldif"});
145 parseMH() if ($SUPPORTED{"mh"});
146 parseMutt() if ($SUPPORTED{"mutt"});
147 parsePine() if ($SUPPORTED{"pine"});
148 }
149
150 ###
151 # cleanup before we say bye bye
152 ###
153 sub done
154 {
155 close WRITE;
156 close READ;
157 }
158
159 ###
160 # Outputs one info line
161 ###
162 sub Oneline
163 {
164 if ($_[0]) {
165 print WRITE "$_[1]";
166 if ($_[2]) {print WRITE ";quoted-printable";}
167 print WRITE ":$_[0]\n";
168 }
169 }
170
171 ###
172 # print a warning to stderr
173 ###
174 sub warnFormat
175 {
176 my ($line) = @_;
177 print STDERR "Skipping ill-formatted line:\n";
178 print STDERR " $line\n";
179 }
180
181 ###
182 # begin a new VCARD entry
183 ###
184 sub beginVcard
185 {
186 my ($fn, $n) = @_;
187
188 print WRITE "BEGIN:VCARD\n";
189 print WRITE "FN:$fn\n";
190 print WRITE "N:";
191 }
192
193 ###
194 # write an email entry to a VCARD
195 sub writeEmail
196 {
197 foreach my $i (@_) {
198 my $email = $i;
199 $email =~ s/.*<(.*\@.*)>/$1/;
200 print WRITE "EMAIL;INTERNET:$email\n";
201 }
202 }
203
204 ###
205 # end a VCARD entry
206 ###
207 sub endVcard
208 {
209 print WRITE "END:VCARD\n\n";
210 }
211
212 ###
213 # parse Eudora alias file
214 ###
215 sub parseEudora
216 {
217 my %eudoras = ();
218 while (<READ>) {
219
220 #s/ /\n/g;
221
222 # Format is:
223 # alias nick email@address
224 # alias nick2 email@address
225 # ...
226 # note nick <field1:value><field2:multi^Cline^Cvalue>...<fieldn:value>unknown^Cfields
227
228 # nicks first
229 if (m/^alias (.*) (.*@.*)$/) {
230 my $nick = $1;
231 my $email = $2;
232
233 $eudoras{"$nick"} = ();
234 $eudoras{"$nick"}{"email"} = $email;
235 }
236 elsif (m/^alias (.*) ,$/) {
237 my $nick = $1;
238 $eudoras{"$nick"} = ();
239 }
240
241 # all else later
242 if (m/note (.*) <(.*)$/) {
243 my $nick = $1;
244 my $info = $2;
245
246 if ($info =~ m/.*name:([^>]*)>[<\$]/) {
247 $eudoras{"$nick"}{"name"} = $1;
248 }
249
250 if ($info =~ m/.*phone:([^>]*)>.*/) {
251 $eudoras{"$nick"}{"phone"} = $1;
252 }
253
254 if ($info =~ m/.*address:([^>]*)>.*/) {
255 my $add = $1;
256 $add =~ s//;/g;
257 $eudoras{"$nick"}{"address"} = $add;
258 }
259
260 if ($info =~ m/.*fax:([^>]*)>.*/) {
261 $eudoras{"$nick"}{"fax"} = $1;
262 print $1;
263 }
264
265 if ($info =~ m/>([^<>]*)$/) {
266 $eudoras{"$nick"}{"note"} = $1;
267 }
268 }
269 }
270
271 my $key;
272 foreach $key (keys %eudoras) {
273 print WRITE "BEGIN:VCARD\n";
274 print WRITE "NICKNAME:$key\n";
275
276 my $fn = $eudoras{"$key"}{"name"};
277 if ($fn) {
278 my @name = split / /, $fn, 2;
279 my $x;
280
281 print WRITE "FN:$fn\n";
282 print WRITE "N:";
283 foreach $x (reverse @name) {
284 print WRITE "$x;";
285 }
286 print WRITE "\n";
287 }
288
289 my $email = $eudoras{"$key"}{"email"};
290
291 writeEmail($email);
292
293 my $phone = $eudoras{"$key"}{"phone"};
294 if ($phone) {
295 print WRITE "TEL;HOME:$phone\n";
296 }
297
298 my $fax = $eudoras{"$key"}{"fax"};
299 if ($fax) {
300 print WRITE "TEL;FAX:$fax\n";
301 }
302
303 my $addr = $eudoras{"$key"}{"address"};
304 if ($addr) {
305 print WRITE "ADR;HOME:Default;;$addr\n";
306 }
307
308 my $note = $eudoras{"$key"}{"note"};
309 if ($note) {
310 $note =~ s//;/g;
311 print WRITE "NOTE:$note\n";
312 }
313
314 endVcard();
315 }
316 }
317
318 ###
319 # parses a mutt aliases file
320 ###
321 sub parseMutt
322 {
323 while (<READ>) {
324 # alias nick email@address (full name)
325 if (m/^alias\s+(\S+)\s+(.*)\s+(<.*>)/) {
326 my @name = split / /, $3, 2;
327 my $x;
328
329 beginVcard($3);
330
331 foreach $x (reverse @name) {
332 print WRITE "$x;";
333 }
334 print WRITE "\n";
335
336 $x = $2;
337 if ($x =~ m/(.*,.*)/) {
338 my @emails = split /,/, $1;
339 writeEmail(@emails);
340 } else {
341 writeEmail($x);
342 }
343
344 endVcard();
345
346 } elsif (m/^alias ([^\s]*) (.*) (<.*>)/) {
347 # alias nick full name <email@address>
348 my @name = split / /, $2, 2;
349 my $x;
350
351 beginVcard($2);
352
353 foreach $x (reverse @name) {
354 print WRITE "$x;";
355 }
356 print WRITE "\n";
357
358 $x = $3;
359 if ($x =~ m/(.*,.*)/) {
360 my @emails = split /,/, $1;
361 foreach $x (@emails) {
362 $x =~ s/[<>]//g;
363 writeEmail($x);
364 }
365 } else {
366 $x =~ s/[<>]//g;
367 writeEmail($x);
368 }
369
370 endVcard();
371
372 } else {
373 warnFormat($_);
374 }
375 }
376 }
377
378 ###
379 # parses a mh alias file
380 ###
381 sub parseMH
382 {
383 while (<READ>) {
384 # alias: email@address, email@address
385 if (m/^(.*): (.*)/) {
386 my $x;
387
388 beginVcard($1, $1);
389
390 $x = $2;
391 if ($x =~ m/(.*,.*)/) {
392 my @emails = split /,/, $1;
393 foreach $x (@emails) {
394 $x =~ s/\s//g;
395 writeEmail($x);
396 }
397 } else {
398 writeEmail($x);
399 }
400
401 endVcard();
402
403 } else {
404 warnFormat($_);
405 }
406 }
407 }
408
409 ###
410 # parses a pine addressbook file
411 ###
412 sub parsePine
413 {
414 while (<READ>) {
415 # nick\tFull Name\temail@address
416 # nick\tFull Name\tName <email@address>
417 # nick\tFull Name\t(email@address,email@address)
418 # nick\tFull Name\t(Name <email@address>,Name <email@address>)
419 # in all of the above, 'Full Name' could also be 'Last, First'
420 # TODO - Continuations: m/[\t,]$/
421 if (m/^(.*)\t(.*)\t(.*\@.*)$/) {
422 my $ns = $2;
423 my $x = $3;
424 my @name = split / /, $ns, 2;
425
426 if ($2 =~ m/(.*), (.*)/) {
427 my $ln = $1;
428 my $fn = $2;
429 beginVcard("$fn $ln");
430 print WRITE "$1;$2;";
431 } else {
432 beginVcard($ns);
433 foreach $x (reverse @name) {
434 print WRITE "$x;";
435 }
436 }
437 print WRITE "\n";
438
439 if ($x =~ m/\((.*)\)/) {
440 my @emails = split /,/, $1;
441 writeEmail(@emails);
442 } else {
443 writeEmail($x);
444 }
445
446 endVcard();
447
448 } else {
449 warnFormat($_);
450 }
451 }
452 }
453
454 ###
455 # parses a abook addressbook file
456 ###
457 sub parseAbook
458 {
459 my $count = 0;
460 my %info;
461 LOOP:
462 while (<READ>) {
463 # [num]
464 # name=Full Name
465 # [email protected],[email protected]
466 # address=Street
467 # city=City
468 # state=State
469 # zip=123123
470 # country=Country
471 # phone=1234/1234525
472 # workphone=1234/123455
473 # fax=fax
474 # mobile=mobile
475 # nick=Nick
476 # url=http://bl;ahlbahl.com
477 # notes=CellPhone#: 12345678901
478
479 next LOOP if (m/^#/);
480
481 if (m/^\[\d+\]$/) {
482 $count++;
483 next LOOP;
484 }
485
486 if (m/^$/) {
487 my $x;
488 # no entries without a note
489 if ($info{'name'}) {
490 my @name = split / /, $info{'name'}, 2;
491
492 beginVcard($info{'name'});
493
494 foreach $x (reverse @name) {
495 print WRITE "$x;";
496 }
497 print WRITE "\n";
498
499 my @emails = split /,/, $info{'email'};
500 writeEmail(@emails);
501
502 if ($info{'address'}) {
503 print WRITE "ADR;HOME:Default;;$info{'address'};";
504 foreach $x ("city", "address", "zip", "country") {
505 print WRITE "$info{$x};";
506 }
507 print WRITE "\n";
508 }
509
510 if ($info{'phone'}) {
511 print WRITE "TEL;HOME:$info{'phone'}\n";
512 }
513 if ($info{'workphone'}) {
514 print write "TEL;WORK:$info{'workphone'}\n";
515 }
516 if ($info{'fax'}) {
517 print write "TEL;FAX:$info{'fax'}\n";
518 }
519 if ($info{'mobile'}) {
520 print write "TEL;CELL:$info{'mobile'}\n";
521 }
522 if ($info{'url'}) {
523 print WRITE "URL:$info{'url'}\n";
524 }
525 if ($info{'notes'}) {
526 print WRITE "NOTE:$info{'notes'}\n";
527 }
528
529 endVcard();
530
531 }
532
533 foreach $x (keys %info) {
534 $info{$x} = "";
535 }
536 next LOOP;
537 }
538
539 if ($count) {
540 my ($key, $val) = split /=/;
541 if ($val) {
542 chomp($val);
543 $info{$key} = $val;
544 }
545 next LOOP;
546 }
547 }
548 }
549
550 ###
551 # parses a Juno address book export file
552 ###
553 sub parseJuno
554 {
555 my $count = 0;
556 my %info;
557 LOOP:
558 while (<READ>) {
559 # Type:Entry
560 # Name:Burdell, George P.
561 # Email:[email protected],[email protected]
562 # Alias:YellowJacket
563
564 next LOOP if (m/^#/);
565
566 if (m/^Type:Entry$/) {
567 $count++;
568 next LOOP;
569 }
570
571 if (m/^$/) {
572 my $x;
573 # no entries without a note
574 if ($info{'Name'}) {
575 my @name = split /,/, $info{'Name'}, 2;
576
577 print WRITE "BEGIN:VCARD\n";
578 print WRITE "FN:";
579 foreach $x (reverse @name) {
580 # Trim leading/trailing whitespace
581 $x =~ s/^\s+//;
582 $x =~ s/\s+$//;
583 print WRITE "$x ";
584 }
585 print WRITE "\n";
586 print WRITE "N:";
587 foreach $x (@name) {
588 # Trim leading/trailing whitespace
589 $x =~ s/^\s+//;
590 $x =~ s/\s+$//;
591 print WRITE "$x;";
592 }
593 print WRITE "\n";
594
595 my @emails = split /,/, $info{'Email'};
596 writeEmail(@emails);
597
598 if ($info{'Alias'}) {
599 print WRITE "NOTE:$info{'Alias'}\n";
600 }
601
602 endVcard();
603
604 }
605
606 foreach $x (keys %info) {
607 $info{$x} = "";
608 }
609 next LOOP;
610 }
611
612 if ($count) {
613 my ($key, $val) = split /:/;
614 if ($val) {
615 chomp($val);
616 $info{$key} = $val;
617 }
618 next LOOP;
619 }
620 }
621 }
622
623 ###
624 # parses a ldif addressbook file
625 ###
626 sub parseLdif
627 {
628 my $count = 1;
629 my $key;
630 my $val;
631 my %info;
632 my %qp;
633 my %v;
634 LOOP:
635 while (<READ>) {
636 # dn: cn=FIRST LNAME,mail=EM
637 # modifytimestamp: 20030227150502Z
638 # cn: FIRST LNAME
639 # mail: EM
640 # xmozillausehtmlmail: TRUE
641 # o: ORG
642 # locality: CITY
643 # givenname: FIRST
644 # sn: LNAME
645 # title: TITLE
646 # streetaddress:: QUREMQ0KQU
647 # QUREMQ0KQUREMg==
648 # postalcode: ZIP
649 # countryname: COUN
650 # telephonenumber: WORKph
651 # facsimiletelephonenumber: FAX
652 # xmozillaanyphone: WORKph
653 # homephone: HOMEPH
654 # cellphone: CELL
655 # ou: DPT
656 # homeurl: URL
657 # st: STAT
658 # xmozillanickname: NICK
659 # description:: Tk9UMQ0KTk9UMg==
660 # pagerphone: PAGER
661 # objectclass: top
662 # objectclass: person
663
664 next LOOP if (m/^#/);
665
666 # if newline is followed by space, remove both
667 if (s/^ //ms) {
668 chomp();
669 s/\r?\n$//;
670 $info{$key} .= $_;
671 next LOOP;
672 }
673
674 if ($key && $qp{$key}) {
675 $val = decode_base64($info{$key});
676 $val = encode_qp($val);
677 $val =~ s/\n/=0A/g;
678 $info{$key} = $val;
679 }
680
681 if (m/^\r?$/) {
682 my $x;
683 # no entries without a note
684 if ($info{'dn'}) {
685 my @name = split /[=,]/, $info{'dn'}, 2;
686
687 beginVcard($info{'cn'});
688
689 if ($info{'sn'}) {print WRITE "$info{'sn'};";}
690 if ($info{'givenname'}) {print WRITE "$info{'givenname'}";}
691 print WRITE "\n";
692
693 my @emails = split /,/, $info{'mail'};
694 writeEmail(@emails);
695
696 if ($info{'streetaddress'}) {
697 print WRITE "ADR";
698 if ( $qp{"streetaddress"} || $qp{"locality"} ||
699 $qp{"address"} || $qp{"st"} ||
700 $qp{"postalcode"} || $qp{"countryname"}) {
701 print WRITE ";quoted-printable";
702 }
703 print WRITE ":;;$info{'streetaddress'};";
704 foreach $x ("locality", "address", "st", "postalcode", "countryname") {
705 print WRITE "$info{$x};";
706 }
707 print WRITE "\n";
708 }
709 Oneline($info{'homephone'}, 'TEL;HOME', $qp{'homephone'});
710 Oneline($info{'telephonenumber'}, 'TEL;WORK', $qp{'telephonenumber'});
711 Oneline($info{'facsimiletelephonenumber'}, 'TEL;FAX', $qp{'facsimiletelephonenumber'});
712 Oneline($info{'cellphone'}, 'TEL;CELL', $qp{'cellphone'});
713 Oneline($info{'pagerphone'}, 'tel;pager', $qp{'pagerphone'});
714 Oneline($info{'xmozillausehtmlmail'}, 'x-mozilla-html', $qp{'xmozillausehtmlmail'});
715 Oneline($info{'title'}, 'title', $qp{'title'});
716 Oneline($info{'homeurl'}, 'URL', $qp{'homeurl'});
717 Oneline($info{'description'}, 'NOTE', $qp{'description'});
718 if ($info{'o'}) {
719 print WRITE "org:$info{'o'};";
720 if ($info{'ou'}) {print WRITE "$info{'ou'}";}
721 print WRITE "\n";
722 }
723
724 endVcard();
725 }
726
727 foreach $x (keys %info) {
728 $info{$x} = "";
729 $qp{$x} = "";
730 }
731 $key = "";
732 next LOOP;
733 }
734
735 ($key, $val) = split /: /;
736 if ($val) {
737 chomp($val);
738 $val =~ s/\r$//;
739 if ($key =~ s/:$//) {
740 $qp{$key} = 1;
741 }
742 $info{$key} = $val;
743 }
744 next LOOP;
745 }
746 }
747
748 ###
749 # prints a list of supported formats
750 ###
751 sub formats
752 {
753 my $key;
754
755 print "$NAME $VERSION can convert the addressbooks of the following:\n";
756 foreach $key (sort(keys %SUPPORTED)) {
757 print "\t$key\n";
758 }
759 }
760
761 ###
762 # prints out helpful information
763 ###
764 sub usage
765 {
766 print "$NAME: convert an addressbook to vcard format\n";
767 print "Usage: $NAME [OPTION...]\n";
768 print "Options:\n";
769 print " -F\t\tshow supported formats\n";
770 print " -f FORMAT\tconvert from FROMAT [ default: mutt ]\n";
771 print " -h\t\tprint this message and exit\n";
772 print " -i FILE\tread input from FILE\n";
773 print " -o FILE\twrite output to FILE\n";
774 print " -v\t\tprint version number and exit\n";
775 }