]> git.plutz.net Git - confetti/blob - cards/cards.sh
modular design
[confetti] / cards / 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   | sed -r '/^[\t ]*$/d'
65 }
66
67 listcards() {
68   filterex='s;^([^\n]+)\n.*$;\1;p'
69   printf '%s\n' "${_GET[filter]}" |tr '^' '\n' \
70   | sed -r 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g' \
71   | while read each; do
72     case $each in
73         name:*) expr='(FN|NICKNAME|N)(\;[^\n]+)*:[^\n]*'"(${each#*:})";;
74       street:*) expr='ADR(\;[^\n]+)*:([^\;]*;){2}[^\;\n]*'"(${each#*:})";;
75          zip:*) expr='ADR(\;[^\n]+)*:([^\;]*;){5}[^\;\n]*'"(${each#*:})";;
76       any:*|:*) expr="[^\n]*"'(\;[^\n]+)*:[^\n]*'"(${each#*:})";;
77            *:*) expr="${each%%:*}"'(\;[^\n]+)*:[^\n]*'"(${each#*:})";;
78              *) expr="(${each})";;
79     esac
80     filterex='/(^|\n)'"${expr}"'/I{'"${filterex}"'}'
81   done
82
83   for file in "${_DATA}/vcard/"*.vcf; do
84     case "${_GET[order]}" in
85       firstname)
86         printf '%s\t%s\n' "$(sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$file")" "$file"
87         ;;
88       lastname)
89         printf '%s\t%s\n' "$(sed -rn 's:^N(;.+)*\:([^;]*;){0} *([^;]*).*$:\3:p' "$file")" "$file"
90         ;;
91       bdate)
92         printf '%s\t%s\n' "$(sed -rn 's:^BDAY(;.+)*\:(.*)$:\2:p' "$file")" "$file"
93         ;;
94       *)  printf 'x\t%s\n' "$file"
95         ;;
96     esac
97   done \
98   | sort -u |sed -r 's;^.*\t;;' \
99   | while read n; do
100     { printf '%s\n' "$n"; cat "$n"; } \
101     | sed -rn ':X;N;$!bX; {'"$filterex"'}'
102   done \
103   | sed -r 's;^(.*/)*;;;'
104 }
105
106 view_card() {  #Parameter: Cardfile
107   id="$1"
108   cardfile="$_DATA/vcard/${id}"
109   cachefile="$_DATA/cache/${id}.cache"
110   if [ "$cachefile" -nt "$cardfile" ]; then
111     cat "$cachefile"
112   else
113     declare -A values
114     vcf_parse "$cardfile"
115     . "$_EXEC/templates/view_card.sh" |tee "$cachefile"
116   fi
117 }
118
119 edit_card() {  #Parameter: Cardfile
120   id="$1"
121   cardfile="$_DATA/vcard/$id"
122   tempfile="$_DATA/temp/$id"
123   [ -f "$tempfile" ] && cardfile="$tempfile"
124
125   declare -A values
126   vcf_parse "$cardfile"
127   force_items $FORCE_ITEMS
128   . "$_EXEC/templates/edit_card.sh"
129 }