forked from tystuyfzand/gotify-smtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
234 lines (186 loc) · 4.82 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"github.com/emersion/go-smtp"
"github.com/gotify/plugin-api"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"strings"
"sync"
"time"
)
var (
s *smtp.Server
users = make(map[string]*Plugin)
userLock = &sync.RWMutex{}
)
// GetGotifyPluginInfo returns gotify plugin info
func GetGotifyPluginInfo() plugin.Info {
return plugin.Info{
Name: "SMTP",
ModulePath: "github.com/tystuyfzand/gotify-smtp",
Author: "Tyler Stuyfzand",
Website: "https://meow.tf",
}
}
// startServer sets up the SMTP server.
// This is only called once, and uses usernames to authenticate to different users.
func startServer() {
s = smtp.NewServer(&Backend{})
s.Addr = ":1025"
s.Domain = "0.0.0.0"
s.ReadTimeout = 10 * time.Second
s.WriteTimeout = 10 * time.Second
s.MaxMessageBytes = 1024 * 1024
s.MaxRecipients = 50
s.AllowInsecureAuth = true
go s.ListenAndServe()
}
// Plugin is plugin instance
type Plugin struct {
userCtx plugin.UserContext
msgHandler plugin.MessageHandler
}
// SetMessageHandler implements plugin.Messenger
// Invoked during initialization
func (c *Plugin) SetMessageHandler(h plugin.MessageHandler) {
c.msgHandler = h
}
// Enable adds users to the context map which maps to a Plugin.
func (c *Plugin) Enable() error {
userLock.Lock()
users[c.userCtx.Name] = c
userLock.Unlock()
return nil
}
// Disable removes users from the context map.
func (c *Plugin) Disable() error {
userLock.Lock()
delete(users, c.userCtx.Name)
userLock.Unlock()
return nil
}
// NewGotifyPluginInstance creates a plugin instance for a user context.
func NewGotifyPluginInstance(ctx plugin.UserContext) plugin.Plugin {
if s == nil {
startServer()
}
return &Plugin{userCtx: ctx}
}
// The Backend implements SMTP server methods.
type Backend struct {
}
// Login handles a login command with username and password.
func (bkd *Backend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
userLock.RLock()
defer userLock.RUnlock()
if instance, ok := users[username]; ok {
return &Session{instance}, nil
}
return nil, errors.New("user not found")
}
// AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails
func (bkd *Backend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
return nil, smtp.ErrAuthRequired
}
type Session struct {
c *Plugin
}
func (s *Session) Mail(from string, opts smtp.MailOptions) error {
return nil
}
func (s *Session) Rcpt(to string) error {
return nil
}
func (s *Session) Data(r io.Reader) error {
if m, err := mail.ReadMessage(r); err != nil {
return err
} else {
var subject string
if subjectHeader, ok := m.Header["Subject"]; ok && len(subjectHeader) > 0 {
subject = subjectHeader[0]
}
mediaType, params, err := mime.ParseMediaType(m.Header.Get("Content-Type"))
var message string
if err == nil && strings.HasPrefix(mediaType, "multipart/") {
message = ParsePart(m.Body, params["boundary"])
} else {
b, err := ioutil.ReadAll(m.Body)
if err != nil {
return err
}
message = string(b)
}
if s.c != nil && s.c.msgHandler != nil {
s.c.msgHandler.SendMessage(plugin.Message{
Title: subject,
Message: message,
})
}
}
return nil
}
func (s *Session) Reset() {
}
func (s *Session) Logout() error {
return nil
}
// ParsePart will find the first text/plain part from a multipart body.
// Adapted from https://github.com/kirabou/parseMIMEemail.go
func ParsePart(body io.Reader, boundary string) string {
reader := multipart.NewReader(body, boundary)
if reader == nil {
return ""
}
// Go through each of the MIME part of the message Body with NextPart(),
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if err != nil {
fmt.Println("Error going through the MIME parts -", err)
break
}
mediaType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
if err == nil && strings.HasPrefix(mediaType, "multipart/") {
// This is a new multipart to be handled recursively
str := ParsePart(part, params["boundary"])
if str != "" {
return str
}
} else {
if strings.HasPrefix(mediaType, "text/plain") {
b, err := ioutil.ReadAll(part)
if err != nil {
continue
}
encoding := strings.ToLower(part.Header.Get("Content-Transfer-Encoding"))
switch {
case strings.Compare(encoding, "base64") == 0:
b, err = base64.StdEncoding.DecodeString(string(b))
if err != nil {
continue
}
case strings.Compare(encoding, "quoted-printable") == 0:
b, err = ioutil.ReadAll(quotedprintable.NewReader(bytes.NewReader(b)))
if err != nil {
continue
}
}
return string(b)
}
}
}
return ""
}
func main() {
panic("Program must be compiled as a Go plugin")
}