|
@@ -24,6 +24,11 @@ import (
|
24
|
24
|
"net"
|
25
|
25
|
"net/textproto"
|
26
|
26
|
"strings"
|
|
27
|
+ "time"
|
|
28
|
+)
|
|
29
|
+
|
|
30
|
+var (
|
|
31
|
+ ErrTimedOut = errors.New("Timed out")
|
27
|
32
|
)
|
28
|
33
|
|
29
|
34
|
// A Client represents a client connection to an SMTP server.
|
|
@@ -48,11 +53,25 @@ type Client struct {
|
48
|
53
|
|
49
|
54
|
// Dial returns a new Client connected to an SMTP server at addr.
|
50
|
55
|
// The addr must include a port, as in "mail.example.com:smtp".
|
51
|
|
-func Dial(addr string) (*Client, error) {
|
52
|
|
- conn, err := net.Dial("tcp", addr)
|
|
56
|
+func Dial(addr string, timeout time.Duration) (*Client, error) {
|
|
57
|
+ var conn net.Conn
|
|
58
|
+ var err error
|
|
59
|
+ start := time.Now()
|
|
60
|
+ if timeout == 0 {
|
|
61
|
+ conn, err = net.Dial("tcp", addr)
|
|
62
|
+ } else {
|
|
63
|
+ conn, err = net.DialTimeout("tcp", addr, timeout)
|
|
64
|
+ }
|
53
|
65
|
if err != nil {
|
54
|
66
|
return nil, err
|
55
|
67
|
}
|
|
68
|
+ if timeout != 0 {
|
|
69
|
+ remaining := timeout - time.Since(start)
|
|
70
|
+ if remaining <= 0 {
|
|
71
|
+ return nil, ErrTimedOut
|
|
72
|
+ }
|
|
73
|
+ conn.SetDeadline(time.Now().Add(remaining))
|
|
74
|
+ }
|
56
|
75
|
host, _, _ := net.SplitHostPort(addr)
|
57
|
76
|
return NewClient(conn, host)
|
58
|
77
|
}
|
|
@@ -316,8 +335,8 @@ var testHookStartTLS func(*tls.Config) // nil, except for tests
|
316
|
335
|
// attachments (see the mime/multipart package), or other mail
|
317
|
336
|
// functionality. Higher-level packages exist outside of the standard
|
318
|
337
|
// library.
|
319
|
|
-// XXX: modified in Oragono to add `requireTLS` and `heloDomain` arguments
|
320
|
|
-func SendMail(addr string, a Auth, heloDomain string, from string, to []string, msg []byte, requireTLS bool) error {
|
|
338
|
+// XXX: modified in Ergo to add `requireTLS`, `heloDomain`, and `timeout` arguments
|
|
339
|
+func SendMail(addr string, a Auth, heloDomain string, from string, to []string, msg []byte, requireTLS bool, timeout time.Duration) error {
|
321
|
340
|
if err := validateLine(from); err != nil {
|
322
|
341
|
return err
|
323
|
342
|
}
|
|
@@ -326,7 +345,7 @@ func SendMail(addr string, a Auth, heloDomain string, from string, to []string,
|
326
|
345
|
return err
|
327
|
346
|
}
|
328
|
347
|
}
|
329
|
|
- c, err := Dial(addr)
|
|
348
|
+ c, err := Dial(addr, timeout)
|
330
|
349
|
if err != nil {
|
331
|
350
|
return err
|
332
|
351
|
}
|