From 3009007e488378029bcf6a5b805f2d8866e653e9 Mon Sep 17 00:00:00 2001 From: youshitsune Date: Fri, 9 Jun 2023 08:02:36 +0200 Subject: [PATCH] Implement listener for new mails --- emailmastodon | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/emailmastodon b/emailmastodon index 614c482..a4e212c 100755 --- a/emailmastodon +++ b/emailmastodon @@ -1,4 +1,70 @@ #!/usr/bin/env python3 +# Bot that mirrors emails to Mastodon account +import argparse import configparser +import imaplib +from mastodon import Mastodon CONFIG_PATH = "./config.ini" + +VERSION = '0.1.0' + +def show_version(): + print("EmailMastodon " + VERSION) + print("License GPLv3+: GNU GPL version 3 or later ") + print("This is free software: you are free to change and redistribute it.") + print("There is NO WARRANTY, to the extent permitted by law.") + +def publish(ctx, token, url): + pass + +def listen(user, passwd, url, port): + mail = imaplib.IMAP4_SSL(url, port=port) + mail.login(user, passwd) + mail.select("INBOX") + status, messages = mail.select() + past_messages = messages + while True: + ctx = [] + status, messages = mail.select() + if past_messages != messages: + for i in range(past_messages+1, messages+1): + res, msg = mail.fetch(str(i), "(RFC822)") + for resp in msg: + if isinstance(resp, tuple): + msg = email.messages_from_bytes(response[1]) + for part in msg.walk(): + try: + body = part.get_payload(decode=True).decode() + except Exception: + pass + else: + ctx.append(body) + + publish("".join(ctx), TOKEN, URL) + past_messages = messages + + + + else: + continue + + + +if __name__ == '__main__': + argparser = argparse.ArgumentParser(description="Bot that mirrors emails to Mastodon account") + argparser.add_argument("--version", dest="version", help="print version information and exit", action="store_true") + args = argparser.parse_args() + + if args.version: + show_version() + exit() + + config = configparser.ConfigParser() + config.read(CONFIG_PATH) + USER_MAIL = config.get('credentials', 'USER_MAIL') + PASS_MAIL = config.get('credentials', 'PASS_MAIL') + URL_MAIL = config.get('credentials', 'URL_MAIL') + PORT_MAIL = config.get('credentials', 'PORT_MAIL') + TOKEN = config.get('credentials', 'TOKEN') + URL = config.get('credentials', 'URL')