aerc/worker/imap/worker.go

178 lines
3.8 KiB
Go
Raw Normal View History

package imap
import (
2018-02-01 03:54:52 +01:00
"crypto/tls"
"crypto/x509"
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"
"git.sr.ht/~sircmpwn/aerc2/worker/types"
)
2018-01-14 11:30:11 +01:00
var errUnsupported = fmt.Errorf("unsupported command")
type imapClient struct {
*client.Client
*idle.IdleClient
}
type IMAPWorker struct {
2018-01-14 11:30:11 +01:00
config struct {
scheme string
insecure bool
addr string
user *url.Userinfo
}
2018-02-02 00:42:03 +01:00
worker *types.Worker
2018-01-14 11:30:11 +01:00
client *imapClient
updates chan client.Update
}
2018-02-02 00:42:03 +01:00
func NewIMAPWorker(worker *types.Worker) *IMAPWorker {
return &IMAPWorker{
2018-02-02 00:42:03 +01:00
worker: worker,
updates: make(chan client.Update, 50),
}
}
2018-02-01 03:54:52 +01:00
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)
}
2018-02-02 01:54:19 +01:00
request := &types.CertificateApprovalRequest{
2018-02-01 03:54:52 +01:00
Message: types.RespondTo(msg),
CertPool: pool,
}
2018-02-02 00:42:03 +01:00
w.worker.PostMessage(request, nil)
2018-02-01 03:54:52 +01:00
2018-02-02 00:42:03 +01:00
response := <-w.worker.Actions
2018-02-01 03:54:52 +01:00
if response.InResponseTo() != request {
2018-02-02 01:54:19 +01:00
return fmt.Errorf("Expected UI to respond to cert request")
2018-02-01 03:54:52 +01:00
}
2018-02-02 01:54:19 +01:00
if approval, ok := response.(*types.ApproveCertificate); !ok {
return fmt.Errorf("Expected UI to send certificate approval")
} else {
if approval.Approved {
return nil
} else {
return fmt.Errorf("UI rejected certificate")
}
2018-02-01 03:54:52 +01:00
}
}
}
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-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, ':') {
w.config.addr += ":" + u.Scheme
}
2018-01-14 11:30:11 +01:00
w.config.scheme = u.Scheme
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
)
2018-02-01 03:54:52 +01:00
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
2018-02-02 01:54:19 +01:00
VerifyPeerCertificate: w.verifyPeerCert(msg),
2018-02-01 03:54:52 +01:00
}
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)}
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)
2018-01-14 11:30:11 +01:00
default:
return errUnsupported
}
2018-01-14 11:30:11 +01:00
return nil
}
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:
2018-02-02 00:42:03 +01:00
w.worker.Logger.Printf("(= %T", update)
}
}
}