2018-01-10 02:39:00 +01:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2018-02-01 03:54:52 +01:00
|
|
|
"crypto/tls"
|
2018-01-14 11:30:11 +01:00
|
|
|
"fmt"
|
2022-02-12 23:08:18 +01:00
|
|
|
"math"
|
2021-12-07 00:00:01 +01:00
|
|
|
"net"
|
2018-01-14 11:30:11 +01:00
|
|
|
"net/url"
|
2021-12-07 00:05:36 +01:00
|
|
|
"strconv"
|
2018-01-14 11:30:11 +01:00
|
|
|
"strings"
|
2021-12-07 00:05:36 +01:00
|
|
|
"time"
|
2018-01-10 17:19:45 +01:00
|
|
|
|
2018-01-14 11:30:11 +01:00
|
|
|
"github.com/emersion/go-imap"
|
2020-09-12 15:05:02 +02:00
|
|
|
sortthread "github.com/emersion/go-imap-sortthread"
|
2018-02-01 03:18:21 +01:00
|
|
|
"github.com/emersion/go-imap/client"
|
2022-02-12 23:08:18 +01:00
|
|
|
"github.com/pkg/errors"
|
2019-07-10 21:00:28 +02:00
|
|
|
"golang.org/x/oauth2"
|
2018-02-01 03:18:21 +01:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib"
|
2022-03-22 09:52:27 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/handlers"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2018-01-10 02:39:00 +01:00
|
|
|
)
|
|
|
|
|
2019-07-18 06:25:42 +02:00
|
|
|
func init() {
|
|
|
|
handlers.RegisterWorkerFactory("imap", NewIMAPWorker)
|
|
|
|
handlers.RegisterWorkerFactory("imaps", NewIMAPWorker)
|
|
|
|
}
|
|
|
|
|
2018-01-14 11:30:11 +01:00
|
|
|
var errUnsupported = fmt.Errorf("unsupported command")
|
|
|
|
|
|
|
|
type imapClient struct {
|
|
|
|
*client.Client
|
2021-11-12 18:12:02 +01:00
|
|
|
thread *sortthread.ThreadClient
|
|
|
|
sort *sortthread.SortClient
|
2018-01-14 11:30:11 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 02:39:00 +01:00
|
|
|
type IMAPWorker struct {
|
2018-01-14 11:30:11 +01:00
|
|
|
config struct {
|
2019-07-10 21:00:28 +02:00
|
|
|
scheme string
|
|
|
|
insecure bool
|
|
|
|
addr string
|
|
|
|
user *url.Userinfo
|
|
|
|
folders []string
|
|
|
|
oauthBearer lib.OAuthBearer
|
2021-12-07 00:05:36 +01:00
|
|
|
// tcp connection parameters
|
|
|
|
connection_timeout time.Duration
|
|
|
|
keepalive_period time.Duration
|
|
|
|
keepalive_probes int
|
|
|
|
keepalive_interval int
|
2018-01-14 11:30:11 +01:00
|
|
|
}
|
|
|
|
|
2019-03-11 04:45:00 +01:00
|
|
|
client *imapClient
|
2019-05-14 02:16:55 +02:00
|
|
|
idleStop chan struct{}
|
|
|
|
idleDone chan error
|
2019-08-22 14:53:27 +02:00
|
|
|
selected *imap.MailboxStatus
|
2019-03-11 04:45:00 +01:00
|
|
|
updates chan client.Update
|
|
|
|
worker *types.Worker
|
2019-03-21 04:23:38 +01:00
|
|
|
// Map of sequence numbers to UIDs, index 0 is seq number 1
|
2022-01-19 13:18:10 +01:00
|
|
|
seqMap []uint32
|
|
|
|
done chan struct{}
|
|
|
|
autoReconnect bool
|
2022-02-12 23:08:18 +01:00
|
|
|
retries int
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 06:25:42 +02:00
|
|
|
func NewIMAPWorker(worker *types.Worker) (types.Backend, error) {
|
2018-01-10 02:39:00 +01:00
|
|
|
return &IMAPWorker{
|
2019-05-14 02:16:55 +02:00
|
|
|
idleDone: make(chan error),
|
|
|
|
updates: make(chan client.Update, 50),
|
|
|
|
worker: worker,
|
2019-08-22 14:53:27 +02:00
|
|
|
selected: &imap.MailboxStatus{},
|
2019-07-18 06:25:42 +02:00
|
|
|
}, nil
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
|
|
|
|
2018-01-14 11:30:11 +01:00
|
|
|
func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
|
2021-11-01 21:38:26 +01:00
|
|
|
if w.client != nil && w.client.State() == imap.SelectedState {
|
2022-01-15 12:07:02 +01:00
|
|
|
close(w.idleStop)
|
2019-05-14 02:16:55 +02:00
|
|
|
if err := <-w.idleDone; err != nil {
|
|
|
|
w.worker.PostMessage(&types.Error{Error: err}, nil)
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 13:18:08 +01:00
|
|
|
defer func() {
|
|
|
|
if w.client != nil && w.client.State() == imap.SelectedState {
|
|
|
|
w.idleStop = make(chan struct{})
|
|
|
|
go func() {
|
2022-03-22 09:52:27 +01:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2022-03-18 09:53:02 +01:00
|
|
|
w.idleDone <- w.client.Idle(w.idleStop, &client.IdleOptions{LogoutTimeout: 0, PollInterval: 0})
|
2022-01-19 13:18:08 +01:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
2019-05-14 02:16:55 +02:00
|
|
|
|
2022-02-12 23:08:18 +01:00
|
|
|
checkConn := func(wait time.Duration) {
|
|
|
|
time.Sleep(wait)
|
2022-02-05 00:07:16 +01:00
|
|
|
w.stopConnectionObserver()
|
|
|
|
w.startConnectionObserver()
|
|
|
|
}
|
|
|
|
|
2020-02-15 12:37:18 +01:00
|
|
|
var reterr error // will be returned at the end, needed to support idle
|
|
|
|
|
2018-01-10 17:19:45 +01:00
|
|
|
switch msg := msg.(type) {
|
2018-02-02 01:54:19 +01:00
|
|
|
case *types.Unsupported:
|
2018-01-14 11:30:11 +01:00
|
|
|
// No-op
|
2018-02-02 01:54:19 +01:00
|
|
|
case *types.Configure:
|
2018-01-14 11:30:11 +01:00
|
|
|
u, err := url.Parse(msg.Config.Source)
|
|
|
|
if err != nil {
|
2022-01-19 13:18:08 +01:00
|
|
|
reterr = err
|
|
|
|
break
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
2018-01-14 11:30:11 +01:00
|
|
|
|
|
|
|
w.config.scheme = u.Scheme
|
|
|
|
if strings.HasSuffix(w.config.scheme, "+insecure") {
|
|
|
|
w.config.scheme = strings.TrimSuffix(w.config.scheme, "+insecure")
|
|
|
|
w.config.insecure = true
|
|
|
|
}
|
|
|
|
|
2019-07-10 21:00:28 +02:00
|
|
|
if strings.HasSuffix(w.config.scheme, "+oauthbearer") {
|
|
|
|
w.config.scheme = strings.TrimSuffix(w.config.scheme, "+oauthbearer")
|
|
|
|
w.config.oauthBearer.Enabled = true
|
|
|
|
q := u.Query()
|
2019-10-13 20:01:16 +02:00
|
|
|
|
|
|
|
oauth2 := &oauth2.Config{}
|
2019-07-10 21:00:28 +02:00
|
|
|
if q.Get("token_endpoint") != "" {
|
2019-10-13 20:01:16 +02:00
|
|
|
oauth2.ClientID = q.Get("client_id")
|
|
|
|
oauth2.ClientSecret = q.Get("client_secret")
|
|
|
|
oauth2.Scopes = []string{q.Get("scope")}
|
|
|
|
oauth2.Endpoint.TokenURL = q.Get("token_endpoint")
|
2019-07-10 21:00:28 +02:00
|
|
|
}
|
2019-10-13 20:01:16 +02:00
|
|
|
w.config.oauthBearer.OAuth2 = oauth2
|
2019-07-10 21:00:28 +02:00
|
|
|
}
|
|
|
|
|
2018-01-14 11:30:11 +01:00
|
|
|
w.config.addr = u.Host
|
|
|
|
if !strings.ContainsRune(w.config.addr, ':') {
|
2019-05-21 01:20:20 +02:00
|
|
|
w.config.addr += ":" + w.config.scheme
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
2018-01-14 11:30:11 +01:00
|
|
|
|
|
|
|
w.config.user = u.User
|
2019-06-12 08:31:51 +02:00
|
|
|
w.config.folders = msg.Config.Folders
|
2021-12-07 00:05:36 +01:00
|
|
|
w.config.connection_timeout = 30 * time.Second
|
|
|
|
w.config.keepalive_period = 0 * time.Second
|
|
|
|
w.config.keepalive_probes = 3
|
|
|
|
w.config.keepalive_interval = 3
|
|
|
|
for key, value := range msg.Config.Params {
|
|
|
|
switch key {
|
|
|
|
case "connection-timeout":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
2022-01-19 13:18:08 +01:00
|
|
|
reterr = fmt.Errorf(
|
2021-12-07 00:05:36 +01:00
|
|
|
"invalid connection-timeout value %v: %v",
|
|
|
|
value, err)
|
2022-01-19 13:18:08 +01:00
|
|
|
break
|
2021-12-07 00:05:36 +01:00
|
|
|
}
|
|
|
|
w.config.connection_timeout = val
|
|
|
|
case "keepalive-period":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
2022-01-19 13:18:08 +01:00
|
|
|
reterr = fmt.Errorf(
|
2021-12-07 00:05:36 +01:00
|
|
|
"invalid keepalive-period value %v: %v",
|
|
|
|
value, err)
|
2022-01-19 13:18:08 +01:00
|
|
|
break
|
2021-12-07 00:05:36 +01:00
|
|
|
}
|
|
|
|
w.config.keepalive_period = val
|
|
|
|
case "keepalive-probes":
|
|
|
|
val, err := strconv.Atoi(value)
|
|
|
|
if err != nil || val < 0 {
|
2022-01-19 13:18:08 +01:00
|
|
|
reterr = fmt.Errorf(
|
2021-12-07 00:05:36 +01:00
|
|
|
"invalid keepalive-probes value %v: %v",
|
|
|
|
value, err)
|
2022-01-19 13:18:08 +01:00
|
|
|
break
|
2021-12-07 00:05:36 +01:00
|
|
|
}
|
|
|
|
w.config.keepalive_probes = val
|
|
|
|
case "keepalive-interval":
|
|
|
|
val, err := time.ParseDuration(value)
|
|
|
|
if err != nil || val < 0 {
|
2022-01-19 13:18:08 +01:00
|
|
|
reterr = fmt.Errorf(
|
2021-12-07 00:05:36 +01:00
|
|
|
"invalid keepalive-interval value %v: %v",
|
|
|
|
value, err)
|
2022-01-19 13:18:08 +01:00
|
|
|
break
|
2021-12-07 00:05:36 +01:00
|
|
|
}
|
|
|
|
w.config.keepalive_interval = int(val.Seconds())
|
|
|
|
}
|
|
|
|
}
|
2018-02-02 01:54:19 +01:00
|
|
|
case *types.Connect:
|
2022-01-15 12:07:02 +01:00
|
|
|
if w.client != nil && w.client.State() == imap.SelectedState {
|
2022-02-05 00:07:16 +01:00
|
|
|
if !w.autoReconnect {
|
|
|
|
w.autoReconnect = true
|
2022-02-12 23:08:18 +01:00
|
|
|
checkConn(0)
|
2022-02-05 00:07:16 +01:00
|
|
|
}
|
2022-01-19 13:18:08 +01:00
|
|
|
reterr = fmt.Errorf("Already connected")
|
|
|
|
break
|
2021-11-01 21:38:26 +01:00
|
|
|
}
|
2022-02-05 00:07:16 +01:00
|
|
|
|
|
|
|
w.autoReconnect = true
|
2021-12-06 23:52:25 +01:00
|
|
|
c, err := w.connect()
|
|
|
|
if err != nil {
|
2022-02-20 00:09:30 +01:00
|
|
|
checkConn(0)
|
2022-01-19 13:18:08 +01:00
|
|
|
reterr = err
|
|
|
|
break
|
2018-01-14 11:30:11 +01:00
|
|
|
}
|
2022-01-15 12:07:02 +01:00
|
|
|
|
2022-01-19 13:18:09 +01:00
|
|
|
w.stopConnectionObserver()
|
|
|
|
|
2018-01-14 11:30:11 +01:00
|
|
|
c.Updates = w.updates
|
2021-11-12 18:12:02 +01:00
|
|
|
w.client = &imapClient{c, sortthread.NewThreadClient(c), sortthread.NewSortClient(c)}
|
2022-01-19 13:18:09 +01:00
|
|
|
|
|
|
|
w.startConnectionObserver()
|
|
|
|
|
2022-03-18 09:53:02 +01:00
|
|
|
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2022-01-19 13:18:10 +01:00
|
|
|
case *types.Reconnect:
|
|
|
|
if !w.autoReconnect {
|
|
|
|
reterr = fmt.Errorf("auto-reconnect is disabled; run connect to enable it")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c, err := w.connect()
|
|
|
|
if err != nil {
|
2022-02-12 23:08:18 +01:00
|
|
|
wait, msg := w.exponentialBackoff()
|
|
|
|
go checkConn(wait)
|
|
|
|
w.retries++
|
|
|
|
reterr = errors.Wrap(err, msg)
|
2022-01-19 13:18:10 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
w.stopConnectionObserver()
|
|
|
|
|
|
|
|
c.Updates = w.updates
|
|
|
|
w.client = &imapClient{c, sortthread.NewThreadClient(c), sortthread.NewSortClient(c)}
|
|
|
|
|
|
|
|
w.startConnectionObserver()
|
|
|
|
|
2022-03-18 09:53:02 +01:00
|
|
|
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2021-11-01 21:38:26 +01:00
|
|
|
case *types.Disconnect:
|
2022-02-05 00:07:16 +01:00
|
|
|
w.autoReconnect = false
|
|
|
|
w.stopConnectionObserver()
|
2021-11-22 21:31:42 +01:00
|
|
|
if w.client == nil || w.client.State() != imap.SelectedState {
|
2022-01-19 13:18:08 +01:00
|
|
|
reterr = fmt.Errorf("Not connected")
|
|
|
|
break
|
2021-11-01 21:38:26 +01:00
|
|
|
}
|
2022-01-19 13:18:09 +01:00
|
|
|
|
2021-11-01 21:38:26 +01:00
|
|
|
if err := w.client.Logout(); err != nil {
|
2022-01-19 13:18:08 +01:00
|
|
|
reterr = err
|
|
|
|
break
|
2021-11-01 21:38:26 +01:00
|
|
|
}
|
2022-03-18 09:53:02 +01:00
|
|
|
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2018-02-02 01:54:19 +01:00
|
|
|
case *types.ListDirectories:
|
2018-02-02 01:34:08 +01:00
|
|
|
w.handleListDirectories(msg)
|
2019-01-13 22:18:10 +01:00
|
|
|
case *types.OpenDirectory:
|
|
|
|
w.handleOpenDirectory(msg)
|
2019-03-11 04:45:00 +01:00
|
|
|
case *types.FetchDirectoryContents:
|
|
|
|
w.handleFetchDirectoryContents(msg)
|
2021-11-12 18:12:02 +01:00
|
|
|
case *types.FetchDirectoryThreaded:
|
|
|
|
w.handleDirectoryThreaded(msg)
|
2019-06-08 19:41:56 +02:00
|
|
|
case *types.CreateDirectory:
|
|
|
|
w.handleCreateDirectory(msg)
|
2020-08-18 22:27:23 +02:00
|
|
|
case *types.RemoveDirectory:
|
|
|
|
w.handleRemoveDirectory(msg)
|
2019-03-15 03:19:04 +01:00
|
|
|
case *types.FetchMessageHeaders:
|
|
|
|
w.handleFetchMessageHeaders(msg)
|
2019-03-31 18:14:37 +02:00
|
|
|
case *types.FetchMessageBodyPart:
|
|
|
|
w.handleFetchMessageBodyPart(msg)
|
2019-03-31 18:17:57 +02:00
|
|
|
case *types.FetchFullMessages:
|
|
|
|
w.handleFetchFullMessages(msg)
|
2019-03-21 04:23:38 +01:00
|
|
|
case *types.DeleteMessages:
|
|
|
|
w.handleDeleteMessages(msg)
|
2020-07-05 16:29:52 +02:00
|
|
|
case *types.FlagMessages:
|
|
|
|
w.handleFlagMessages(msg)
|
2020-05-25 16:59:48 +02:00
|
|
|
case *types.AnsweredMessages:
|
|
|
|
w.handleAnsweredMessages(msg)
|
2019-05-14 22:34:42 +02:00
|
|
|
case *types.CopyMessages:
|
|
|
|
w.handleCopyMessages(msg)
|
2019-05-16 01:41:21 +02:00
|
|
|
case *types.AppendMessage:
|
|
|
|
w.handleAppendMessage(msg)
|
2019-06-24 22:31:37 +02:00
|
|
|
case *types.SearchDirectory:
|
|
|
|
w.handleSearchDirectory(msg)
|
2018-01-14 11:30:11 +01:00
|
|
|
default:
|
2020-02-15 12:37:18 +01:00
|
|
|
reterr = errUnsupported
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
2019-05-14 02:16:55 +02:00
|
|
|
|
2020-02-15 12:37:18 +01:00
|
|
|
return reterr
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 22:18:10 +01:00
|
|
|
func (w *IMAPWorker) handleImapUpdate(update client.Update) {
|
|
|
|
w.worker.Logger.Printf("(= %T", update)
|
2022-04-28 21:51:54 +02:00
|
|
|
checkBounds := func(idx, size int) bool {
|
|
|
|
if idx < 0 || idx >= size {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-01-13 22:18:10 +01:00
|
|
|
switch update := update.(type) {
|
|
|
|
case *client.MailboxUpdate:
|
|
|
|
status := update.Mailbox
|
2019-03-11 04:45:00 +01:00
|
|
|
if w.selected.Name == status.Name {
|
2019-08-22 14:53:27 +02:00
|
|
|
w.selected = status
|
2019-03-11 04:45:00 +01:00
|
|
|
}
|
2019-01-13 22:18:10 +01:00
|
|
|
w.worker.PostMessage(&types.DirectoryInfo{
|
2019-07-08 04:43:56 +02:00
|
|
|
Info: &models.DirectoryInfo{
|
|
|
|
Flags: status.Flags,
|
|
|
|
Name: status.Name,
|
|
|
|
ReadOnly: status.ReadOnly,
|
|
|
|
|
|
|
|
Exists: int(status.Messages),
|
|
|
|
Recent: int(status.Recent),
|
|
|
|
Unseen: int(status.Unseen),
|
|
|
|
},
|
2019-01-13 22:18:10 +01:00
|
|
|
}, nil)
|
2019-06-09 20:55:04 +02:00
|
|
|
case *client.MessageUpdate:
|
|
|
|
msg := update.Message
|
|
|
|
if msg.Uid == 0 {
|
2022-04-28 21:51:54 +02:00
|
|
|
if ok := checkBounds(int(msg.SeqNum)-1, len(w.seqMap)); !ok {
|
|
|
|
w.worker.Logger.Println("MessageUpdate error: index out of range")
|
|
|
|
return
|
|
|
|
}
|
2019-06-09 20:55:04 +02:00
|
|
|
msg.Uid = w.seqMap[msg.SeqNum-1]
|
|
|
|
}
|
|
|
|
w.worker.PostMessage(&types.MessageInfo{
|
2019-07-08 04:43:56 +02:00
|
|
|
Info: &models.MessageInfo{
|
2019-07-08 04:43:58 +02:00
|
|
|
BodyStructure: translateBodyStructure(msg.BodyStructure),
|
|
|
|
Envelope: translateEnvelope(msg.Envelope),
|
2020-03-03 14:45:06 +01:00
|
|
|
Flags: translateImapFlags(msg.Flags),
|
2019-07-08 04:43:56 +02:00
|
|
|
InternalDate: msg.InternalDate,
|
|
|
|
Uid: msg.Uid,
|
|
|
|
},
|
2019-06-09 20:55:04 +02:00
|
|
|
}, nil)
|
2019-05-14 02:23:23 +02:00
|
|
|
case *client.ExpungeUpdate:
|
|
|
|
i := update.SeqNum - 1
|
2022-04-28 21:51:54 +02:00
|
|
|
if ok := checkBounds(int(i), len(w.seqMap)); !ok {
|
|
|
|
w.worker.Logger.Println("ExpungeUpdate error: index out of range")
|
|
|
|
return
|
|
|
|
}
|
2019-05-14 02:23:23 +02:00
|
|
|
uid := w.seqMap[i]
|
|
|
|
w.seqMap = append(w.seqMap[:i], w.seqMap[i+1:]...)
|
|
|
|
w.worker.PostMessage(&types.MessagesDeleted{
|
|
|
|
Uids: []uint32{uid},
|
|
|
|
}, nil)
|
2019-01-13 22:18:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 23:08:18 +01:00
|
|
|
func (w *IMAPWorker) exponentialBackoff() (time.Duration, string) {
|
|
|
|
maxWait := 16
|
|
|
|
if w.retries > 0 {
|
|
|
|
backoff := int(math.Pow(2.0, float64(w.retries)))
|
|
|
|
if backoff > maxWait {
|
|
|
|
backoff = maxWait
|
|
|
|
}
|
|
|
|
waitStr := fmt.Sprintf("%ds", backoff)
|
|
|
|
wait, err := time.ParseDuration(waitStr)
|
|
|
|
if err == nil {
|
|
|
|
return wait, fmt.Sprintf("wait %s before reconnect", waitStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0 * time.Second, ""
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:18:09 +01:00
|
|
|
func (w *IMAPWorker) startConnectionObserver() {
|
2022-02-20 00:09:30 +01:00
|
|
|
emitConnErr := func(errMsg string) {
|
|
|
|
w.worker.PostMessage(&types.ConnError{
|
|
|
|
Error: fmt.Errorf(errMsg),
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
if w.client == nil {
|
|
|
|
emitConnErr("imap client not connected")
|
|
|
|
return
|
|
|
|
}
|
2022-01-19 13:18:09 +01:00
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-w.client.LoggedOut():
|
2022-02-05 00:07:16 +01:00
|
|
|
if w.autoReconnect {
|
2022-02-20 00:09:30 +01:00
|
|
|
emitConnErr("imap: logged out")
|
2022-02-05 00:07:16 +01:00
|
|
|
}
|
2022-01-19 13:18:09 +01:00
|
|
|
case <-w.done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *IMAPWorker) stopConnectionObserver() {
|
|
|
|
if w.done != nil {
|
|
|
|
close(w.done)
|
|
|
|
}
|
|
|
|
w.done = make(chan struct{})
|
|
|
|
}
|
|
|
|
|
2021-12-06 23:52:25 +01:00
|
|
|
func (w *IMAPWorker) connect() (*client.Client, error) {
|
|
|
|
var (
|
2021-12-07 00:00:01 +01:00
|
|
|
conn *net.TCPConn
|
|
|
|
c *client.Client
|
2021-12-06 23:52:25 +01:00
|
|
|
)
|
2021-12-07 00:00:01 +01:00
|
|
|
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", w.config.addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err = net.DialTCP("tcp", nil, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-07 00:05:36 +01:00
|
|
|
if w.config.connection_timeout > 0 {
|
|
|
|
end := time.Now().Add(w.config.connection_timeout)
|
|
|
|
err = conn.SetDeadline(end)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if w.config.keepalive_period > 0 {
|
|
|
|
err = w.setKeepaliveParameters(conn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 00:00:01 +01:00
|
|
|
serverName, _, _ := net.SplitHostPort(w.config.addr)
|
|
|
|
tlsConfig := &tls.Config{ServerName: serverName}
|
|
|
|
|
2021-12-06 23:52:25 +01:00
|
|
|
switch w.config.scheme {
|
|
|
|
case "imap":
|
2021-12-07 00:00:01 +01:00
|
|
|
c, err = client.New(conn)
|
2021-12-06 23:52:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !w.config.insecure {
|
2021-12-07 00:00:01 +01:00
|
|
|
if err = c.StartTLS(tlsConfig); err != nil {
|
2021-12-06 23:52:25 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "imaps":
|
2021-12-07 00:00:01 +01:00
|
|
|
tlsConn := tls.Client(conn, tlsConfig)
|
|
|
|
c, err = client.New(tlsConn)
|
2021-12-06 23:52:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
|
|
|
|
}
|
2021-12-07 00:00:01 +01:00
|
|
|
|
2021-12-06 23:52:25 +01:00
|
|
|
c.ErrorLog = w.worker.Logger
|
|
|
|
|
|
|
|
if w.config.user != nil {
|
|
|
|
username := w.config.user.Username()
|
|
|
|
password, hasPassword := w.config.user.Password()
|
|
|
|
if !hasPassword {
|
|
|
|
// TODO: ask password
|
|
|
|
}
|
|
|
|
|
|
|
|
if w.config.oauthBearer.Enabled {
|
|
|
|
if err := w.config.oauthBearer.Authenticate(
|
|
|
|
username, password, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if err := c.Login(username, password); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetDebug(w.worker.Logger.Writer())
|
|
|
|
|
|
|
|
if _, err := c.Select(imap.InboxName, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-12 23:08:18 +01:00
|
|
|
w.retries = 0
|
|
|
|
|
2021-12-06 23:52:25 +01:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2021-12-07 00:05:36 +01:00
|
|
|
// Set additional keepalive parameters.
|
|
|
|
// Uses new interfaces introduced in Go1.11, which let us get connection's file
|
|
|
|
// descriptor, without blocking, and therefore without uncontrolled spawning of
|
|
|
|
// threads (not goroutines, actual threads).
|
|
|
|
func (w *IMAPWorker) setKeepaliveParameters(conn *net.TCPConn) error {
|
|
|
|
err := conn.SetKeepAlive(true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Idle time before sending a keepalive probe
|
|
|
|
err = conn.SetKeepAlivePeriod(w.config.keepalive_period)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rawConn, e := conn.SyscallConn()
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
err = rawConn.Control(func(fdPtr uintptr) {
|
|
|
|
fd := int(fdPtr)
|
|
|
|
// Max number of probes before failure
|
2021-12-11 20:53:10 +01:00
|
|
|
err := lib.SetTcpKeepaliveProbes(fd, w.config.keepalive_probes)
|
2021-12-07 00:05:36 +01:00
|
|
|
if err != nil {
|
|
|
|
w.worker.Logger.Printf(
|
|
|
|
"cannot set tcp keepalive probes: %v\n", err)
|
|
|
|
}
|
|
|
|
// Wait time after an unsuccessful probe
|
2021-12-11 20:53:10 +01:00
|
|
|
err = lib.SetTcpKeepaliveInterval(fd, w.config.keepalive_interval)
|
2021-12-07 00:05:36 +01:00
|
|
|
if err != nil {
|
|
|
|
w.worker.Logger.Printf(
|
|
|
|
"cannot set tcp keepalive interval: %v\n", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-10 02:39:00 +01:00
|
|
|
func (w *IMAPWorker) Run() {
|
|
|
|
for {
|
|
|
|
select {
|
2018-02-02 00:42:03 +01:00
|
|
|
case msg := <-w.worker.Actions:
|
|
|
|
msg = w.worker.ProcessAction(msg)
|
2018-01-14 11:30:11 +01:00
|
|
|
if err := w.handleMessage(msg); err == errUnsupported {
|
2018-02-02 01:54:19 +01:00
|
|
|
w.worker.PostMessage(&types.Unsupported{
|
2018-01-14 11:30:11 +01:00
|
|
|
Message: types.RespondTo(msg),
|
2018-02-02 00:42:03 +01:00
|
|
|
}, nil)
|
2018-01-14 11:30:11 +01:00
|
|
|
} else if err != nil {
|
2018-02-02 01:54:19 +01:00
|
|
|
w.worker.PostMessage(&types.Error{
|
2018-01-14 11:30:11 +01:00
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
2018-02-02 00:42:03 +01:00
|
|
|
}, nil)
|
2018-01-14 11:30:11 +01:00
|
|
|
}
|
|
|
|
case update := <-w.updates:
|
2019-01-13 22:18:10 +01:00
|
|
|
w.handleImapUpdate(update)
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|