blob: 599e6ac4f089bcb81f5745df7f31bd4fb01e751f (
plain) (
blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#!/bin/sh
usage()
{
cat <<- EOF
Usage: ${0##*/} [options] [ contacts.vcf ]
convert vcf file to csv file
Options:
-h show this help cruft
EOF
}
die()
{
: "${1:?}"
printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2
exit "${2:-1}"
}
warn()
{
: "${1:?}"
printf "\033[33;1mwarn: %b\033[0m\n" "$*" 1>&2
}
########
# MAIN #
# ######
name=
no=
vcf=
nos=
if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit
elif [ "$(file --brief --dereference --mime-type "$1")" != "text/vcard" ]; then
die "input is not a vcf file"
else
vcf="$1"
shift
fi
strings "$vcf" | while read -r line; do
case "$line" in
'FN:'*)
name="${line##*:}"
case "$name" in
*,*)
name="$(printf "%s" "$name" | tr -d ',')"
;;
esac
;;
'TEL;TYPE='*':'*)
# avoid repetitions in same vacrd entry
no="${line##*:}"
case "$nos" in
*"$no"*)
continue
;;
esac
nos="${nos}:${no}"
# make sure $no is E.164-formatted
case "$no" in
*-*)
no="$(printf "%s" "$no" | tr -d '-')"
;;
esac
case "$no" in
+[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
# gotta go fast \+[0-9]{12}
;;
+974[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) # qatar
# gotta go fast \+974{8}
;;
*)
warn "illegal format: ${name}, ${no}"
continue;
;;
esac
[ "$1" = "-w" ] &&
continue
[ -n "$no" ] &&
printf "%s, %s\n" "$no" "$name"
;;
'END:VCARD')
nos=
;;
esac
done
|