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