Compare commits

...

3 Commits

Author SHA1 Message Date
youshitsune 6eaa6a19d5 Fix errors 2023-06-09 08:32:10 +02:00
youshitsune 3009007e48 Implement listener for new mails 2023-06-09 08:32:10 +02:00
youshitsune c0b220959f Initial commit 2023-06-09 08:32:10 +02:00
2 changed files with 66 additions and 0 deletions

7
config.ini Normal file
View File

@ -0,0 +1,7 @@
[credentials]
USER_MAIL = username
PASS_MAIL = password
URL_MAIL = example.org
PORT_MAIL = 993
TOKEN = token
URL = example1.org

59
emailmastodon Executable file
View File

@ -0,0 +1,59 @@
#!/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 <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)
_, messages = mail.select()
past_messages = int(messages[0].decode())
while True:
ctx = []
_, messages = mail.select()
if past_messages != int(messages[0].decode()):
for i in range(past_messages+1, int(messages[0].decode())+1):
_, msg = mail.fetch(str(i), "(RFC822)")
for resp in msg:
if isinstance(resp, tuple):
msg = email.messages_from_bytes(response[1])
body = msg.get_payload(decode=True).decode()
ctx.append(body)
publish("".join(ctx), TOKEN, URL)
past_messages = int(messages[0].decode())
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')