aboutsummaryrefslogtreecommitdiff
path: root/mta_recv.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-01-12 11:56:16 +0800
committerRunxi Yu <me@runxiyu.org>2025-01-12 11:56:16 +0800
commitb99783387a4bd51eba2ea3726910011b389b7c0f (patch)
treea35fab31e8e0e8abe2a85269ec9d5c45c36183ac /mta_recv.go
parentAdd TLS cert configuration handling (diff)
downloadmaild-b99783387a4bd51eba2ea3726910011b389b7c0f.tar.gz
maild-b99783387a4bd51eba2ea3726910011b389b7c0f.tar.zst
maild-b99783387a4bd51eba2ea3726910011b389b7c0f.zip
Use bufio.ReadWriter
Diffstat (limited to 'mta_recv.go')
-rw-r--r--mta_recv.go88
1 files changed, 44 insertions, 44 deletions
diff --git a/mta_recv.go b/mta_recv.go
index ea1a1ef..1f680fa 100644
--- a/mta_recv.go
+++ b/mta_recv.go
@@ -18,21 +18,21 @@ const (
server_state_rcpt
)
-func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Writer) error {
+func handle_incoming_server_connection(conn *bufio.ReadWriter) error {
var my_server_name string
var routes map[string]string
config_consistent_run(func() {
my_server_name = config.Server_name
routes = config.Routes
})
- _, _ = writer.WriteString("220 " + my_server_name + " " + VERSION + "\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("220 " + my_server_name + " " + VERSION + "\r\n")
+ _ = conn.Flush()
server_state := server_state_begin
var remote_server_name string
var current_mail_from string
var current_rcpt_to []string
for {
- line, err := reader.ReadString('\n')
+ line, err := conn.ReadString('\n')
if err != nil {
return err
}
@@ -52,77 +52,77 @@ func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Write
switch cmd {
case "HELO":
if param == "" { // TODO: actually validate the hostname
- _, _ = writer.WriteString("501 Syntax: HELO hostname\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("501 Syntax: HELO hostname\r\n")
+ _ = conn.Flush()
break
}
remote_server_name = param
_ = remote_server_name // TODO
server_state = server_state_helo
- _, _ = writer.WriteString("250 " + my_server_name + "\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("250 " + my_server_name + "\r\n")
+ _ = conn.Flush()
case "MAIL":
switch server_state {
case server_state_begin:
- _, _ = writer.WriteString("503 5.5.1 Error: send HELO/EHLO first\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("503 5.5.1 Error: send HELO/EHLO first\r\n")
+ _ = conn.Flush()
break switch_cmd
case server_state_helo:
break
case server_state_mail:
- _, _ = writer.WriteString("503 5.5.1 Error: nested MAIL command\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("503 5.5.1 Error: nested MAIL command\r\n")
+ _ = conn.Flush()
break switch_cmd
}
if len(param) <= len("FROM:") || strings.ToUpper(param[:len("FROM:")]) != "FROM:" {
- _, _ = writer.WriteString("501 5.5.4 Syntax: MAIL FROM:<address>\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("501 5.5.4 Syntax: MAIL FROM:<address>\r\n")
+ _ = conn.Flush()
break
}
current_mail_from = param[len("FROM:"):]
current_rcpt_to = []string{}
server_state = server_state_mail
- _, _ = writer.WriteString("250 2.1.0 Ok\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("250 2.1.0 Ok\r\n")
+ _ = conn.Flush()
// TODO: Address validation
case "RCPT":
if server_state != server_state_mail && server_state != server_state_rcpt {
- _, _ = writer.WriteString("503 5.5.1 Error: need MAIL command\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("503 5.5.1 Error: need MAIL command\r\n")
+ _ = conn.Flush()
break
}
if len(param) <= len("TO:") || strings.ToUpper(param[:len("TO:")]) != "TO:" {
- _, _ = writer.WriteString("501 5.5.4 Syntax: RCPT TO:<address>\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("501 5.5.4 Syntax: RCPT TO:<address>\r\n")
+ _ = conn.Flush()
break
}
recipient, _, _ := mailkit.Strip_angle_brackets(param[len("TO:"):])
_, ok := routes[recipient]
if !ok {
- _, _ = writer.WriteString("550 5.1.1 <" + recipient + ">: Recipient address rejected: User unknown in local recipient table\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("550 5.1.1 <" + recipient + ">: Recipient address rejected: User unknown in local recipient table\r\n")
+ _ = conn.Flush()
break switch_cmd
}
current_rcpt_to = append(current_rcpt_to, recipient)
server_state = server_state_rcpt
- _, _ = writer.WriteString("250 2.1.5 Ok\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("250 2.1.5 Ok\r\n")
+ _ = conn.Flush()
case "DATA":
if server_state != server_state_rcpt {
- _, _ = writer.WriteString("503 5.5.1 Error: need RCPT command\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("503 5.5.1 Error: need RCPT command\r\n")
+ _ = conn.Flush()
break
}
- _, _ = writer.WriteString("354 End data with <CR><LF>.<CR><LF>\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("354 End data with <CR><LF>.<CR><LF>\r\n")
+ _ = conn.Flush()
var current_data []byte
for {
- tmp, err := reader.ReadSlice('\r')
+ tmp, err := conn.ReadSlice('\r')
if err != nil {
return err
}
- // reader.ReadSlice returns an internal buffer that gets
+ // conn.ReadSlice returns an internal buffer that gets
// overwritten on the next reader operation. So we must
// make a copy; also we have to allocate data_part to
// the correct length because [[builtin.copy]] copies
@@ -130,7 +130,7 @@ func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Write
data_part := make([]byte, len(tmp))
copy(data_part, tmp)
- next_four, err := reader.Peek(4)
+ next_four, err := conn.Peek(4)
if err != nil {
return err
}
@@ -140,7 +140,7 @@ func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Write
}
current_data = slices.Concat(current_data, data_part)
}
- _, err := reader.Discard(4)
+ _, err := conn.Discard(4)
if err != nil {
return err
}
@@ -149,7 +149,7 @@ func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Write
for _, recipient := range current_rcpt_to {
inbox, ok := routes[recipient]
if !ok {
- _, _ = writer.WriteString("550 5.1.1 <" + recipient + ">: Recipient address rejected: User unknown in local recipient table\r\n")
+ _, _ = conn.WriteString("550 5.1.1 <" + recipient + ">: Recipient address rejected: User unknown in local recipient table\r\n")
break switch_cmd
}
inboxes_to_deliver_to[inbox] = struct{}{}
@@ -159,28 +159,28 @@ func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Write
}
}
if err == nil {
- _, _ = writer.WriteString("250 2.0.0 Ok: Accepted\r\n")
+ _, _ = conn.WriteString("250 2.0.0 Ok: Accepted\r\n")
} else {
- _, _ = writer.WriteString("500 2.0.0 Error: " + err.Error() + "\r\n")
+ _, _ = conn.WriteString("500 2.0.0 Error: " + err.Error() + "\r\n")
}
- _ = writer.Flush()
+ _ = conn.Flush()
server_state = server_state_helo
case "QUIT":
- _, _ = writer.WriteString("221 2.0.0 Bye\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("221 2.0.0 Bye\r\n")
+ _ = conn.Flush()
return nil
case "NOOP":
- _, _ = writer.WriteString("250 2.0.0 Ok\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("250 2.0.0 Ok\r\n")
+ _ = conn.Flush()
case "RSET":
if server_state != server_state_begin {
server_state = server_state_helo
}
- _, _ = writer.WriteString("250 2.0.0 Ok\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("250 2.0.0 Ok\r\n")
+ _ = conn.Flush()
default:
- _, _ = writer.WriteString("500 5.5.2 Error: command not recognized\r\n")
- _ = writer.Flush()
+ _, _ = conn.WriteString("500 5.5.2 Error: command not recognized\r\n")
+ _ = conn.Flush()
}
}
}