51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package imapx
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"fmt"
|
|
|
|
"github.com/emersion/go-imap/v2/imapclient"
|
|
)
|
|
|
|
type Endpoint struct {
|
|
Host string
|
|
Port int
|
|
TLSMode string // ssl | starttls | plain
|
|
}
|
|
|
|
func (e Endpoint) addr() string { return fmt.Sprintf("%s:%d", e.Host, e.Port) }
|
|
|
|
func Connect(ctx context.Context, ep Endpoint) (*imapclient.Client, error) {
|
|
var (
|
|
c *imapclient.Client
|
|
err error
|
|
)
|
|
switch ep.TLSMode {
|
|
case "ssl":
|
|
c, err = imapclient.DialTLS(ep.addr(), &imapclient.Options{
|
|
TLSConfig: &tls.Config{ServerName: ep.Host},
|
|
})
|
|
case "starttls":
|
|
c, err = imapclient.DialStartTLS(ep.addr(), &imapclient.Options{
|
|
TLSConfig: &tls.Config{ServerName: ep.Host},
|
|
})
|
|
case "plain":
|
|
c, err = imapclient.DialInsecure(ep.addr(), nil)
|
|
default:
|
|
return nil, fmt.Errorf("unknown tls_mode %q", ep.TLSMode)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func TestEndpoint(ctx context.Context, ep Endpoint) error {
|
|
c, err := Connect(ctx, ep)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.Logout().Wait()
|
|
}
|