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