-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcardsearch.pl
executable file
·81 lines (67 loc) · 2.12 KB
/
vcardsearch.pl
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
#!/opt/local/bin/perl
use Encode qw(decode_utf8);
use utf8;
use Text::vCard::Addressbook;
use Getopt::Std;
#parse input as UTF-8
@ARGV = map { decode_utf8($_, 1) } @ARGV;
#output is also UTF-8
binmode STDOUT, ":encoding(UTF-8)";
my ($matchcount, $searchterm, @addresses, $vcffile, $cjk);
sub usage {
print STDERR << "EOF";
Search a vCard format file and return data formatted for mutt
usage: $0 [-h] [-c] [-f vcf file] [-s search term]
-h : this help message
-c : check for CJK names and return LASTFIRST
-f : the vCard file to parse
-s : the search term (UTF-8 compliant)
example:
$0 -f /home/ca/.mutt/vcards.vcf -s guenther
vcardsearch: 1 match(es)
procmail4life\@openbsd.org Philip Guenther
EOF
exit;
}
sub init {
getopts('hf:s:c');
&usage if $opt_h;
&usage if !$opt_f or !$opt_s;
$searchterm = $opt_s;
if (! -f "$opt_f") {
print "Cannot open $opt_f - exiting\n";
exit;
}
$vcffile = $opt_f;
}
sub checkcjk {
local $_ = shift;
$cjk = 1 if /\p{Hiragana}|\p{Katakana}|\p{Han}/;
}
&init;
my $address_book = Text::vCard::Addressbook->new({
source_file => $vcffile
});
foreach my $vcard ( $address_book->vcards() ) {
if ( $vcard->fullname() =~ /$searchterm/i || $vcard->email() =~ /$searchterm/i ) {
&checkcjk($vcard->fullname()) if $opt_c;
my $emailaddresses = $vcard->get( { 'node_type' => 'email' } );
foreach my $email ( @{$emailaddresses} ) {
$matchcount++;
# this is the format that mutt expects
# reverse name order for CJK and remove spaces
if ( $cjk eq 1 ) {
my ($first, $last) = split(/ /, ($vcard->fullname()));
push(@addresses, $email->value() . "\t" . $last . $first . "\n");
} else {
push(@addresses, $email->value() . "\t" . $vcard->fullname() . "\n");
}
}
}
}
# mutt expects a match count line followed by matches
# this should display nicely with the tab completion To input
print "vcardsearch:" . "\t" . "$matchcount match(es)\n";
foreach my $emailaddress (@addresses) {
print $emailaddress;
}