initial commit
ben/matrix-smtp-webhook/pipeline/head This commit looks good Details

main
commit 5b58fc0552

1
.gitignore vendored

@ -0,0 +1 @@
*.conf

@ -0,0 +1,15 @@
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y python3-pip
RUN python3 -m pip install requests aiosmtpd loguru
RUN apt-get clean
COPY smtpd.py /opt/smtpd.py
ENV MATRIX_WEBHOOK_URL "https://matrix.sudo.is/_webhook/incoming"
WORKDIR /opt
USER nobody
ENTRYPOINT ["python3", "-m", "aiosmtpd", "-n", "-c", "smtpd.WebhookHandler", "-l", "localhost:8025"]

30
Jenkinsfile vendored

@ -0,0 +1,30 @@
pipeline {
agent any
triggers {
cron "H 23 * * *"
}
options {
timestamps()
ansiColor("xterm")
disableConcurrentBuilds()
}
stages {
stage('build') {
steps {
sh "./build.sh"
}
}
stage('push') {
steps {
sh "./push.sh"
}
}
}
post {
cleanup {
cleanWs(deleteDirs: true,
disableDeferredWipeout: true,
notFailBuild: true)
}
}
}

@ -0,0 +1,5 @@
#!/bin/sh
set -e
docker build --network bridgewithdns -t matrix-smtp-webhook:latest .

@ -0,0 +1,13 @@
#!/bin/sh
set -e
export REGISTRY=dockreg.sudo.is
export IMAGE=matrix-smtp-webhook:latest
echo "${REGISTRY}/${IMAGE}"
sleep 2
docker tag ${IMAGE} ${REGISTRY}/${IMAGE}
docker push ${REGISTRY}/${IMAGE}

@ -0,0 +1,44 @@
import os
import asyncio
from aiosmtpd.controller import Controller
import requests
from loguru import logger
def matrix_msg(msg, room=None):
url = os.environ['MATRIX_WEBHOOK_URL']
if room is None:
room = os.environ['MATRIX_WEBHOOK_ROOM']
payload = {
'token': os.environ['MATRIX_WEBHOOK_TOKEN'],
'text': msg,
'room': room
}
r = requests.post(url, json=payload)
if r.status_code != 200:
logger.warning(f"status: {r.status_code}, response: {r.text}")
class WebhookHandler:
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
envelope.rcpt_tos.append(address)
return '250 OK'
async def handle_DATA(self, server, session, envelope):
c = envelope.content.decode('utf8', errors='replace').splitlines()
content = [f" {a.strip()}" for a in c]
msg_from = envelope.mail_from
msg_to = envelope.rcpt_tos
logger.success(f"from: {msg_from}, to: {msg_to}")
msg = "\n".join([
f"*From**: `{msg_from}`",
f"**To**: `{msg_to}`",
"".join(content)
])
matrix_msg(msg)
print('End of message')
return '250 Message accepted for delivery'
Loading…
Cancel
Save