]> git.plutz.net Git - confetti/blob - courses/courses.sh
stubs for course lists
[confetti] / courses / courses.sh
1 #!/bin/zsh
2
3 # Copyright 2014, 2016, 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 [ -z "${_GET[order]}" ] && _GET[order]=DOW
21
22 listcards() {
23   ls -1 ${_DATA}/vcard/*vcf 2>/dev/null |while read file; do
24     fn=$(sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$file")
25     echo "$fn\t$file"
26   done |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
27 }
28
29
30 listcourses() {
31   case "${_GET[filtertype]}" in
32     *) ls -1 ${_DATA}/ical/*ics 2>/dev/null
33       ;;
34   esac |case "${_GET[order]}" in
35     DOW)
36       while read file; do
37         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")"
38         echo "$(date -d "$icstime" "+%u %H%M%S")\t$file"
39       done
40       ;;
41     TOD)
42       while read file; do
43         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")"
44         echo "$(date -d "$icstime" +%H%M%S)\t$file"
45       done
46       ;;
47     *)
48       sed -r 's:^.*$:x\t&:'
49       ;;
50   esac |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
51 }
52
53 list_attendance() {
54   id="$1"
55   sed -rn 's:'$id'\t(.+)$:\1:p' "$_DATA/mappings/attendance" |while read each; do
56     card="$_DATA/vcard/$each"
57     if [ -r "$card" ]; then
58       aname="$(sed -rn '
59           /^N(;.+)*:/{
60           h;
61           s;^N(\;.+)*:([^\;]*\;){3} *([^\;]*).*$;\3;p; g;
62           s;^N(\;.+)*:([^\;]*\;){1} *([^\;]*).*$;\3;p; g;
63           s;^N(\;.+)*:([^\;]*\;){2} *([^\;]*).*$;\3;p; g;
64           s;^N(\;.+)*:([^\;]*\;){0} *([^\;]*).*$;\3;p; g;
65           s;^N(\;.+)*:([^\;]*\;){4} *([^\;]*).*$;\3;p; g;
66           }' "$card" \
67         | sed -r ':X;N;$!bX; s;([\;\n\r,]| )+; ;g;; s;^ +| +$;;g;'
68       )"
69       fname="$(sed -rn 's;^FN(\;.+)*:(.+)\r?$;\2;g; s;([\;\n,]| )+; ;g;; s;^ +| +$;;g;' "$card")"
70       nname="$(sed -rn 's;^NICKNAME(\;.+)*:(.+)\r?$;\2;g; s;([\;\n,]| )+; ;g;; s;^ +| +$;;g;' "$card")"
71       byear="$(sed -rn 's:^BDAY(\;.+)*\:([0-9]{4})(-[0-9][0-9]){2}.*$:\2:p' "$_DATA/vcard/$each")"
72
73       printf '%s %s (*%04i)\n' "$each" "${aname:-${fname:-${nname}}}" "$byear"
74     fi
75   done
76 }
77
78 course_mail() {
79   id="$1"
80   coursemail=""
81   sed -rn 's:'$id'\t(.+)$:\1:p' "$_DATA/mappings/attendance" |while read each; do
82     coursemail="$coursemail$(sed -rn 's:^EMAIL(;.+)*\:(.+)\r$:\2,:p' "$_DATA/vcard/$each")"
83   done
84   echo "$coursemail"
85 }
86
87 ics_parse() {
88   unset key
89   sed -r ':X;N;$!bX; s;\r\n[ \t];;g; s;\r\n;\n;g;' "$1" \
90   | sed -rn '
91     # === turn property names to upper case, strip group names ===
92     h; s;^([^;:]+);;;
93     x; s;^([^;:\.]+\.)?([^;:]+).*$;\2;;
94     y;abcdefghijklmnopqrstuvwxyz;ABCDEFGHIJKLMNOPQRSTUVWXYZ;
95     G; s;\n;;;
96
97     /^([A-Z0-9-]+)((\;[A-z0-9-]+=?|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$/{
98       h;
99       s;^([A-Z0-9-]+)((\;[A-z0-9-]+=?|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;key='\''\1'\'';;
100       H; g;
101
102       s;^([A-Z0-9-]+)((\;[A-z0-9-]+=?|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\2;;
103       s;'\'';'\'\\\\\'\'';g
104       s;\;([A-Z0-9-]+)(=|=(([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*))?;\ntag[\1]='\''\3'\'';g
105       s;^\n+;;; s;\n+$;;;
106       /^.+$/H; g;
107
108       s;^([A-Z0-9-]+)((\;[A-z0-9-]+=?|\;[A-z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\6;;
109       # :X s;((^|[^\\])(\\\\)*)[;,];\1\n;g; s;((^|[^\\])(\\\\)*)\\([;,]);\4;g; tX;
110       s;'\'';'\'\\\\\'\'';g
111       s;^.*$;value='\''&'\'';
112       H; g;
113
114       s;^[^\n]*[\n ]+;;
115       s;\n+$;;;
116
117       p;
118     }
119     ' \
120   | while read -r line; do
121     declare -A tag
122     case "$line" in
123       value*) eval "$line";;
124       tag*)   eval "$line";;
125       key*)
126         if [ -z "$key" ]; then
127           eval "$line"
128         else
129           values[$key]="$value"
130           for t in ${(k)tag}; do
131             values[${key}_$t]="$tag[$t]"
132           done
133           eval "$line"
134           if [ -n "$values[$key]" ]; then
135             n=0
136             while [ -n "$values[$key$n]" ]; do n=$(($n + 1)); done
137             key=$key$n
138           fi
139           unset value
140           unset tag
141         fi
142       ;;
143     esac
144   done
145 }
146
147 view_course() {  #Parameter: Calendarfile
148   id="$1"
149   calendarfile="$_DATA/ical/${id}"
150   cachefile="$_DATA/cache/${id}.cache"
151   unset key
152   if [ "$cachefile" -nt "$calendarfile" ]; then
153     cat "$cachefile"
154   else
155     declare -A values
156     ics_parse "$calendarfile"
157     . ${_EXEC}/templates/view_course.sh |tee "$cachefile"
158   fi
159 }
160
161 edit_course() {  #Parameter: Calendarfile
162   id="$1"
163   calendarfile="$_DATA/ical/$id"
164   tempfile="$_DATA/temp/$id"
165   [ -f "$tempfile" ] && calendarfile="$tempfile"
166   unset key
167
168   declare -A values
169   ics_parse "$calendarfile"
170   . ${_EXEC}/templates/edit_course.sh
171 }