Skip to content

imapclient: return greeting text on WaitGreeting #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions imap.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (
MailboxAttrSent MailboxAttr = "\\Sent"
MailboxAttrTrash MailboxAttr = "\\Trash"
MailboxAttrImportant MailboxAttr = "\\Important" // RFC 8457
MailboxAttrTemplates MailboxAttr = "\\Templates"
)

// Flag is a message flag.
Expand Down
19 changes: 10 additions & 9 deletions imapclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ type Client struct {
dec *imapwire.Decoder
encMutex sync.Mutex

greetingCh chan struct{}
greetingCh chan string
greetingRecv bool
greetingErr error

Expand Down Expand Up @@ -177,7 +177,7 @@ func New(conn net.Conn, options *Options) *Client {
br: br,
bw: bw,
dec: imapwire.NewDecoder(br, imapwire.ConnSideClient),
greetingCh: make(chan struct{}),
greetingCh: make(chan string, 1),
decCh: make(chan struct{}),
state: imap.ConnStateNone,
enabled: make(imap.CapSet),
Expand Down Expand Up @@ -295,7 +295,7 @@ func (c *Client) setState(state imap.ConnState) {
// and block until it's received. If the capabilities cannot be fetched, nil is
// returned.
func (c *Client) Caps() imap.CapSet {
if err := c.WaitGreeting(); err != nil {
if _, err := c.WaitGreeting(); err != nil {
return nil
}

Expand Down Expand Up @@ -905,6 +905,7 @@ func (c *Client) readResponseData(typ string) error {
if c.greetingErr == nil && code != "CAPABILITY" {
c.setCaps(nil) // request initial capabilities
}
c.greetingCh <- text
close(c.greetingCh)
}
case "CAPABILITY":
Expand Down Expand Up @@ -982,16 +983,16 @@ func (c *Client) readResponseData(typ string) error {
return nil
}

// WaitGreeting waits for the server's initial greeting.
func (c *Client) WaitGreeting() error {
// WaitGreeting waits for the server's initial greeting and its text.
func (c *Client) WaitGreeting() (string, error) {
select {
case <-c.greetingCh:
return c.greetingErr
case greetingText := <-c.greetingCh:
return greetingText, c.greetingErr
case <-c.decCh:
if c.decErr != nil {
return fmt.Errorf("got error before greeting: %v", c.decErr)
return "", fmt.Errorf("got error before greeting: %v", c.decErr)
}
return fmt.Errorf("connection closed before greeting")
return "", fmt.Errorf("connection closed before greeting")
}
}

Expand Down
18 changes: 17 additions & 1 deletion imapclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,22 @@ func (sw *swapWriter) Swap(w io.Writer) {
sw.mutex.Unlock()
}

func TestWaitGreeting(t *testing.T) {
client, server := newClientServerPair(t, imap.ConnStateAuthenticated)
defer client.Close()
defer server.Close()

greetingText, err := client.WaitGreeting()
if err != nil {
t.Errorf("WaitGreeting() = %v", err)
}

// imapmemserver or dovecot PREAUTH greetings
if greetingText != "IMAP server ready" && greetingText != "Logged in as test-user" {
t.Errorf("Unexpected greeting text = %s", greetingText)
}
}

func TestLogin(t *testing.T) {
client, server := newClientServerPair(t, imap.ConnStateNotAuthenticated)
defer client.Close()
Expand Down Expand Up @@ -271,7 +287,7 @@ func TestWaitGreeting_eof(t *testing.T) {
t.Fatalf("serverConn.Close() = %v", err)
}

if err := client.WaitGreeting(); err == nil {
if _, err := client.WaitGreeting(); err == nil {
t.Fatalf("WaitGreeting() should fail")
}
}
1 change: 1 addition & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func canonInit() {
imap.MailboxAttrSent,
imap.MailboxAttrTrash,
imap.MailboxAttrImportant,
imap.MailboxAttrTemplates,
}

canonFlag = make(map[string]imap.Flag)
Expand Down