From faf7c3566be12f22f346f978d3a5fcf68e4bc8b4 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 16 Jan 2025 12:34:46 +0800 Subject: IMAP: Re-add server protocol state --- serve_imap.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/serve_imap.go b/serve_imap.go index 524f130..ad50c27 100644 --- a/serve_imap.go +++ b/serve_imap.go @@ -15,17 +15,28 @@ import ( "go.lindenii.runxiyu.org/lindenii-common/clog" ) +type imap_server_state_t uint + +const ( + imap_server_state_not_authenticated imap_server_state_t = iota + imap_server_state_authenticated + imap_server_state_selected + imap_server_state_logout +) + type imap_recv_session struct { buf_conn *bufio.ReadWriter net_conn net.Conn tls_conn *tls.Conn my_server_name string db *pgxpool.Pool + server_state imap_server_state_t } const IMAP_CAPABILITIES = "CAPABILITY IMAP4rev2 AUTH=PLAIN" func (session *imap_recv_session) handle(ctx context.Context) error { + session.server_state = imap_server_state_not_authenticated session.buf_conn = bufio.NewReadWriter(bufio.NewReader(session.net_conn), bufio.NewWriter(session.net_conn)) _, _ = session.buf_conn.WriteString("* OK [" + IMAP_CAPABILITIES + "] " + VERSION + "\r\n") _ = session.buf_conn.Flush() @@ -52,6 +63,11 @@ func (session *imap_recv_session) handle(ctx context.Context) error { _, _ = session.buf_conn.WriteString(tag + " OK CAPABILITY completed\r\n") _ = session.buf_conn.Flush() case "AUTHENTICATE": + if session.server_state != imap_server_state_authenticated { + _, _ = session.buf_conn.WriteString(line + " TODO you are already authenticated") + _ = session.buf_conn.Flush() + break switch_cmd + } space_that_ends_the_method := strings.IndexByte(param, ' ') method := param[:space_that_ends_the_method] switch method { @@ -74,7 +90,12 @@ func (session *imap_recv_session) handle(ctx context.Context) error { session.buf_conn.WriteString(" TODO need three sasl segments\r\n") break switch_cmd } - authcid, passwd := segments[1], segments[2] + authcid, passwd := string(segments[1]), string(segments[2]) + if authcid == "test" && passwd == "test" { // TODO + session.server_state = imap_server_state_authenticated + } else { + + } default: _, _ = session.buf_conn.WriteString(line + " TODO i don't know this authentication method\r\n") _ = session.buf_conn.Flush() -- cgit v1.2.3