Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple channels, Attachments and Docker #9

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: ci

on:
schedule:
- cron: "0 10 * * *"
push:
tags:
- "v*.*"

jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
# list of Docker images to use as base name for tags
images: |
${{ secrets.DOCKER_HUB_USERNAME }}/discord-smtp
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app/.env
config.ini
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the repo shouldn't include IDE configuration files. I'd delete this file and add .vscode to the gitignore

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Events",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/app/discord_relay.py",
"console": "integratedTerminal"
}
]
}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM python:3
FROM python:3.7.16-slim

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .
COPY app .

CMD [ "python", "./discord_relay.py" ]
84 changes: 81 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,101 @@ It currently only allows one username/password combination, which are set by env

This is designed for use internally only, specifically within a Docker network. I would not recommend exposing this to the internet (or any untrusted network) in its current form, especially if not using TLS.

It uses a discord bot for connecting to discord. This allows for the bot to talk to multiple channels, rather than via a single web hook. These instructions were used in the creation of a bot and obtain the DISCORD_TOKEN - https://realpython.com/how-to-make-a-discord-bot-python/

# Environment variables
| Variable | Description | Example |
|-------------|----------------------|--------------------------------------------------|
| WEBHOOK_URL | Discord webhook URL. | `https://discord.com/api/webhooks/xxxxxx/yyyyyy` |
| DISCORD_TOKEN | Discord bot token | kj4234234jh2k4242k42434234 |
| SMTP_USERNAME | Accepted username for SMTP | `testuser` |
| SMTP_PASSWORD | Accepted password for SMTP | `testpass` |
| TLS_CERT_CHAIN | File path to full TLS certificate chain | `fullchain.pem` |
| TLS_KEY | File path to private key for certificate | `privkey.pem` |

# Running
# Configuration File

The configuration file is used for some configuration information. SMTP_USERNAME, SMTP_PASSWORD can be otained from configuration or environment variable. If set in both, the environment variable will take precendence.

## Example configuration

```
[smtp]
#username=
#password=
port=2525

[default]
# Create a discord channels, and copy the channel ID to this default section.
# if this section is missing it will fail to start
# for subsequent sections, is no channel ID is specified it will default to this ID.
channelid=<discord channel id>

[camera]
#emails from an account called camera@any domain
channelid=<discord channel id>
from=camera@.+

[test-machine]
#emails to an account called test@any domain
channelid=<discord channel id>
to=test@.+

[other]
#emails with test in the subject
subject=.+test.?
```

## Configuration Notes

SMTP Port. To set the port internally use a `port` option in the `[smtp]` section.

`default` section must contain a `channelid` - this channel id is used if any of the filters do not have their own channel id.

All other sections can be named for your own reference, they should contain at least one filter and optionally, a channel id.

The filters a python format regex and can be filtered on the `to` email address, `from` address or the `subject`. When any of these filters match, they will return the assigned channel id for posting the message.

# Running Command Line
1. Install the requirements:
```
pip install -r requirements.txt
```
2. Set the environment variables as desired

3. Run the server:
3. Set the configuration file `config.ini` in the directory with `discord_relay.py`

4. Run the server:
```
python discord_relay.py
```

# Running Docker

## Docker Compose

```
version: '3'
# compose file for discord-smtp
services:
discord-smtp:
container_name: discord-smtp
image: registry.internal.andc.nz/discord-smtp
restart: always
environment:
- PYTHONUNBUFFERED=1
#- SMTP_USERNAME=${SMTP_USERNAME}
#- SMTP_PASSWORD=${SMTP_PASSWORD}
- DISCORD_TOKEN=${DISCORD_TOKEN}
volumes:
- $PWD/config.ini:/conf/config.ini
ports:
- "2525:2525"
```

The above compose file has authentication turned off, left in the env variables to show the format.
The DISCORD_TOKEN is passed in from a .env file where the format is:
DISCORD_TOKEN='token from discord bot'

The config.ini file is being passed in directly as well.

Having `PYTHONUNBUFFERED` set to 1 will show any console messages when running detached (logging0)
Loading