Skip to content

Commit

Permalink
Allow custom dialer. Closes #12.
Browse files Browse the repository at this point in the history
Co-authored-by: kubuzetto <[email protected]>
  • Loading branch information
kubuzetto authored and knadh committed Feb 13, 2024
1 parent aaa1bf2 commit 620e5d6
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pop3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
}
Expand Down

0 comments on commit 620e5d6

Please sign in to comment.