zangtumb/session.go

73 lines
1.3 KiB
Go
Raw Normal View History

2020-10-08 16:36:17 -04:00
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"strings"
"time"
"github.com/amalfra/maildir"
)
// A Session is returned after successful login.
type Session struct {
MailFrom string
RcptTo string
LocalUser string
MailDomain string
Valid bool
}
func (s *Session) Rcpt(to string) error {
log.Println("Rcpt to:", to)
if !SmtpBackend.CheckValidRcpt(to) {
time.Sleep(10 * time.Second)
return errors.New("YU NO MAIL!!! WE NU RELAY")
}
s.RcptTo = to
if strings.Contains(to, "@") {
rcptArray := strings.Split(to, "@")
s.MailDomain = rcptArray[1]
s.LocalUser = rcptArray[0]
s.Valid = true
} else {
s.Valid = false
}
return nil
}
func (s *Session) Data(r io.Reader) error {
log.Println("Start Saving Message")
if !s.Valid {
log.Println("Message Was Not Valid: ", s)
return nil
}
if b, err := ioutil.ReadAll(r); err != nil {
return err
} else {
//using the default Maildir structure
mad := fmt.Sprintf("%s/%s/Maildir", SmtpBackend.MailBaseFolder, s.LocalUser)
log.Println("Maildir is:", mad)
myMaildir := maildir.NewMaildir(mad)
_, errMd := myMaildir.Add(string(b))
if errMd != nil {
log.Println("Error Saving mail ")
return errMd
}
}
return nil
}
func (s *Session) Reset() {
s.LocalUser = ""
s.MailDomain = ""
s.MailFrom = ""
s.RcptTo = ""
s.Valid = false
}