-
Notifications
You must be signed in to change notification settings - Fork 0
/
icalparse.bash
executable file
·455 lines (354 loc) · 10.8 KB
/
icalparse.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/bin/bash
# vim:set ts=4 sw=4 tw=80 et ai si:
# ( settings from: http://datapax.com.au/code_conventions/ )
#
#/**********************************************************************
# iCalParse
# Copyright (C) 2016-2018 Todd Harbour
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 3 ONLY, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program, in the file COPYING or COPYING.txt; if
# not, see http://www.gnu.org/licenses/ , or write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# **********************************************************************/
# icalparse
# ---------
# Simple viewer for iCal files
# Config paths
_ETC_CONF="/etc/icalparse.conf"
_HOME_CONF="${HOME}/.icalparserc"
# [ CONFIG_START
# iCal Parse Default Configuration
# ================================
# DEBUG
# This defines debug mode which will output verbose info to stderr
# or, if configured, the debug file ( ERROR_LOG ).
DEBUG=0
# ERROR_LOG
# The file to output errors and debug statements (when DEBUG != 0) instead of
# stderr.
#ERROR_LOG="/tmp/icalparse.log"
# ] CONFIG_END
###
# Config loading
###
[ ! -z "${_ETC_CONF}" ] && [ -r "${_ETC_CONF}" ] && . "${_ETC_CONF}"
[ ! -z "${_HOME_CONF}" ] && [ -r "${_HOME_CONF}" ] && . "${_HOME_CONF}"
# Version
APP_NAME="iCal Parse (icalparse)"
APP_VER="0.01"
#TODO:
#APP_URL="http://www.datapax.com.au/icalparse/"
# Program name
PROG="$(basename "${0}")"
# exit condition constants
ERR_NONE=0
ERR_MISSINGDEP=1
ERR_UNKNOWNOPT=2
ERR_INVALIDOPT=3
ERR_MISSINGPARAM=4
# Defaults not in config
files=()
# Params:
# NONE
function show_version() {
echo -e "\
${APP_NAME} v${APP_VER}\n\
${APP_URL}\n\
"
}
# Params:
# NONE
function show_usage() {
show_version
cat <<EOF
${APP_NAME} parses an iCal file, displaying a summary.
Usage: ${PROG} -h|--help
${PROG} -V|--version
${PROG} [-v|--verbose] <file>
-h|--help - Displays this help
-V|--version - Displays the program version
-v|--verbose - Displays extra debugging information. This is the same
as setting DEBUG=1 in your config.
<file> - The calendar file to parse.
Example: ${PROG} appointment.ics
EOF
}
# Params:
# $1 = (s) command to look for
# $2 = [(s) suspected package name]
function check_for_cmd() {
# Check for ${1} command
cmd="UNKNOWN"
[ $# -gt 0 ] && cmd="${1}" && shift 1
pkg="${cmd}"
[ $# -gt 0 ] && pkg="${1}" && shift 1
which "${cmd}" >/dev/null 2>&1 || {
cat >&2 <<EOF
ERROR: Cannot find ${cmd}. This is required.
Ensure you have ${pkg} installed or search for ${cmd}
in your distribution's packages.
EOF
exit ${ERR_MISSINGDEP}
}
return ${ERR_NONE}
}
# Debug echo
function decho() {
# Not debugging, get out of here then
[ ${DEBUG} -le 0 ] && return
echo >&2 "DEBUG: ${@}"
}
function elpush() {
decho "ELPUSH: ${1}"
[ "${1%%:*}" != "BEGIN" ] && return 1
stack+=("${1#*:}")
}
function elpop() {
decho "ELPOP : ${1}"
[ "${1%%:*}" != "END" ] && return 1
[ "${stack[$(( ${#stack[@]} - 1 ))]}" != "${1#*:}" ] && return 2
unset stack[${#stack[@]}]
}
function demultiline() {
awk \
'
BEGIN { ORS=""; } # default: no newline between output records
NR==1 { print; next; } # first line: print
/^[^ ]/ { print "\n"; print; next; } # if it doesnt start with a space: print newline before
{ sub(/^ /, ""); print; } # other lines (next; has not been called yet)
'
}
# Load timezones
function loadtimezones() {
read -r line
[ "${line}" != "BEGIN:VCALENDAR" ] && {
echo >&2 "ERROR: Expected 'BEGIN:VCALENDAR' not found"
return 1
}
elpush "BEGIN:VCALENDAR"
while read -r line; do #{
key="${line%%:*}"
value="${line#*:}"
[ "${key}" == "BEGIN" ] && {
elpush "${line}"
continue
}
[ "${key}" == "END" ] && {
elpop "${line}"
[ "${value}" == "VTIMEZONE" ]\
&& [ ! -z "${tzid}" ]\
&& [ ! -z "${tzoff}" ]\
&& {
tz[${tzid//[ \/]/_}]="${tzoff}"
decho "SET TZ: \"${tzid}\" = ${tzoff}"
}
continue
}
stacktop="${stack[$(( ${#stack[@]} - 1 ))]}"
stacknext=""
[ ${#stack[@]} -ge 2 ] && stacknext="${stack[$(( ${#stack[@]} - 2 ))]}"
[ "${stacktop}" == "VTIMEZONE" ] && {
[ "${key}" == "TZID" ] && {
decho "${key}: ${value}"
tzid="${value}"
tzid="${tzid//\"/}"
continue
}
}
# TODO: Process daylight and calculate WHEN it's daylight or not.
[ "${stacknext}" == "VTIMEZONE" ]\
&& [ "${stacktop}" == "STANDARD" ]\
&& [ "${key}" == "TZOFFSETTO" ]\
&& {
decho "${key}: ${value}"
tzoff="${value}"
continue
}
done #}
}
# Load calendar events
function loadevents() {
summ=""
loc=""
read -r line
[ "${line}" != "BEGIN:VCALENDAR" ] && {
echo >&2 "ERROR: Expected 'BEGIN:VCALENDAR' not found"
return 1
}
elpush "BEGIN:VCALENDAR"
while read -r line; do #{
key="${line%%:*}"
value="${line#*:}"
[ "${key}" == "BEGIN" ] && {
elpush "${line}"
continue
}
[ "${key}" == "END" ] && {
elpop "${line}"
[ "${value}" == "VEVENT" ]\
&& [ ! -z "${summ}" ]\
&& [ ! -z "${tm[0]}" ]\
&& {
# "DESCRIPTION: meh"
spcs=" "
# NOTE: sed de'escapes '\,', '\;' and converts '\n' to newline.
cat <<EOF
[ NEW EVENT [
START : ${tm[0]}
END : ${tm[1]}
SUMMARY : ${summ}
LOCATION : ${loc}
DESCRIPTION: $(\
echo "${desc}"\
|sed 's#\\n#\n#g;s#\\,#,#g;s#\\;#;#g'\
|fold -s -w $((80 - ${#spcs}))\
|sed '2,$s#^#'"${spcs}"'#g'\
)
] NEW EVENT ]
EOF
}
continue
}
stacktop="${stack[$(( ${#stack[@]} - 1 ))]}"
stacknext=""
[ ${#stack[@]} -ge 2 ] && stacknext="${stack[$(( ${#stack[@]} - 2 ))]}"
[ "${stacktop}" == "VEVENT" ] && {
# Inside event
[ "${key%%;*}" == "SUMMARY" ] && {
decho "${key}: ${value}"
summ="${value}"
continue
}
[ "${key%%;*}" == "LOCATION" ] && {
decho "${key}: ${value}"
loc="${value}"
continue
}
[ "${key%%;*}" == "DESCRIPTION" ] && {
decho "${key}: ${value}"
desc="${value}"
continue
}
# DTSTART;TZID=Arabian Standard Time:20160711T100000
[ "${key%%;*}" == "DTSTART" ]\
|| [ "${key%%;*}" == "DTEND" ]\
&& {
ctz=""
# Walk through the key options
tzp1="${key}"
tzp2="${key}"
while [ 1 ]; do #{
[ "${tzp2}" == "${tzp2#*;}" ] && break
tzp2="${tzp2#*;}"
[ "${tzp2:0:4}" == "TZID" ] && {
ctz="${tzp2%%;*}"
ctz="${ctz%%:*}"
ctz="${ctz//\"/}"
ctz="${ctz##*=}"
decho "${key%%;*}:TZ: ${ctz}"
[ -z "${tz[${ctz//[ \/]/_}]}" ] && {
echo >&2 "ERROR: Unknown timezone for ${key%%;*}: ${ctz}"
ctz=""
} || {
decho "- FOUND TZ: ${ctz}"
ctz="${tz[${ctz//[ \/]/_}]}"
}
}
done #}
tmp="$(date -d "
${value:0:4}-${value:4:2}-${value:6:2}\
\
${value:9:2}:${value:11:2}:${value:13:2}\
\
${ctz}\
" +'%Y-%m-%d %H:%M %z')"
decho "${key%%;*}: ${tmp}"
[ "${key%%;*}" == "DTSTART" ] && tm[0]="${tmp}"
[ "${key%%;*}" == "DTEND" ] && tm[1]="${tmp}"
continue
}
}
done #}
}
# START #
# If debug file, redirect stderr out to it
[ ! -z "${ERROR_LOG}" ] && exec 2>>"${ERROR_LOG}"
decho "START"
# Check for wget
#check_for_cmd "COMMAND" "PACKAGE"
moreparams=1
decho "Processing ${#} params..."
while [ ${#} -gt 0 ]; do #{
decho "Command line param: ${1}"
[ ${moreparams} -gt 0 ] && {
case "${1}" in #{
# Verbose mode # [-v|--verbose]
-v|--verbose)
decho "Verbose mode specified"
DEBUG=1
shift 1; continue
;;
# Help # -h|--help
-h|--help)
decho "Help"
show_usage
exit ${ERR_NONE}
;;
# Version # -V|--version
-V|--version)
decho "Version"
show_version
exit ${ERR_NONE}
;;
*)
[ "${1}" == "--" ] && {
# No more parameters to come
moreparams=0
shift 1; continue
}
[ "${1}" == "-" ] && {
# Read stdin
set -- "/dev/stdin"
# FALL THROUGH TO FILE HANDLER BELOW
}
[ "${1:0:1}" == "-" ] && {
# Assume a parameter
echo >&2 "ERROR: Unrecognised parameter ${1}..."
exit ${ERR_UNKNOWNOPT}
}
;;
esac #}
}
# File
decho "File specified ( ${1} )"
files+=("${1}")
shift 1
done #}
for f in "${files[@]}"; do #{
decho "FILE: ${f}"
[ "${f}" == "/dev/stdin" ] && {
decho "PROCESS STDIN"
f="$(dos2unix </dev/stdin|demultiline)"
} || {
decho "PROCESS FILE: ${f}"
[ ! -r "${f}" ] && {
echo >&2 "ERROR: Unable to read file ${f}..."
continue
}
f="$(dos2unix <"${f}"|demultiline)"
}
loadtimezones < <(echo "${f}")
loadevents < <(echo "${f}")
done #}
decho "DONE"