2018-01-10 02:39:00 +01:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
2018-02-01 03:54:52 +01:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2018-01-14 11:30:11 +01:00
|
|
|
"fmt"
|
2018-02-01 03:18:21 +01:00
|
|
|
"log"
|
2018-01-14 11:30:11 +01:00
|
|
|
"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"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/worker/types"
|
2018-01-10 02:39:00 +01:00
|
|
|
)
|
|
|
|
|
2018-01-14 11:30:11 +01:00
|
|
|
var errUnsupported = fmt.Errorf("unsupported command")
|
|
|
|
|
|
|
|
type imapClient struct {
|
|
|
|
*client.Client
|
|
|
|
*idle.IdleClient
|
|
|
|
}
|
|
|
|
|
2018-01-10 02:39:00 +01:00
|
|
|
type IMAPWorker struct {
|
|
|
|
messages chan types.WorkerMessage
|
|
|
|
actions chan types.WorkerMessage
|
2018-01-14 11:30:11 +01:00
|
|
|
|
|
|
|
config struct {
|
|
|
|
scheme string
|
|
|
|
insecure bool
|
|
|
|
addr string
|
|
|
|
user *url.Userinfo
|
|
|
|
}
|
|
|
|
|
|
|
|
client *imapClient
|
|
|
|
updates chan client.Update
|
2018-02-01 03:18:21 +01:00
|
|
|
logger *log.Logger
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:18:21 +01:00
|
|
|
func NewIMAPWorker(logger *log.Logger) *IMAPWorker {
|
2018-01-10 02:39:00 +01:00
|
|
|
return &IMAPWorker{
|
|
|
|
messages: make(chan types.WorkerMessage, 50),
|
|
|
|
actions: make(chan types.WorkerMessage, 50),
|
2018-01-14 11:30:11 +01:00
|
|
|
updates: make(chan client.Update, 50),
|
2018-02-01 03:18:21 +01:00
|
|
|
logger: logger,
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 15:04:18 +01:00
|
|
|
func (w *IMAPWorker) GetMessages() chan types.WorkerMessage {
|
|
|
|
return w.messages
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *IMAPWorker) PostAction(msg types.WorkerMessage) {
|
|
|
|
w.actions <- msg
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:54:52 +01:00
|
|
|
func (w *IMAPWorker) postMessage(msg types.WorkerMessage) {
|
|
|
|
w.logger.Printf("=> %T\n", msg)
|
|
|
|
w.messages <- msg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *IMAPWorker) verifyPeerCert(msg types.WorkerMessage) func(
|
|
|
|
rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
|
|
|
|
|
|
|
return func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
for _, rawCert := range rawCerts {
|
|
|
|
cert, err := x509.ParseCertificate(rawCert)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pool.AddCert(cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
request := types.ApproveCertificate{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
CertPool: pool,
|
|
|
|
}
|
|
|
|
w.postMessage(request)
|
|
|
|
|
|
|
|
response := <-w.actions
|
|
|
|
if response.InResponseTo() != request {
|
|
|
|
return fmt.Errorf("Expected UI to answer cert request")
|
|
|
|
}
|
|
|
|
switch response.(type) {
|
|
|
|
case types.Ack:
|
|
|
|
return nil
|
|
|
|
case types.Disconnect:
|
|
|
|
return fmt.Errorf("UI rejected certificate")
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Expected UI to answer cert request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 11:30:11 +01:00
|
|
|
func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error {
|
2018-01-10 17:19:45 +01:00
|
|
|
switch msg := msg.(type) {
|
2018-01-10 02:39:00 +01:00
|
|
|
case types.Ping:
|
2018-01-14 11:30:11 +01:00
|
|
|
// No-op
|
|
|
|
case types.Configure:
|
|
|
|
u, err := url.Parse(msg.Config.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
w.config.addr = u.Host
|
|
|
|
if !strings.ContainsRune(w.config.addr, ':') {
|
|
|
|
w.config.addr += ":" + u.Scheme
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
2018-01-14 11:30:11 +01:00
|
|
|
|
|
|
|
w.config.scheme = u.Scheme
|
|
|
|
w.config.user = u.User
|
|
|
|
case types.Connect:
|
|
|
|
var (
|
|
|
|
c *client.Client
|
|
|
|
err error
|
|
|
|
)
|
2018-02-01 03:54:52 +01:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
VerifyPeerCertificate: w.verifyPeerCert(&msg),
|
|
|
|
}
|
2018-01-14 11:30:11 +01:00
|
|
|
switch w.config.scheme {
|
|
|
|
case "imap":
|
|
|
|
c, err = client.Dial(w.config.addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !w.config.insecure {
|
2018-02-01 03:54:52 +01:00
|
|
|
if err := c.StartTLS(tlsConfig); err != nil {
|
2018-01-14 11:30:11 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "imaps":
|
2018-02-01 03:54:52 +01:00
|
|
|
c, err = client.DialTLS(w.config.addr, tlsConfig)
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 02:39:00 +01:00
|
|
|
}
|
2018-01-14 11:30:11 +01:00
|
|
|
return nil
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *IMAPWorker) Run() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-w.actions:
|
2018-02-01 03:54:52 +01:00
|
|
|
w.logger.Printf("<= %T\n", msg)
|
2018-01-14 11:30:11 +01:00
|
|
|
if err := w.handleMessage(msg); err == errUnsupported {
|
2018-02-01 03:54:52 +01:00
|
|
|
w.postMessage(types.Unsupported{
|
2018-01-14 11:30:11 +01:00
|
|
|
Message: types.RespondTo(msg),
|
2018-02-01 03:54:52 +01:00
|
|
|
})
|
2018-01-14 11:30:11 +01:00
|
|
|
} else if err != nil {
|
2018-02-01 03:54:52 +01:00
|
|
|
w.postMessage(types.Error{
|
2018-01-14 11:30:11 +01:00
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
2018-02-01 03:54:52 +01:00
|
|
|
})
|
2018-01-14 11:30:11 +01:00
|
|
|
} else {
|
2018-02-01 03:54:52 +01:00
|
|
|
w.postMessage(types.Ack{
|
2018-01-14 11:30:11 +01:00
|
|
|
Message: types.RespondTo(msg),
|
2018-02-01 03:54:52 +01:00
|
|
|
})
|
2018-01-14 11:30:11 +01:00
|
|
|
}
|
|
|
|
case update := <-w.updates:
|
2018-02-01 03:54:52 +01:00
|
|
|
w.logger.Printf("[= %T", update)
|
2018-01-10 02:39:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|