package main

import (
	"bytes"
	"log"
	"net"
    "time"
	
)



func mailHandler(origin net.Addr, from string, to []string, data []byte) {
	SmtpSession := new(Session)

	log.Printf("Received mail from %s for %q from %s", from, to, origin.String())
	log.Println(" Recipients: ", to)

	for _, rcptTo := range to {
		SmtpSession.Reset()

		if rcptErr := SmtpSession.Rcpt(rcptTo); rcptErr == nil {
			SmtpSession.MailFrom = from
			if dataErr := SmtpSession.Data(bytes.NewReader(data)); dataErr != nil {
				log.Println("Problem Saving Message: ", dataErr.Error())
			}
			
		} else {
			log.Println(rcptErr)
		}

	}

}


func handlerRcpt(remoteAddr net.Addr, from string, to string) bool {

	log.Println("CHECK #2 Rcpt to:", to)
	if !SmtpBackend.CheckValidRcpt(to) {
		log.Printf("%s [ %s -> %s ] %s BAD!!!!", "YU NO MAIL!!! WE NU RELAY!!! ", from, to, remoteAddr.String() )
		time.Sleep(10 * time.Second)
		return false
	}else{
		log.Printf("%s [ %s -> %s ] %s GOOD!!!!", "WE DO!!!", from, to, remoteAddr.String() )
	}

	return true


}