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