aboutsummaryrefslogtreecommitdiff
path: root/systems/eldiron/ejabberd/warn_xmpp_email.py
blob: d482b43471c3c40dcb9286a48d2cb7ad5bff7b61 (plain) (blame)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!@pythonEnv@/bin/python3

import sys
import json
import slixmpp
import asyncio
import logging
import io

CONFIG = json.load(open("@scriptEnv@", "r"))

def sanitize(string):
    import re
    from unidecode import unidecode
    return re.compile(r"[^-.A-Za-z0-9_]").sub("_", unidecode(string))

def parse_email():
    import email
    from email.header import decode_header

    mail = email.message_from_file(sys.stdin)
    try:
        d = decode_header(mail["subject"])[0]
        if d[1] is not None:
            subject = d[0].decode(d[1])
        else:
            subject = d[0]
    except Exception as e:
        subject = mail["subject"]
    sender = mail["from"]
    recipient = mail["X-Original-To"]

    body = ""
    html = None
    files = {}
    for part in mail.walk():
        if part.get_content_type() == "text/plain":
            body += "\n-------------------\n"
            try:
                body += part.get_payload(decode=True).decode(encoding=part.get_content_charset() or "utf-8")
            except Exception as e:
                body += part.get_payload(decode=False)
        elif part.get_content_type() == "text/html":
            html = part.get_payload(decode=True)
        elif part.get_content_type() != "text/html" and\
                part.get_content_maintype() != "multipart":

            filename = part.get_filename() or "{}.dat".format(part["Content-ID"])
            files[sanitize(filename)] = (part.get_content_type(), part.get_payload(decode=True))

    return [body, html, subject, sender, recipient, files]

[body, html, subject, sender, recipient, files] = parse_email()

class Bot(slixmpp.ClientXMPP):
    def __init__(self, jid, password, body, html, subject, sender, recipient, files):
        super().__init__(jid, password)

        self.got_error = False
        self.body = body
        self.html = html
        self.subject = subject
        self.sender = sender
        self.recipient = recipient
        self.files = files
        self.register_plugin('xep_0363')
        self.add_event_handler("session_start", self.session_start)
        self.add_event_handler("message", self.message)

    @asyncio.coroutine
    def session_start(self, event):
        files = []
        if self.html is not None:
            url = yield from self['xep_0363'].upload_file(
                    "mail.html",
                    content_type="text/html",
                    input_file=io.BytesIO(self.html))
            files.append(("HTML version", url))
        for f in self.files:
            url = yield from self['xep_0363'].upload_file(
                    f,
                    content_type=self.files[f][0],
                    input_file=io.BytesIO(self.files[f][1])
                    )
            files.append((f, url))

        text = """
New e-mail message from {sender}
Subject: {subject}
{body}
""".format(sender=self.sender, subject=self.subject, body=self.body)
        if len(files) > 0:
            text += "\n\nAttachments:"
        for f in files:
            text += "\n{}: {}".format(f[0], f[1])
        self.send_message(mto=self.recipient, mbody=text, msubject=self.subject, mtype='message')
        yield from asyncio.sleep(5)
        self.disconnect()

    @asyncio.coroutine
    def message(self, msg):
        if msg["type"] == "error":
            self.got_error = True

logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s')
xmpp = Bot(CONFIG["jid"], CONFIG["password"], body, html, subject, sender, recipient, files)
xmpp.connect()
xmpp.process(forever=False)
if xmpp.got_error:
    sys.exit(1)
else:
    sys.exit(0)