2023-08-13 03:04:16 +01:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/emersion/go-imap/client"
|
|
|
|
"github.com/emersion/go-sasl"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Imap struct {
|
|
|
|
Server string `yaml:"server"`
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
Separator string `yaml:"separator"`
|
|
|
|
}
|
|
|
|
|
2023-08-21 00:26:22 +01:00
|
|
|
var defaultDialer = client.Dial
|
|
|
|
|
2023-08-13 03:04:16 +01:00
|
|
|
func (i *Imap) MakeClient(user string) (*Client, error) {
|
|
|
|
// dial imap server
|
2023-08-21 00:26:22 +01:00
|
|
|
imapClient, err := defaultDialer(i.Server)
|
2023-08-13 03:04:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare login details
|
|
|
|
un := fmt.Sprintf("%s%s%s", user, i.Separator, i.Username)
|
|
|
|
saslLogin := sasl.NewPlainClient("", un, i.Password)
|
|
|
|
|
|
|
|
// authenticate
|
|
|
|
err = imapClient.Authenticate(saslLogin)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// new client
|
|
|
|
return &Client{ic: imapClient}, nil
|
|
|
|
}
|