]> git.plutz.net Git - confetti/blob - pages/cards.sh
parsing and processing of aggregated values, split values, vcard escapes
[confetti] / pages / cards.sh
1 #!/bin/zsh
2
3 # Copyright 2014 - 2016 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 BR='
21 '
22
23 case $PROFILE in
24 medical)
25   view_card="$_EXEC/templates/view_client.sh"
26   edit_card="$_EXEC/templates/edit_client.sh"
27   _GET[order]="${_GET[order]:-lastname}"
28   _GET[filtertype]="${_GET[filtertype]:-name}"
29   profile_medical=x
30 ;;
31 circus)
32   view_card="$_EXEC/templates/view_attendee.sh"
33   edit_card="$_EXEC/templates/edit_attendee.sh"
34   _GET[order]="${_GET[order]:-firstname}"
35   _GET[filtertype]="${_GET[filtertype]:-any}"
36   profile_circus=x
37 ;;
38 esac
39
40 edit="${_GET[edit]}"
41 [ \! -f "vcard/$edit" -a \! -f "temp/$edit" ] && edit=''
42
43 listcourses() {
44   ls -1 ${_DATA}/ical/*ics |while read file; do
45     icstime="$(sed -rn 's:^DTSTART\:(TZID=.*\:)?([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z?\r$:\2-\3-\4 \5\:\6\:\7:p' "$file")"
46     echo "$(date -d "$icstime" "+%u %H%M%S")\t$file"
47   done |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
48 }
49
50 list_hi_companies(){
51   sed -rn 's;^X-HEALTH-INSURANCE:([^\;]+)\;.*$;\1;p' ${_DATA}/vcard/*vcf \
52   | sort -u
53 }
54
55 listcards() {
56   case "${_GET[filtertype]}" in
57     any)
58        grep -il "${_GET[filter]}" ${_DATA}/vcard/*vcf
59       ;;
60     name)
61        egrep -xil "(FN|NICKNAME|N)(;.+)*:.*${_GET[filter]}.*" ${_DATA}/vcard/*vcf
62       ;;
63     street)
64        egrep -xil "(ADR)(;.+)*:([^;]*;){2}${_GET[filter]}.*" ${_DATA}/vcard/*vcf
65       ;;
66     zip)
67        egrep -xil "(ADR)(;.+)*:([^;]*;){5}${_GET[filter]}.*" ${_DATA}/vcard/*vcf
68       ;;
69     telephone)
70        egrep -xil "(TEL)(;.+)*:.*${_GET[filter]}.*" ${_DATA}/vcard/*vcf
71       ;;
72     birth)
73        egrep -xil "(BDAY)(;.+)*:${_GET[filter]}.*" ${_DATA}/vcard/*vcf
74       ;;
75     course)
76       ;;
77     *) ls -1 ${_DATA}/vcard/*vcf 2>/dev/null
78       ;;
79   esac |case "${_GET[order]}" in
80     firstname)
81       while read file; do
82         fn=$(sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$file")
83         echo "$fn\t$file"
84       done
85       ;;
86     lastname)
87       while read file; do
88         ln=$(sed -rn 's:^N(;.+)*\:([^;]*;){0} *([^;]*).*$:\3:p' "$file")
89         echo "$ln\t$file"
90       done
91       ;;
92     bdate)
93       while read file; do
94         bd=$(sed -rn 's:^BDAY(;.+)*\:(.*)$:\2:p' "$file")
95         echo "$bd\t$file"
96       done
97       ;;
98     *)
99       sed -r 's:^.*$:x\t&:'
100       ;;
101   esac |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
102 }
103
104 vcf_parse() {
105   unset key
106   sed -r ':X;N;$!bX; s;\r\n[ \t];;g; s;\r\n;\n;g;' "$1" \
107   | sed -rn '
108     # === turn property names to upper case, strip group names ===
109     h; s;^([^;:]+);;;
110     x; s;^([^;:\.]+\.)?([^;:]+).*$;\2;;
111     y;abcdefghijklmnopqrstuvwxyz;ABCDEFGHIJKLMNOPQRSTUVWXYZ;
112     G; s;\n;;;
113
114     # === strip trailing CR (but keep CRs in property value) ===
115     s;\r$;;;
116
117     # === Normalise various known vendor properties ===
118                 s;^X-MS-CARDPICTURE(\;|:);PHOTO\1;;
119                         s;^X-GENDER(\;|:);GENDER\1;;
120                    s;^X-ANNIVERSARY(\;|:);ANNIVERSARY\1;;
121          s;^X-EVOLUTION-ANNIVERSARY(\;|:);ANNIVERSARY\1;;
122     s;^X-KADDRESSBOOK-X-ANNIVERSARY(\;|:);ANNIVERSARY\1;;
123             s;^X-EVOLUTION-BLOG-URL(\;|:);URL\1;;
124                               s;^AGENT(\;|:);RELATED\;VALUE=text\;TYPE=agent\1;;
125                         s;^X-ASSISTANT(\;|:);RELATED\;VALUE=text\;TYPE=assistant\1;;
126               s;^X-EVOLUTION-ASSISTANT(\;|:);RELATED\;VALUE=text\;TYPE=assistant\1;;
127     s;^X-KADDRESSBOOK-X-ASSISTANTSNAME(\;|:);RELATED\;VALUE=text\;TYPE=assistant\1;;
128                           s;^X-MANAGER(\;|:);RELATED\;VALUE=text\;TYPE=manager\1;;
129                 s;^X-EVOLUTION-MANAGER(\;|:);RELATED\;VALUE=text\;TYPE=manager\1;;
130       s;^X-KADDRESSBOOK-X-MANAGERSNAME(\;|:);RELATED\;VALUE=text\;TYPE=manager\1;;
131                            s;^X-SPOUSE(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;;
132                  s;^X-EVOLUTION-SPOUSE(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;;
133         s;^X-KADDRESSBOOK-X-SPOUSENAME(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;;
134
135     # === Normalise obsolete vendor IM properties ===
136             s;^X-AIM((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;;
137             s;^X-ICQ((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;;
138     s;^X-GOOGLE-TALK((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;;
139          s;^X-JABBER((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;;
140             s;^X-MSN((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:msn:;;
141           s;^X-YAHOO((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:ymsgr:;;
142             s;^X-SIP((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(sip:)?;IMPP\1:sip:;;
143
144     # === Update obsolete LABEL property ===
145     s;^LABEL((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;ADR\1\;LABEL="\5":;;
146
147     /^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$/{
148       h;
149       s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;key='\''\1'\'';;
150       H; g;
151
152       s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\2;;
153       s;'\'';'\'\\\\\'\'';g
154       s;\;([A-Z0-9-]+)(=|=(([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*))?;\ntag[\1]='\''\3'\'';g
155       s;^\n+;;; s;\n+$;;;
156       /^.+$/H; g;
157
158       s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\6;;
159       # :X s;((^|[^\\])(\\\\)*)[;,];\1\n;g; s;((^|[^\\])(\\\\)*)\\([;,]);\4;g; tX;
160       s;'\'';'\'\\\\\'\'';g
161       s;^.*$;value='\''&'\'';
162       H; g;
163
164       s;^[^\n]*[\n ]+;;
165       s;\n+$;;;
166
167       p;
168     }
169     ' \
170   | while read -r line; do
171     declare -A tag
172     case "$line" in
173       value*) eval "$line";;
174       tag*)   eval "$line";;
175       key*)
176         if [ -z "$key" ]; then
177           eval "$line"
178         else
179           printf '%s\n' "$value" |sed -rn '
180             :X
181             s;((^|[^\\])(\\\\)*),;\1\n;g; 
182             tX;
183             s;\\,;,;g;
184             p
185           ' \
186           | while read -r val; do
187             while [ -n "${values[$key$n]+x}" ]; do n=$((${n=-1} + 1)); done
188             if printf '%s\n' "$val" |grep -qE '((^|[^\\])(\\\\)*)\;'; then
189               m=0
190               values[${key}${n}]="${val}"
191               printf '%s\n' "$val" |sed -rn '
192                 :X
193                 s;((^|[^\\])(\\\\)*)\;;\1\n;g; 
194                 tX;
195                 s;\\\;;\;;g;
196                 p
197               ' \
198               | while read -r v; do
199                 values[${key}${n}+${m}]="$(
200                   printf %s\\n "${v}" \
201                   | sed -rn '
202                     :X
203                     s;((^|[^\\])(\\\\)*)\\n;\1\n;g; 
204                     tX;
205                     s;\\\\;\\;g;
206                     p
207                   '
208                 )"
209                 m=$(($m + 1))
210               done
211             else
212               values[${key}${n}]="$(
213                 printf %s\\n "${val}" \
214                 | sed -rn '
215                   :X
216                   s;((^|[^\\])(\\\\)*)\\n;\1\n;g; 
217                   tX;
218                   s;\\\;;\;;g;
219                   s;\\\\;\\;g;
220                   p
221                 '
222               )"
223             fi
224             for t in ${(k)tag}; do
225               values[${key}${n}_${t}]="${tag[$t]}"
226             done
227           done
228
229           eval "$line"
230           unset n
231           while [ -n "${values[$key$n]+x}" ]; do n=$((${n=-1} + 1)); done
232           unset value
233           unset tag
234         fi
235       ;;
236     esac
237   done
238 }
239
240 view_card() {  #Parameter: Cardfile
241   id="$1"
242   cardfile="$_DATA/vcard/${id}"
243   cachefile="$_DATA/cache/${id}.cache"
244   if [ "$cachefile" -nt "$cardfile" ]; then
245     cat "$cachefile"
246   else
247     declare -A values
248     vcf_parse "$cardfile"
249     . $view_card |tee "$cachefile"
250   fi
251 }
252
253 edit_card() {  #Parameter: Cardfile
254   id="$1"
255   cardfile="$_DATA/vcard/$id"
256   tempfile="$_DATA/temp/$id"
257   [ -f "$tempfile" ] && cardfile="$tempfile"
258
259   declare -A values
260   vcf_parse "$cardfile"
261   . $edit_card
262 }