2018-01-10 01:39:00 +00:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2018-01-14 10:30:11 +00:00
|
|
|
"fmt"
|
2018-02-01 02:18:21 +00:00
|
|
|
"log"
|
2018-01-14 10:30:11 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2018-01-10 16:19:45 +00:00
|
|
|
|
2018-02-01 02:18:21 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2018-01-14 10:30:11 +00:00
|
|
|
"github.com/emersion/go-imap"
|
|
|
|
"github.com/emersion/go-imap-idle"
|
2018-02-01 02:18:21 +00:00
|
|
|
"github.com/emersion/go-imap/client"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/worker/types"
|
2018-01-10 01:39:00 +00:00
|
|
|
)
|
|
|
|
|
2018-01-14 10:30:11 +00:00
|
|
|
var errUnsupported = fmt.Errorf("unsupported command")
|
|
|
|
|
|
|
|
type imapClient struct {
|
|
|
|
*client.Client
|
|
|
|
*idle.IdleClient
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:39:00 +00:00
|
|
|
type IMAPWorker struct {
|
|
|
|
messages chan types.WorkerMessage
|
|
|
|
actions chan types.WorkerMessage
|
2018-01-14 10:30:11 +00:00
|
|
|
|
|
|
|
config struct {
|
|
|
|
scheme string
|
|
|
|
insecure bool
|
|
|
|
addr string
|
|
|
|
user *url.Userinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
client *imapClient
|
|
|
|
updates chan client.Update
|
2018-02-01 02:18:21 +00:00
|
|
|
logger *log.Logger
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 02:18:21 +00:00
|
|
|
func NewIMAPWorker(logger *log.Logger) *IMAPWorker {
|
2018-01-10 01:39:00 +00:00
|
|
|
return &IMAPWorker{
|
|
|
|
messages: make(chan types.WorkerMessage, 50),
|
|
|
|
actions: make(chan types.WorkerMessage, 50),
|
2018-01-14 10:30:11 +00:00
|
|
|
updates: make(chan client.Update, 50),
|
2018-02-01 02:18:21 +00:00
|
|
|
logger: logger,
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 14:04:18 +00:00
|
|
|
func (w *IMAPWorker) GetMessages() chan types.WorkerMessage {
|
|
|
|
return w.messages
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *IMAPWorker) PostAction(msg types.WorkerMessage) {
|
|
|
|
w.actions <- msg
|
|
|
|
}
|
|
|
|
|
2018-01-14 10:30:11 +00:00
|
|
|
func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
|
2018-01-10 16:19:45 +00:00
|
|
|
switch msg := msg.(type) {
|
2018-01-10 01:39:00 +00:00
|
|
|
case types.Ping:
|
2018-01-14 10:30:11 +00:00
|
|
|
// No-op
|
|
|
|
case types.Configure:
|
|
|
|
u, err := url.Parse(msg.Config.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
2018-01-14 10:30:11 +00: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, ':') {
|
|
|
|
w.config.addr += ":" + u.Scheme
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
2018-01-14 10:30:11 +00:00
|
|
|
|
|
|
|
w.config.scheme = u.Scheme
|
|
|
|
w.config.user = u.User
|
|
|
|
case types.Connect:
|
|
|
|
// TODO: populate TLS config
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if err := c.StartTLS(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "imaps":
|
|
|
|
c, err = client.DialTLS(w.config.addr, nil)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := c.Select(imap.InboxName, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Updates = w.updates
|
|
|
|
w.client = &imapClient{c, idle.NewClient(c)}
|
|
|
|
|
|
|
|
// TODO: don't idle right away
|
|
|
|
go w.client.IdleWithFallback(nil, 0)
|
|
|
|
default:
|
|
|
|
return errUnsupported
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
2018-01-14 10:30:11 +00:00
|
|
|
return nil
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 02:18:21 +00:00
|
|
|
// Logs an action but censors passwords
|
|
|
|
func (w *IMAPWorker) logAction(msg types.WorkerMessage) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case types.Configure:
|
|
|
|
src := msg.Config.Source
|
|
|
|
msg.Config.Source = "[obsfucated]"
|
|
|
|
w.logger.Printf("<= %s", spew.Sdump(msg))
|
|
|
|
msg.Config.Source = src
|
|
|
|
default:
|
|
|
|
w.logger.Printf("<= %s", spew.Sdump(msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:39:00 +00:00
|
|
|
func (w *IMAPWorker) Run() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-w.actions:
|
2018-02-01 02:18:21 +00:00
|
|
|
w.logAction(msg)
|
2018-01-14 10:30:11 +00:00
|
|
|
if err := w.handleMessage(msg); err == errUnsupported {
|
|
|
|
w.messages <- types.Unsupported{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
w.messages <- types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.messages <- types.Ack{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case update := <-w.updates:
|
2018-02-01 02:18:21 +00:00
|
|
|
w.logger.Printf("[= %s", spew.Sdump(update))
|
2018-01-10 01:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|