networking

This commit is contained in:
saintilya
2026-04-29 17:49:51 +02:00
parent 58cbf34618
commit fbb6307d77

View File

@@ -11,12 +11,14 @@ import (
) )
type Session struct { type Session struct {
Conn net.Conn Conn net.Conn
Reader *bufio.Reader Reader *bufio.Reader
Writer *bufio.Writer Writer *bufio.Writer
MailFrom string MailFrom string
RcptTo []string RcptTo []string
RemoteAddr string RemoteAddr string
hasMailFrom bool
hasRcptTo bool
} }
func init() { func init() {
@@ -62,11 +64,60 @@ func handle_conn(conn net.Conn) {
upper := strings.ToUpper(l) upper := strings.ToUpper(l)
switch { switch {
case strings.HasPrefix(upper, "LHLO "):
s.reply("250-nzx056.love")
s.reply("250-8BITMIME")
s.reply("250 PIPELINING")
case strings.HasPrefix(upper, "MAIL FROM:"):
s.MailFrom = extractSender(l[len("MAIL FROM:"):])
s.hasMailFrom = true
s.RcptTo = nil
s.reply("250 2.1.0 Ok")
case strings.HasPrefix(upper, "RCPT TO:"):
rcpt := strings.ToLower(extractSender(l[len("RCPT TO:"):]))
s.hasRcptTo = false
if !strings.HasSuffix(rcpt, "@nzx056.love") {
s.reply("550 5.1.1 User unknown")
loggingshit.Log("got an email for unknown domain %s, skipping", 1, rcpt)
continue
}
s.RcptTo = append(s.RcptTo, rcpt)
s.reply("250 2.1.5 Ok")
case upper == "DATA":
if !s.hasMailFrom {
s.reply("503 5.5.1 Need MAIL FROM first")
continue
}
if !s.hasRcptTo {
s.reply("503 5.5.1 Need RCPT TO first")
continue
}
s.reply("354 End data with <CR><LF>.<CR><LF>")
} }
} }
} }
func extractSender(s string) string {
s = strings.TrimSpace(s)
if strings.HasPrefix(s, "<") {
end := strings.Index(s, ">")
if end >= 0 {
return s[1:end]
}
}
fields := strings.Fields(s)
if len(fields) == 0 {
return ""
}
return strings.Trim(fields[0], "<>")
}
func (s *Session) readline() (string, error) { func (s *Session) readline() (string, error) {
l, err := s.Reader.ReadString('\n') l, err := s.Reader.ReadString('\n')
if err != nil { if err != nil {