]> git.plutz.net Git - confetti/blob - pdiread.sh
styling for iban assignment
[confetti] / pdiread.sh
1 #!/bin/zsh
2
3 # Copyright 2014 - 2018, 2023 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 unescape(){
29   local in out=''
30   [ $# -gt 0 ] && in="$*" || in="$(cat)"
31   while [ "$in" ]; do case $in in
32     \\\\*) out="${out}\\"; in="${in#\\\\}" ;;
33     \\n*) out="${out}${BR}"; in="${in#\\n}" ;;
34     \\*) in="${in#\\}" ;;
35     *) out="${out}${in%%[\\]*}"; in="${in#"${in%%[\\]*}"}" ;;
36   esac; done
37   printf '%s\n' "$out"
38  }
39
40 pdi_load() {
41   # normalise PDI file for processing with pdi_* functions
42   # functions in this library can only be applied to normalised data
43   # Usage example:
44   # data="$(pdi_load file.vcf)"
45
46   sed -srn '
47     # === Read entire file into buffer ===
48     :X $bY; N; bX; :Y s;^.*$;\n&\n;;
49
50     # === Join continuing lines, strip trailing CRs ===
51     s;\r*\n[ \t];;g;
52     s;\r*\n;\n;g;
53
54     # === turn property names to upper case, strip group names ===
55     s;\n([^;:\.\n]+\.)([^;:\n]+);\n\2;g;
56     :upcase
57     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;
58     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;
59     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;
60     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;
61     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;
62     s;(\n[^;:]*)z;\1Z;g;
63     t upcase;
64
65     # === Insert empty attribute fields where no attributes are present ===
66     s;\n([^;:]+):;\n\1\;:;g;
67
68     # === Unscramble aggregated fields ===
69     :disag
70     s;\n([^:\n]+:)(([^\n]*[^\])?(\\\\)*),;\n\1\2\n\1;;
71     t disag;
72
73     # === Insert FN when only N is present ===
74     /\nFN[;:]/!{
75       s,\nN(;[^:]*)?:([^;\n]*);([^;\n]*);([^;\n]*);([^;\n]*);([^;\n]*);?\n,&FN;:\5 \3 \4 \2 \6\n,;
76       :despace
77       s,(\nFN;:[^\n]*)  ([^\n]*\n),\1 \2,;
78       s,(\nFN;:) ([^\n]*\n),\1\2,;
79       s,(\nFN;:[^\n]*) (\n),\1\2,;
80       t despace;
81     }
82     /\nFN[;:]/!{ s,\n(N[;:][^\n]*)\n,&F\1\n,; }  # Fallback
83
84     # === Normalise various known vendor properties ===
85                 s;\nX-MS-CARDPICTURE(\;|:);\nPHOTO\1;g;
86                         s;\nX-GENDER(\;|:);\nGENDER\1;g;
87                    s;\nX-ANNIVERSARY(\;|:);\nANNIVERSARY\1;g;
88          s;\nX-EVOLUTION-ANNIVERSARY(\;|:);\nANNIVERSARY\1;g;
89     s;\nX-KADDRESSBOOK-X-ANNIVERSARY(\;|:);\nANNIVERSARY\1;g;
90             s;\nX-EVOLUTION-BLOG-URL(\;|:);\nURL\1;g;
91                            s;\nAGENT(\;|:);\nRELATED\;VALUE=text\;TYPE=agent\1;g;
92                      s;\nX-ASSISTANT(\;|:);\nRELATED\;VALUE=text\;TYPE=assistant\1;g;
93            s;\nX-EVOLUTION-ASSISTANT(\;|:);\nRELATED\;VALUE=text\;TYPE=assistant\1;g;
94  s;\nX-KADDRESSBOOK-X-ASSISTANTSNAME(\;|:);\nRELATED\;VALUE=text\;TYPE=assistant\1;g;
95                        s;\nX-MANAGER(\;|:);\nRELATED\;VALUE=text\;TYPE=manager\1;g;
96              s;\nX-EVOLUTION-MANAGER(\;|:);\nRELATED\;VALUE=text\;TYPE=manager\1;g;
97    s;\nX-KADDRESSBOOK-X-MANAGERSNAME(\;|:);\nRELATED\;VALUE=text\;TYPE=manager\1;g;
98                         s;\nX-SPOUSE(\;|:);\nRELATED\;VALUE=text\;TYPE=spouse\1;g;
99               s;\nX-EVOLUTION-SPOUSE(\;|:);\nRELATED\;VALUE=text\;TYPE=spouse\1;g;
100      s;\nX-KADDRESSBOOK-X-SPOUSENAME(\;|:);\nRELATED\;VALUE=text\;TYPE=spouse\1;g;
101
102     # === Normalise obsolete vendor IM properties ===
103             s;\nX-AIM((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:aim:;g;
104             s;\nX-ICQ((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:aim:;g;
105     s;\nX-GOOGLE-TALK((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:xmpp:;g;
106          s;\nX-JABBER((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:xmpp:;g;
107             s;\nX-MSN((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:msn:;g;
108           s;\nX-YAHOO((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;\nIMPP\1:ymsgr:;g;
109             s;\nX-SIP((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(sip:)?;\nIMPP\1:sip:;g;
110
111     # === Update obsolete LABEL property ===
112     s;\nLABEL((\;[A-Za-z0-9-]+|\;[A-Za-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)\n;\nADR\1\;LABEL="\5":\n;g;
113
114     p;' "$@"
115 }
116
117 pdi_count(){
118   local card="$1" name="$2" rc='' cnt=0
119   while rc="${card#*${BR}${name};}"; do
120     [ "${rc}" != "${card}" ] || break
121     card="$rc"
122     cnt=$(($cnt + 1))
123   done
124   printf %i\\n $cnt
125 }
126
127 pdi_attrib(){
128   local card=":$1" name="$2" cnt="${3:-1}" attr="$4"
129   while [ $cnt -gt 0 ]; do
130     [ "${card#*${BR}${name};}" = "$card" ] && return 1
131     card="${card#*${BR}${name};}"
132     cnt=$((cnt - 1))
133   done
134   card="${card%%:*}"
135   if [ "$attr" ]; then
136     case $card in
137       *\;"$attr"=*) card="${card#*;${attr}=}";;
138       "$attr"=*) card="${card#${attr}=}";;
139       "$attr"|*\;"$attr"|"$attr"\;*|*\;"$attr"\;*) return 0;;
140       *) return 1;;
141     esac
142     case $card in
143       \"*\"\;*|\'*\'\;*)
144         card="${card#[\"\']}"; card="${card%%[\"\'];*}"
145         ;;
146       \"*\"|\'*\')
147         card="${card#[\"\']}"; card="${card%%[\"\']}"
148         ;;
149       *\;*) card="${card%%;*}";;
150     esac
151   fi
152   printf %s\\n "${card}"
153 }
154
155 pdi_value(){
156   local card="${BR}$1" name="$2" cnt="${3:-1}"
157   while [ "$cnt" -gt 0 ]; do
158     [ "${card#*${BR}${name};*:}" = "$card" ] && return 1
159     card="${card#*${BR}${name};*:}"
160     cnt=$((cnt - 1))
161   done
162   printf %s\\n "${card%%${BR}*}"
163 }
164
165 pdi_update_value(){
166   local card="${BR}$1" name="$2" cnt="$3" val="$4"
167   while [ "$cnt" -gt 0 ]; do
168     if [ "${card#*${BR}${name};*:}" = "${card}" ]; then
169        printf '%s\n%s;:' "${card%${BR}END;:VCARD*}" "${name}"
170        card="${BR}END;:VCARD"
171        break;
172     else
173        printf '%s\n%s;' "${card%%${BR}${name};*}" "${name}"
174        card="${card#*${BR}${name};}"
175        printf '%s:' "${card%%:*}"
176        card="${card#*:}"
177     fi
178     cnt=$((cnt - 1))
179   done
180   printf '%s\n%s\n' "$val" "${card#*${BR}}"
181 }
182
183 pdi_update_attrib(){
184   local card="${BR}$1" name="$2" cnt="$3" val="$4"
185   while [ "$cnt" -gt 0 ]; do
186     if [ "${card#*${BR}${name};*:}" = "${card}" ]; then
187        printf '%s\n%s;' "${card%${BR}END;:VCARD*}" "${name}"
188        card=":${BR}END;:VCARD"
189        break;
190     else
191        printf '%s\n%s;' "${card%%${BR}${name};*}" "${name}"
192        card="${card#*${BR}${name};}"
193     fi
194     cnt=$((cnt - 1))
195   done
196   printf '%s:%s\n' "$val" "${card#*:}"
197 }