~nova/fletcher

ref: c1acd163f33f54a9b253f908f8387cb0c0a07fa7 fletcher/utils/inbox2discord.py -rw-r--r-- 2.8 KiB
c1acd163 — Novalinium muple 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import imaplib
import email
from email.header import decode_header
import configparser
import discord
import os
import sys

FLETCHER_CONFIG = os.getenv('FLETCHER_CONFIG', './.fletcherrc')

config = configparser.ConfigParser()
config.read(FLETCHER_CONFIG)
imap = imaplib.IMAP4_SSL("localhost")
imap.login(config['imap']['username'], config['imap']['password'])
status, messages = imap.select("INBOX")
message_len = int(messages[0].decode('utf8'))
notifications = []
if not message_len:
    sys.exit(0)
for i in range(message_len, 0, -1):
    # fetch the email message by ID
    res, msg = imap.fetch(str(i), "(RFC822)")
    for response in msg:
        if isinstance(response, tuple):
            # parse a bytes email into a message object
            msg = email.message_from_bytes(response[1])
            # decode the email subject
            subject, encoding = decode_header(msg["Subject"])[0]
            if isinstance(subject, bytes):
                # if it's a bytes, decode to str
                subject = subject.decode(encoding)
            # decode email sender
            From, encoding = decode_header(msg.get("From"))[0]
            if isinstance(From, bytes):
                From = From.decode(encoding)
            if From != "Glowfic Constellation <glowfic.constellation@gmail.com>":
                continue
            To = decode_header(msg.get("To"))[0][0]
            if "+" not in To:
                imap.store(str(i), '+FLAGS', '\\Deleted')
                continue
            for part in msg.walk():
                # extract content type of email
                content_type = part.get_content_type()
                content_disposition = str(part.get("Content-Disposition"))
                try:
                    # get the email body
                    body = part.get_payload(decode=True).decode()
                except:
                    pass
                if content_type == "text/plain" and "attachment" not in content_disposition:
                    # print text/plain emails and skip attachments
                    url = "http"+body.split("http")[1].split("\r\n\r\n")[0]
                    notifications.append((int(To.split("+")[1].split("@")[0]), f"{subject}\n{url}"))
                    imap.store(str(i), '+FLAGS', '\\Deleted')
imap.expunge()
imap.close()
imap.logout()
# print(notifications)
# sys.exit(0)

client = discord.Client(chunk_guilds_at_startup=None, guild_subscriptions=False, intents=None)
# token from https://discordapp.com/developers
token = config['discord']['botToken']

Ans = None

@client.event
async def on_ready():
    for notification in notifications:
        try:
            await (await client.fetch_user(notification[0])).send(notification[1])
        except Exception as e:
            print(e)
    await client.close()
    pass

client.run(token)