zangtumb/session.go

69 lines
1.2 KiB
Go

package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"strings"
"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("CHECK #1 Rcpt to:", to)
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
}