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