Compare commits
53 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14c0ebf1f1 | ||
|
|
af3e23f630 | ||
|
|
691bf31dff | ||
|
|
76bffd931c | ||
|
|
7e92c023c8 | ||
|
|
a73735d849 | ||
|
|
08aa23b397 | ||
|
|
613767a86d | ||
|
|
eb88b74ff7 | ||
|
|
9a121b6ed5 | ||
|
|
d434edd930 | ||
|
|
daeb72aa58 | ||
|
|
a608fffea8 | ||
|
|
31a1399736 | ||
|
|
40f2ec9492 | ||
|
|
59ed33638b | ||
|
|
715ec1ef2a | ||
|
|
47767e2ab2 | ||
|
|
8823867ba5 | ||
|
|
c4e136674a | ||
|
|
d4b6c7bd1f | ||
|
|
d5676ecc07 | ||
|
|
eacdbe587b | ||
|
|
78210e6487 | ||
|
|
ca758f8825 | ||
|
|
321d1da79f | ||
|
|
bac3447db2 | ||
|
|
86890c1f89 | ||
|
|
4c96e6a11f | ||
|
|
4d01579505 | ||
|
|
db135c0cb1 | ||
|
|
bbb6bec35f | ||
|
|
5945ddc8a0 | ||
|
|
2b5095b0b2 | ||
|
|
af1b664274 | ||
|
|
17c8d06a33 | ||
|
|
085cdf5dbf | ||
|
|
1f896d1b26 | ||
|
|
7d435f7ba8 | ||
|
|
2427d41ae3 | ||
|
|
e4c425fb2e | ||
|
|
41f3ad947e | ||
|
|
12a2d4c6f9 | ||
|
|
fda0d62087 | ||
|
|
a92b4c64ae | ||
|
|
61cc9b21c5 | ||
|
|
a3c81ab232 | ||
|
|
ab95fe5d2d | ||
|
|
104e948b9c | ||
|
|
67f504f888 | ||
|
|
bf970fc699 | ||
|
|
4b1ce195b4 | ||
|
|
60d3fbbba5 |
179
README.md
179
README.md
@@ -11,32 +11,47 @@ It can't be used with arbitrary email providers, but setup your own provider "wi
|
|||||||
|
|
||||||
### Receive
|
### Receive
|
||||||
|
|
||||||
- [x] SMTP server
|
- [x] SMTP server (plaintext and SSL)
|
||||||
- [x] Matrix bot
|
- [x] Matrix bot
|
||||||
- [x] Configuration in room's account data
|
- [x] Configuration in room's account data
|
||||||
- [x] Receive emails to matrix rooms
|
- [x] Receive emails to matrix rooms
|
||||||
- [x] Receive attachments
|
- [x] Receive attachments
|
||||||
- [x] Map email threads to matrix threads
|
- [x] Map email threads to matrix threads
|
||||||
|
|
||||||
|
#### deep dive
|
||||||
|
|
||||||
|
> features in that section considered as "nice to have", but not a priority
|
||||||
|
|
||||||
|
- [ ] DKIM verification
|
||||||
|
- [ ] SPF verification
|
||||||
|
- [ ] DMARC verification
|
||||||
|
- [ ] Blocklists
|
||||||
|
|
||||||
### Send
|
### Send
|
||||||
|
|
||||||
- [ ] SMTP client
|
- [x] SMTP client
|
||||||
|
- [x] Send a message to matrix room with special format to send a new email
|
||||||
- [ ] Reply to matrix thread sends reply into email thread
|
- [ ] Reply to matrix thread sends reply into email thread
|
||||||
- [ ] Send a message to matrix room with special format to send a new email
|
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
|
### 1. Bot (mandatory)
|
||||||
|
|
||||||
env vars
|
env vars
|
||||||
|
|
||||||
* **POSTMOOGLE_HOMESERVER** - homeserver url, eg: `https://matrix.example.com`
|
* **POSTMOOGLE_HOMESERVER** - homeserver url, eg: `https://matrix.example.com`
|
||||||
* **POSTMOOGLE_LOGIN** - user login/localpart, eg: `moogle`
|
* **POSTMOOGLE_LOGIN** - user login/localpart, eg: `moogle`
|
||||||
* **POSTMOOGLE_PASSWORD** - user password
|
* **POSTMOOGLE_PASSWORD** - user password
|
||||||
* **POSTMOOGLE_DOMAIN** - SMTP domain to listen for new emails
|
* **POSTMOOGLE_DOMAIN** - SMTP domain to listen for new emails
|
||||||
* **POSTMOOGLE_PORT** - SMTP port to listen for new emails
|
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>other optional config parameters</summary>
|
<summary>other optional config parameters</summary>
|
||||||
|
|
||||||
|
* **POSTMOOGLE_PORT** - SMTP port to listen for new emails
|
||||||
|
* **POSTMOOGLE_TLS_PORT** - secure SMTP port to listen for new emails. Requires valid cert and key as well
|
||||||
|
* **POSTMOOGLE_TLS_CERT** - path to your SSL certificate (chain)
|
||||||
|
* **POSTMOOGLE_TLS_KEY** - path to your SSL certificate's private key
|
||||||
|
* **POSTMOOGLE_TLS_REQUIRED** - require TLS connection, **even** on the non-TLS port (`POSTMOOGLE_PORT`). TLS connections are always required on the TLS port (`POSTMOOGLE_TLS_PORT`) regardless of this setting.
|
||||||
* **POSTMOOGLE_NOENCRYPTION** - disable encryption support
|
* **POSTMOOGLE_NOENCRYPTION** - disable encryption support
|
||||||
* **POSTMOOGLE_STATUSMSG** - presence status message
|
* **POSTMOOGLE_STATUSMSG** - presence status message
|
||||||
* **POSTMOOGLE_SENTRY_DSN** - sentry DSN
|
* **POSTMOOGLE_SENTRY_DSN** - sentry DSN
|
||||||
@@ -45,12 +60,163 @@ env vars
|
|||||||
* **POSTMOOGLE_DB_DIALECT** - database dialect (postgres, sqlite3)
|
* **POSTMOOGLE_DB_DIALECT** - database dialect (postgres, sqlite3)
|
||||||
* **POSTMOOGLE_MAXSIZE** - max email size (including attachments) in megabytes
|
* **POSTMOOGLE_MAXSIZE** - max email size (including attachments) in megabytes
|
||||||
* **POSTMOOGLE_ADMINS** - a space-separated list of admin users. See `POSTMOOGLE_USERS` for syntax examples
|
* **POSTMOOGLE_ADMINS** - a space-separated list of admin users. See `POSTMOOGLE_USERS` for syntax examples
|
||||||
* <s>**POSTMOOGLE_USERS**</s> - deprecated and ignored, use `!pm users` instead
|
|
||||||
|
|
||||||
You can find default values in [config/defaults.go](config/defaults.go)
|
You can find default values in [config/defaults.go](config/defaults.go)
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
|
### 2. DNS (optional)
|
||||||
|
|
||||||
|
The following configuration is needed only if you want to send outgoing emails via Postmoogle (it's not necessary if you only want to receive emails).
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>TL;DR</summary>
|
||||||
|
|
||||||
|
1. Configure DMARC record
|
||||||
|
2. Configure SPF record
|
||||||
|
3. Configure MX record
|
||||||
|
4. Configure DKIM record (use `!pm dkim`)
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
**First**, add a new DMARC DNS record of the `TXT` type for subdomain `_dmarc` with a proper policy. The simplest policy you can use is: `v=DMARC1; p=quarantine;`.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Example</summary>
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ dig txt _dmarc.example.com
|
||||||
|
|
||||||
|
; <<>> DiG 9.18.6 <<>> txt _dmarc.example.com
|
||||||
|
;; global options: +cmd
|
||||||
|
;; Got answer:
|
||||||
|
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57306
|
||||||
|
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
|
||||||
|
|
||||||
|
;; OPT PSEUDOSECTION:
|
||||||
|
; EDNS: version: 0, flags:; udp: 1232
|
||||||
|
;; QUESTION SECTION:
|
||||||
|
;_dmarc.example.com. IN TXT
|
||||||
|
|
||||||
|
;; ANSWER SECTION:
|
||||||
|
_dmarc.example.com. 1799 IN TXT "v=DMARC1; p=quarantine;"
|
||||||
|
|
||||||
|
;; Query time: 46 msec
|
||||||
|
;; SERVER: 1.1.1.1#53(1.1.1.1) (UDP)
|
||||||
|
;; WHEN: Sun Sep 04 21:31:30 EEST 2022
|
||||||
|
;; MSG SIZE rcvd: 79
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
**Second**, add a new SPF DNS record of the `TXT` type for your domain that will be used with Postmoogle, with format: `v=spf1 ip4:SERVER_IP -all` (replace `SERVER_IP` with your server's IP address)
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Example</summary>
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ dig txt example.com
|
||||||
|
|
||||||
|
; <<>> DiG 9.18.6 <<>> txt example.com
|
||||||
|
;; global options: +cmd
|
||||||
|
;; Got answer:
|
||||||
|
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24796
|
||||||
|
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1
|
||||||
|
|
||||||
|
;; OPT PSEUDOSECTION:
|
||||||
|
; EDNS: version: 0, flags:; udp: 1232
|
||||||
|
;; QUESTION SECTION:
|
||||||
|
;example.com. IN TXT
|
||||||
|
|
||||||
|
;; ANSWER SECTION:
|
||||||
|
example.com. 1799 IN TXT "v=spf1 ip4:111.111.111.111 -all"
|
||||||
|
|
||||||
|
;; Query time: 36 msec
|
||||||
|
;; SERVER: 1.1.1.1#53(1.1.1.1) (UDP)
|
||||||
|
;; WHEN: Sun Sep 04 21:35:04 EEST 2022
|
||||||
|
;; MSG SIZE rcvd: 255
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
**Third**, add a new MX DNS record of the `MX` type for your domain that will be used with postmoogle. It should point to the same (sub-)domain.
|
||||||
|
Looks odd, but some mail servers will refuse to interact with your mail server (and Postmoogle is already a mail server) without MX records.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Example</summary>
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dig MX example.com
|
||||||
|
|
||||||
|
; <<>> DiG 9.18.6 <<>> MX example.com
|
||||||
|
;; global options: +cmd
|
||||||
|
;; Got answer:
|
||||||
|
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12688
|
||||||
|
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
|
||||||
|
|
||||||
|
;; OPT PSEUDOSECTION:
|
||||||
|
; EDNS: version: 0, flags:; udp: 1232
|
||||||
|
;; QUESTION SECTION:
|
||||||
|
;example.com. IN MX
|
||||||
|
|
||||||
|
;; ANSWER SECTION:
|
||||||
|
example.com. 1799 IN MX 10 example.com.
|
||||||
|
|
||||||
|
;; Query time: 40 msec
|
||||||
|
;; SERVER: 1.1.1.1#53(1.1.1.1) (UDP)
|
||||||
|
;; WHEN: Tue Sep 06 16:44:47 EEST 2022
|
||||||
|
;; MSG SIZE rcvd: 59
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
**Fourth** (and the last one), add new DKIM DNS record of `TXT` type for subdomain `postmoogle._domainkey` that will be used with postmoogle.
|
||||||
|
|
||||||
|
You can get that signature using the `!pm dkim` command:
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>!pm dkim</summary>
|
||||||
|
|
||||||
|
DKIM signature is: `v=DKIM1; k=rsa; p=MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAxJVqmBHhK9FY93q1o3WEaP2GKMh/3LNMyvi1uSjOKxIyfWv685KxX1EUrbHakQRCTtUM7efKEsXsXBh+DQru2TE32yFpL9afA5BbHj3KePGFY8KJ2m0sQxbQcvn2KjJC0IQ15mk0rninPhtphU/2zLsd6e7Rl1m3L+9Osk320GbfDgSKjRPcSiwVMbLJpSOP0H0F3cIu+c1fHZHfmWy0O+us42C3HTLTlD779LTnQnKlAOQD/+DYYqz6TGGxEwUG2BRQ8O8w7/wXEkg/6a/MxNtPnc59g29CpqRsDkuYiR3UIpqzLDoqHlaoKNbYy34R+4aIjfNpmZyR5kIumws+3MJtJt9UhBTMloqd8lZDPaPmX2NEDqbcSTkHMQrphk+EWSCc7OvbKRaXZ0SyJLpLjxRwKrpeO0JAI0ZpnAFS11uBEe9GSS8uzIIFNYVD1vHloAFKvUJEhyuVyz9/SyqTnArN3ZTiC5cqD1MB86q5QPrKqZfp1dAnv7xAJThL0AP/AgMBAAE=`.
|
||||||
|
You need to add it to your DNS records (if not already):
|
||||||
|
Add new DNS record with type = `TXT`, key (subdomain/from): `postmoogle._domainkey` and value (to):
|
||||||
|
|
||||||
|
```
|
||||||
|
v=DKIM1; k=rsa; p=MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAxJVqmBHhK9FY93q1o3WEaP2GKMh/3LNMyvi1uSjOKxIyfWv685KxX1EUrbHakQRCTtUM7efKEsXsXBh+DQru2TE32yFpL9afA5BbHj3KePGFY8KJ2m0sQxbQcvn2KjJC0IQ15mk0rninPhtphU/2zLsd6e7Rl1m3L+9Osk320GbfDgSKjRPcSiwVMbLJpSOP0H0F3cIu+c1fHZHfmWy0O+us42C3HTLTlD779LTnQnKlAOQD/+DYYqz6TGGxEwUG2BRQ8O8w7/wXEkg/6a/MxNtPnc59g29CpqRsDkuYiR3UIpqzLDoqHlaoKNbYy34R+4aIjfNpmZyR5kIumws+3MJtJt9UhBTMloqd8lZDPaPmX2NEDqbcSTkHMQrphk+EWSCc7OvbKRaXZ0SyJLpLjxRwKrpeO0JAI0ZpnAFS11uBEe9GSS8uzIIFNYVD1vHloAFKvUJEhyuVyz9/SyqTnArN3ZTiC5cqD1MB86q5QPrKqZfp1dAnv7xAJThL0AP/AgMBAAE=
|
||||||
|
```
|
||||||
|
|
||||||
|
Without that record other email servers may reject your emails as spam, kupo.
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Example</summary>
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ dig TXT postmoogle._domainkey.example.com
|
||||||
|
|
||||||
|
; <<>> DiG 9.18.6 <<>> TXT postmoogle._domainkey.example.com
|
||||||
|
;; global options: +cmd
|
||||||
|
;; Got answer:
|
||||||
|
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59014
|
||||||
|
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
|
||||||
|
|
||||||
|
;; OPT PSEUDOSECTION:
|
||||||
|
; EDNS: version: 0, flags:; udp: 1232
|
||||||
|
;; QUESTION SECTION:
|
||||||
|
;postmoogle._domainkey.example.com. IN TXT
|
||||||
|
|
||||||
|
;; ANSWER SECTION:
|
||||||
|
postmoogle._domainkey.example.com. 600 IN TXT "v=DKIM1; k=rsa; p=MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAxJVqmBHhK9FY93q1o3WEaP2GKMh/3LNMyvi1uSjOKxIyfWv685KxX1EUrbHakQRCTtUM7efKEsXsXBh+DQru2TE32yFpL9afA5BbHj3KePGFY8KJ2m0sQxbQcvn2KjJC0IQ15mk0rninPhtphU/2zLsd6e7Rl1m3L+9Osk320GbfDgSKjRPcSiwVMbLJpSOP0H0F3cIu+c1fHZHfmWy0O+us42C3HTLTlD779LTnQnKlAOQD/+DYYqz6TGGxEwUG2BRQ8O8w7/wXEkg/6a/MxNtPnc59g29CpqRsDkuYiR3UIpqzLDoqHlaoKNbYy34R+4aIjfNpmZyR5kIumws+3MJtJt9UhBTMloqd8lZDPaPmX2NEDqbcSTkHMQrphk+EWSCc7OvbKRaXZ0SyJLpLjxRwKrpeO0JAI0ZpnAFS11uBEe9GSS8uzIIFNYVD1vHloAFKvUJEhyuVyz9/SyqTnArN3ZTiC5cqD1MB86q5QPrKqZfp1dAnv7xAJThL0AP/AgMBAAE="
|
||||||
|
|
||||||
|
;; Query time: 90 msec
|
||||||
|
;; SERVER: 1.1.1.1#53(1.1.1.1) (UDP)
|
||||||
|
;; WHEN: Mon Sep 05 16:16:21 EEST 2022
|
||||||
|
;; MSG SIZE rcvd: 525
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
### How to start
|
### How to start
|
||||||
@@ -82,8 +248,9 @@ If you want to change them - check available options in the help message (`!pm h
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
* **!pm mailboxes** - Show the list of all mailboxes
|
* **!pm dkim** - Get DKIM signature
|
||||||
* **!pm users** - Get or set allowed users patterns
|
* **!pm users** - Get or set allowed users patterns
|
||||||
|
* **!pm mailboxes** - Show the list of all mailboxes
|
||||||
* **!pm delete** <mailbox> - Delete specific mailbox
|
* **!pm delete** <mailbox> - Delete specific mailbox
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|||||||
@@ -17,17 +17,24 @@ func parseMXIDpatterns(patterns []string, defaultPattern string) ([]*regexp.Rege
|
|||||||
return utils.WildcardMXIDsToRegexes(patterns)
|
return utils.WildcardMXIDsToRegexes(patterns)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) allowAnyone(actorID id.UserID, targetRoomID id.RoomID) bool {
|
func (b *Bot) allowUsers(actorID id.UserID) bool {
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) allowOwner(actorID id.UserID, targetRoomID id.RoomID) bool {
|
|
||||||
if len(b.allowedUsers) != 0 {
|
if len(b.allowedUsers) != 0 {
|
||||||
if !utils.Match(actorID.String(), b.allowedUsers) {
|
if !utils.Match(actorID.String(), b.allowedUsers) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) allowAnyone(actorID id.UserID, targetRoomID id.RoomID) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) allowOwner(actorID id.UserID, targetRoomID id.RoomID) bool {
|
||||||
|
if !b.allowUsers(actorID) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
cfg, err := b.getRoomSettings(targetRoomID)
|
cfg, err := b.getRoomSettings(targetRoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Error(context.Background(), targetRoomID, "failed to retrieve settings: %v", err)
|
b.Error(context.Background(), targetRoomID, "failed to retrieve settings: %v", err)
|
||||||
@@ -45,3 +52,17 @@ func (b *Bot) allowOwner(actorID id.UserID, targetRoomID id.RoomID) bool {
|
|||||||
func (b *Bot) allowAdmin(actorID id.UserID, targetRoomID id.RoomID) bool {
|
func (b *Bot) allowAdmin(actorID id.UserID, targetRoomID id.RoomID) bool {
|
||||||
return utils.Match(actorID.String(), b.allowedAdmins)
|
return utils.Match(actorID.String(), b.allowedAdmins)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) allowSend(actorID id.UserID, targetRoomID id.RoomID) bool {
|
||||||
|
if !b.allowUsers(actorID) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := b.getRoomSettings(targetRoomID)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(context.Background(), targetRoomID, "failed to retrieve settings: %v", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return !cfg.NoSend()
|
||||||
|
}
|
||||||
|
|||||||
10
bot/bot.go
10
bot/bot.go
@@ -13,6 +13,8 @@ import (
|
|||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
"maunium.net/go/mautrix/format"
|
"maunium.net/go/mautrix/format"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Bot represents matrix bot
|
// Bot represents matrix bot
|
||||||
@@ -25,6 +27,7 @@ type Bot struct {
|
|||||||
rooms sync.Map
|
rooms sync.Map
|
||||||
botcfg cache.Cache[botSettings]
|
botcfg cache.Cache[botSettings]
|
||||||
cfg cache.Cache[roomSettings]
|
cfg cache.Cache[roomSettings]
|
||||||
|
mta utils.MTA
|
||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
lp *linkpearl.Linkpearl
|
lp *linkpearl.Linkpearl
|
||||||
mu map[id.RoomID]*sync.Mutex
|
mu map[id.RoomID]*sync.Mutex
|
||||||
@@ -37,7 +40,6 @@ func New(
|
|||||||
log *logger.Logger,
|
log *logger.Logger,
|
||||||
prefix string,
|
prefix string,
|
||||||
domain string,
|
domain string,
|
||||||
envUsers []string,
|
|
||||||
admins []string,
|
admins []string,
|
||||||
) (*Bot, error) {
|
) (*Bot, error) {
|
||||||
b := &Bot{
|
b := &Bot{
|
||||||
@@ -50,7 +52,7 @@ func New(
|
|||||||
lp: lp,
|
lp: lp,
|
||||||
mu: map[id.RoomID]*sync.Mutex{},
|
mu: map[id.RoomID]*sync.Mutex{},
|
||||||
}
|
}
|
||||||
users, err := b.initBotUsers(envUsers)
|
users, err := b.initBotUsers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -66,7 +68,7 @@ func New(
|
|||||||
}
|
}
|
||||||
b.allowedAdmins = allowedAdmins
|
b.allowedAdmins = allowedAdmins
|
||||||
|
|
||||||
b.commands = b.buildCommandList()
|
b.commands = b.initCommands()
|
||||||
|
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
@@ -78,7 +80,7 @@ func (b *Bot) Error(ctx context.Context, roomID id.RoomID, message string, args
|
|||||||
|
|
||||||
sentry.GetHubFromContext(ctx).CaptureException(err)
|
sentry.GetHubFromContext(ctx).CaptureException(err)
|
||||||
if roomID != "" {
|
if roomID != "" {
|
||||||
b.SendError(ctx, roomID, message)
|
b.SendError(ctx, roomID, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import (
|
|||||||
const (
|
const (
|
||||||
commandHelp = "help"
|
commandHelp = "help"
|
||||||
commandStop = "stop"
|
commandStop = "stop"
|
||||||
|
commandSend = "send"
|
||||||
|
commandDKIM = "dkim"
|
||||||
commandUsers = botOptionUsers
|
commandUsers = botOptionUsers
|
||||||
commandDelete = "delete"
|
commandDelete = "delete"
|
||||||
commandMailboxes = "mailboxes"
|
commandMailboxes = "mailboxes"
|
||||||
@@ -38,7 +40,7 @@ func (c commandList) get(key string) *command {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) buildCommandList() commandList {
|
func (b *Bot) initCommands() commandList {
|
||||||
return commandList{
|
return commandList{
|
||||||
// special commands
|
// special commands
|
||||||
{
|
{
|
||||||
@@ -51,6 +53,11 @@ func (b *Bot) buildCommandList() commandList {
|
|||||||
description: "Disable bridge for the room and clear all configuration",
|
description: "Disable bridge for the room and clear all configuration",
|
||||||
allowed: b.allowOwner,
|
allowed: b.allowOwner,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: commandSend,
|
||||||
|
description: "Send email",
|
||||||
|
allowed: b.allowSend,
|
||||||
|
},
|
||||||
{allowed: b.allowOwner}, // delimiter
|
{allowed: b.allowOwner}, // delimiter
|
||||||
// options commands
|
// options commands
|
||||||
{
|
{
|
||||||
@@ -66,6 +73,15 @@ func (b *Bot) buildCommandList() commandList {
|
|||||||
allowed: b.allowOwner,
|
allowed: b.allowOwner,
|
||||||
},
|
},
|
||||||
{allowed: b.allowOwner}, // delimiter
|
{allowed: b.allowOwner}, // delimiter
|
||||||
|
{
|
||||||
|
key: roomOptionNoSend,
|
||||||
|
description: fmt.Sprintf(
|
||||||
|
"Get or set `%s` of the room (`true` - enable email sending; `false` - disable email sending)",
|
||||||
|
roomOptionNoSend,
|
||||||
|
),
|
||||||
|
sanitizer: utils.SanitizeBoolString,
|
||||||
|
allowed: b.allowOwner,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
key: roomOptionNoSender,
|
key: roomOptionNoSender,
|
||||||
description: fmt.Sprintf(
|
description: fmt.Sprintf(
|
||||||
@@ -117,6 +133,11 @@ func (b *Bot) buildCommandList() commandList {
|
|||||||
description: "Get or set allowed users",
|
description: "Get or set allowed users",
|
||||||
allowed: b.allowAdmin,
|
allowed: b.allowAdmin,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
key: commandDKIM,
|
||||||
|
description: "Get DKIM signature",
|
||||||
|
allowed: b.allowAdmin,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
key: commandMailboxes,
|
key: commandMailboxes,
|
||||||
description: "Show the list of all mailboxes",
|
description: "Show the list of all mailboxes",
|
||||||
@@ -146,6 +167,10 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, commandSlice
|
|||||||
b.sendHelp(ctx)
|
b.sendHelp(ctx)
|
||||||
case commandStop:
|
case commandStop:
|
||||||
b.runStop(ctx)
|
b.runStop(ctx)
|
||||||
|
case commandSend:
|
||||||
|
b.runSend(ctx)
|
||||||
|
case commandDKIM:
|
||||||
|
b.runDKIM(ctx, commandSlice)
|
||||||
case commandUsers:
|
case commandUsers:
|
||||||
b.runUsers(ctx, commandSlice)
|
b.runUsers(ctx, commandSlice)
|
||||||
case commandDelete:
|
case commandDelete:
|
||||||
@@ -157,7 +182,7 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, commandSlice
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) parseCommand(message string) []string {
|
func (b *Bot) parseCommand(message string, toLower bool) []string {
|
||||||
if message == "" {
|
if message == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -167,8 +192,11 @@ func (b *Bot) parseCommand(message string) []string {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
message = strings.ToLower(strings.TrimSpace(strings.Replace(message, b.prefix, "", 1)))
|
message = strings.Replace(message, b.prefix, "", 1)
|
||||||
return strings.Split(message, " ")
|
if toLower {
|
||||||
|
message = strings.ToLower(message)
|
||||||
|
}
|
||||||
|
return strings.Split(strings.TrimSpace(message), " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
|
func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
|
||||||
@@ -181,7 +209,7 @@ func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
|
|||||||
msg.WriteString(b.prefix)
|
msg.WriteString(b.prefix)
|
||||||
msg.WriteString(" ")
|
msg.WriteString(" ")
|
||||||
msg.WriteString(roomOptionMailbox)
|
msg.WriteString(roomOptionMailbox)
|
||||||
msg.WriteString("` command.\n")
|
msg.WriteString(" SOME_INBOX` command.\n")
|
||||||
|
|
||||||
msg.WriteString("You will then be able to send emails to `SOME_INBOX@")
|
msg.WriteString("You will then be able to send emails to `SOME_INBOX@")
|
||||||
msg.WriteString(b.domain)
|
msg.WriteString(b.domain)
|
||||||
@@ -237,3 +265,50 @@ func (b *Bot) sendHelp(ctx context.Context) {
|
|||||||
|
|
||||||
b.SendNotice(ctx, evt.RoomID, msg.String())
|
b.SendNotice(ctx, evt.RoomID, msg.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) runSend(ctx context.Context) {
|
||||||
|
evt := eventFromContext(ctx)
|
||||||
|
if !b.allowSend(evt.Sender, evt.RoomID) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
commandSlice := b.parseCommand(evt.Content.AsMessage().Body, false)
|
||||||
|
to, subject, body, err := utils.ParseSend(commandSlice)
|
||||||
|
if err == utils.ErrInvalidArgs {
|
||||||
|
b.SendNotice(ctx, evt.RoomID, fmt.Sprintf(
|
||||||
|
"Usage:\n"+
|
||||||
|
"```\n"+
|
||||||
|
"%s send someone@example.com\n"+
|
||||||
|
"Subject goes here on a line of its own\n"+
|
||||||
|
"Email content goes here\n"+
|
||||||
|
"on as many lines\n"+
|
||||||
|
"as you want.\n"+
|
||||||
|
"```",
|
||||||
|
b.prefix))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := b.getRoomSettings(evt.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(ctx, evt.RoomID, "failed to retrieve room settings: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mailbox := cfg.Mailbox()
|
||||||
|
if mailbox == "" {
|
||||||
|
b.SendNotice(ctx, evt.RoomID, "mailbox is not configured, kupo")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
from := mailbox + "@" + b.domain
|
||||||
|
ID := fmt.Sprintf("<%s@%s>", evt.ID, b.domain)
|
||||||
|
data := utils.
|
||||||
|
NewEmail(ID, "", subject, from, to, body, "", nil).
|
||||||
|
Compose(b.getBotSettings().DKIMPrivateKey())
|
||||||
|
err = b.mta.Send(from, to, data)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(ctx, evt.RoomID, "cannot send email: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b.SendNotice(ctx, evt.RoomID, "Email has been sent")
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"gitlab.com/etke.cc/go/secgen"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
@@ -130,3 +131,38 @@ func (b *Bot) runUsers(ctx context.Context, commandSlice []string) {
|
|||||||
b.allowedUsers = allowedUsers
|
b.allowedUsers = allowedUsers
|
||||||
b.SendNotice(ctx, evt.RoomID, "allowed users updated")
|
b.SendNotice(ctx, evt.RoomID, "allowed users updated")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) runDKIM(ctx context.Context, commandSlice []string) {
|
||||||
|
evt := eventFromContext(ctx)
|
||||||
|
cfg := b.getBotSettings()
|
||||||
|
if len(commandSlice) > 1 && commandSlice[1] == "reset" {
|
||||||
|
cfg.Set(botOptionDKIMPrivateKey, "")
|
||||||
|
cfg.Set(botOptionDKIMSignature, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
signature := cfg.DKIMSignature()
|
||||||
|
if signature == "" {
|
||||||
|
var private string
|
||||||
|
var derr error
|
||||||
|
signature, private, derr = secgen.DKIM()
|
||||||
|
if derr != nil {
|
||||||
|
b.Error(ctx, evt.RoomID, "cannot generate DKIM signature: %v", derr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cfg.Set(botOptionDKIMSignature, signature)
|
||||||
|
cfg.Set(botOptionDKIMPrivateKey, private)
|
||||||
|
err := b.setBotSettings(cfg)
|
||||||
|
if err != nil {
|
||||||
|
b.Error(ctx, evt.RoomID, "cannot save bot options: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
b.SendNotice(ctx, evt.RoomID, fmt.Sprintf(
|
||||||
|
"DKIM signature is: `%s`.\n"+
|
||||||
|
"You need to add it to your DNS records (if not already):\n"+
|
||||||
|
"Add new DNS record with type = `TXT`, key (subdomain/from): `postmoogle._domainkey` and value (to):\n ```\n%s\n```\n"+
|
||||||
|
"Without that record other email servers may reject your emails as spam, kupo.\n"+
|
||||||
|
"To reset the signature, send `%s dkim reset`",
|
||||||
|
signature, signature, b.prefix))
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,7 +48,10 @@ func (b *Bot) getOption(ctx context.Context, name string) {
|
|||||||
|
|
||||||
value := cfg.Get(name)
|
value := cfg.Get(name)
|
||||||
if value == "" {
|
if value == "" {
|
||||||
b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set, kupo.", name))
|
msg := fmt.Sprintf("`%s` is not set, kupo.\n"+
|
||||||
|
"To set it, send a `%s %s VALUE` command.",
|
||||||
|
name, b.prefix, name)
|
||||||
|
b.SendNotice(ctx, evt.RoomID, msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +59,10 @@ func (b *Bot) getOption(ctx context.Context, name string) {
|
|||||||
value = value + "@" + b.domain
|
value = value + "@" + b.domain
|
||||||
}
|
}
|
||||||
|
|
||||||
b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room is `%s`", name, value))
|
msg := fmt.Sprintf("`%s` of this room is `%s`\n"+
|
||||||
|
"To set it to a new value, send a `%s %s VALUE` command.",
|
||||||
|
name, value, b.prefix, name)
|
||||||
|
b.SendNotice(ctx, evt.RoomID, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) setOption(ctx context.Context, name, value string) {
|
func (b *Bot) setOption(ctx context.Context, name, value string) {
|
||||||
@@ -98,9 +104,5 @@ func (b *Bot) setOption(ctx context.Context, name, value string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if name == roomOptionMailbox {
|
|
||||||
value = value + "@" + b.domain
|
|
||||||
}
|
|
||||||
|
|
||||||
b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room set to `%s`", name, value))
|
b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room set to `%s`", name, value))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ func (b *Bot) syncRooms() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, roomID := range resp.JoinedRooms {
|
for _, roomID := range resp.JoinedRooms {
|
||||||
b.migrateSettings(roomID)
|
|
||||||
cfg, serr := b.getRoomSettings(roomID)
|
cfg, serr := b.getRoomSettings(roomID)
|
||||||
if serr != nil {
|
if serr != nil {
|
||||||
b.log.Warn("cannot get %s settings: %v", roomID, err)
|
b.log.Warn("cannot get %s settings: %v", roomID, err)
|
||||||
|
|||||||
159
bot/email.go
159
bot/email.go
@@ -3,53 +3,32 @@ package bot
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
"maunium.net/go/mautrix/format"
|
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// account data key
|
// account data keys
|
||||||
const acMessagePrefix = "cc.etke.postmoogle.message"
|
const (
|
||||||
|
acMessagePrefix = "cc.etke.postmoogle.message"
|
||||||
|
acLastEventPrefix = "cc.etke.postmoogle.last"
|
||||||
|
)
|
||||||
|
|
||||||
// event keys
|
// event keys
|
||||||
const (
|
const (
|
||||||
eventMessageIDkey = "cc.etke.postmoogle.messageID"
|
eventMessageIDkey = "cc.etke.postmoogle.messageID"
|
||||||
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
|
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
|
||||||
|
eventSubjectKey = "cc.etke.postmoogle.subject"
|
||||||
|
eventFromKey = "cc.etke.postmoogle.from"
|
||||||
)
|
)
|
||||||
|
|
||||||
func email2content(email *utils.Email, cfg roomSettings, threadID id.EventID) *event.Content {
|
// SetMTA sets mail transfer agent instance to the bot
|
||||||
var text strings.Builder
|
func (b *Bot) SetMTA(mta utils.MTA) {
|
||||||
if !cfg.NoSender() {
|
b.mta = mta
|
||||||
text.WriteString("From: ")
|
|
||||||
text.WriteString(email.From)
|
|
||||||
text.WriteString("\n\n")
|
|
||||||
}
|
|
||||||
if !cfg.NoSubject() {
|
|
||||||
text.WriteString("# ")
|
|
||||||
text.WriteString(email.Subject)
|
|
||||||
text.WriteString("\n\n")
|
|
||||||
}
|
|
||||||
if email.HTML != "" && !cfg.NoHTML() {
|
|
||||||
text.WriteString(format.HTMLToMarkdown(email.HTML))
|
|
||||||
} else {
|
|
||||||
text.WriteString(email.Text)
|
|
||||||
}
|
|
||||||
|
|
||||||
parsed := format.RenderMarkdown(text.String(), true, true)
|
|
||||||
parsed.RelatesTo = utils.RelatesTo(cfg.NoThreads(), threadID)
|
|
||||||
|
|
||||||
content := event.Content{
|
|
||||||
Raw: map[string]interface{}{
|
|
||||||
eventMessageIDkey: email.MessageID,
|
|
||||||
eventInReplyToKey: email.InReplyTo,
|
|
||||||
},
|
|
||||||
Parsed: parsed,
|
|
||||||
}
|
|
||||||
return &content
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMapping returns mapping of mailbox = room
|
// GetMapping returns mapping of mailbox = room
|
||||||
@@ -67,7 +46,7 @@ func (b *Bot) GetMapping(mailbox string) (id.RoomID, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send email to matrix room
|
// Send email to matrix room
|
||||||
func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
|
func (b *Bot) Send2Matrix(ctx context.Context, email *utils.Email) error {
|
||||||
roomID, ok := b.GetMapping(utils.Mailbox(email.To))
|
roomID, ok := b.GetMapping(utils.Mailbox(email.To))
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("room not found")
|
return errors.New("room not found")
|
||||||
@@ -87,8 +66,7 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
|
|||||||
b.setThreadID(roomID, email.MessageID, threadID)
|
b.setThreadID(roomID, email.MessageID, threadID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
content := email.Content(threadID, cfg.ContentOptions())
|
||||||
content := email2content(email, cfg, threadID)
|
|
||||||
eventID, serr := b.lp.Send(roomID, content)
|
eventID, serr := b.lp.Send(roomID, content)
|
||||||
if serr != nil {
|
if serr != nil {
|
||||||
return utils.UnwrapError(serr)
|
return utils.UnwrapError(serr)
|
||||||
@@ -98,6 +76,7 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
|
|||||||
b.setThreadID(roomID, email.MessageID, eventID)
|
b.setThreadID(roomID, email.MessageID, eventID)
|
||||||
threadID = eventID
|
threadID = eventID
|
||||||
}
|
}
|
||||||
|
b.setLastEventID(roomID, threadID, eventID)
|
||||||
|
|
||||||
if !cfg.NoFiles() {
|
if !cfg.NoFiles() {
|
||||||
b.sendFiles(ctx, roomID, email.Files, cfg.NoThreads(), threadID)
|
b.sendFiles(ctx, roomID, email.Files, cfg.NoThreads(), threadID)
|
||||||
@@ -105,6 +84,84 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) getParentEmail(evt *event.Event) (string, string, string) {
|
||||||
|
content := evt.Content.AsMessage()
|
||||||
|
parentID := utils.EventParent(evt.ID, content)
|
||||||
|
if parentID == evt.ID {
|
||||||
|
return "", "", ""
|
||||||
|
}
|
||||||
|
parentID = b.getLastEventID(evt.RoomID, parentID)
|
||||||
|
parentEvt, err := b.lp.GetClient().GetEvent(evt.RoomID, parentID)
|
||||||
|
if err != nil {
|
||||||
|
b.log.Error("cannot get parent event: %v", err)
|
||||||
|
return "", "", ""
|
||||||
|
}
|
||||||
|
if parentEvt.Content.Parsed == nil {
|
||||||
|
perr := parentEvt.Content.ParseRaw(event.EventMessage)
|
||||||
|
if perr != nil {
|
||||||
|
b.log.Error("cannot parse event content: %v", perr)
|
||||||
|
return "", "", ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
to := utils.EventField[string](&parentEvt.Content, eventFromKey)
|
||||||
|
inReplyTo := utils.EventField[string](&parentEvt.Content, eventMessageIDkey)
|
||||||
|
if inReplyTo == "" {
|
||||||
|
inReplyTo = parentID.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
subject := utils.EventField[string](&parentEvt.Content, eventSubjectKey)
|
||||||
|
if subject != "" {
|
||||||
|
subject = "Re: " + subject
|
||||||
|
} else {
|
||||||
|
subject = strings.SplitN(content.Body, "\n", 1)[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return to, inReplyTo, subject
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send2Email sends message to email
|
||||||
|
// TODO rewrite to thread replies only
|
||||||
|
func (b *Bot) Send2Email(ctx context.Context, to, subject, body string) error {
|
||||||
|
var inReplyTo string
|
||||||
|
evt := eventFromContext(ctx)
|
||||||
|
cfg, err := b.getRoomSettings(evt.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
mailbox := cfg.Mailbox()
|
||||||
|
if mailbox == "" {
|
||||||
|
return fmt.Errorf("mailbox not configured, kupo")
|
||||||
|
}
|
||||||
|
from := mailbox + "@" + b.domain
|
||||||
|
pTo, pInReplyTo, pSubject := b.getParentEmail(evt)
|
||||||
|
inReplyTo = pInReplyTo
|
||||||
|
if pTo != "" && to == "" {
|
||||||
|
to = pTo
|
||||||
|
}
|
||||||
|
if pSubject != "" && subject == "" {
|
||||||
|
subject = pSubject
|
||||||
|
}
|
||||||
|
|
||||||
|
content := evt.Content.AsMessage()
|
||||||
|
if subject == "" {
|
||||||
|
subject = strings.SplitN(content.Body, "\n", 1)[0]
|
||||||
|
}
|
||||||
|
if body == "" {
|
||||||
|
if content.FormattedBody != "" {
|
||||||
|
body = content.FormattedBody
|
||||||
|
} else {
|
||||||
|
body = content.Body
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ID := evt.ID.String()[1:] + "@" + b.domain
|
||||||
|
data := utils.
|
||||||
|
NewEmail(ID, inReplyTo, subject, from, to, body, "", nil).
|
||||||
|
Compose(b.getBotSettings().DKIMPrivateKey())
|
||||||
|
return b.mta.Send(from, to, data)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) sendFiles(ctx context.Context, roomID id.RoomID, files []*utils.File, noThreads bool, parentID id.EventID) {
|
func (b *Bot) sendFiles(ctx context.Context, roomID id.RoomID, files []*utils.File, noThreads bool, parentID id.EventID) {
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
req := file.Convert()
|
req := file.Convert()
|
||||||
@@ -114,10 +171,10 @@ func (b *Bot) sendFiles(ctx context.Context, roomID id.RoomID, files []*utils.Fi
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
_, err = b.lp.Send(roomID, &event.MessageEventContent{
|
_, err = b.lp.Send(roomID, &event.MessageEventContent{
|
||||||
MsgType: event.MsgFile,
|
MsgType: file.MsgType,
|
||||||
Body: req.FileName,
|
Body: req.FileName,
|
||||||
URL: resp.ContentURI.CUString(),
|
URL: resp.ContentURI.CUString(),
|
||||||
RelatesTo: utils.RelatesTo(noThreads, parentID),
|
RelatesTo: utils.RelatesTo(!noThreads, parentID),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Error(ctx, roomID, "cannot send uploaded file %s: %v", req.FileName, err)
|
b.Error(ctx, roomID, "cannot send uploaded file %s: %v", req.FileName, err)
|
||||||
@@ -152,3 +209,31 @@ func (b *Bot) setThreadID(roomID id.RoomID, messageID string, eventID id.EventID
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) getLastEventID(roomID id.RoomID, threadID id.EventID) id.EventID {
|
||||||
|
key := acLastEventPrefix + "." + threadID.String()
|
||||||
|
data := map[string]id.EventID{}
|
||||||
|
err := b.lp.GetClient().GetRoomAccountData(roomID, key, &data)
|
||||||
|
if err != nil {
|
||||||
|
if !strings.Contains(err.Error(), "M_NOT_FOUND") {
|
||||||
|
b.log.Error("cannot retrieve account data %s: %v", key, err)
|
||||||
|
return threadID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data["eventID"]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) setLastEventID(roomID id.RoomID, threadID id.EventID, eventID id.EventID) {
|
||||||
|
key := acLastEventPrefix + "." + threadID.String()
|
||||||
|
data := map[string]id.EventID{
|
||||||
|
"eventID": eventID,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := b.lp.GetClient().SetRoomAccountData(roomID, key, data)
|
||||||
|
if err != nil {
|
||||||
|
if !strings.Contains(err.Error(), "M_NOT_FOUND") {
|
||||||
|
b.log.Error("cannot save account data %s: %v", key, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ func (b *Bot) handle(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
message := strings.TrimSpace(content.Body)
|
message := strings.TrimSpace(content.Body)
|
||||||
cmd := b.parseCommand(message)
|
cmd := b.parseCommand(message, true)
|
||||||
if cmd == nil {
|
if cmd == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ const acBotSettingsKey = "cc.etke.postmoogle.config"
|
|||||||
// bot options keys
|
// bot options keys
|
||||||
const (
|
const (
|
||||||
botOptionUsers = "users"
|
botOptionUsers = "users"
|
||||||
|
botOptionDKIMSignature = "dkim.pub"
|
||||||
|
botOptionDKIMPrivateKey = "dkim.pem"
|
||||||
)
|
)
|
||||||
|
|
||||||
type botSettings map[string]string
|
type botSettings map[string]string
|
||||||
@@ -40,25 +42,28 @@ func (s botSettings) Users() []string {
|
|||||||
return []string{value}
|
return []string{value}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) initBotUsers(envUsers []string) ([]string, error) {
|
// DKIMSignature (DNS TXT record)
|
||||||
|
func (s botSettings) DKIMSignature() string {
|
||||||
|
return s.Get(botOptionDKIMSignature)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DKIMPrivateKey keep it secret
|
||||||
|
func (s botSettings) DKIMPrivateKey() string {
|
||||||
|
return s.Get(botOptionDKIMPrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) initBotUsers() ([]string, error) {
|
||||||
config := b.getBotSettings()
|
config := b.getBotSettings()
|
||||||
cfgUsers := config.Users()
|
cfgUsers := config.Users()
|
||||||
if len(cfgUsers) > 0 {
|
if len(cfgUsers) > 0 {
|
||||||
// already migrated
|
|
||||||
return cfgUsers, nil
|
return cfgUsers, nil
|
||||||
}
|
}
|
||||||
if len(envUsers) == 0 {
|
|
||||||
_, homeserver, err := b.lp.GetClient().UserID.Parse()
|
_, homeserver, err := b.lp.GetClient().UserID.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.Set(botOptionUsers, "@*:"+homeserver)
|
config.Set(botOptionUsers, "@*:"+homeserver)
|
||||||
} else {
|
|
||||||
// Initialize from environment variable
|
|
||||||
// TODO: remove this migration later and always initialize to `"@*:"+homeserver`
|
|
||||||
config.Set(botOptionUsers, strings.Join(envUsers, " "))
|
|
||||||
}
|
|
||||||
|
|
||||||
return config.Users(), b.setBotSettings(config)
|
return config.Users(), b.setBotSettings(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package bot
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
@@ -16,6 +15,7 @@ const acRoomSettingsKey = "cc.etke.postmoogle.settings"
|
|||||||
const (
|
const (
|
||||||
roomOptionOwner = "owner"
|
roomOptionOwner = "owner"
|
||||||
roomOptionMailbox = "mailbox"
|
roomOptionMailbox = "mailbox"
|
||||||
|
roomOptionNoSend = "nosend"
|
||||||
roomOptionNoSender = "nosender"
|
roomOptionNoSender = "nosender"
|
||||||
roomOptionNoSubject = "nosubject"
|
roomOptionNoSubject = "nosubject"
|
||||||
roomOptionNoHTML = "nohtml"
|
roomOptionNoHTML = "nohtml"
|
||||||
@@ -25,13 +25,6 @@ const (
|
|||||||
|
|
||||||
type roomSettings map[string]string
|
type roomSettings map[string]string
|
||||||
|
|
||||||
// settingsOld of a room
|
|
||||||
type settingsOld struct {
|
|
||||||
Mailbox string
|
|
||||||
Owner id.UserID
|
|
||||||
NoSender bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get option
|
// Get option
|
||||||
func (s roomSettings) Get(key string) string {
|
func (s roomSettings) Get(key string) string {
|
||||||
return s[strings.ToLower(strings.TrimSpace(key))]
|
return s[strings.ToLower(strings.TrimSpace(key))]
|
||||||
@@ -50,6 +43,10 @@ func (s roomSettings) Owner() string {
|
|||||||
return s.Get(roomOptionOwner)
|
return s.Get(roomOptionOwner)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s roomSettings) NoSend() bool {
|
||||||
|
return utils.Bool(s.Get(roomOptionNoSend))
|
||||||
|
}
|
||||||
|
|
||||||
func (s roomSettings) NoSender() bool {
|
func (s roomSettings) NoSender() bool {
|
||||||
return utils.Bool(s.Get(roomOptionNoSender))
|
return utils.Bool(s.Get(roomOptionNoSender))
|
||||||
}
|
}
|
||||||
@@ -70,26 +67,18 @@ func (s roomSettings) NoFiles() bool {
|
|||||||
return utils.Bool(s.Get(roomOptionNoFiles))
|
return utils.Bool(s.Get(roomOptionNoFiles))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove after migration
|
// ContentOptions converts room display settings to content options
|
||||||
func (b *Bot) migrateSettings(roomID id.RoomID) {
|
func (s roomSettings) ContentOptions() *utils.ContentOptions {
|
||||||
var config settingsOld
|
return &utils.ContentOptions{
|
||||||
err := b.lp.GetClient().GetRoomAccountData(roomID, acRoomSettingsKey, &config)
|
HTML: !s.NoHTML(),
|
||||||
if err != nil {
|
Sender: !s.NoSender(),
|
||||||
// any error = no need to migrate
|
Subject: !s.NoSubject(),
|
||||||
return
|
Threads: !s.NoThreads(),
|
||||||
}
|
|
||||||
|
|
||||||
if config.Mailbox == "" {
|
FromKey: eventFromKey,
|
||||||
return
|
SubjectKey: eventSubjectKey,
|
||||||
}
|
MessageIDKey: eventMessageIDkey,
|
||||||
cfg := roomSettings{}
|
InReplyToKey: eventInReplyToKey,
|
||||||
cfg.Set(roomOptionMailbox, config.Mailbox)
|
|
||||||
cfg.Set(roomOptionOwner, config.Owner.String())
|
|
||||||
cfg.Set(roomOptionNoSender, strconv.FormatBool(config.NoSender))
|
|
||||||
|
|
||||||
err = b.setRoomSettings(roomID, cfg)
|
|
||||||
if err != nil {
|
|
||||||
b.log.Error("cannot migrate settings: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +97,9 @@ func (b *Bot) getRoomSettings(roomID id.RoomID) (roomSettings, error) {
|
|||||||
// In such cases, just return a default (empty) settings object.
|
// In such cases, just return a default (empty) settings object.
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
b.cfg.Set(roomID.String(), config)
|
b.cfg.Set(roomID.String(), config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
bot/sync.go
14
bot/sync.go
@@ -5,9 +5,13 @@ import (
|
|||||||
|
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
|
|
||||||
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (b *Bot) initSync() {
|
func (b *Bot) initSync() {
|
||||||
|
b.lp.SetJoinPermit(b.joinPermit)
|
||||||
|
|
||||||
b.lp.OnEventType(
|
b.lp.OnEventType(
|
||||||
event.StateMember,
|
event.StateMember,
|
||||||
func(_ mautrix.EventSource, evt *event.Event) {
|
func(_ mautrix.EventSource, evt *event.Event) {
|
||||||
@@ -26,6 +30,16 @@ func (b *Bot) initSync() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// joinPermit is called by linkpearl when processing "invite" events and deciding if rooms should be auto-joined or not
|
||||||
|
func (b *Bot) joinPermit(evt *event.Event) bool {
|
||||||
|
if !utils.Match(evt.Sender.String(), b.allowedUsers) {
|
||||||
|
b.log.Debug("Rejecting room invitation from unallowed user: %s", evt.Sender)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) onMembership(evt *event.Event) {
|
func (b *Bot) onMembership(evt *event.Event) {
|
||||||
ctx := newContext(evt)
|
ctx := newContext(evt)
|
||||||
|
|
||||||
|
|||||||
22
cmd/cmd.go
22
cmd/cmd.go
@@ -21,6 +21,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
mxb *bot.Bot
|
mxb *bot.Bot
|
||||||
|
smtpserv *smtp.Server
|
||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,11 +39,13 @@ func main() {
|
|||||||
log.Debug("starting internal components...")
|
log.Debug("starting internal components...")
|
||||||
initSentry(cfg)
|
initSentry(cfg)
|
||||||
initBot(cfg)
|
initBot(cfg)
|
||||||
|
initSMTP(cfg)
|
||||||
initShutdown(quit)
|
initShutdown(quit)
|
||||||
defer recovery()
|
defer recovery()
|
||||||
|
|
||||||
go startBot(cfg.StatusMsg)
|
go startBot(cfg.StatusMsg)
|
||||||
if err := smtp.Start(cfg.Domain, cfg.Port, cfg.LogLevel, cfg.MaxSize, mxb); err != nil {
|
|
||||||
|
if err := smtpserv.Start(); err != nil {
|
||||||
//nolint:gocritic
|
//nolint:gocritic
|
||||||
log.Fatal("SMTP server crashed: %v", err)
|
log.Fatal("SMTP server crashed: %v", err)
|
||||||
}
|
}
|
||||||
@@ -83,7 +86,7 @@ func initBot(cfg *config.Config) {
|
|||||||
log.Fatal("cannot initialize matrix bot: %v", err)
|
log.Fatal("cannot initialize matrix bot: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mxb, err = bot.New(lp, mxlog, cfg.Prefix, cfg.Domain, cfg.Users, cfg.Admins)
|
mxb, err = bot.New(lp, mxlog, cfg.Prefix, cfg.Domain, cfg.Admins)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// nolint // Fatal = panic, not os.Exit()
|
// nolint // Fatal = panic, not os.Exit()
|
||||||
log.Fatal("cannot start matrix bot: %v", err)
|
log.Fatal("cannot start matrix bot: %v", err)
|
||||||
@@ -91,6 +94,20 @@ func initBot(cfg *config.Config) {
|
|||||||
log.Debug("bot has been created")
|
log.Debug("bot has been created")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initSMTP(cfg *config.Config) {
|
||||||
|
smtpserv = smtp.NewServer(&smtp.Config{
|
||||||
|
Domain: cfg.Domain,
|
||||||
|
Port: cfg.Port,
|
||||||
|
TLSCert: cfg.TLS.Cert,
|
||||||
|
TLSKey: cfg.TLS.Key,
|
||||||
|
TLSPort: cfg.TLS.Port,
|
||||||
|
TLSRequired: cfg.TLS.Required,
|
||||||
|
LogLevel: cfg.LogLevel,
|
||||||
|
MaxSize: cfg.MaxSize,
|
||||||
|
Bot: mxb,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func initShutdown(quit chan struct{}) {
|
func initShutdown(quit chan struct{}) {
|
||||||
listener := make(chan os.Signal, 1)
|
listener := make(chan os.Signal, 1)
|
||||||
signal.Notify(listener, os.Interrupt, syscall.SIGABRT, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
|
signal.Notify(listener, os.Interrupt, syscall.SIGABRT, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
|
||||||
@@ -114,6 +131,7 @@ func startBot(statusMsg string) {
|
|||||||
|
|
||||||
func shutdown() {
|
func shutdown() {
|
||||||
log.Info("Shutting down...")
|
log.Info("Shutting down...")
|
||||||
|
smtpserv.Stop()
|
||||||
mxb.Stop()
|
mxb.Stop()
|
||||||
|
|
||||||
sentry.Flush(5 * time.Second)
|
sentry.Flush(5 * time.Second)
|
||||||
|
|||||||
@@ -20,8 +20,13 @@ func New() *Config {
|
|||||||
NoEncryption: env.Bool("noencryption"),
|
NoEncryption: env.Bool("noencryption"),
|
||||||
MaxSize: env.Int("maxsize", defaultConfig.MaxSize),
|
MaxSize: env.Int("maxsize", defaultConfig.MaxSize),
|
||||||
StatusMsg: env.String("statusmsg", defaultConfig.StatusMsg),
|
StatusMsg: env.String("statusmsg", defaultConfig.StatusMsg),
|
||||||
Users: env.Slice("users"),
|
|
||||||
Admins: env.Slice("admins"),
|
Admins: env.Slice("admins"),
|
||||||
|
TLS: TLS{
|
||||||
|
Cert: env.String("tls.cert", defaultConfig.TLS.Cert),
|
||||||
|
Key: env.String("tls.key", defaultConfig.TLS.Key),
|
||||||
|
Required: env.Bool("tls.required"),
|
||||||
|
Port: env.String("tls.port", defaultConfig.TLS.Port),
|
||||||
|
},
|
||||||
Sentry: Sentry{
|
Sentry: Sentry{
|
||||||
DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN),
|
DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -11,4 +11,7 @@ var defaultConfig = &Config{
|
|||||||
DSN: "local.db",
|
DSN: "local.db",
|
||||||
Dialect: "sqlite3",
|
Dialect: "sqlite3",
|
||||||
},
|
},
|
||||||
|
TLS: TLS{
|
||||||
|
Port: "587",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,14 +22,15 @@ type Config struct {
|
|||||||
MaxSize int
|
MaxSize int
|
||||||
// StatusMsg of the bot
|
// StatusMsg of the bot
|
||||||
StatusMsg string
|
StatusMsg string
|
||||||
// Users DEPRECATED holds list of allowed users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = homeserver only
|
|
||||||
Users []string
|
|
||||||
// Admins holds list of admin users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = no admins
|
// Admins holds list of admin users (wildcards supported), e.g.: @*:example.com, @bot.*:example.com, @admin:*. Empty = no admins
|
||||||
Admins []string
|
Admins []string
|
||||||
|
|
||||||
// DB config
|
// DB config
|
||||||
DB DB
|
DB DB
|
||||||
|
|
||||||
|
// TLS config
|
||||||
|
TLS TLS
|
||||||
|
|
||||||
// Sentry config
|
// Sentry config
|
||||||
Sentry Sentry
|
Sentry Sentry
|
||||||
}
|
}
|
||||||
@@ -42,6 +43,14 @@ type DB struct {
|
|||||||
Dialect string
|
Dialect string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TLS config
|
||||||
|
type TLS struct {
|
||||||
|
Cert string
|
||||||
|
Key string
|
||||||
|
Port string
|
||||||
|
Required bool
|
||||||
|
}
|
||||||
|
|
||||||
// Sentry config
|
// Sentry config
|
||||||
type Sentry struct {
|
type Sentry struct {
|
||||||
DSN string
|
DSN string
|
||||||
|
|||||||
31
e2e/cert.pem
Normal file
31
e2e/cert.pem
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
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
|
||||||
|
kwoU0mIWLJVQtTelX1zvQVvs4PZa+iAzkniEjb0=
|
||||||
|
-----END CERTIFICATE-----
|
||||||
52
e2e/key.pem
Normal file
52
e2e/key.pem
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
-----BEGIN PRIVATE KEY-----
|
||||||
|
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
|
||||||
|
G9eNyx0/usGECLor9sfMmhy55arHNv8=
|
||||||
|
-----END PRIVATE KEY-----
|
||||||
14
go.mod
14
go.mod
@@ -4,15 +4,18 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
git.sr.ht/~xn/cache/v2 v2.0.0
|
git.sr.ht/~xn/cache/v2 v2.0.0
|
||||||
|
github.com/emersion/go-msgauth v0.6.6
|
||||||
github.com/emersion/go-smtp v0.15.0
|
github.com/emersion/go-smtp v0.15.0
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.1
|
||||||
github.com/getsentry/sentry-go v0.13.0
|
github.com/getsentry/sentry-go v0.13.0
|
||||||
github.com/jhillyerd/enmime v0.10.0
|
github.com/jhillyerd/enmime v0.10.0
|
||||||
github.com/lib/pq v1.10.6
|
github.com/lib/pq v1.10.6
|
||||||
github.com/mattn/go-sqlite3 v1.14.14
|
github.com/mattn/go-sqlite3 v1.14.14
|
||||||
gitlab.com/etke.cc/go/env v1.0.0
|
gitlab.com/etke.cc/go/env v1.0.0
|
||||||
gitlab.com/etke.cc/go/logger v1.1.0
|
gitlab.com/etke.cc/go/logger v1.1.0
|
||||||
gitlab.com/etke.cc/linkpearl v0.0.0-20220826133247-10dcaedb8085
|
gitlab.com/etke.cc/go/secgen v1.1.1
|
||||||
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c
|
gitlab.com/etke.cc/linkpearl v0.0.0-20220831124140-598117f26c77
|
||||||
|
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b
|
||||||
maunium.net/go/mautrix v0.12.0
|
maunium.net/go/mautrix v0.12.0
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,18 +30,19 @@ require (
|
|||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.16 // indirect
|
github.com/mattn/go-isatty v0.0.16 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.12 // indirect
|
github.com/mattn/go-runewidth v0.0.12 // indirect
|
||||||
|
github.com/mikesmitty/edkey v0.0.0-20170222072505-3356ea4e686a // indirect
|
||||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/rivo/uniseg v0.2.0 // indirect
|
github.com/rivo/uniseg v0.2.0 // indirect
|
||||||
github.com/rs/zerolog v1.27.0 // indirect
|
github.com/rs/zerolog v1.28.0 // indirect
|
||||||
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
|
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
|
||||||
github.com/tidwall/gjson v1.14.3 // indirect
|
github.com/tidwall/gjson v1.14.3 // indirect
|
||||||
github.com/tidwall/match v1.1.1 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
github.com/tidwall/pretty v1.2.0 // indirect
|
github.com/tidwall/pretty v1.2.0 // indirect
|
||||||
github.com/tidwall/sjson v1.2.5 // indirect
|
github.com/tidwall/sjson v1.2.5 // indirect
|
||||||
github.com/yuin/goldmark v1.4.12 // indirect
|
github.com/yuin/goldmark v1.4.12 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 // indirect
|
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
|
||||||
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
|
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
|
||||||
golang.org/x/text v0.3.7 // indirect
|
golang.org/x/text v0.3.7 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
maunium.net/go/maulogger/v2 v2.3.2 // indirect
|
maunium.net/go/maulogger/v2 v2.3.2 // indirect
|
||||||
|
|||||||
46
go.sum
46
go.sum
@@ -6,10 +6,20 @@ github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a/go.mod h1:2GxOXO
|
|||||||
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/emersion/go-message v0.11.2/go.mod h1:C4jnca5HOTo4bGN9YdqNQM9sITuT3Y0K6bSUw9RklvY=
|
||||||
|
github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4=
|
||||||
|
github.com/emersion/go-milter v0.3.3/go.mod h1:ablHK0pbLB83kMFBznp/Rj8aV+Kc3jw8cxzzmCNLIOY=
|
||||||
|
github.com/emersion/go-msgauth v0.6.6 h1:buv5lL8v/3v4RpHnQFS2IPhE3nxSRX+AxnrEJbDbHhA=
|
||||||
|
github.com/emersion/go-msgauth v0.6.6/go.mod h1:A+/zaz9bzukLM6tRWRgJ3BdrBi+TFKTvQ3fGMFOI9SM=
|
||||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
|
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
|
||||||
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
|
||||||
github.com/emersion/go-smtp v0.15.0 h1:3+hMGMGrqP/lqd7qoxZc1hTU8LY8gHV9RFGWlqSDmP8=
|
github.com/emersion/go-smtp v0.15.0 h1:3+hMGMGrqP/lqd7qoxZc1hTU8LY8gHV9RFGWlqSDmP8=
|
||||||
github.com/emersion/go-smtp v0.15.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
|
github.com/emersion/go-smtp v0.15.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
|
||||||
|
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
|
||||||
|
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.1 h1:TRWk7se+TOjCYgRth7+1/OYLNiRNIotknkFtf/dnN7Q=
|
||||||
|
github.com/gabriel-vasile/mimetype v1.4.1/go.mod h1:05Vi0w3Y9c/lNvJOdmIwvrrAhX3rYhfQQCaf9VJcv7M=
|
||||||
github.com/getsentry/sentry-go v0.13.0 h1:20dgTiUSfxRB/EhMPtxcL9ZEbM1ZdR+W/7f7NWD+xWo=
|
github.com/getsentry/sentry-go v0.13.0 h1:20dgTiUSfxRB/EhMPtxcL9ZEbM1ZdR+W/7f7NWD+xWo=
|
||||||
github.com/getsentry/sentry-go v0.13.0/go.mod h1:EOsfu5ZdvKPfeHYV6pTVQnsjfp30+XA7//UooKNumH0=
|
github.com/getsentry/sentry-go v0.13.0/go.mod h1:EOsfu5ZdvKPfeHYV6pTVQnsjfp30+XA7//UooKNumH0=
|
||||||
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
|
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
|
||||||
@@ -30,6 +40,7 @@ github.com/jhillyerd/enmime v0.10.0 h1:DZEzhptPRBesvN3gf7K1BOh4rfpqdsdrEoxW1Edr/
|
|||||||
github.com/jhillyerd/enmime v0.10.0/go.mod h1:Qpe8EEemJMFAF8+NZoWdpXvK2Yb9dRF0k/z6mkcDHsA=
|
github.com/jhillyerd/enmime v0.10.0/go.mod h1:Qpe8EEemJMFAF8+NZoWdpXvK2Yb9dRF0k/z6mkcDHsA=
|
||||||
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
|
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
|
||||||
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
github.com/martinlindhe/base36 v1.0.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8=
|
||||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
@@ -41,6 +52,8 @@ github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxm
|
|||||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||||
github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw=
|
github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw=
|
||||||
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||||
|
github.com/mikesmitty/edkey v0.0.0-20170222072505-3356ea4e686a h1:eU8j/ClY2Ty3qdHnn0TyW3ivFoPC/0F1gQZz8yTxbbE=
|
||||||
|
github.com/mikesmitty/edkey v0.0.0-20170222072505-3356ea4e686a/go.mod h1:v8eSC2SMp9/7FTKUncp7fH9IwPfw+ysMObcEz5FWheQ=
|
||||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
||||||
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
|
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
|
||||||
@@ -51,12 +64,13 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
|||||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||||
github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs=
|
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
|
||||||
github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U=
|
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
|
||||||
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
|
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
|
||||||
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
|
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||||
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
@@ -74,21 +88,31 @@ gitlab.com/etke.cc/go/env v1.0.0 h1:J98BwzOuELnjsVPFvz5wa79L7IoRV9CmrS41xLYXtSw=
|
|||||||
gitlab.com/etke.cc/go/env v1.0.0/go.mod h1:e1l4RM5MA1sc0R1w/RBDAESWRwgo5cOG9gx8BKUn2C4=
|
gitlab.com/etke.cc/go/env v1.0.0/go.mod h1:e1l4RM5MA1sc0R1w/RBDAESWRwgo5cOG9gx8BKUn2C4=
|
||||||
gitlab.com/etke.cc/go/logger v1.1.0 h1:Yngp/DDLmJ0jJNLvLXrfan5Gi5QV+r7z6kCczTv8t4U=
|
gitlab.com/etke.cc/go/logger v1.1.0 h1:Yngp/DDLmJ0jJNLvLXrfan5Gi5QV+r7z6kCczTv8t4U=
|
||||||
gitlab.com/etke.cc/go/logger v1.1.0/go.mod h1:8Vw5HFXlZQ5XeqvUs5zan+GnhrQyYtm/xe+yj8H/0zk=
|
gitlab.com/etke.cc/go/logger v1.1.0/go.mod h1:8Vw5HFXlZQ5XeqvUs5zan+GnhrQyYtm/xe+yj8H/0zk=
|
||||||
gitlab.com/etke.cc/linkpearl v0.0.0-20220826133247-10dcaedb8085 h1:Qr6o2ERYCEkZAOhvIl4cwgUOHBn4sHq+yReCMjg8kvE=
|
gitlab.com/etke.cc/go/secgen v1.1.1 h1:RmKOki725HIhWJHzPtAc9X4YvBneczndchpMgoDkE8w=
|
||||||
gitlab.com/etke.cc/linkpearl v0.0.0-20220826133247-10dcaedb8085/go.mod h1:CqwzwxVogKG6gDWTPTen3NyWbTESg42jxoTfXXwDGKQ=
|
gitlab.com/etke.cc/go/secgen v1.1.1/go.mod h1:3pJqRGeWApzx7qXjABqz2o2SMCNpKSZao/gXVdasqE8=
|
||||||
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503 h1:vJ2V3lFLg+bBhgroYuRfyN583UzVveQmIXjc8T/y3to=
|
gitlab.com/etke.cc/linkpearl v0.0.0-20220831124140-598117f26c77 h1:O9t4Sw/nu0JDUX+3KYjaqBi887opyNZ0imE+i2sV+q8=
|
||||||
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
gitlab.com/etke.cc/linkpearl v0.0.0-20220831124140-598117f26c77/go.mod h1:CqwzwxVogKG6gDWTPTen3NyWbTESg42jxoTfXXwDGKQ=
|
||||||
|
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||||
|
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
|
||||||
|
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||||
golang.org/x/net v0.0.0-20210501142056-aec3718b3fa0/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
golang.org/x/net v0.0.0-20210501142056-aec3718b3fa0/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
|
||||||
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c h1:JVAXQ10yGGVbSyoer5VILysz6YKjdNT2bsvlayjqhes=
|
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b h1:ZmngSVLe/wycRns9MKikG9OWIEjGcGAkacif7oYQaUY=
|
||||||
|
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM=
|
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
|
||||||
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
|||||||
33
smtp/msa.go
Normal file
33
smtp/msa.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package smtp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/emersion/go-smtp"
|
||||||
|
"github.com/getsentry/sentry-go"
|
||||||
|
"gitlab.com/etke.cc/go/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// msa is mail submission agent, implements smtp.Backend
|
||||||
|
type msa struct {
|
||||||
|
log *logger.Logger
|
||||||
|
domain string
|
||||||
|
bot Bot
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *msa) newSession() *msasession {
|
||||||
|
return &msasession{
|
||||||
|
ctx: sentry.SetHubOnContext(context.Background(), sentry.CurrentHub().Clone()),
|
||||||
|
log: m.log,
|
||||||
|
bot: m.bot,
|
||||||
|
domain: m.domain,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *msa) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
|
||||||
|
return nil, smtp.ErrAuthUnsupported
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *msa) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
|
||||||
|
return m.newSession(), nil
|
||||||
|
}
|
||||||
@@ -12,24 +12,24 @@ import (
|
|||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type session struct {
|
type msasession struct {
|
||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
|
bot Bot
|
||||||
domain string
|
domain string
|
||||||
client Client
|
|
||||||
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
to string
|
to string
|
||||||
from string
|
from string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) Mail(from string, opts smtp.MailOptions) error {
|
func (s *msasession) Mail(from string, opts smtp.MailOptions) error {
|
||||||
sentry.GetHubFromContext(s.ctx).Scope().SetTag("from", from)
|
sentry.GetHubFromContext(s.ctx).Scope().SetTag("from", from)
|
||||||
s.from = from
|
s.from = from
|
||||||
s.log.Debug("mail from %s, options: %+v", from, opts)
|
s.log.Debug("mail from %s, options: %+v", from, opts)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) Rcpt(to string) error {
|
func (s *msasession) Rcpt(to string) error {
|
||||||
sentry.GetHubFromContext(s.ctx).Scope().SetTag("to", to)
|
sentry.GetHubFromContext(s.ctx).Scope().SetTag("to", to)
|
||||||
|
|
||||||
if utils.Hostname(to) != s.domain {
|
if utils.Hostname(to) != s.domain {
|
||||||
@@ -37,7 +37,7 @@ func (s *session) Rcpt(to string) error {
|
|||||||
return smtp.ErrAuthRequired
|
return smtp.ErrAuthRequired
|
||||||
}
|
}
|
||||||
|
|
||||||
_, ok := s.client.GetMapping(utils.Mailbox(to))
|
_, ok := s.bot.GetMapping(utils.Mailbox(to))
|
||||||
if !ok {
|
if !ok {
|
||||||
s.log.Debug("mapping for %s not found", to)
|
s.log.Debug("mapping for %s not found", to)
|
||||||
return smtp.ErrAuthRequired
|
return smtp.ErrAuthRequired
|
||||||
@@ -48,31 +48,27 @@ func (s *session) Rcpt(to string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) parseAttachments(parts []*enmime.Part) []*utils.File {
|
func (s *msasession) parseAttachments(parts []*enmime.Part) []*utils.File {
|
||||||
files := make([]*utils.File, 0, len(parts))
|
files := make([]*utils.File, 0, len(parts))
|
||||||
for _, attachment := range parts {
|
for _, attachment := range parts {
|
||||||
for _, err := range attachment.Errors {
|
for _, err := range attachment.Errors {
|
||||||
s.log.Warn("attachment error: %v", err)
|
s.log.Warn("attachment error: %v", err)
|
||||||
}
|
}
|
||||||
file := utils.NewFile(attachment.FileName, attachment.ContentType, attachment.Content)
|
file := utils.NewFile(attachment.FileName, attachment.Content)
|
||||||
files = append(files, file)
|
files = append(files, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) Data(r io.Reader) error {
|
func (s *msasession) Data(r io.Reader) error {
|
||||||
parser := enmime.NewParser()
|
parser := enmime.NewParser()
|
||||||
eml, err := parser.ReadEnvelope(r)
|
eml, err := parser.ReadEnvelope(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
attachments := s.parseAttachments(eml.Attachments)
|
files := s.parseAttachments(eml.Attachments)
|
||||||
inlines := s.parseAttachments(eml.Inlines)
|
|
||||||
files := make([]*utils.File, 0, len(attachments)+len(inlines))
|
|
||||||
files = append(files, attachments...)
|
|
||||||
files = append(files, inlines...)
|
|
||||||
|
|
||||||
email := utils.NewEmail(
|
email := utils.NewEmail(
|
||||||
eml.GetHeader("Message-Id"),
|
eml.GetHeader("Message-Id"),
|
||||||
@@ -84,11 +80,11 @@ func (s *session) Data(r io.Reader) error {
|
|||||||
eml.HTML,
|
eml.HTML,
|
||||||
files)
|
files)
|
||||||
|
|
||||||
return s.client.Send(s.ctx, email)
|
return s.bot.Send2Matrix(s.ctx, email)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) Reset() {}
|
func (s *msasession) Reset() {}
|
||||||
|
|
||||||
func (s *session) Logout() error {
|
func (s *msasession) Logout() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
132
smtp/mta.go
Normal file
132
smtp/mta.go
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
package smtp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/smtp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitlab.com/etke.cc/go/logger"
|
||||||
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
|
"gitlab.com/etke.cc/postmoogle/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Bot interface to send emails into matrix
|
||||||
|
type Bot interface {
|
||||||
|
GetMapping(string) (id.RoomID, bool)
|
||||||
|
Send2Matrix(ctx context.Context, email *utils.Email) error
|
||||||
|
SetMTA(mta utils.MTA)
|
||||||
|
}
|
||||||
|
|
||||||
|
// mta is Mail Transfer Agent
|
||||||
|
type mta struct {
|
||||||
|
log *logger.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
// SMTPAddrs priority list
|
||||||
|
var SMTPAddrs = []string{":25", ":587", ":465"}
|
||||||
|
|
||||||
|
func NewMTA(loglevel string) utils.MTA {
|
||||||
|
return &mta{
|
||||||
|
log: logger.New("smtp/mta.", loglevel),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mta) Send(from, to, data string) error {
|
||||||
|
m.log.Debug("Sending email from %s to %s", from, to)
|
||||||
|
conn, err := m.connect(from, to)
|
||||||
|
if err != nil {
|
||||||
|
m.log.Error("cannot connect to SMTP server of %s: %v", to, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
err = conn.Mail(from)
|
||||||
|
if err != nil {
|
||||||
|
m.log.Error("cannot call MAIL command: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = conn.Rcpt(to)
|
||||||
|
if err != nil {
|
||||||
|
m.log.Error("cannot send RCPT command: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var w io.WriteCloser
|
||||||
|
w, err = conn.Data()
|
||||||
|
if err != nil {
|
||||||
|
m.log.Error("cannot send DATA command: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer w.Close()
|
||||||
|
m.log.Debug("sending DATA:\n%s", data)
|
||||||
|
_, err = strings.NewReader(data).WriteTo(w)
|
||||||
|
if err != nil {
|
||||||
|
m.log.Debug("cannot write DATA: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log.Debug("email has been sent")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mta) tryServer(localname, mxhost, addr string) *smtp.Client {
|
||||||
|
m.log.Debug("trying SMTP connection to %s%s", mxhost, addr)
|
||||||
|
conn, err := smtp.Dial(mxhost + addr)
|
||||||
|
if err != nil {
|
||||||
|
m.log.Warn("cannot connect to the %s%s: %v", mxhost, addr, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = conn.Hello(localname)
|
||||||
|
if err != nil {
|
||||||
|
m.log.Warn("cannot call HELLO command of the %s%s: %v", mxhost, addr, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if ok, _ := conn.Extension("STARTTLS"); ok {
|
||||||
|
m.log.Debug("%s supports STARTTLS", mxhost)
|
||||||
|
config := &tls.Config{ServerName: mxhost}
|
||||||
|
err = conn.StartTLS(config)
|
||||||
|
if err != nil {
|
||||||
|
m.log.Warn("STARTTLS connection to the %s failed: %v", mxhost, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mta) connect(from, to string) (*smtp.Client, error) {
|
||||||
|
localname := strings.SplitN(from, "@", 2)[1]
|
||||||
|
hostname := strings.SplitN(to, "@", 2)[1]
|
||||||
|
|
||||||
|
m.log.Debug("performing MX lookup of %s", hostname)
|
||||||
|
mxs, err := net.LookupMX(hostname)
|
||||||
|
if err != nil {
|
||||||
|
m.log.Error("cannot perform MX lookup: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, mx := range mxs {
|
||||||
|
for _, addr := range SMTPAddrs {
|
||||||
|
client := m.tryServer(localname, strings.TrimSuffix(mx.Host, "."), addr)
|
||||||
|
if client != nil {
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there are no MX records, according to https://datatracker.ietf.org/doc/html/rfc5321#section-5.1,
|
||||||
|
// we're supposed to try talking directly to the host.
|
||||||
|
if len(mxs) == 0 {
|
||||||
|
for _, addr := range SMTPAddrs {
|
||||||
|
client := m.tryServer(localname, hostname, addr)
|
||||||
|
if client != nil {
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("target SMTP server not found")
|
||||||
|
}
|
||||||
137
smtp/server.go
137
smtp/server.go
@@ -1,56 +1,125 @@
|
|||||||
package smtp
|
package smtp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/emersion/go-smtp"
|
"github.com/emersion/go-smtp"
|
||||||
"github.com/getsentry/sentry-go"
|
|
||||||
"gitlab.com/etke.cc/go/logger"
|
"gitlab.com/etke.cc/go/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type backend struct {
|
type Config struct {
|
||||||
|
Domain string
|
||||||
|
Port string
|
||||||
|
|
||||||
|
TLSCert string
|
||||||
|
TLSKey string
|
||||||
|
TLSPort string
|
||||||
|
TLSRequired bool
|
||||||
|
|
||||||
|
LogLevel string
|
||||||
|
MaxSize int
|
||||||
|
Bot Bot
|
||||||
|
}
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
domain string
|
msa *smtp.Server
|
||||||
client Client
|
errs chan error
|
||||||
|
|
||||||
|
port string
|
||||||
|
tlsPort string
|
||||||
|
tlsCfg *tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *backend) newSession() *session {
|
// NewServer creates new SMTP server
|
||||||
return &session{
|
func NewServer(cfg *Config) *Server {
|
||||||
ctx: sentry.SetHubOnContext(context.Background(), sentry.CurrentHub().Clone()),
|
log := logger.New("smtp/msa.", cfg.LogLevel)
|
||||||
log: b.log,
|
sender := NewMTA(cfg.LogLevel)
|
||||||
domain: b.domain,
|
receiver := &msa{
|
||||||
client: b.client,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
|
|
||||||
return nil, smtp.ErrAuthUnsupported
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
|
|
||||||
return b.newSession(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Start(domain, port, loglevel string, maxSize int, client Client) error {
|
|
||||||
log := logger.New("smtp.", loglevel)
|
|
||||||
be := &backend{
|
|
||||||
log: log,
|
log: log,
|
||||||
domain: domain,
|
bot: cfg.Bot,
|
||||||
client: client,
|
domain: cfg.Domain,
|
||||||
}
|
}
|
||||||
s := smtp.NewServer(be)
|
receiver.bot.SetMTA(sender)
|
||||||
s.Addr = ":" + port
|
|
||||||
s.Domain = domain
|
s := smtp.NewServer(receiver)
|
||||||
s.AuthDisabled = true
|
s.Domain = cfg.Domain
|
||||||
s.ReadTimeout = 10 * time.Second
|
s.ReadTimeout = 10 * time.Second
|
||||||
s.WriteTimeout = 10 * time.Second
|
s.WriteTimeout = 10 * time.Second
|
||||||
s.MaxMessageBytes = maxSize * 1024 * 1024
|
s.MaxMessageBytes = cfg.MaxSize * 1024 * 1024
|
||||||
|
s.EnableREQUIRETLS = cfg.TLSRequired
|
||||||
if log.GetLevel() == "DEBUG" || log.GetLevel() == "TRACE" {
|
if log.GetLevel() == "DEBUG" || log.GetLevel() == "TRACE" {
|
||||||
s.Debug = os.Stdout
|
s.Debug = os.Stdout
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Starting SMTP server on %s:%s", domain, port)
|
server := &Server{
|
||||||
return s.ListenAndServe()
|
msa: s,
|
||||||
|
log: log,
|
||||||
|
port: cfg.Port,
|
||||||
|
tlsPort: cfg.TLSPort,
|
||||||
|
}
|
||||||
|
server.loadTLSConfig(cfg.TLSCert, cfg.TLSKey)
|
||||||
|
return server
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start SMTP server
|
||||||
|
func (s *Server) Start() error {
|
||||||
|
s.errs = make(chan error, 1)
|
||||||
|
go s.listen(s.port, nil)
|
||||||
|
if s.tlsCfg != nil {
|
||||||
|
go s.listen(s.tlsPort, s.tlsCfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return <-s.errs
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop SMTP server
|
||||||
|
func (s *Server) Stop() {
|
||||||
|
err := s.msa.Close()
|
||||||
|
if err != nil {
|
||||||
|
s.log.Error("cannot stop SMTP server properly: %v", err)
|
||||||
|
}
|
||||||
|
s.log.Info("SMTP server has been stopped")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) listen(port string, tlsCfg *tls.Config) {
|
||||||
|
var l net.Listener
|
||||||
|
var err error
|
||||||
|
if tlsCfg != nil {
|
||||||
|
l, err = tls.Listen("tcp", ":"+port, tlsCfg)
|
||||||
|
} else {
|
||||||
|
l, err = net.Listen("tcp", ":"+port)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
s.log.Error("cannot start listener on %s: %v", port, err)
|
||||||
|
s.errs <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.log.Info("Starting SMTP server on port %s", port)
|
||||||
|
|
||||||
|
err = s.msa.Serve(l)
|
||||||
|
if err != nil {
|
||||||
|
s.log.Error("cannot start SMTP server on %s: %v", port, err)
|
||||||
|
s.errs <- err
|
||||||
|
close(s.errs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) loadTLSConfig(cert, key string) {
|
||||||
|
if cert == "" || key == "" {
|
||||||
|
s.log.Warn("SSL certificate is not provided")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsCert, err := tls.LoadX509KeyPair(cert, key)
|
||||||
|
if err != nil {
|
||||||
|
s.log.Error("cannot load SSL certificate: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.tlsCfg = &tls.Config{Certificates: []tls.Certificate{tlsCert}}
|
||||||
|
s.msa.TLSConfig = s.tlsCfg
|
||||||
}
|
}
|
||||||
|
|||||||
15
smtp/smtp.go
15
smtp/smtp.go
@@ -1,15 +0,0 @@
|
|||||||
package smtp
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"maunium.net/go/mautrix/id"
|
|
||||||
|
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Client interface to send emails
|
|
||||||
type Client interface {
|
|
||||||
GetMapping(string) (id.RoomID, bool)
|
|
||||||
Send(ctx context.Context, email *utils.Email) error
|
|
||||||
}
|
|
||||||
25
utils/command.go
Normal file
25
utils/command.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrInvalidArgs returned when a command's arguments are invalid
|
||||||
|
var ErrInvalidArgs = fmt.Errorf("invalid arguments")
|
||||||
|
|
||||||
|
// ParseSend parses "!pm send" command, returns to, subject, body, err
|
||||||
|
func ParseSend(commandSlice []string) (string, string, string, error) {
|
||||||
|
if len(commandSlice) < 3 {
|
||||||
|
return "", "", "", ErrInvalidArgs
|
||||||
|
}
|
||||||
|
|
||||||
|
message := strings.Join(commandSlice, " ")
|
||||||
|
lines := strings.Split(message, "\n")
|
||||||
|
commandSlice = strings.Split(lines[0], " ")
|
||||||
|
to := commandSlice[1]
|
||||||
|
subject := lines[1]
|
||||||
|
body := strings.Join(lines[2:], "\n")
|
||||||
|
|
||||||
|
return to, subject, body, nil
|
||||||
|
}
|
||||||
137
utils/email.go
137
utils/email.go
@@ -1,7 +1,26 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/emersion/go-msgauth/dkim"
|
||||||
|
"maunium.net/go/mautrix/event"
|
||||||
|
"maunium.net/go/mautrix/format"
|
||||||
|
"maunium.net/go/mautrix/id"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MTA is mail transfer agent
|
||||||
|
type MTA interface {
|
||||||
|
Send(from, to, data string) error
|
||||||
|
}
|
||||||
|
|
||||||
// Email object
|
// Email object
|
||||||
type Email struct {
|
type Email struct {
|
||||||
|
Date string
|
||||||
MessageID string
|
MessageID string
|
||||||
InReplyTo string
|
InReplyTo string
|
||||||
From string
|
From string
|
||||||
@@ -12,9 +31,25 @@ type Email struct {
|
|||||||
Files []*File
|
Files []*File
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ContentOptions represents settings that specify how an email is to be converted to a Matrix message
|
||||||
|
type ContentOptions struct {
|
||||||
|
// On/Off
|
||||||
|
Sender bool
|
||||||
|
Subject bool
|
||||||
|
HTML bool
|
||||||
|
Threads bool
|
||||||
|
|
||||||
|
// Keys
|
||||||
|
MessageIDKey string
|
||||||
|
InReplyToKey string
|
||||||
|
SubjectKey string
|
||||||
|
FromKey string
|
||||||
|
}
|
||||||
|
|
||||||
// NewEmail constructs Email object
|
// NewEmail constructs Email object
|
||||||
func NewEmail(messageID, inReplyTo, subject, from, to, text, html string, files []*File) *Email {
|
func NewEmail(messageID, inReplyTo, subject, from, to, text, html string, files []*File) *Email {
|
||||||
email := &Email{
|
email := &Email{
|
||||||
|
Date: time.Now().UTC().Format(time.RFC1123Z),
|
||||||
MessageID: messageID,
|
MessageID: messageID,
|
||||||
InReplyTo: inReplyTo,
|
InReplyTo: inReplyTo,
|
||||||
From: from,
|
From: from,
|
||||||
@@ -35,3 +70,105 @@ func NewEmail(messageID, inReplyTo, subject, from, to, text, html string, files
|
|||||||
|
|
||||||
return email
|
return email
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Content converts the email object to a Matrix event content
|
||||||
|
func (e *Email) Content(threadID id.EventID, options *ContentOptions) *event.Content {
|
||||||
|
var text strings.Builder
|
||||||
|
if options.Sender {
|
||||||
|
text.WriteString("From: ")
|
||||||
|
text.WriteString(e.From)
|
||||||
|
text.WriteString("\n\n")
|
||||||
|
}
|
||||||
|
if options.Subject {
|
||||||
|
text.WriteString("# ")
|
||||||
|
text.WriteString(e.Subject)
|
||||||
|
text.WriteString("\n\n")
|
||||||
|
}
|
||||||
|
if e.HTML != "" && options.HTML {
|
||||||
|
text.WriteString(format.HTMLToMarkdown(e.HTML))
|
||||||
|
} else {
|
||||||
|
text.WriteString(e.Text)
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed := format.RenderMarkdown(text.String(), true, true)
|
||||||
|
parsed.RelatesTo = RelatesTo(options.Threads, threadID)
|
||||||
|
|
||||||
|
content := event.Content{
|
||||||
|
Raw: map[string]interface{}{
|
||||||
|
options.MessageIDKey: e.MessageID,
|
||||||
|
options.InReplyToKey: e.InReplyTo,
|
||||||
|
options.SubjectKey: e.Subject,
|
||||||
|
options.FromKey: e.From,
|
||||||
|
},
|
||||||
|
Parsed: parsed,
|
||||||
|
}
|
||||||
|
return &content
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compose converts the email object to a string (to be used for delivery via SMTP) and possibly DKIM-signs it
|
||||||
|
func (e *Email) Compose(privkey string) string {
|
||||||
|
var data strings.Builder
|
||||||
|
|
||||||
|
domain := strings.SplitN(e.From, "@", 2)[1]
|
||||||
|
data.WriteString("From: ")
|
||||||
|
data.WriteString(e.From)
|
||||||
|
data.WriteString("\r\n")
|
||||||
|
|
||||||
|
data.WriteString("To: ")
|
||||||
|
data.WriteString(e.To)
|
||||||
|
data.WriteString("\r\n")
|
||||||
|
|
||||||
|
data.WriteString("Message-Id: ")
|
||||||
|
data.WriteString(e.MessageID)
|
||||||
|
data.WriteString("\r\n")
|
||||||
|
|
||||||
|
data.WriteString("Date: ")
|
||||||
|
data.WriteString(e.Date)
|
||||||
|
data.WriteString("\r\n")
|
||||||
|
|
||||||
|
if e.InReplyTo != "" {
|
||||||
|
data.WriteString("In-Reply-To: ")
|
||||||
|
data.WriteString(e.InReplyTo)
|
||||||
|
data.WriteString("\r\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
data.WriteString("Subject: ")
|
||||||
|
data.WriteString(e.Subject)
|
||||||
|
data.WriteString("\r\n")
|
||||||
|
|
||||||
|
data.WriteString("\r\n")
|
||||||
|
|
||||||
|
data.WriteString(e.Text)
|
||||||
|
data.WriteString("\r\n")
|
||||||
|
|
||||||
|
return e.sign(domain, privkey, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Email) sign(domain, privkey string, data strings.Builder) string {
|
||||||
|
if privkey == "" {
|
||||||
|
return data.String()
|
||||||
|
}
|
||||||
|
pemblock, _ := pem.Decode([]byte(privkey))
|
||||||
|
if pemblock == nil {
|
||||||
|
return data.String()
|
||||||
|
}
|
||||||
|
parsedkey, err := x509.ParsePKCS8PrivateKey(pemblock.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return data.String()
|
||||||
|
}
|
||||||
|
signer := parsedkey.(crypto.Signer)
|
||||||
|
|
||||||
|
options := &dkim.SignOptions{
|
||||||
|
Domain: domain,
|
||||||
|
Selector: "postmoogle",
|
||||||
|
Signer: signer,
|
||||||
|
}
|
||||||
|
|
||||||
|
var msg strings.Builder
|
||||||
|
err = dkim.Sign(&msg, strings.NewReader(data.String()), options)
|
||||||
|
if err != nil {
|
||||||
|
return data.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
return msg.String()
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,25 +2,32 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gabriel-vasile/mimetype"
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
|
"maunium.net/go/mautrix/event"
|
||||||
)
|
)
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
Name string
|
Name string
|
||||||
Type string
|
Type string
|
||||||
|
MsgType event.MessageType
|
||||||
Length int
|
Length int
|
||||||
Content []byte
|
Content []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFile(name, contentType string, content []byte) *File {
|
func NewFile(name string, content []byte) *File {
|
||||||
file := &File{
|
file := &File{
|
||||||
Name: name,
|
Name: name,
|
||||||
Type: contentType,
|
|
||||||
Content: content,
|
Content: content,
|
||||||
}
|
}
|
||||||
file.Length = len(content)
|
file.Length = len(content)
|
||||||
|
|
||||||
|
mtype := mimetype.Detect(content)
|
||||||
|
file.Type = mtype.String()
|
||||||
|
file.MsgType = mimeMsgType(file.Type)
|
||||||
|
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,3 +40,23 @@ func (f *File) Convert() mautrix.ReqUploadMedia {
|
|||||||
FileName: f.Name,
|
FileName: f.Name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mimeMsgType(mime string) event.MessageType {
|
||||||
|
if mime == "" {
|
||||||
|
return event.MsgFile
|
||||||
|
}
|
||||||
|
if !strings.Contains(mime, "/") {
|
||||||
|
return event.MsgFile
|
||||||
|
}
|
||||||
|
msection := strings.Split(mime, "/")[0]
|
||||||
|
switch msection {
|
||||||
|
case "image":
|
||||||
|
return event.MsgImage
|
||||||
|
case "video":
|
||||||
|
return event.MsgVideo
|
||||||
|
case "audio":
|
||||||
|
return event.MsgAudio
|
||||||
|
default:
|
||||||
|
return event.MsgFile
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,13 +6,19 @@ import (
|
|||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RelatesTo block of matrix event content
|
// RelatesTo returns relation object of a matrix event (either threads or reply-to)
|
||||||
func RelatesTo(noThreads bool, parentID id.EventID) *event.RelatesTo {
|
func RelatesTo(threads bool, parentID id.EventID) *event.RelatesTo {
|
||||||
if parentID == "" {
|
if parentID == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if noThreads {
|
if threads {
|
||||||
|
return &event.RelatesTo{
|
||||||
|
Type: event.RelThread,
|
||||||
|
EventID: parentID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &event.RelatesTo{
|
return &event.RelatesTo{
|
||||||
InReplyTo: &event.InReplyTo{
|
InReplyTo: &event.InReplyTo{
|
||||||
EventID: parentID,
|
EventID: parentID,
|
||||||
@@ -20,10 +26,43 @@ func RelatesTo(noThreads bool, parentID id.EventID) *event.RelatesTo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &event.RelatesTo{
|
// EventParent returns parent event ID (either from thread or from reply-to relation)
|
||||||
Type: event.RelThread,
|
func EventParent(currentID id.EventID, content *event.MessageEventContent) id.EventID {
|
||||||
EventID: parentID,
|
if content == nil {
|
||||||
|
return currentID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if content.GetRelatesTo() == nil {
|
||||||
|
return currentID
|
||||||
|
}
|
||||||
|
|
||||||
|
threadParent := content.RelatesTo.GetThreadParent()
|
||||||
|
if threadParent != "" {
|
||||||
|
return threadParent
|
||||||
|
}
|
||||||
|
|
||||||
|
replyParent := content.RelatesTo.GetReplyTo()
|
||||||
|
if replyParent != "" {
|
||||||
|
return replyParent
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentID
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventField returns field value from raw event content
|
||||||
|
func EventField[T comparable](content *event.Content, field string) T {
|
||||||
|
var zero T
|
||||||
|
raw := content.Raw[field]
|
||||||
|
if raw == nil {
|
||||||
|
return zero
|
||||||
|
}
|
||||||
|
|
||||||
|
v, ok := raw.(T)
|
||||||
|
if !ok {
|
||||||
|
return zero
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnwrapError tries to unwrap a error into something meaningful, like mautrix.HTTPError or mautrix.RespError
|
// UnwrapError tries to unwrap a error into something meaningful, like mautrix.HTTPError or mautrix.RespError
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ func Mailbox(email string) string {
|
|||||||
if index == -1 {
|
if index == -1 {
|
||||||
return email
|
return email
|
||||||
}
|
}
|
||||||
return email[:strings.LastIndex(email, "@")]
|
return email[:index]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hostname returns hostname part from email address
|
// Hostname returns hostname part from email address
|
||||||
|
|||||||
Reference in New Issue
Block a user