aboutsummaryrefslogtreecommitdiff
path: root/filters/calendar
blob: 8d7e67dc5b8b4ada09c70731748025ac6b1e7b22 (plain)
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
#!/usr/bin/awk -f
# ex: ft=awk
#
# awk filter for aerc to parse text/calendar mime-types
#
# Based on the ical2org.awk script by Eric S Fraga and updated by Guide Van
# Hoecke. Adapted to aerc by Koni Marti <koni.marti@gmail.com>
#

BEGIN {
	UIDS[0];
	people_attending[0];
	people_partstat[0];
	people_rsvp[0];

	# use a colon to separate the type of data line from the actual contents
	FS = ":";
}

{
	# remove carriage return from every line
	gsub(/\r/, "")
}

/^[ ]/ {
	# this block deals with the continuation lines that start with a whitespace
	#
	line = $0
	# remove trailing whitespaces
	gsub(/^[ ]/, "", line)

	# assumes continuation lines start with a space
	if (indescription) {
		entry = entry line
	} else if (insummary) {
		summary = summary line
	} else if (inattendee) {
		attendee = attendee line
	} else if (inorganizer) {
		organizer = organizer line
	} else if (inlocation) {
		location = location unescape(line, 0)
	}
}

/^BEGIN:VALARM/,/^END:VALARM/ {
	next
}

/^BEGIN:VEVENT/ {
	# start of an event: initialize global values used for each event
	start_date = "";
	end_date = "";
	entry = ""
	id = ""

	indescription = 0;
	insummary = 0
	inattendee = 0
	inorganizer = 0
	inlocation = 0

	location = ""
	status = ""
	summary = ""
	attendee = ""
	organizer = ""

	rrend = ""
	rcount = ""
	intfreq = ""
	idx = 0

	delete people_attending;
	delete people_partstat;
	delete people_rsvp;
}

/^[A-Z]/ {
	if (attendee != "" && inattendee==1)
		add_attendee(attendee)

	if (organizer != "" && inorganizer==1)
		organizer = find_full_name(organizer)

	indescription = 0;
	insummary = 0;
	inattendee = 0;
	inorganizer = 0;
	inlocation = 0;
}

/^DTSTART[:;]/ {
	tz = get_value($0, "TZID=[^:;]*", "=")
	start_date = datetimestring($2, tz);
}

/^DTEND[:;]/ {
	tz = get_value($0, "TZID=[^:;]*", "=")
	end_date = datetimestring($2, tz);
}

/^RRULE[:]/ {
	freq = get_value($0, "FREQ=[^:;]*", "=")
	interval = get_value($0, "INTERVAL=[^:;]*", "=")
	rrend = get_value($0, "UNTIL=[^:;]*", "=")
	rcount = get_value($0, "COUNT=[^:;]*", "=")
	intfreq = tolower(freq)
	if (interval != "")
		intfreq = " +" interval intfreq
}

/^METHOD/ {
	method = $2
}

/^UID/ {
	line = prepare($0)
	id = line
}

/^STATUS/ {
	line = prepare($0)
	status = line
}

/^DESCRIPTION/ {
	line = prepare($0)
	entry = entry line
	indescription = 1;
}

/^SUMMARY/ {
	line = prepare($0)
	summary = line
	insummary = 1;
}

/^ORGANIZER/ {
	organizer = $0
	inorganizer = 1;
}

/^LOCATION/ {
	line = prepare($0)
	location = unescape(line, 0);
	inlocation = 1;
}

/^ATTENDEE/ {
	attendee = $0
	inattendee = 1;
}

/^END:VEVENT/ {
	#output event
	if (method != "") {
		printf    "\n  This is a meeting %s\n\n",  method
	}
	fmt = "  %-14s%s\n"
	is_duplicate = (id in UIDS);
	if(is_duplicate == 0) {
		printf fmt, "SUMMARY", unescape(summary, 0)
		printf fmt, "START", start_date
		printf fmt, "END", end_date
		if (intfreq != "") {
			printf "\n"fmt, "RECURRENCE", intfreq
			if (rcount != "")
				printf fmt, "COUNTS", rcount
			if (rrend != "")
				printf fmt, "END DATE", rrend

		}
		if(location != "")
			printf fmt, "LOCATION",  location
		if(organizer != "")
			printf fmt, "ORGANIZER", organizer
		printf "  %-14s", "ATTENDEES "
		for (idx in people_attending) {
			if (idx == 1){
				printf "%s,\n", people_attending[idx]
			}
			else if (idx == length(people_attending)){
				printf "  %-14s%s\n", "", people_attending[idx]
			}
			else{
				printf "  %-14s%s,\n", "", people_attending[idx]
			}
		}
		printf "\n\n  %-14s\n", "DETAILED LIST:"
		for (idx in people_attending) {
			printf fmt, "ATTENDEE [" idx "]", people_attending[idx]
			partstat = people_partstat[idx]
			if (partstat != "") {
				printf fmt, "", "STATUS\t" partstat
			}
			rsvp = people_rsvp[idx]
			if (rsvp != "") {
				printf fmt, "", "RSVP\t" rsvp
			}
		}
		if(entry != "")
			print "\n" unescape(entry, 1);
		UIDS[id] = 1;
	}
}

func prepare(line) {
	gsub($1, "", line)
	gsub(/^[: ]/, "", line)
	return line
}

function unescape(input, preserve_newlines)
{
	ret = input
	gsub(/\\,/, ",", ret)
	gsub(/\\;/, ";", ret)
	if (preserve_newlines)
		gsub(/\\n/, "\n", ret)
	else
		gsub(/\\n/, " ", ret)
	return ret
}


function datetimestring(input, tzInput)
{
	timestr = input
	pos = index(timestr, "T")
	if (pos < 0) {
		return timestr
	}

	date = substr(timestr, 1, pos)
	time = substr(timestr, pos+1, length(timestr))

	year = substr(date, 1, 4)
	month = substr(date, 5, 2)
	day = substr(date, 7, 2)

	hour = substr(time, 1, 2)
	min = substr(time, 3, 2)
	sec = substr(time, 5, 2)

	return sprintf("%4d/%02d/%02d %02d:%02d:%02d %s", year, month, day, hour, min, sec, tzInput)
}

function add_attendee(attendee)
{
	CN = find_full_name(attendee)
	if (CN != "") {
		idx = idx + 1
		people_attending[idx] = CN;
		people_partstat[idx] = get_value(attendee, "PARTSTAT=[^;:]+", "=")
		people_rsvp[idx] = get_value(attendee, "RSVP=[^;:]+", "=")
	}
}

function find_full_name(line)
{
	name = get_value(line, "CN=[^;:]+", "=")
	gsub(/"[^"]*"/,"",line)
	email = get_value(line, "(mailto|MAILTO):[^;]+", ":")

	if (name == "") {
		return sprintf("<%s>", email)
	} else {
		return sprintf("%s <%s>", name, email)
	}
}

function get_value(line, regexp, sep) {
	value = ""
	match(line, regexp)
	{
		z = split(substr(line,RSTART,RLENGTH),data,sep)
		if (z > 1) {
			value = data[2]
		}
	}
	return value
}