WIP: mail publish
This commit is contained in:
parent
43a2acb756
commit
3001e57bcc
4 changed files with 130 additions and 0 deletions
102
server/mailserver.go
Normal file
102
server/mailserver.go
Normal file
|
@ -0,0 +1,102 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/emersion/go-smtp"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/mail"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// mailBackend implements SMTP server methods.
|
||||
type mailBackend struct {
|
||||
s *Server
|
||||
}
|
||||
|
||||
func (b *mailBackend) Login(state *smtp.ConnectionState, username, password string) (smtp.Session, error) {
|
||||
return &Session{s: b.s}, nil
|
||||
}
|
||||
|
||||
func (b *mailBackend) AnonymousLogin(state *smtp.ConnectionState) (smtp.Session, error) {
|
||||
return &Session{s: b.s}, nil
|
||||
}
|
||||
|
||||
// Session is returned after EHLO.
|
||||
type Session struct {
|
||||
s *Server
|
||||
from, to string
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (s *Session) AuthPlain(username, password string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) Mail(from string, opts smtp.MailOptions) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.from = from
|
||||
log.Println("Mail from:", from)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) Rcpt(to string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.to = to
|
||||
log.Println("Rcpt to:", to)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) Data(r io.Reader) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
b, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("Data:", string(b))
|
||||
msg, err := mail.ReadMessage(bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body, err := io.ReadAll(msg.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
topic := strings.TrimSuffix(s.to, "@ntfy.sh")
|
||||
url := fmt.Sprintf("%s/%s", s.s.config.BaseURL, topic)
|
||||
req, err := http.NewRequest("PUT", url, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
subject := msg.Header.Get("Subject")
|
||||
if subject != "" {
|
||||
req.Header.Set("Title", subject)
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
s.s.handle(rr, req)
|
||||
if rr.Code != http.StatusOK {
|
||||
return errors.New("error: " + rr.Body.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) Reset() {
|
||||
s.mu.Lock()
|
||||
s.from = ""
|
||||
s.to = ""
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Session) Logout() error {
|
||||
return nil
|
||||
}
|
|
@ -8,6 +8,7 @@ import (
|
|||
firebase "firebase.google.com/go"
|
||||
"firebase.google.com/go/messaging"
|
||||
"fmt"
|
||||
"github.com/emersion/go-smtp"
|
||||
"google.golang.org/api/option"
|
||||
"heckel.io/ntfy/util"
|
||||
"html/template"
|
||||
|
@ -238,10 +239,16 @@ func (s *Server) Run() error {
|
|||
errChan <- s.httpsServer.ListenAndServeTLS(s.config.CertFile, s.config.KeyFile)
|
||||
}()
|
||||
}
|
||||
if true {
|
||||
go func() {
|
||||
errChan <- s.mailserver()
|
||||
}()
|
||||
}
|
||||
s.mu.Unlock()
|
||||
go s.runManager()
|
||||
go s.runAtSender()
|
||||
go s.runFirebaseKeepliver()
|
||||
|
||||
return <-errChan
|
||||
}
|
||||
|
||||
|
@ -722,6 +729,21 @@ func (s *Server) updateStatsAndPrune() {
|
|||
s.messages, len(s.topics), subscribers, messages, len(s.visitors))
|
||||
}
|
||||
|
||||
func (s *Server) mailserver() error {
|
||||
ms := smtp.NewServer(&mailBackend{s})
|
||||
|
||||
ms.Addr = ":1025"
|
||||
ms.Domain = "localhost"
|
||||
ms.ReadTimeout = 10 * time.Second
|
||||
ms.WriteTimeout = 10 * time.Second
|
||||
ms.MaxMessageBytes = 1024 * 1024
|
||||
ms.MaxRecipients = 50
|
||||
ms.AllowInsecureAuth = true
|
||||
|
||||
log.Println("Starting server at", ms.Addr)
|
||||
return ms.ListenAndServe()
|
||||
}
|
||||
|
||||
func (s *Server) runManager() {
|
||||
for {
|
||||
select {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue