86 lines
1.5 KiB
Go
86 lines
1.5 KiB
Go
package network
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"strings"
|
|
|
|
"git.uwushka.cc/nzx056/nzx056/nzx_tmpmail/loggingshit"
|
|
)
|
|
|
|
type Session struct {
|
|
Conn net.Conn
|
|
Reader *bufio.Reader
|
|
Writer *bufio.Writer
|
|
MailFrom string
|
|
RcptTo []string
|
|
RemoteAddr string
|
|
}
|
|
|
|
func init() {
|
|
loggingshit.Log("lmtp init", 1)
|
|
ln, err := net.Listen("tcp", "127.0.0.1:2525")
|
|
if err != nil {
|
|
loggingshit.Log("lmtp init failed with error %v", 3, err)
|
|
}
|
|
|
|
for {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
loggingshit.Log("failed to accept connection with error %v, skipping", 2, err)
|
|
continue
|
|
}
|
|
|
|
go handle_conn(conn)
|
|
}
|
|
}
|
|
|
|
func handle_conn(conn net.Conn) {
|
|
defer conn.Close()
|
|
|
|
s := &Session{
|
|
Conn: conn,
|
|
Writer: bufio.NewWriter(conn),
|
|
Reader: bufio.NewReader(conn),
|
|
RemoteAddr: conn.RemoteAddr().String(),
|
|
}
|
|
|
|
s.reply("220 nzx056.love LMTP ready")
|
|
|
|
for {
|
|
l, err := s.readline()
|
|
if err != nil {
|
|
if err != io.EOF {
|
|
loggingshit.Log("readline failed for %s with error: %v", 2, s.RemoteAddr, err)
|
|
}
|
|
loggingshit.Log("caught EOF from %s, closing conn", 2, s.RemoteAddr)
|
|
return
|
|
}
|
|
|
|
upper := strings.ToUpper(l)
|
|
|
|
switch {
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Session) readline() (string, error) {
|
|
l, err := s.Reader.ReadString('\n')
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
l = strings.TrimRight(l, "\r\n")
|
|
loggingshit.Log("readline %s from %s", 1, l, s.RemoteAddr)
|
|
return l, nil
|
|
}
|
|
|
|
func (s *Session) reply(st string) {
|
|
loggingshit.Log("write to %s: %s", 1, s.RemoteAddr, st)
|
|
fmt.Fprintf(s.Writer, "%s\r\n", st)
|
|
s.Writer.Flush()
|
|
}
|