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