#!/usr/local/bin/perl -w # mutt.vcard.filter - vcard filter for use with the mutt autoview facility # Copyright (C) 1997 David A Pearson # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the license, or # (at your option) any later version. # # 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., 675 Mass Ave, Cambridge, MA 02139, USA. # # # This little perl script is a simple filter for text/x-vcard # attachments. I'm pretty sure I've *not* included everything # possible in here, but it "works for me". Feel free to improve # in any way you see fit. # # Here is how I use it. In my ~/.mutt_mailcap (use your filename of # choice) I have the following entry: # # text/x-vcard; mutt.vcard.filter; copiousoutput # # All you then need to do is add a line like: # # auto_view text/x-vcard # # to your ~/.muttrc (use your filename of choice). # # All comments/flames/feedback can be directed to: # # davep@hagbard.demon.co.uk # [ Retrieved from http://www.hagbard.demon.co.uk/mutt.html ] # Modified 22-Dec-1997 by Rob Funk # - recognize command-line switches # - only show "additional information" with -a switch # - only show country with -c switch # - only show street address with -s switch # - only show zip with -z switch # - only show borders with -b switch # - put city, state, and zip (if shown) on single line use strict; my $in_card = 0; my @address = (); my @contacts = (); my @additional = (); my @notes = (); my $name = ""; my $title = ""; my $org = ""; my $found_note = 0; my $len; my $i; my $addr_line; my $contact_line; my $wrap_line; my $switches; # current switches: # -s Street Address # -z Zip code # -c Country # -a Additional Info # -b border lines $switches = join("",grep(/^-/,@ARGV)); $switches =~ s/-//g; @ARGV = grep(!/^-/,@ARGV); # leave no switches in @ARGV while (<>) { chomp; if ( $in_card ) { if ( /^fn:\s*(.*)$/i ) { $name = $1; } elsif ( /^n:\s*(.*);\s*(.*)$/i ) { @additional = ( "", "Additional information:", "" ) if $#additional == -1; @additional = ( @additional, "Last Name:\t$1", "First Name:\t$2" ); } elsif ( /^title:\s*(.*)$/i ) { $title = $1; } elsif ( /^org:\s*(.*)$/i ) { $org = $1; } elsif ( /^adr:\s*(.*)$/i ) { my $addr = $1; my @tmpaddr = (); @tmpaddr = split( /;/, $addr ); if ($switches=~/s/) { # include street address @address = @tmpaddr[0,1,2]; } if (defined($tmpaddr[3]) && defined($tmpaddr[4])) { @address = (@address, "$tmpaddr[3], $tmpaddr[4]"); } if ($switches=~/z/) { # include zip @address = (@address, $tmpaddr[5]); } if ($switches=~/c/) { # -c to get country @address = (@address, @tmpaddr[6,]); } @address = grep(!/^$/,@address); # filter out empty entries } elsif ( /^email;\s*(.*?):\s*(.*)$/i || /^tel;\s*(.*?):\s*(.*)$/i ) { my $type = $1; my $value = $2; @contacts = ( @contacts, uc( substr( $type, 0, 1 ) ) . substr( $type, 1 ) . ": $value" ); } elsif ( /^note:\s*(.*)$/i ) { @notes = ( "" ) if $#notes == -1; @notes = ( @notes, $1 ); $found_note = 1; } elsif ( /^=.{2}=$/ && $found_note ) { my $line = <>; chomp( $line ); @notes = ( "" ) if $#notes == -1; @notes = ( @notes, $line ); } elsif ( /^end:\s*vcard\s*$/i ) { $in_card = 0; } } else { $in_card = /^begin:\s*vcard\s*$/i; } } @address = ( $org, @address ) if $org; @address = ( $title, @address ) if $title; @address = ( $name, @address ) if $name; format STDOUT = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $addr_line, $contact_line . $len = $#address > $#contacts ? $#address : $#contacts; if ($switches=~/b/) { # -b to get border print "" . ( "=" x 76 ) . "\n"; } for ( $i = 0; $i <= $len; $i++ ) { $addr_line = $i <= $#address ? $address[ $i ] : ""; $contact_line = $i <= $#contacts ? $contacts[ $i ] : ""; write; } format WRAP = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $wrap_line . $~ = "WRAP"; # change format from STDOUT for ( $i = 0; $i <= $#notes; $i++ ) { $wrap_line = "$notes[ $i ]\n"; while ($wrap_line) { write; } } if ( $switches=~/a/ ) { # -a to get "additional info", usually just name for ( $i = 0; $i <= $#additional; $i++ ) { print "$additional[ $i ]\n"; } } if ($switches=~/b/) { # -b to get border print "" . ( "=" x 76 ) . "\n"; }