From 620e5d62741f70e919dade27dbcbdf85e8385b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Devrim=20=C5=9Eahin?= Date: Thu, 8 Feb 2024 20:56:49 +0300 Subject: [PATCH] Allow custom dialer. Closes #12. Co-authored-by: kubuzetto --- pop3.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pop3.go b/pop3.go index 4297313..15ef399 100644 --- a/pop3.go +++ b/pop3.go @@ -17,7 +17,8 @@ import ( // Client implements a Client e-mail client. type Client struct { - opt Opt + opt Opt + dialer Dialer } // Conn is a stateful connection with the POP3 server/ @@ -34,11 +35,16 @@ type Opt struct { // Default is 3 seconds. DialTimeout time.Duration `json:"dial_timeout"` + Dialer Dialer `json:"-"` TLSEnabled bool `json:"tls_enabled"` TLSSkipVerify bool `json:"tls_skip_verify"` } +type Dialer interface { + Dial(network, address string) (net.Conn, error) +} + // MessageID contains the ID and size of an individual message. type MessageID struct { // ID is the numerical index (non-unique) of the message. @@ -64,9 +70,16 @@ func New(opt Opt) *Client { opt.DialTimeout = time.Second * 3 } - return &Client{ - opt: opt, + c := &Client{ + opt: opt, + dialer: opt.Dialer, } + + if c.dialer == nil { + c.dialer = &net.Dialer{Timeout: opt.DialTimeout} + } + + return c } // NewConn creates and returns live POP3 server connection. @@ -75,7 +88,7 @@ func (c *Client) NewConn() (*Conn, error) { addr = fmt.Sprintf("%s:%d", c.opt.Host, c.opt.Port) ) - conn, err := net.DialTimeout("tcp", addr, c.opt.DialTimeout) + conn, err := c.dialer.Dial("tcp", addr) if err != nil { return nil, err }