prepare for new laptop
[dotfiles.git] / email / .local / bin / mutt.vcard.filter
1 #!/usr/bin/perl -Tw
2
3 # mutt.vcard.filter - vcard filter for use with the mutt autoview facility
4 # Copyright (C) 1997,1998,1999 David A Pearson
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 # This little perl script is a simple filter for text/x-vcard
21 # attachments. I'm pretty sure I've *not* included everything
22 # possible in here, but it "works for me". Feel free to improve
23 # in any way you see fit.
24 #
25 # Here is how I use it. In my ~/.mutt_mailcap (use your filename of
26 # choice) I have the following entry:
27 #
28 # text/x-vcard; mutt.vcard.filter; copiousoutput
29 #
30 # All you then need to do is add a line like:
31 #
32 # auto_view text/x-vcard
33 #
34 # to your ~/.muttrc (use your filename of choice).
35 #
36 # All comments/flames/feedback can be directed to:
37 #
38 # davep@hagbard.demon.co.uk
39 #
40
41 use strict;
42
43 my $in_card = 0;
44 my @address = ();
45 my @contacts = ();
46 my @additional = ();
47 my @notes = ();
48 my $name = "";
49 my $title = "";
50 my $org = "";
51 my $found_note = 0;
52 my $len;
53 my $i;
54 my $addr_line;
55 my $contact_line;
56
57 while ( <> )
58 {
59 if ( $in_card )
60 {
61 if ( /^fn:\s*(.*)$/i )
62 {
63 $name = $1;
64 }
65 elsif ( /^n:\s*(.*);\s*(.*)$/i )
66 {
67 @additional = ( "", "Additional information:", "" ) if $#additional == -1;
68
69 @additional = ( @additional, "Last Name:\t$1", "First Name:\t$2" );
70 }
71 elsif ( /^title:\s*(.*)$/i )
72 {
73 $title = $1;
74 }
75 elsif ( /^org:\s*(.*)$/i )
76 {
77 $org = $1;
78 }
79 elsif ( /^adr:\s*(.*)$/i )
80 {
81 my $addr = $1;
82
83 $addr =~ s/;+/;/g;
84
85 @address = split( /;/, $addr );
86 }
87 elsif ( /^email;\s*(.*?):\s*(.*)$/i || /^tel;\s*(.*?):\s*(.*)$/i )
88 {
89 my $type = $1;
90 my $value = $2;
91
92 @contacts = ( @contacts, uc( substr( $type, 0, 1 ) ) .
93 substr( $type, 1 ) . ": $value" );
94 }
95 elsif ( /^note:\s*(.*)$/i )
96 {
97 @notes = ( "" ) if $#notes == -1;
98 @notes = ( @notes, $1 );
99
100 $found_note = 1;
101 }
102 elsif ( /^=.{2}=$/ && $found_note )
103 {
104 my $line = <>;
105
106 chomp( $line );
107
108 @notes = ( "" ) if $#notes == -1;
109 @notes = ( @notes, $line );
110 }
111 elsif ( /^end:\s*vcard$/i )
112 {
113 $in_card = 0;
114 }
115 }
116 else
117 {
118 $in_card = /^begin:\s*vcard\s*$/i;
119 }
120 }
121
122 @address = ( $org, @address ) if $org;
123 @address = ( $title, @address ) if $title;
124 @address = ( $name, @address ) if $name;
125
126 format STDOUT =
127 @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
128 $addr_line, $contact_line
129 .
130
131 $len = $#address > $#contacts ? $#address : $#contacts;
132
133 print "" . ( "=" x 76 ) . "\n";
134
135 for ( $i = 0; $i <= $len; $i++ )
136 {
137 $addr_line = $i <= $#address ? $address[ $i ] : "";
138 $contact_line = $i <= $#contacts ? $contacts[ $i ] : "";
139 write;
140 }
141
142 for ( $i = 0; $i <= $#notes; $i++ )
143 {
144 print "$notes[ $i ]\n";
145 }
146
147 for ( $i = 0; $i <= $#additional; $i++ )
148 {
149 print "$additional[ $i ]\n";
150 }
151
152 print "" . ( "=" x 76 ) . "\n";