Implement listener for new mails

This commit is contained in:
youshitsune 2023-06-09 08:02:36 +02:00
parent c0b220959f
commit 3009007e48

View File

@ -1,4 +1,70 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Bot that mirrors emails to Mastodon account
import argparse
import configparser import configparser
import imaplib
from mastodon import Mastodon
CONFIG_PATH = "./config.ini" CONFIG_PATH = "./config.ini"
VERSION = '0.1.0'
def show_version():
print("EmailMastodon " + VERSION)
print("License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>")
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')