~coder_kalyan/msync

2b5d909882bd5372b81e26bdae87ddc7cb020d35 — Kalyan Sriram 1 year, 7 months ago c46272f
add basic smtp server
5 files changed, 122 insertions(+), 1 deletions(-)

A cmd/msync/main.go
M go.mod
A go.sum
A server/server.go
A server/smtp.go
A cmd/msync/main.go => cmd/msync/main.go +14 -0
@@ 0,0 1,14 @@
package main

import (
	//"fmt"

	"git.sr.ht/~coder_kalyan/msync/server"
)

func main() {
	srv := server.NewServer()
	srv.Start()

	//fmt.Printf("%+v\n", srv)
}

M go.mod => go.mod +1 -1
@@ 4,5 4,5 @@ go 1.17

require (
	github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 // indirect
	github.com/emersion/go-smtp v0.15.0 // indirect
	github.com/emersion/go-smtp v0.15.1-0.20211006082444-62f6b38f85e4 // indirect
)

A go.sum => go.sum +6 -0
@@ 0,0 1,6 @@
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-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.1-0.20211006082444-62f6b38f85e4 h1:6unG0XYwWUlJjsbYDI06qcRH5Fe0o978bgL8zNydJ8k=
github.com/emersion/go-smtp v0.15.1-0.20211006082444-62f6b38f85e4/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=

A server/server.go => server/server.go +42 -0
@@ 0,0 1,42 @@
package server

import (
	"log"
	"time"

	"github.com/emersion/go-smtp"
)

type Server struct {
	smtp struct {
		backend *Backend
		server *smtp.Server
	}
}

func NewServer() (srv *Server) {
	srv = &Server{}

	srv.smtp.backend = &Backend{}
	srv.smtp.server = smtp.NewServer(srv.smtp.backend)

	// TODO configurable
	srv.smtp.server.Addr = ":1025"
	srv.smtp.server.Domain = "localhost"
	srv.smtp.server.ReadTimeout = 10 * time.Second
	srv.smtp.server.WriteTimeout = 10 * time.Second
	srv.smtp.server.MaxMessageBytes = 1024 * 1024
	srv.smtp.server.MaxRecipients = 50
	srv.smtp.server.AllowInsecureAuth = true

	return
}

func (srv *Server) Start() error {
	log.Println("Starting server at ", srv.smtp.server.Addr)
	if err := srv.smtp.server.ListenAndServe(); err != nil {
		return err
	}

	return nil
}

A server/smtp.go => server/smtp.go +59 -0
@@ 0,0 1,59 @@
package server

import (
	"errors"
	"io"
	"io/ioutil"
	"log"

	"github.com/emersion/go-smtp"
)

type Backend struct{}

func (backend *Backend) NewSession(_ smtp.ConnectionState, _ string) (smtp.Session, error) {
	return &Session{}, nil
}

type Session struct{}

func (s *Session) AuthPlain(username string, password string) error {
	if username != "username" || password != "password" {
		return errors.New("Invalid username or password")
	}

	log.Println("Connection")

	return nil
}

func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
	log.Println("Mail from:", from)

	return nil
}

func (s *Session) Rcpt(to string) error {
	log.Println("Rcpt to:", to)

	return nil
}

func (s *Session) Data(r io.Reader) error {
	if b, err := ioutil.ReadAll(r); err != nil {
		return err
	} else {
		log.Println("Data:", string(b))
	}

	return nil
}

func (s *Session) Reset() {
	log.Println("Reset")
}

func (s *Session) Logout() error {
	return nil
}