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