@@ -0,0 +1,2482 @@
+import email%0Aimport imaplib%0Aimport os%0Aimport sys%0A%0A%0Aclass EmailProcessor():%0A    mail = None%0A%0A    def __init__(self, user, password, host='mail.mega.co.nz', port=993):%0A        self.mail = imaplib.IMAP4_SSL(host, port)%0A        self.mail.login(user, password)%0A        self.mail.select('Inbox')%0A%0A    def get_validation_link_from_email(self, to, intent, delete=False):%0A        %22%22%22Get validation link from email.%22%22%22%0A        link = None%0A        messages = self.get_message_from_email(to)%0A        if not messages:%0A            return None%0A        for message in messages:%0A            sub = list(message)%5B0%5D%0A            text = message%5Bsub%5D%0A            if not text%5B1%5D:%0A                continue%0A            for line in text%5B1%5D.splitlines():%0A                if line.startswith('https://') and ('#' + intent) in line:%0A                    link = line.strip()%0A                    break%0A            if link:  # already found it, quit%0A                #if delete:%0A                    # This should move the email to Trash! But it doesn't work%0A                    #self.mail.store(text%5B0%5D, '+X-GM-LABELS', '%5C%5CTrash')%0A                break%0A        self.mail.close()%0A        self.mail.logout()%0A        return link%0A%0A    def get_message_from_email(self, to):%0A        %22%22%22Get message from email.%22%22%22%0A%0A        # sort all emails sent to the right address, newest first%0A        result, data = self.mail.sort('REVERSE DATE', 'UTF-8', '(To %22%7B%7D%22)'.format(to))%0A        if len(data%5B0%5D) == 0:%0A            return None%0A%0A        # get all potential emails%0A        messages = %5B%5D%0A        for n in data%5B0%5D.split():%0A            _, d = self.mail.fetch(n, %22(RFC822)%22)%0A            body = d%5B0%5D%5B1%5D%0A            msg = email.message_from_string(body.decode('utf-8'))%0A            subject = msg%5B'subject'%5D%0A            text = None%0A            for m in msg.walk():%0A                content_type = m.get_content_type()%0A                if content_type == 'text/plain':%0A                    text = m.get_payload(decode=True).decode('raw-unicode-escape')%0A                    break%0A            if text:%0A                messages.append(%7Bsubject: %5Bn, text%5D%7D)%0A        return messages%0A%0Auser = os.getenv('TEST_USER') or sys.argv%5B1%5D%0Apassword = os.getenv('TEST_PASS') or sys.argv%5B2%5D%0Ato = sys.argv%5B3%5D%0Aintent = %22%22%0Aif sys.argv%5B4%5D == %22confirm%22:%0A    intent = %22confirm%22%0Aelif sys.argv%5B4%5D == %22delete%22:%0A    intent = %22cancel%22%0Aep = EmailProcessor(user, password)%0Alink = ep.get_validation_link_from_email(to, intent, delete=True)%0Aif link:%0A    print(link, end = '')

Add Python script used to retrieve confirmation links from emails