]> git.plutz.net Git - confetti/blob - pdiread.sh
fix in displaying error message
[confetti] / pdiread.sh
1 #!/bin/zsh
2
3 # Copyright 2014 - 2018 Paul Hänsch
4 #
5 # This file is part of Confetti.
6
7 # Confetti is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # Confetti is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with Confetti.  If not, see <http://www.gnu.org/licenses/>. 
19
20 # This is a parsing library for the Personal Data Interchange format (PDI)
21 # PDI is the format for encoding VCard (.vcf) and iCalendar (.ics) files
22
23 [ -n "$include_pdi" ] && return 0
24 include_pdi="$0"
25
26 BR='
27 '
28
29 pdi_load() {
30   # normalise PDI file for processing with pdi_* functions
31   # functions in this library can only be applied to normalised data
32   # Usage example:
33   # data="$(pdi_load file.vcf)"
34
35   sed -srn '
36     # === Read entire file into buffer ===
37     :X $bY; N; bX; :Y s;^.*$;\n&\n;;
38
39     # === Join continuing lines, strip trailing CRs ===
40     s;\r*\n[ \t];;g;
41     s;\r*\n;\n;g;
42
43     # === turn property names to upper case, strip group names ===
44     s;\n([^;:\.\n]+\.)([^;:\n]+);\n\2;g;
45     :upcase
46     s;(\n[^;:]*)a;\1A;g; s;(\n[^;:]*)b;\1B;g; s;(\n[^;:]*)c;\1C;g; s;(\n[^;:]*)d;\1D;g; s;(\n[^;:]*)e;\1E;g;
47     s;(\n[^;:]*)f;\1F;g; s;(\n[^;:]*)g;\1G;g; s;(\n[^;:]*)h;\1H;g; s;(\n[^;:]*)i;\1I;g; s;(\n[^;:]*)j;\1J;g;
48     s;(\n[^;:]*)k;\1K;g; s;(\n[^;:]*)l;\1L;g; s;(\n[^;:]*)m;\1M;g; s;(\n[^;:]*)n;\1N;g; s;(\n[^;:]*)o;\1O;g;
49     s;(\n[^;:]*)p;\1P;g; s;(\n[^;:]*)q;\1Q;g; s;(\n[^;:]*)r;\1R;g; s;(\n[^;:]*)s;\1S;g; s;(\n[^;:]*)t;\1T;g;
50     s;(\n[^;:]*)u;\1U;g; s;(\n[^;:]*)v;\1V;g; s;(\n[^;:]*)w;\1W;g; s;(\n[^;:]*)x;\1X;g; s;(\n[^;:]*)y;\1Y;g;
51     s;(\n[^;:]*)z;\1Z;g;
52     t upcase;
53
54     # === Insert empty attribute fields where no attributes are present ===
55     s;\n([^;:]+):;\n\1\;:;g;
56
57     # === Unscramble aggregated fields ===
58     :disag
59     s;\n([^:\n]+:)(([^\n]*[^\])?(\\\\)*),;\n\1\2\n\1;;
60     t disag;
61
62     # === Insert FN when only N is present ===
63     /\nFN[;:]/!{
64       s,\nN(;[^:]*)?:([^;\n]*);([^;\n]*);([^;\n]*);([^;\n]*);([^;\n]*);?\n,&FN;:\5 \3 \4 \2 \6\n,;
65       :despace
66       s,(\nFN;:[^\n]*)  ([^\n]*\n),\1 \2,;
67       s,(\nFN;:) ([^\n]*\n),\1\2,;
68       s,(\nFN;:[^\n]*) (\n),\1\2,;
69       t despace;
70     }
71     /\nFN[;:]/!{ s,\n(N[;:][^\n]*)\n,&F\1\n,; }  # Fallback
72
73     # === Normalise various known vendor properties ===
74                 s;\nX-MS-CARDPICTURE(\;|:);\nPHOTO\1;g;
75                         s;\nX-GENDER(\;|:);\nGENDER\1;g;
76                    s;\nX-ANNIVERSARY(\;|:);\nANNIVERSARY\1;g;
77          s;\nX-EVOLUTION-ANNIVERSARY(\;|:);\nANNIVERSARY\1;g;
78     s;\nX-KADDRESSBOOK-X-ANNIVERSARY(\;|:);\nANNIVERSARY\1;g;
79             s;\nX-EVOLUTION-BLOG-URL(\;|:);\nURL\1;g;
80                            s;\nAGENT(\;|:);\nRELATED\;VALUE=text\;TYPE=agent\1;g;
81                      s;\nX-ASSISTANT(\;|:);\nRELATED\;VALUE=text\;TYPE=assistant\1;g;
82            s;\nX-EVOLUTION-ASSISTANT(\;|:);\nRELATED\;VALUE=text\;TYPE=assistant\1;g;
83  s;\nX-KADDRESSBOOK-X-ASSISTANTSNAME(\;|:);\nRELATED\;VALUE=text\;TYPE=assistant\1;g;
84                        s;\nX-MANAGER(\;|:);\nRELATED\;VALUE=text\;TYPE=manager\1;g;
85              s;\nX-EVOLUTION-MANAGER(\;|:);\nRELATED\;VALUE=text\;TYPE=manager\1;g;
86    s;\nX-KADDRESSBOOK-X-MANAGERSNAME(\;|:);\nRELATED\;VALUE=text\;TYPE=manager\1;g;
87                         s;\nX-SPOUSE(\;|:);\nRELATED\;VALUE=text\;TYPE=spouse\1;g;
88               s;\nX-EVOLUTION-SPOUSE(\;|:);\nRELATED\;VALUE=text\;TYPE=spouse\1;g;
89      s;\nX-KADDRESSBOOK-X-SPOUSENAME(\;|:);\nRELATED\;VALUE=text\;TYPE=spouse\1;g;
90
91     # === Normalise obsolete vendor IM properties ===
92             s;\nX-AIM((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:aim:;g;
93             s;\nX-ICQ((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:aim:;g;
94     s;\nX-GOOGLE-TALK((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:xmpp:;g;
95          s;\nX-JABBER((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:xmpp:;g;
96             s;\nX-MSN((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:msn:;g;
97           s;\nX-YAHOO((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:ymsgr:;g;
98             s;\nX-SIP((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(sip:)?;\nIMPP\1:sip:;g;
99
100     # === Update obsolete LABEL property ===
101     s;\nLABEL((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)\n;\nADR\1\;LABEL="\5":\n;g;
102
103     p;' "$@"
104 }
105
106 pdi_count(){
107   local card="$1" name="$2" rc='' cnt=0
108   while rc="${card#*${BR}${name};}"; do
109     [ "${rc}" != "${card}" ] || break
110     card="$rc"
111     cnt=$(($cnt + 1))
112   done
113   printf %i\\n $cnt
114 }
115
116 pdi_attrib(){
117   local card=":$1" name="$2" cnt="${3:-1}" attr="$4"
118   while [ $cnt -gt 0 ]; do
119     [ "${card#*${BR}${name};}" = "$card" ] && return 1
120     card="${card#*${BR}${name};}"
121     cnt=$((cnt - 1))
122   done
123   card="${card%%:*}"
124   if [ "$attr" ]; then
125     case $card in
126       *\;"$attr"=*) card="${card#*;${attr}=}";;
127       "$attr"=*) card="${card#${attr}=}";;
128       "$attr"|*\;"$attr"|"$attr"\;*|*\;"$attr"\;*) return 0;;
129       *) return 1;;
130     esac
131     case $card in
132       \"*\"\;*|\'*\'\;*)
133         card="${card#[\"\']}"; card="${card%%[\"\'];*}"
134         ;;
135       \"*\"|\'*\')
136         card="${card#[\"\']}"; card="${card%%[\"\']}"
137         ;;
138       *\;*) card="${card%%;*}";;
139     esac
140   fi
141   printf %s\\n "${card}"
142 }
143
144 pdi_value(){
145   local card="${BR}$1" name="$2" cnt="${3:-1}"
146   while [ "$cnt" -gt 0 ]; do
147     [ "${card#*${BR}${name};*:}" = "$card" ] && return 1
148     card="${card#*${BR}${name};*:}"
149     cnt=$((cnt - 1))
150   done
151   printf %s\\n "${card%%${BR}*}"
152 }
153
154 pdi_update_value(){
155   local card="${BR}$1" name="$2" cnt="$3" val="$4"
156   while [ "$cnt" -gt 0 ]; do
157     if [ "${card#*${BR}${name};*:}" = "${card}" ]; then
158        printf '%s\n%s;:' "${card%${BR}END;:VCARD*}" "${name}"
159        card="${BR}END;:VCARD"
160        break;
161     else
162        printf '%s\n%s;' "${card%%${BR}${name};*}" "${name}"
163        card="${card#*${BR}${name};}"
164        printf '%s:' "${card%%:*}"
165        card="${card#*:}"
166     fi
167     cnt=$((cnt - 1))
168   done
169   printf '%s\n%s\n' "$val" "${card#*${BR}}"
170 }
171
172 pdi_update_attrib(){
173   local card="${BR}$1" name="$2" cnt="$3" val="$4"
174   while [ "$cnt" -gt 0 ]; do
175     if [ "${card#*${BR}${name};*:}" = "${card}" ]; then
176        printf '%s\n%s;' "${card%${BR}END;:VCARD*}" "${name}"
177        card=":${BR}END;:VCARD"
178        break;
179     else
180        printf '%s\n%s;' "${card%%${BR}${name};*}" "${name}"
181        card="${card#*${BR}${name};}"
182     fi
183     cnt=$((cnt - 1))
184   done
185   printf '%s:%s\n' "$val" "${card#*:}"
186 }