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