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