Last active
October 25, 2023 08:39
-
-
Save monperrus/111d7d62702a1120ed931ed117cdaabf to your computer and use it in GitHub Desktop.
detect calendar email responses for evolution filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
detect calendar email responses for evolution filter | |
returns Unix code 1 if is a calendar answer | |
Martin Monperrus | |
Oct 2023 | |
""" | |
from datetime import datetime, timedelta | |
import json | |
import itertools | |
import os.path | |
import time | |
import ics | |
import base64 | |
import binascii | |
import sys | |
from email import message_from_bytes | |
def action_if_calendar_reply_found(): | |
sys.exit(2) | |
mime_message = message_from_bytes(sys.stdin.buffer.read()) | |
contenttype = mime_message.get_content_type() | |
#print(contenttype) | |
if "text/calendar" in contenttype.lower() and "method=reply" in contenttype.lower(): | |
action_if_calendar_reply_found() | |
if mime_message.get_content_type() == "multipart/alternative" or mime_message.get_content_type() == "multipart/mixed": | |
for part in mime_message.get_payload(): | |
#try: | |
#print(part.get_content_type()) | |
if part.get_content_type() == "text/calendar" or part.get_content_type() == "application/ics" : | |
contenttype= [x for x in part._headers if x[0].lower()=="content-type"][0][1] | |
encoding= [x for x in part._headers if x[0].lower()=="content-transfer-encoding"][0][1] | |
calendar_data = part.get_payload() | |
if encoding.lower() == 'base64': | |
calendar_data = base64.b64decode(calendar_data).decode('utf-8') | |
#print(dir(part)) | |
#print(calendar_data.decode('utf-8')) | |
# some MUAs add method=REPLY, that's good! | |
if "method=REPLY".lower() in contenttype.lower(): | |
print("multipart/method=REPLY") | |
action_if_calendar_reply_found() | |
else: | |
# but some MUAs don't | |
calendar = ics.Calendar(calendar_data) | |
event = [x for x in calendar.events][0] | |
#print(calendar.method.lower()) | |
if calendar.method.lower() == "REPLY".lower(): | |
#print(calendar_data.decode('utf-8')) | |
print("multipart/calendar.method.lower()") | |
action_if_calendar_reply_found() | |
#except Exception as e: print(e) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment