aerc/worker/imap/worker.go

204 lines
4.5 KiB
Go
Raw Normal View History

package imap
import (
2018-02-01 03:54:52 +01:00
"crypto/tls"
2018-01-14 11:30:11 +01:00
"fmt"
"net/url"
"strings"
2018-01-10 17:19:45 +01:00
2018-01-14 11:30:11 +01:00
"github.com/emersion/go-imap"
"github.com/emersion/go-imap-idle"
2018-02-01 03:18:21 +01:00
"github.com/emersion/go-imap/client"
2019-05-18 02:57:10 +02:00
"git.sr.ht/~sircmpwn/aerc/worker/types"
)
2018-01-14 11:30:11 +01:00
var errUnsupported = fmt.Errorf("unsupported command")
type imapClient struct {
*client.Client
2019-05-14 02:16:55 +02:00
idle *idle.IdleClient
2018-01-14 11:30:11 +01:00
}
type IMAPWorker struct {
2018-01-14 11:30:11 +01:00
config struct {
scheme string
insecure bool
addr string
user *url.Userinfo
}
client *imapClient
2019-05-14 02:16:55 +02:00
idleStop chan struct{}
idleDone chan error
selected imap.MailboxStatus
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
seqMap []uint32
}
2018-02-02 00:42:03 +01:00
func NewIMAPWorker(worker *types.Worker) *IMAPWorker {
return &IMAPWorker{
2019-05-14 02:16:55 +02:00
idleDone: make(chan error),
updates: make(chan client.Update, 50),
worker: worker,
}
}
2018-01-14 11:30:11 +01:00
func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
2019-05-14 02:16:55 +02:00
if w.idleStop != nil {
close(w.idleStop)
if err := <-w.idleDone; err != nil {
w.worker.PostMessage(&types.Error{Error: err}, nil)
}
}
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 {
return err
}
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
}
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-14 11:30:11 +01:00
w.config.user = u.User
2018-02-02 01:54:19 +01:00
case *types.Connect:
2018-01-14 11:30:11 +01:00
var (
c *client.Client
err error
)
switch w.config.scheme {
case "imap":
c, err = client.Dial(w.config.addr)
if err != nil {
return err
}
if !w.config.insecure {
2019-05-20 20:01:59 +02:00
if err := c.StartTLS(&tls.Config{}); err != nil {
2018-01-14 11:30:11 +01:00
return err
}
}
case "imaps":
2019-05-20 20:01:59 +02:00
c, err = client.DialTLS(w.config.addr, &tls.Config{})
2018-01-14 11:30:11 +01:00
if err != nil {
return err
}
default:
return fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
}
if w.config.user != nil {
username := w.config.user.Username()
password, hasPassword := w.config.user.Password()
if !hasPassword {
// TODO: ask password
}
if err := c.Login(username, password); err != nil {
return err
}
}
c.SetDebug(w.worker.Logger.Writer())
2018-01-14 11:30:11 +01:00
if _, err := c.Select(imap.InboxName, false); err != nil {
return err
}
c.Updates = w.updates
w.client = &imapClient{c, idle.NewClient(c)}
2018-02-02 01:54:19 +01:00
w.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
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)
case *types.FetchDirectoryContents:
w.handleFetchDirectoryContents(msg)
case *types.CreateDirectory:
w.handleCreateDirectory(msg)
case *types.FetchMessageHeaders:
w.handleFetchMessageHeaders(msg)
2019-03-31 18:14:37 +02:00
case *types.FetchMessageBodyPart:
w.handleFetchMessageBodyPart(msg)
case *types.FetchFullMessages:
w.handleFetchFullMessages(msg)
2019-03-21 04:23:38 +01:00
case *types.DeleteMessages:
w.handleDeleteMessages(msg)
2019-05-14 22:34:42 +02:00
case *types.CopyMessages:
w.handleCopyMessages(msg)
case *types.AppendMessage:
w.handleAppendMessage(msg)
2018-01-14 11:30:11 +01:00
default:
return errUnsupported
}
2019-05-14 02:16:55 +02:00
if w.idleStop != nil {
w.idleStop = make(chan struct{})
go func() {
w.idleDone <- w.client.idle.IdleWithFallback(w.idleStop, 0)
}()
}
2018-01-14 11:30:11 +01:00
return nil
}
2019-01-13 22:18:10 +01:00
func (w *IMAPWorker) handleImapUpdate(update client.Update) {
w.worker.Logger.Printf("(= %T", update)
switch update := update.(type) {
case *client.MailboxUpdate:
status := update.Mailbox
if w.selected.Name == status.Name {
w.selected = *status
}
2019-01-13 22:18:10 +01:00
w.worker.PostMessage(&types.DirectoryInfo{
Flags: status.Flags,
2019-01-14 01:37:06 +01:00
Name: status.Name,
ReadOnly: status.ReadOnly,
2019-01-13 22:18:10 +01:00
Exists: int(status.Messages),
Recent: int(status.Recent),
Unseen: int(status.Unseen),
}, nil)
2019-05-14 02:23:23 +02:00
case *client.ExpungeUpdate:
i := update.SeqNum - 1
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
}
}
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)
}
}
}