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