aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2024-12-31 16:44:14 +0000
committerRunxi Yu <me@runxiyu.org>2024-12-31 16:45:10 +0000
commit2c7ee2f2405db2071b43a9ad5b69930040b765e8 (patch)
tree11f6f368b4be1d86e90584e4a94598deccd0afb8
parentImplement QUIT and RSET (diff)
downloadmaild-2c7ee2f2405db2071b43a9ad5b69930040b765e8.tar.gz
maild-2c7ee2f2405db2071b43a9ad5b69930040b765e8.tar.zst
maild-2c7ee2f2405db2071b43a9ad5b69930040b765e8.zip
File splitting
-rw-r--r--incoming.go12
-rw-r--r--main.go157
-rw-r--r--mta_recv.go151
3 files changed, 164 insertions, 156 deletions
diff --git a/incoming.go b/incoming.go
new file mode 100644
index 0000000..f915ac0
--- /dev/null
+++ b/incoming.go
@@ -0,0 +1,12 @@
+package main
+
+import "go.lindenii.runxiyu.org/lindenii-common/clog"
+
+func deliver_incoming(envelope_from string, envelope_recipients []string, data []byte) {
+ clog.Debug(
+ "incoming_mail",
+ "envelope_from", envelope_from,
+ "envelope_recipients", envelope_recipients,
+ "data", string(data),
+ )
+}
diff --git a/main.go b/main.go
index d894d8c..8d6b797 100644
--- a/main.go
+++ b/main.go
@@ -2,11 +2,8 @@ package main
import (
"bufio"
- "bytes"
"net"
"os"
- "slices"
- "strings"
"go.lindenii.runxiyu.org/lindenii-common/clog"
"go.lindenii.runxiyu.org/lindenii-common/scfg"
@@ -38,7 +35,7 @@ func main() {
}
go func() {
- err := handle_connection(bufio.NewReader(conn), bufio.NewWriter(conn))
+ err := handle_incoming_server_connection(bufio.NewReader(conn), bufio.NewWriter(conn))
if err != nil {
clog.Error("connection handler returned error", "err", err)
}
@@ -46,155 +43,3 @@ func main() {
}
}
-
-type server_state_t uint
-
-const (
- server_state_begin server_state_t = iota
- server_state_helo
- server_state_mail
- server_state_rcpt
-)
-
-func handle_connection(reader *bufio.Reader, writer *bufio.Writer) error {
- _, _ = writer.WriteString("220 " + config.Server_name + " " + VERSION + "\r\n")
- _ = writer.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')
- if err != nil {
- return err
- }
- line = strings.TrimSuffix(line, "\n")
- line = strings.TrimSuffix(line, "\r")
- cmd_end := strings.IndexByte(line, ' ')
- var param_start int
- if cmd_end == -1 {
- cmd_end = len(line)
- param_start = len(line)
- } else {
- param_start = cmd_end + 1
- }
- cmd := strings.ToUpper(line[:cmd_end])
- param := line[param_start:]
- switch_cmd:
- switch cmd {
- case "HELO":
- if param == "" { // TODO: actually validate the hostname
- _, _ = writer.WriteString("501 Syntax: HELO hostname\r\n")
- _ = writer.Flush()
- break
- }
- remote_server_name = param
- _ = remote_server_name // TODO
- server_state = server_state_helo
- _, _ = writer.WriteString("250 " + config.Server_name + "\r\n")
- _ = writer.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()
- 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()
- 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()
- 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()
- // 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()
- 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()
- break
- }
- current_rcpt_to = append(current_rcpt_to, param[len("TO:"):])
- server_state = server_state_rcpt
- _, _ = writer.WriteString("250 2.1.5 Ok\r\n")
- _ = writer.Flush()
- case "DATA":
- if server_state != server_state_rcpt {
- _, _ = writer.WriteString("503 5.5.1 Error: need RCPT command\r\n")
- _ = writer.Flush()
- break
- }
- _, _ = writer.WriteString("354 End data with <CR><LF>.<CR><LF>\r\n")
- _ = writer.Flush()
- var current_data []byte
- for {
- tmp, err := reader.ReadSlice('\r')
- if err != nil {
- return err
- }
-
- // reader.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
- // min(len(dst), len(src)) items.
- data_part := make([]byte, len(tmp))
- copy(data_part, tmp)
-
- next_four, err := reader.Peek(4)
- if err != nil {
- return err
- }
- if bytes.Equal(next_four, []byte{'\n', '.', '\r', '\n'}) {
- current_data = slices.Concat(current_data, data_part[:len(data_part)-1])
- break
- }
- current_data = slices.Concat(current_data, data_part)
- }
- _, err := reader.Discard(4)
- if err != nil {
- return err
- }
- deliver_incoming(current_mail_from, current_rcpt_to, current_data)
- server_state = server_state_helo
- _, _ = writer.WriteString("250 2.0.0 Ok: Accepted\r\n")
- _ = writer.Flush()
- case "QUIT":
- _, _ = writer.WriteString("221 2.0.0 Bye\r\n")
- _ = writer.Flush()
- return nil
- case "RSET":
- if server_state != server_state_begin {
- server_state = server_state_helo
- }
- _, _ = writer.WriteString("250 2.0.0 Ok\r\n")
- _ = writer.Flush()
- default:
- _, _ = writer.WriteString("500 5.5.2 Error: command not recognized\r\n")
- _ = writer.Flush()
- }
- }
-}
-
-func deliver_incoming(envelope_from string, envelope_recipients []string, data []byte) {
- clog.Debug(
- "incoming_mail",
- "envelope_from", envelope_from,
- "envelope_recipients", envelope_recipients,
- "data", string(data),
- )
-}
diff --git a/mta_recv.go b/mta_recv.go
new file mode 100644
index 0000000..2b4679a
--- /dev/null
+++ b/mta_recv.go
@@ -0,0 +1,151 @@
+package main
+
+import (
+ "bufio"
+ "bytes"
+ "slices"
+ "strings"
+)
+
+type server_state_t uint
+
+const (
+ server_state_begin server_state_t = iota
+ server_state_helo
+ server_state_mail
+ server_state_rcpt
+)
+
+func handle_incoming_server_connection(reader *bufio.Reader, writer *bufio.Writer) error {
+ _, _ = writer.WriteString("220 " + config.Server_name + " " + VERSION + "\r\n")
+ _ = writer.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')
+ if err != nil {
+ return err
+ }
+ line = strings.TrimSuffix(line, "\n")
+ line = strings.TrimSuffix(line, "\r")
+ cmd_end := strings.IndexByte(line, ' ')
+ var param_start int
+ if cmd_end == -1 {
+ cmd_end = len(line)
+ param_start = len(line)
+ } else {
+ param_start = cmd_end + 1
+ }
+ cmd := strings.ToUpper(line[:cmd_end])
+ param := line[param_start:]
+ switch_cmd:
+ switch cmd {
+ case "HELO":
+ if param == "" { // TODO: actually validate the hostname
+ _, _ = writer.WriteString("501 Syntax: HELO hostname\r\n")
+ _ = writer.Flush()
+ break
+ }
+ remote_server_name = param
+ _ = remote_server_name // TODO
+ server_state = server_state_helo
+ _, _ = writer.WriteString("250 " + config.Server_name + "\r\n")
+ _ = writer.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()
+ 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()
+ 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()
+ 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()
+ // 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()
+ 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()
+ break
+ }
+ current_rcpt_to = append(current_rcpt_to, param[len("TO:"):])
+ server_state = server_state_rcpt
+ _, _ = writer.WriteString("250 2.1.5 Ok\r\n")
+ _ = writer.Flush()
+ case "DATA":
+ if server_state != server_state_rcpt {
+ _, _ = writer.WriteString("503 5.5.1 Error: need RCPT command\r\n")
+ _ = writer.Flush()
+ break
+ }
+ _, _ = writer.WriteString("354 End data with <CR><LF>.<CR><LF>\r\n")
+ _ = writer.Flush()
+ var current_data []byte
+ for {
+ tmp, err := reader.ReadSlice('\r')
+ if err != nil {
+ return err
+ }
+
+ // reader.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
+ // min(len(dst), len(src)) items.
+ data_part := make([]byte, len(tmp))
+ copy(data_part, tmp)
+
+ next_four, err := reader.Peek(4)
+ if err != nil {
+ return err
+ }
+ if bytes.Equal(next_four, []byte{'\n', '.', '\r', '\n'}) {
+ current_data = slices.Concat(current_data, data_part[:len(data_part)-1])
+ break
+ }
+ current_data = slices.Concat(current_data, data_part)
+ }
+ _, err := reader.Discard(4)
+ if err != nil {
+ return err
+ }
+ deliver_incoming(current_mail_from, current_rcpt_to, current_data)
+ server_state = server_state_helo
+ _, _ = writer.WriteString("250 2.0.0 Ok: Accepted\r\n")
+ _ = writer.Flush()
+ case "QUIT":
+ _, _ = writer.WriteString("221 2.0.0 Bye\r\n")
+ _ = writer.Flush()
+ return nil
+ case "RSET":
+ if server_state != server_state_begin {
+ server_state = server_state_helo
+ }
+ _, _ = writer.WriteString("250 2.0.0 Ok\r\n")
+ _ = writer.Flush()
+ default:
+ _, _ = writer.WriteString("500 5.5.2 Error: command not recognized\r\n")
+ _ = writer.Flush()
+ }
+ }
+}