Debian Codegraph @

filters/calendar

raw
1 #!/usr/bin/awk -f
2 # ex: ft=awk
3 #
4 # awk filter for aerc to parse text/calendar mime-types
5 #
6 # Based on the ical2org.awk script by Eric S Fraga and updated by Guide Van
7 # Hoecke. Adapted to aerc by Koni Marti <[email protected]>
8 #
9
10 BEGIN {
11 UIDS[0];
12 people_attending[0];
13 people_partstat[0];
14 people_rsvp[0];
15 excluded_dates[0];
16
17 # use a colon to separate the type of data line from the actual contents
18 FS = ":";
19 }
20
21 {
22 # remove carriage return from every line
23 gsub(/\r/, "")
24 }
25
26 /^[ ]/ {
27 # this block deals with the continuation lines that start with a whitespace
28 #
29 line = $0
30 # remove trailing whitespaces
31 gsub(/^[ ]/, "", line)
32
33 # assumes continuation lines start with a space
34 if (indescription) {
35 entry = entry line
36 } else if (insummary) {
37 summary = summary line
38 } else if (inattendee) {
39 attendee = attendee line
40 } else if (inorganizer) {
41 organizer = organizer line
42 } else if (inlocation) {
43 location = location unescape(line, 0)
44 }
45 }
46
47 /^BEGIN:VALARM/,/^END:VALARM/ {
48 next
49 }
50
51 /^BEGIN:VEVENT/ {
52 # start of an event: initialize global values used for each event
53 start_date = "";
54 end_date = "";
55 entry = ""
56 id = ""
57
58 excluded = 0;
59
60 indescription = 0;
61 insummary = 0
62 inattendee = 0
63 inorganizer = 0
64 inlocation = 0
65
66 location = ""
67 status = ""
68 summary = ""
69 attendee = ""
70 organizer = ""
71
72 rrend = ""
73 rcount = ""
74 intfreq = ""
75 idx = 0
76
77 delete people_attending;
78 delete people_partstat;
79 delete people_rsvp;
80 delete excluded_dates;
81 }
82
83 /^[A-Z]/ {
84 if (attendee != "" && inattendee==1)
85 add_attendee(attendee)
86
87 if (organizer != "" && inorganizer==1)
88 organizer = find_full_name(organizer)
89
90 if (excluded != "" && inexcluded==1)
91 add_excluded(excluded)
92
93 indescription = 0;
94 insummary = 0;
95 inattendee = 0;
96 inorganizer = 0;
97 inlocation = 0;
98 }
99
100 /^DTSTART[:;]/ {
101 tz = get_value($0, "TZID=[^:;]*", "=")
102 start_date = datetimestring($2, tz);
103 }
104
105 /^DTEND[:;]/ {
106 tz = get_value($0, "TZID=[^:;]*", "=")
107 end_date = datetimestring($2, tz);
108 }
109
110 /^RRULE[:]/ {
111 freq = get_value($0, "FREQ=[^:;]*", "=")
112 interval = get_value($0, "INTERVAL=[^:;]*", "=")
113 rrend = datetimestring(get_value($0, "UNTIL=[^:;]*", "="), "")
114 rcount = get_value($0, "COUNT=[^:;]*", "=")
115 intfreq = tolower(freq)
116 if (interval != "")
117 intfreq = " +" interval intfreq
118 }
119
120 /^EXDATE;/ {
121 tz = get_value($0, "TZID=[^:;]*", "=")
122 excluded_dates[excluded] = datetimestring($2, tz);
123 excluded = excluded + 1;
124 }
125
126 /^METHOD/ {
127 method = $2
128 }
129
130 /^UID/ {
131 line = prepare($0)
132 id = line
133 }
134
135 /^STATUS/ {
136 line = prepare($0)
137 status = line
138 }
139
140 /^DESCRIPTION/ {
141 line = prepare($0)
142 entry = entry line
143 indescription = 1;
144 }
145
146 /^SUMMARY/ {
147 line = prepare($0)
148 summary = line
149 insummary = 1;
150 }
151
152 /^ORGANIZER/ {
153 organizer = $0
154 inorganizer = 1;
155 }
156
157 /^LOCATION/ {
158 line = prepare($0)
159 location = unescape(line, 0);
160 inlocation = 1;
161 }
162
163 /^ATTENDEE/ {
164 attendee = $0
165 inattendee = 1;
166 }
167
168 /^END:VEVENT/ {
169 #output event
170 if (method != "") {
171 printf "\n This is a meeting %s\n\n", method
172 }
173 fmt = " %-14s%s\n"
174 is_duplicate = (id in UIDS);
175 if(is_duplicate == 0) {
176 printf fmt, "SUMMARY", unescape(summary, 0)
177 printf fmt, "START", start_date
178 printf fmt, "END", end_date
179 if (intfreq != "") {
180 if (rrend != "")
181 printf fmt, "END DATE", rrend
182 printf fmt, "RECURRENCE", intfreq
183 if (rcount != "")
184 printf fmt, "COUNTS", rcount
185 }
186 if (excluded != 0) {
187 printf "\n %-14s", "EXCLUDING"
188 for (idx in excluded_dates) {
189 if (idx==0) {
190 printf "%s,\n", excluded_dates[idx]
191 }
192 else if (idx == (length(excluded_dates)-1)){
193 printf " %-14s%s\n", "", excluded_dates[idx]
194 }
195 else{
196 printf " %-14s%s,\n", "", excluded_dates[idx]
197 }
198
199 }
200 printf "\n"
201 }
202 if(location != "")
203 printf fmt, "LOCATION", location
204 if(organizer != "")
205 printf fmt, "ORGANIZER", organizer
206 if (notEmpty(people_attending)) {
207 printf " %-14s", "ATTENDEES "
208 for (idx in people_attending) {
209 if (idx == 1){
210 printf "%s,\n", people_attending[idx]
211 }
212 else if (idx == length(people_attending)){
213 printf " %-14s%s\n", "", people_attending[idx]
214 }
215 else{
216 printf " %-14s%s,\n", "", people_attending[idx]
217 }
218 }
219 printf "\n\n %-14s\n", "DETAILED LIST:"
220 for (idx in people_attending) {
221 printf fmt, "ATTENDEE [" idx "]", people_attending[idx]
222 partstat = people_partstat[idx]
223 if (partstat != "") {
224 printf fmt, "", "STATUS\t" partstat
225 }
226 rsvp = people_rsvp[idx]
227 if (rsvp != "") {
228 printf fmt, "", "RSVP\t" rsvp
229 }
230 }
231 }
232 if(entry != "")
233 print "\n" unescape(entry, 1);
234 UIDS[id] = 1;
235 }
236 }
237
238 function notEmpty(array)
239 {
240 # "length(array) > 0" isn't POSIX-comapoptible, length accepts only strings
241 for (idx in array) return 1;
242 return 0;
243 }
244
245 function prepare(line)
246 {
247 gsub($1, "", line)
248 gsub(/^[: ]/, "", line)
249 return line
250 }
251
252 function unescape(input, preserve_newlines)
253 {
254 ret = input
255 gsub(/\\,/, ",", ret)
256 gsub(/\\;/, ";", ret)
257 if (preserve_newlines)
258 gsub(/\\n/, "\n", ret)
259 else
260 gsub(/\\n/, " ", ret)
261 return ret
262 }
263
264
265 function datetimestring(input, tzInput)
266 {
267 timestr = input
268 pos = index(timestr, "T")
269 if (pos < 0) {
270 return timestr
271 }
272 if (index(timestr, "Z") == length(timestr)) {
273 tzInput = "UTC"
274 }
275
276 date = substr(timestr, 1, pos)
277 time = substr(timestr, pos+1, length(timestr))
278
279 year = substr(date, 1, 4)
280 month = substr(date, 5, 2)
281 day = substr(date, 7, 2)
282
283 hour = substr(time, 1, 2)
284 min = substr(time, 3, 2)
285 sec = substr(time, 5, 2)
286
287 return sprintf("%4d/%02d/%02d %02d:%02d:%02d %s", year, month, day, hour, min, sec, tzInput)
288 }
289
290 function add_attendee(attendee)
291 {
292 CN = find_full_name(attendee)
293 if (CN != "") {
294 idx = idx + 1
295 people_attending[idx] = CN;
296 people_partstat[idx] = get_value(attendee, "PARTSTAT=[^;:]+", "=")
297 people_rsvp[idx] = get_value(attendee, "RSVP=[^;:]+", "=")
298 }
299 }
300
301 function add_excluded(excluded)
302 {
303 excluded_dates[idx] = excluded;
304 idx = idx + 1;
305 }
306
307 function find_full_name(line)
308 {
309 name = get_value(line, "CN=[^;:]+", "=")
310 gsub(/"[^"]*"/,"",line)
311 email = get_value(line, "(mailto|MAILTO):[^;]+", ":")
312
313 if (name == "") {
314 return sprintf("<%s>", email)
315 } else {
316 return sprintf("%s <%s>", name, email)
317 }
318 }
319
320 function get_value(line, regexp, sep) {
321 value = ""
322 match(line, regexp)
323 {
324 z = split(substr(line,RSTART,RLENGTH),data,sep)
325 if (z > 1) {
326 value = data[2]
327 }
328 }
329 return value
330 }