2019-12-02 16:58:19 +00:00
|
|
|
package koushin
|
|
|
|
|
|
|
|
import (
|
2019-12-02 18:53:09 +00:00
|
|
|
"fmt"
|
2019-12-02 17:21:45 +00:00
|
|
|
|
2019-12-02 16:58:19 +00:00
|
|
|
"github.com/emersion/go-imap"
|
2019-12-03 10:12:26 +00:00
|
|
|
imapclient "github.com/emersion/go-imap/client"
|
2019-12-03 16:35:04 +00:00
|
|
|
"github.com/emersion/go-message/charset"
|
2019-12-02 16:58:19 +00:00
|
|
|
)
|
|
|
|
|
2019-12-03 16:35:04 +00:00
|
|
|
func init() {
|
|
|
|
imap.CharsetReader = charset.Reader
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:51:42 +00:00
|
|
|
func (s *Server) dialIMAP() (*imapclient.Client, error) {
|
2019-12-02 16:58:19 +00:00
|
|
|
var c *imapclient.Client
|
|
|
|
var err error
|
|
|
|
if s.imap.tls {
|
|
|
|
c, err = imapclient.DialTLS(s.imap.host, nil)
|
|
|
|
if err != nil {
|
2019-12-03 13:48:43 +00:00
|
|
|
return nil, fmt.Errorf("failed to connect to IMAPS server: %v", err)
|
2019-12-02 16:58:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c, err = imapclient.Dial(s.imap.host)
|
|
|
|
if err != nil {
|
2019-12-03 13:48:11 +00:00
|
|
|
return nil, fmt.Errorf("failed to connect to IMAP server: %v", err)
|
2019-12-02 16:58:19 +00:00
|
|
|
}
|
|
|
|
if !s.imap.insecure {
|
|
|
|
if err := c.StartTLS(nil); err != nil {
|
|
|
|
c.Close()
|
2019-12-03 13:48:11 +00:00
|
|
|
return nil, fmt.Errorf("STARTTLS failed: %v", err)
|
2019-12-02 16:58:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, err
|
|
|
|
}
|