#!@python@/bin/python import json import sys import subprocess from io import BytesIO from email.message import EmailMessage import configparser from os import environ from dateutil.parser import isoparse def main(): notmuchConfig = configparser.ConfigParser() notmuchConfig.read(environ.get('NOTMUCH_CONFIG')) callbackMessage = json.load(sys.stdin) msg = EmailMessage() authors = ', '.join(map(lambda author: author['name'], callbackMessage['feed_item']['authors'])) msg['From'] = f"{callbackMessage['feed_definition']['title']} ({authors}) " msg['To'] = f"{notmuchConfig['user']['name']} <{notmuchConfig['user']['primary_email']}>" msg['Subject'] = callbackMessage['feed_item']['title'] msg['Item-Identifier'] = f"{callbackMessage['feed_item']['identifier']}@imm.invalid" for link in callbackMessage['feed_item']['links']: msg.add_header('Link', link['uri']) if 'date' in callbackMessage['feed_item']: date = isoparse(callbackMessage['feed_item']['date']) msg['Date'] = date.strftime('%a, %e %b %Y %T %z') subprocess.run( args=['notmuch', 'insert'], check=True, input=bytes(msg) ) if __name__ == '__main__': sys.exit(main())