From fbb6307d7740784fb813cc45539e2f06210c8fec Mon Sep 17 00:00:00 2001 From: saintilya Date: Wed, 29 Apr 2026 17:49:51 +0200 Subject: [PATCH] networking --- nzx_tmpmail/network/lmtp.go | 63 +++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/nzx_tmpmail/network/lmtp.go b/nzx_tmpmail/network/lmtp.go index 58fda19..e626c8e 100644 --- a/nzx_tmpmail/network/lmtp.go +++ b/nzx_tmpmail/network/lmtp.go @@ -11,12 +11,14 @@ import ( ) type Session struct { - Conn net.Conn - Reader *bufio.Reader - Writer *bufio.Writer - MailFrom string - RcptTo []string - RemoteAddr string + Conn net.Conn + Reader *bufio.Reader + Writer *bufio.Writer + MailFrom string + RcptTo []string + RemoteAddr string + hasMailFrom bool + hasRcptTo bool } func init() { @@ -62,11 +64,60 @@ func handle_conn(conn net.Conn) { upper := strings.ToUpper(l) 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 .") } } } +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) { l, err := s.Reader.ReadString('\n') if err != nil {