diff options
-rw-r--r-- | common_imap.go | 31 | ||||
-rw-r--r-- | common_smtp.go | 19 | ||||
-rw-r--r-- | serve_imap.go | 40 | ||||
-rw-r--r-- | serve_mx.go | 26 |
4 files changed, 67 insertions, 49 deletions
diff --git a/common_imap.go b/common_imap.go new file mode 100644 index 0000000..3d79124 --- /dev/null +++ b/common_imap.go @@ -0,0 +1,31 @@ +package main + +import ( + "errors" + "strings" +) + +var err_imap_generic = errors.New("Error in IMAP command received by server.") + +func parse_imap_line(line string) (tag, cmd, param string, err error) { + line = strings.TrimSuffix(line, "\n") + line = strings.TrimSuffix(line, "\r") + tag_end := strings.IndexByte(line, ' ') + if tag_end == -1 { + err = err_imap_generic + return + } + tag = line[:tag_end] + rest := line[tag_end+1:] + cmd_end := strings.IndexByte(rest, ' ') + var param_start int + if cmd_end == -1 { + cmd_end = len(rest) + param_start = len(rest) + } else { + param_start = cmd_end + 1 + } + cmd = strings.ToUpper(rest[:cmd_end]) + param = rest[param_start:] + return +} diff --git a/common_smtp.go b/common_smtp.go new file mode 100644 index 0000000..5c458db --- /dev/null +++ b/common_smtp.go @@ -0,0 +1,19 @@ +package main + +import "strings" + +func parse_smtp_line(line string) (cmd, param string) { + 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:] + return +} diff --git a/serve_imap.go b/serve_imap.go index 2789ca3..01bb334 100644 --- a/serve_imap.go +++ b/serve_imap.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "net" - "strings" "github.com/jackc/pgx/v5/pgxpool" "go.lindenii.runxiyu.org/lindenii-common/clog" @@ -36,35 +35,18 @@ func (session *imap_recv_session) handle(ctx context.Context) error { _ = session.buf_conn.Flush() for { - var tag, cmd, param string - { - line, err := session.buf_conn.ReadString('\n') - if err != nil { - if err == io.EOF { - return err_connection_handler_eof - } - return err - } - line = strings.TrimSuffix(line, "\n") - line = strings.TrimSuffix(line, "\r") - tag_end := strings.IndexByte(line, ' ') - if tag_end == -1 { - _, _ = session.buf_conn.WriteString(line + " BAD Error in IMAP command received by server.\r\n") - _ = session.buf_conn.Flush() - continue - } - tag = line[:tag_end] - rest := line[tag_end+1:] - cmd_end := strings.IndexByte(rest, ' ') - var param_start int - if cmd_end == -1 { - cmd_end = len(rest) - param_start = len(rest) - } else { - param_start = cmd_end + 1 + line, err := session.buf_conn.ReadString('\n') + if err != nil { + if err == io.EOF { + return err_connection_handler_eof } - cmd = strings.ToUpper(rest[:cmd_end]) - param = rest[param_start:] + return err + } + tag, cmd, param, err := parse_imap_line(line) + if err != nil { + _, _ = session.buf_conn.WriteString(line + " BAD " + err.Error() + "\r\n") + _ = session.buf_conn.Flush() + continue } fmt.Printf("received tag=%s, cmd=%s, param=%s\n", tag, cmd, param) } diff --git a/serve_mx.go b/serve_mx.go index b8e6d44..f938c02 100644 --- a/serve_mx.go +++ b/serve_mx.go @@ -49,28 +49,14 @@ func (session *mx_recv_session) handle(ctx context.Context) error { _ = session.buf_conn.Flush() session.server_state = mx_server_state_begin for { - var cmd, param string - { - line, err := session.buf_conn.ReadString('\n') - if err != nil { - if err == io.EOF { - return err_connection_handler_eof - } - 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 + line, err := session.buf_conn.ReadString('\n') + if err != nil { + if err == io.EOF { + return err_connection_handler_eof } - cmd = strings.ToUpper(line[:cmd_end]) - param = line[param_start:] + return err } + cmd, param := parse_smtp_line(line) switch_cmd: switch cmd { case "STARTTLS": |