]> git.plutz.net Git - confetti/blob - pages/cards.sh
more reliable color selection, bugfix regarding backend color transparency
[confetti] / pages / cards.sh
1 #!/bin/zsh
2
3 # Copyright 2014 - 2016 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 case $PROFILE in
24 medical)
25   view_card="$_EXEC/templates/view_client.sh"
26   edit_card="$_EXEC/templates/edit_client.sh"
27   _GET[order]="${_GET[order]:-lastname}"
28   profile_medical=x
29 ;;
30 circus)
31   view_card="$_EXEC/templates/view_attendee.sh"
32   edit_card="$_EXEC/templates/edit_attendee.sh"
33   _GET[order]="${_GET[order]:-firstname}"
34   profile_circus=x
35 ;;
36 esac
37
38 edit="${_GET[edit]}"
39 [ \! -f "vcard/$edit" -a \! -f "temp/$edit" ] && edit=''
40 _GET[filtertype]="${_GET[filtertype]:-any}"
41
42 listcourses() {
43   ls -1 ${_DATA}/ical/*ics |while read file; do
44     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")"
45     echo "$(date -d "$icstime" "+%u %H%M%S")\t$file"
46   done |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
47 }
48
49 list_hi_companies(){
50   sed -rn 's;^X-HEALTH-INSURANCE:([^\;]+)\;.*$;\1;p' ${_DATA}/vcard/*vcf \
51   | sort -u
52 }
53
54 listcards() {
55   case "${_GET[filtertype]}" in
56     any)
57        grep -il "${_GET[filter]}" ${_DATA}/vcard/*vcf
58       ;;
59     name)
60        egrep -xil "(FN|NICKNAME|N)(;.+)*:.*${_GET[filter]}.*" ${_DATA}/vcard/*vcf
61       ;;
62     street)
63        egrep -xil "(ADR)(;.+)*:([^;]*;){2}${_GET[filter]}.*" ${_DATA}/vcard/*vcf
64       ;;
65     zip)
66        egrep -xil "(ADR)(;.+)*:([^;]*;){5}${_GET[filter]}.*" ${_DATA}/vcard/*vcf
67       ;;
68     telephone)
69        egrep -xil "(TEL)(;.+)*:.*${_GET[filter]}.*" ${_DATA}/vcard/*vcf
70       ;;
71     birth)
72        egrep -xil "(BDAY)(;.+)*:${_GET[filter]}.*" ${_DATA}/vcard/*vcf
73       ;;
74     course)
75       ;;
76     *) ls -1 ${_DATA}/vcard/*vcf 2>/dev/null
77       ;;
78   esac |case "${_GET[order]}" in
79     firstname)
80       while read file; do
81         fn=$(sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$file")
82         echo "$fn\t$file"
83       done
84       ;;
85     lastname)
86       while read file; do
87         ln=$(sed -rn 's:^N(;.+)*\:([^;]*;){0} *([^;]*).*$:\3:p' "$file")
88         echo "$ln\t$file"
89       done
90       ;;
91     bdate)
92       while read file; do
93         bd=$(sed -rn 's:^BDAY(;.+)*\:(.*)$:\2:p' "$file")
94         echo "$bd\t$file"
95       done
96       ;;
97     *)
98       sed -r 's:^.*$:x\t&:'
99       ;;
100   esac |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
101 }
102
103 vcf_parse() {
104   tr -d '\n' <"$1" |sed -r 's:\r ::g;s:\r:\n:g' \
105   | sed -rn '
106     s:^X-MS-CARDPICTURE:PHOTO:p;
107     s:^X-GENDER:GENDER:p;
108     s:^X-ANNIVERSARY:ANNIVERSARY:p;
109     s:^X-EVOLUTION-ANNIVERSARY:ANNIVERSARY:p;
110     s:^X-KADDRESSBOOK-X-Anniversary:ANNIVERSARY:p;
111     s:^X-AIM(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:aim\:\2:p;
112     s:^X-ICQ(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:aim\:\2:p;
113     s:^X-GOOGLE-TALK(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:xmpp\:\2:p;
114     s:^X-JABBER(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:xmpp\:\2:p;
115     s:^X-MSN(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:msn\:\2:p;
116     s:^X-YAHOO(;[^"\:]+|;"[^"]+")*\:(.*)$:IMPP\1\:ymsgr\:\2:p;
117     s:^X-SIP(;[^"\:]+|;"[^"]+")*\:(sip\:)?(.*)$:IMPP\1\:sip\:\3:p;
118     s:^LABEL(;[^"\:]+|;"[^"]+")*\:(.*)$:ADR;LABEL="\2"\1\::p;
119     s:^X-EVOLUTION-BLOG-URL:URL:p;
120
121     s:^AGENT:RELATED\;TYPE=agent:p;
122     s:^X-ASSISTANT:RELATED\;TYPE=assistant;VALUE=text:p;
123     s:^X-EVOLUTION-ASSISTANT:RELATED\;TYPE=assistant;VALUE=text:p;
124     s:^X-KADDRESSBOOK-X-AssistantsName:RELATED\;TYPE=assistant;VALUE=text:p;
125     s:^X-MANAGER:RELATED\;TYPE=manager;VALUE=text:p;
126     s:^X-EVOLUTION-MANAGER:RELATED\;TYPE=manager;VALUE=text:p;
127     s:^X-KADDRESSBOOK-X-ManagersName:RELATED\;TYPE=manager;VALUE=text:p;
128     s:^X-SPOUSE:RELATED\;TYPE=spouse;VALUE=text:p;
129     s:^X-EVOLUTION-SPOUSE:RELATED\;TYPE=spouse;VALUE=text:p;
130     s:^X-KADDRESSBOOK-X-SpouseName:RELATED\;TYPE=spouse;VALUE=text:p;
131
132     s:^([A-Z].*)$:\1:p;
133     ' \
134   | sed -r 's:^([^;\:]+)(;[^"\:]+|;"[^"]+")*\:(.*)$:key="\1"\nvalue="\3"\ntag=\2:g' \
135   | while read -r line; do
136     case "$line" in
137       key=*) printf %s\\n "$line"
138         ;;
139       value=*) printf %s\\n "${line}"
140         ;;
141       tag=*) ot=''
142              printf %s "$line" \
143              | sed -r 's:^tag=::;s:\;([A-Z+_-]+="[^"]+"|[A-Z+_-]+=[^\;]+):\n\1:g;' \
144              | sed -r 's:([A-Z+_-]+)="?(.*)"?:tag\[\1\]="\2":g' \
145              | sed -r '/^ *$/d' \
146              | sort |while read -r tag; do
147                nt="$(printf %s "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\1:')"
148                nv="$(printf %s "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\2:')"
149                [ "$nt" = "$ot" ] && vl="$nv,$vl" || vl="$nv"
150                printf %s\\n "tag[$nt]=\"$vl\""
151                ot="$nt"
152              done
153         ;;
154     esac
155   done \
156   | sed -r 's:[\\$`]:\\&:g'
157 }
158
159 view_card() {  #Parameter: Cardfile
160   id="$1"
161   cardfile="$_DATA/vcard/${id}"
162   cachefile="$_DATA/cache/${id}.cache"
163   unset key
164   if [ "$cachefile" -nt "$cardfile" ]; then
165     cat "$cachefile"
166   else
167     declare -A tags
168     declare -A values
169     vcf_parse "$cardfile" |while read -r line; do
170       declare -A tag
171       case "$line" in
172         value*) eval "$line";;
173         tag*)   eval "$line";;
174         key*)
175           if [ -z "$key" ]; then
176             eval "$line"
177           else
178             values[$key]="${value//\\r\\n/$BR}"
179             for t in ${(k)tag}; do
180               tags[${key}_$t]="$tag[$t]"
181             done
182             eval "$line"
183             if [ -n "$values[$key]" ]; then
184               n=0
185               while [ -n "$values[$key$n]" ]; do n=$(($n + 1)); done
186               key=$key$n
187             fi
188             unset value
189             unset tag
190           fi
191         ;;
192       esac
193     done
194     . $view_card |tee "$cachefile"
195   fi
196 }
197
198 edit_card() {  #Parameter: Cardfile
199   id="$1"
200   cardfile="$_DATA/vcard/$id"
201   tempfile="$_DATA/temp/$id"
202   [ -f "$tempfile" ] && cardfile="$tempfile"
203   unset key
204
205   declare -A tags
206   declare -A values
207   vcf_parse "$cardfile" |while read -r line; do
208     declare -A tag
209     case "$line" in
210       value*) eval "$line";;
211       tag*)   eval "$line";;
212       key*)
213         if [ -z "$key" ]; then
214           eval "$line"
215         else
216           [ -n "$value" ] && values[$key]="${value//\\r\\n/$BR}" || values[$key]='\r'
217           for t in ${(k)tag}; do
218             tags[${key}_$t]="$tag[$t]"
219           done
220           eval "$line"
221           if [ -n "$values[$key]" ]; then
222             n=0
223             while [ -n "$values[${key}${n}]" ]; do n=$(($n + 1)); done
224             key=$key$n
225           fi
226             unset value
227             unset tag
228         fi
229       ;;
230     esac
231   done
232   . $edit_card
233 }