2018-01-10 01:39:00 +00:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2018-01-14 10:30:11 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2021-12-06 23:05:36 +00:00
|
|
|
"time"
|
2018-01-10 16:19:45 +00:00
|
|
|
|
2018-01-14 10:30:11 +00:00
|
|
|
"github.com/emersion/go-imap"
|
2020-09-12 13:05:02 +00:00
|
|
|
sortthread "github.com/emersion/go-imap-sortthread"
|
2018-02-01 02:18:21 +00:00
|
|
|
"github.com/emersion/go-imap/client"
|
2022-02-12 22:08:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-02-01 02:18:21 +00:00
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib"
|
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/handlers"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2018-01-10 01:39:00 +00:00
|
|
|
)
|
|
|
|
|
2019-07-18 04:25:42 +00:00
|
|
|
func init() {
|
|
|
|
handlers.RegisterWorkerFactory("imap", NewIMAPWorker)
|
|
|
|
handlers.RegisterWorkerFactory("imaps", NewIMAPWorker)
|
|
|
|
}
|
|
|
|
|
2022-04-29 23:08:54 +00:00
|
|
|
var (
|
|
|
|
errUnsupported = fmt.Errorf("unsupported command")
|
2022-04-29 23:08:58 +00:00
|
|
|
errClientNotReady = fmt.Errorf("client not ready")
|
2022-04-29 23:08:54 +00:00
|
|
|
errNotConnected = fmt.Errorf("not connected")
|
|
|
|
errAlreadyConnected = fmt.Errorf("already connected")
|
|
|
|
)
|
2018-01-14 10:30:11 +00:00
|
|
|
|
|
|
|
type imapClient struct {
|
|
|
|
*client.Client
|
2021-11-12 17:12:02 +00:00
|
|
|
thread *sortthread.ThreadClient
|
|
|
|
sort *sortthread.SortClient
|
2018-01-14 10:30:11 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 23:08:54 +00:00
|
|
|
type imapConfig struct {
|
2022-04-29 23:08:56 +00:00
|
|
|
scheme string
|
|
|
|
insecure bool
|
|
|
|
addr string
|
|
|
|
user *url.Userinfo
|
|
|
|
folders []string
|
|
|
|
oauthBearer lib.OAuthBearer
|
|
|
|
idle_timeout time.Duration
|
2022-04-29 23:08:57 +00:00
|
|
|
idle_debounce time.Duration
|
2022-04-29 23:08:56 +00:00
|
|
|
reconnect_maxwait time.Duration
|
2022-04-29 23:08:54 +00:00
|
|
|
// tcp connection parameters
|
|
|
|
connection_timeout time.Duration
|
|
|
|
keepalive_period time.Duration
|
|
|
|
keepalive_probes int
|
|
|
|
keepalive_interval int
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:39:00 +00:00
|
|
|
type IMAPWorker struct {
|
2022-04-29 23:08:54 +00:00
|
|
|
config imapConfig
|
2018-01-14 10:30:11 +00:00
|
|
|
|
2019-03-11 03:45:00 +00:00
|
|
|
client *imapClient
|
2019-08-22 12:53:27 +00:00
|
|
|
selected *imap.MailboxStatus
|
2019-03-11 03:45:00 +00:00
|
|
|
updates chan client.Update
|
|
|
|
worker *types.Worker
|
2019-03-21 03:23:38 +00:00
|
|
|
// Map of sequence numbers to UIDs, index 0 is seq number 1
|
2022-04-29 23:08:55 +00:00
|
|
|
seqMap []uint32
|
|
|
|
|
2022-04-29 23:08:56 +00:00
|
|
|
idler *idler
|
|
|
|
observer *observer
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 04:25:42 +00:00
|
|
|
func NewIMAPWorker(worker *types.Worker) (types.Backend, error) {
|
2018-01-10 01:39:00 +00:00
|
|
|
return &IMAPWorker{
|
2019-05-14 00:16:55 +00:00
|
|
|
updates: make(chan client.Update, 50),
|
|
|
|
worker: worker,
|
2019-08-22 12:53:27 +00:00
|
|
|
selected: &imap.MailboxStatus{},
|
2022-04-29 23:08:55 +00:00
|
|
|
idler: newIdler(imapConfig{}, worker),
|
2022-04-29 23:08:56 +00:00
|
|
|
observer: newObserver(imapConfig{}, worker),
|
2019-07-18 04:25:42 +00:00
|
|
|
}, nil
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-29 23:08:55 +00:00
|
|
|
func (w *IMAPWorker) newClient(c *client.Client) {
|
|
|
|
c.Updates = w.updates
|
|
|
|
w.client = &imapClient{c, sortthread.NewThreadClient(c), sortthread.NewSortClient(c)}
|
|
|
|
w.idler.SetClient(w.client)
|
2022-04-29 23:08:56 +00:00
|
|
|
w.observer.SetClient(w.client)
|
2022-04-29 23:08:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 10:30:11 +00:00
|
|
|
func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
|
2022-01-19 12:18:08 +00:00
|
|
|
defer func() {
|
2022-04-29 23:08:55 +00:00
|
|
|
w.idler.Start()
|
2022-01-19 12:18:08 +00:00
|
|
|
}()
|
2022-04-29 23:08:55 +00:00
|
|
|
if err := w.idler.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var reterr error // will be returned at the end, needed to support idle
|
2019-05-14 00:16:55 +00:00
|
|
|
|
2022-04-29 23:08:58 +00:00
|
|
|
// when client is nil allow only certain messages to be handled
|
|
|
|
if w.client == nil {
|
|
|
|
switch msg.(type) {
|
|
|
|
case *types.Connect, *types.Reconnect, *types.Disconnect, *types.Configure:
|
|
|
|
default:
|
|
|
|
return errClientNotReady
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 23:08:55 +00:00
|
|
|
// set connection timeout for calls to imap server
|
|
|
|
if w.client != nil {
|
|
|
|
w.client.Timeout = w.config.connection_timeout
|
|
|
|
}
|
2020-02-15 11:37:18 +00:00
|
|
|
|
2018-01-10 16:19:45 +00:00
|
|
|
switch msg := msg.(type) {
|
2018-02-02 00:54:19 +00:00
|
|
|
case *types.Unsupported:
|
2018-01-14 10:30:11 +00:00
|
|
|
// No-op
|
2018-02-02 00:54:19 +00:00
|
|
|
case *types.Configure:
|
2022-04-29 23:08:54 +00:00
|
|
|
reterr = w.handleConfigure(msg)
|
2018-02-02 00:54:19 +00:00
|
|
|
case *types.Connect:
|
2022-01-15 11:07:02 +00:00
|
|
|
if w.client != nil && w.client.State() == imap.SelectedState {
|
2022-04-29 23:08:56 +00:00
|
|
|
if !w.observer.AutoReconnect() {
|
|
|
|
w.observer.SetAutoReconnect(true)
|
|
|
|
w.observer.EmitIfNotConnected()
|
2022-02-04 23:07:16 +00:00
|
|
|
}
|
2022-04-29 23:08:54 +00:00
|
|
|
reterr = errAlreadyConnected
|
2022-01-19 12:18:08 +00:00
|
|
|
break
|
2021-11-01 20:38:26 +00:00
|
|
|
}
|
2022-02-04 23:07:16 +00:00
|
|
|
|
2022-04-29 23:08:56 +00:00
|
|
|
w.observer.SetAutoReconnect(true)
|
2021-12-06 22:52:25 +00:00
|
|
|
c, err := w.connect()
|
|
|
|
if err != nil {
|
2022-04-29 23:08:56 +00:00
|
|
|
w.observer.EmitIfNotConnected()
|
2022-01-19 12:18:08 +00:00
|
|
|
reterr = err
|
|
|
|
break
|
2018-01-14 10:30:11 +00:00
|
|
|
}
|
2022-01-15 11:07:02 +00:00
|
|
|
|
2022-04-29 23:08:55 +00:00
|
|
|
w.newClient(c)
|
2022-01-19 12:18:09 +00:00
|
|
|
|
2022-03-18 08:53:02 +00:00
|
|
|
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2022-01-19 12:18:10 +00:00
|
|
|
case *types.Reconnect:
|
2022-04-29 23:08:56 +00:00
|
|
|
if !w.observer.AutoReconnect() {
|
2022-01-19 12:18:10 +00:00
|
|
|
reterr = fmt.Errorf("auto-reconnect is disabled; run connect to enable it")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c, err := w.connect()
|
|
|
|
if err != nil {
|
2022-04-29 23:08:56 +00:00
|
|
|
errReconnect := w.observer.DelayedReconnect()
|
|
|
|
reterr = errors.Wrap(errReconnect, err.Error())
|
2022-01-19 12:18:10 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-04-29 23:08:55 +00:00
|
|
|
w.newClient(c)
|
2022-01-19 12:18:10 +00:00
|
|
|
|
2022-03-18 08:53:02 +00:00
|
|
|
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2021-11-01 20:38:26 +00:00
|
|
|
case *types.Disconnect:
|
2022-04-29 23:08:56 +00:00
|
|
|
w.observer.SetAutoReconnect(false)
|
|
|
|
w.observer.Stop()
|
2021-11-22 20:31:42 +00:00
|
|
|
if w.client == nil || w.client.State() != imap.SelectedState {
|
2022-04-29 23:08:54 +00:00
|
|
|
reterr = errNotConnected
|
2022-01-19 12:18:08 +00:00
|
|
|
break
|
2021-11-01 20:38:26 +00:00
|
|
|
}
|
2022-01-19 12:18:09 +00:00
|
|
|
|
2021-11-01 20:38:26 +00:00
|
|
|
if err := w.client.Logout(); err != nil {
|
2022-01-19 12:18:08 +00:00
|
|
|
reterr = err
|
|
|
|
break
|
2021-11-01 20:38:26 +00:00
|
|
|
}
|
2022-03-18 08:53:02 +00:00
|
|
|
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2018-02-02 00:54:19 +00:00
|
|
|
case *types.ListDirectories:
|
2018-02-02 00:34:08 +00:00
|
|
|
w.handleListDirectories(msg)
|
2019-01-13 21:18:10 +00:00
|
|
|
case *types.OpenDirectory:
|
|
|
|
w.handleOpenDirectory(msg)
|
2019-03-11 03:45:00 +00:00
|
|
|
case *types.FetchDirectoryContents:
|
|
|
|
w.handleFetchDirectoryContents(msg)
|
2021-11-12 17:12:02 +00:00
|
|
|
case *types.FetchDirectoryThreaded:
|
|
|
|
w.handleDirectoryThreaded(msg)
|
2019-06-08 17:41:56 +00:00
|
|
|
case *types.CreateDirectory:
|
|
|
|
w.handleCreateDirectory(msg)
|
2020-08-18 20:27:23 +00:00
|
|
|
case *types.RemoveDirectory:
|
|
|
|
w.handleRemoveDirectory(msg)
|
2019-03-15 02:19:04 +00:00
|
|
|
case *types.FetchMessageHeaders:
|
|
|
|
w.handleFetchMessageHeaders(msg)
|
2019-03-31 16:14:37 +00:00
|
|
|
case *types.FetchMessageBodyPart:
|
|
|
|
w.handleFetchMessageBodyPart(msg)
|
2019-03-31 16:17:57 +00:00
|
|
|
case *types.FetchFullMessages:
|
|
|
|
w.handleFetchFullMessages(msg)
|
2019-03-21 03:23:38 +00:00
|
|
|
case *types.DeleteMessages:
|
|
|
|
w.handleDeleteMessages(msg)
|
2020-07-05 14:29:52 +00:00
|
|
|
case *types.FlagMessages:
|
|
|
|
w.handleFlagMessages(msg)
|
2020-05-25 14:59:48 +00:00
|
|
|
case *types.AnsweredMessages:
|
|
|
|
w.handleAnsweredMessages(msg)
|
2019-05-14 20:34:42 +00:00
|
|
|
case *types.CopyMessages:
|
|
|
|
w.handleCopyMessages(msg)
|
2019-05-15 23:41:21 +00:00
|
|
|
case *types.AppendMessage:
|
|
|
|
w.handleAppendMessage(msg)
|
2019-06-24 20:31:37 +00:00
|
|
|
case *types.SearchDirectory:
|
|
|
|
w.handleSearchDirectory(msg)
|
2018-01-14 10:30:11 +00:00
|
|
|
default:
|
2020-02-15 11:37:18 +00:00
|
|
|
reterr = errUnsupported
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
2019-05-14 00:16:55 +00:00
|
|
|
|
2022-04-29 23:08:55 +00:00
|
|
|
// we don't want idle to timeout, so set timeout to zero
|
|
|
|
if w.client != nil {
|
|
|
|
w.client.Timeout = 0
|
|
|
|
}
|
|
|
|
|
2020-02-15 11:37:18 +00:00
|
|
|
return reterr
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 21:18:10 +00:00
|
|
|
func (w *IMAPWorker) handleImapUpdate(update client.Update) {
|
|
|
|
w.worker.Logger.Printf("(= %T", update)
|
2022-04-28 19:51:54 +00:00
|
|
|
checkBounds := func(idx, size int) bool {
|
|
|
|
if idx < 0 || idx >= size {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-01-13 21:18:10 +00:00
|
|
|
switch update := update.(type) {
|
|
|
|
case *client.MailboxUpdate:
|
|
|
|
status := update.Mailbox
|
2019-03-11 03:45:00 +00:00
|
|
|
if w.selected.Name == status.Name {
|
2019-08-22 12:53:27 +00:00
|
|
|
w.selected = status
|
2019-03-11 03:45:00 +00:00
|
|
|
}
|
2019-01-13 21:18:10 +00:00
|
|
|
w.worker.PostMessage(&types.DirectoryInfo{
|
2019-07-08 02:43:56 +00: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 21:18:10 +00:00
|
|
|
}, nil)
|
2019-06-09 18:55:04 +00:00
|
|
|
case *client.MessageUpdate:
|
|
|
|
msg := update.Message
|
|
|
|
if msg.Uid == 0 {
|
2022-04-28 19:51:54 +00: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 18:55:04 +00:00
|
|
|
msg.Uid = w.seqMap[msg.SeqNum-1]
|
|
|
|
}
|
|
|
|
w.worker.PostMessage(&types.MessageInfo{
|
2019-07-08 02:43:56 +00:00
|
|
|
Info: &models.MessageInfo{
|
2019-07-08 02:43:58 +00:00
|
|
|
BodyStructure: translateBodyStructure(msg.BodyStructure),
|
|
|
|
Envelope: translateEnvelope(msg.Envelope),
|
2020-03-03 13:45:06 +00:00
|
|
|
Flags: translateImapFlags(msg.Flags),
|
2019-07-08 02:43:56 +00:00
|
|
|
InternalDate: msg.InternalDate,
|
|
|
|
Uid: msg.Uid,
|
|
|
|
},
|
2019-06-09 18:55:04 +00:00
|
|
|
}, nil)
|
2019-05-14 00:23:23 +00:00
|
|
|
case *client.ExpungeUpdate:
|
|
|
|
i := update.SeqNum - 1
|
2022-04-28 19:51:54 +00:00
|
|
|
if ok := checkBounds(int(i), len(w.seqMap)); !ok {
|
|
|
|
w.worker.Logger.Println("ExpungeUpdate error: index out of range")
|
|
|
|
return
|
|
|
|
}
|
2019-05-14 00:23:23 +00: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 21:18:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:39:00 +00:00
|
|
|
func (w *IMAPWorker) Run() {
|
|
|
|
for {
|
|
|
|
select {
|
2018-02-01 23:42:03 +00:00
|
|
|
case msg := <-w.worker.Actions:
|
|
|
|
msg = w.worker.ProcessAction(msg)
|
2022-04-29 23:08:55 +00:00
|
|
|
|
2018-01-14 10:30:11 +00:00
|
|
|
if err := w.handleMessage(msg); err == errUnsupported {
|
2018-02-02 00:54:19 +00:00
|
|
|
w.worker.PostMessage(&types.Unsupported{
|
2018-01-14 10:30:11 +00:00
|
|
|
Message: types.RespondTo(msg),
|
2018-02-01 23:42:03 +00:00
|
|
|
}, nil)
|
2018-01-14 10:30:11 +00:00
|
|
|
} else if err != nil {
|
2018-02-02 00:54:19 +00:00
|
|
|
w.worker.PostMessage(&types.Error{
|
2018-01-14 10:30:11 +00:00
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
2018-02-01 23:42:03 +00:00
|
|
|
}, nil)
|
2018-01-14 10:30:11 +00:00
|
|
|
}
|
2022-04-29 23:08:55 +00:00
|
|
|
|
2018-01-14 10:30:11 +00:00
|
|
|
case update := <-w.updates:
|
2019-01-13 21:18:10 +00:00
|
|
|
w.handleImapUpdate(update)
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|