aboutsummaryrefslogtreecommitdiff
path: root/filters
diff options
context:
space:
mode:
authorJens Grassel <jens@wegtam.com>2022-03-22 14:18:09 +0100
committerRobin Jarry <robin@jarry.cc>2022-03-23 20:56:22 +0100
commit374d3a0d01a3dbaa246acd9bbd23df743addf477 (patch)
tree6670dcca9d1975925e77910f69321878efc054bf /filters
parentae83373fa63883f03bd5580ad3937d1e5fa428ed (diff)
downloadaerc-374d3a0d01a3dbaa246acd9bbd23df743addf477.tar.gz
aerc-374d3a0d01a3dbaa246acd9bbd23df743addf477.zip
Add filter script for ics files.
This is a python script for python 3 using the vobject library to show details about an ics file (text/calendar attachment). Signed-off-by: Jens Grassel <jens@wegtam.com> Tested-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'filters')
-rwxr-xr-xfilters/show-ics-details.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/filters/show-ics-details.py b/filters/show-ics-details.py
new file mode 100755
index 00000000..bb3ad3d8
--- /dev/null
+++ b/filters/show-ics-details.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python3
+
+"""Parse a vcard file given via stdin and output some details.
+Currently the following details are displayed if present:
+
+- start date and time
+- the summary information of the event
+- a list of attendees
+- the description of the event
+
+Please note: if multiple events are included in the data then only the
+first one will be parsed and displayed!
+
+REQUIREMENTS:
+- Python 3
+- Python 3 - vobject library
+
+To use as a filter in aerc, add the following line to your aerc.config:
+text/calendar=show-ics-details.py
+"""
+
+import re
+import sys
+
+import vobject
+
+
+def remove_mailto(message: str) -> str:
+ """Remove a possible existing 'mailto:' from the given message.
+
+ Keyword arguments:
+ message -- A message string.
+ """
+ return re.sub(r'^mailto:', '', message, flags=re.IGNORECASE)
+
+def extract_field(cal: vobject.icalendar.VCalendar2_0, name: str) -> str:
+ """Extract the desired field from the given calendar object.
+
+ Keyword arguments:
+ cal -- A VCalendar 2.0 object.
+ name -- The field name.
+ """
+ try:
+ match name.strip():
+ case 'attendees':
+ attendees = []
+ for attendee in cal.vevent.attendee_list:
+ attendees.append(remove_mailto(attendee.valueRepr()).strip())
+ return ', '.join(attendees)
+ case 'description':
+ return cal.vevent.description.valueRepr().strip()
+ case 'dtstart':
+ return str(cal.vevent.dtstart.valueRepr()).strip()
+ case 'organizer':
+ return remove_mailto(cal.vevent.organizer.valueRepr()).strip()
+ case 'summary':
+ return cal.vevent.summary.valueRepr().strip()
+ case _:
+ return ''
+ except AttributeError:
+ return ''
+
+attendees = ''
+description = ''
+dtstart = ''
+error = ''
+organizer = ''
+summary = ''
+
+try:
+ cal = vobject.readOne(sys.stdin)
+ attendees = extract_field(cal, 'attendees')
+ description = extract_field(cal, 'description')
+ dtstart = extract_field(cal, 'dtstart')
+ organizer = extract_field(cal, 'organizer')
+ summary = extract_field(cal, 'summary')
+except vobject.base.ParseError:
+ error = '**Sorry, but we could not parse the calendar!**'
+
+if error:
+ print(error)
+ print("")
+
+print(f"Date/Time : {dtstart}")
+print(f"Summary : {summary}")
+print(f"Organizer : {organizer}")
+print(f"Attendees : {attendees}")
+print("")
+print(description)