2019-12-20 19:21:35 +01:00
|
|
|
package completer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-07-31 15:15:27 +02:00
|
|
|
"errors"
|
2019-12-20 19:21:35 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-12-30 15:41:32 +01:00
|
|
|
"mime"
|
2019-12-20 19:21:35 +01:00
|
|
|
"net/mail"
|
|
|
|
"os/exec"
|
2022-01-07 05:54:28 +01:00
|
|
|
"regexp"
|
2019-12-20 19:21:35 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/shlex"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Completer is used to autocomplete text inputs based on the configured
|
|
|
|
// completion commands.
|
|
|
|
type Completer struct {
|
|
|
|
// AddressBookCmd is the command to run for completing email addresses. This
|
|
|
|
// command must output one completion on each line with fields separated by a
|
|
|
|
// tab character. The first field must be the address, and the second field,
|
|
|
|
// if present, the contact name. Only the email address field is required.
|
|
|
|
// The name field is optional. Additional fields are ignored.
|
|
|
|
AddressBookCmd string
|
|
|
|
|
|
|
|
errHandler func(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// A CompleteFunc accepts a string to be completed and returns a slice of
|
2022-01-07 05:54:28 +01:00
|
|
|
// completions candidates with a prefix to prepend to the chosen candidate
|
|
|
|
type CompleteFunc func(string) ([]string, string)
|
2019-12-20 19:21:35 +01:00
|
|
|
|
|
|
|
// New creates a new Completer with the specified address book command.
|
2022-07-11 11:11:36 +02:00
|
|
|
func New(addressBookCmd string, errHandler func(error)) *Completer {
|
2019-12-20 19:21:35 +01:00
|
|
|
return &Completer{
|
|
|
|
AddressBookCmd: addressBookCmd,
|
|
|
|
errHandler: errHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForHeader returns a CompleteFunc appropriate for the specified mail header. In
|
|
|
|
// the case of To, From, etc., the completer will get completions from the
|
|
|
|
// configured address book command. For other headers, a noop completer will be
|
|
|
|
// returned. If errors arise during completion, the errHandler will be called.
|
|
|
|
func (c *Completer) ForHeader(h string) CompleteFunc {
|
|
|
|
if isAddressHeader(h) {
|
|
|
|
if c.AddressBookCmd == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// wrap completeAddress in an error handler
|
2022-01-07 05:54:28 +01:00
|
|
|
return func(s string) ([]string, string) {
|
|
|
|
completions, prefix, err := c.completeAddress(s)
|
2019-12-20 19:21:35 +01:00
|
|
|
if err != nil {
|
|
|
|
c.handleErr(err)
|
2022-01-07 05:54:28 +01:00
|
|
|
return []string{}, ""
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
2022-01-07 05:54:28 +01:00
|
|
|
return completions, prefix
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isAddressHeader determines whether the address completer should be used for
|
|
|
|
// header h.
|
|
|
|
func isAddressHeader(h string) bool {
|
|
|
|
switch strings.ToLower(h) {
|
|
|
|
case "to", "from", "cc", "bcc":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// completeAddress uses the configured address book completion command to fetch
|
2022-01-07 05:54:28 +01:00
|
|
|
// completions for the specified string, returning a slice of completions and
|
|
|
|
// a prefix to be prepended to the selected completion, or an error.
|
|
|
|
func (c *Completer) completeAddress(s string) ([]string, string, error) {
|
|
|
|
prefix, candidate := c.parseAddress(s)
|
|
|
|
cmd, err := c.getAddressCmd(candidate)
|
2019-12-20 19:21:35 +01:00
|
|
|
if err != nil {
|
2022-01-07 05:54:28 +01:00
|
|
|
return nil, "", err
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return nil, "", fmt.Errorf("stdout: %w", err)
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
2022-07-11 11:11:36 +02:00
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return nil, "", fmt.Errorf("stderr: %w", err)
|
2022-07-11 11:11:36 +02:00
|
|
|
}
|
2019-12-20 19:21:35 +01:00
|
|
|
if err := cmd.Start(); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return nil, "", fmt.Errorf("cmd start: %w", err)
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
|
|
|
// Wait returns an error if the exit status != 0, which some completion
|
|
|
|
// programs will do to signal no matches. We don't want to spam the user with
|
|
|
|
// spurious error messages, so we'll ignore any errors that arise at this
|
|
|
|
// point.
|
2022-07-29 22:31:54 +02:00
|
|
|
defer cmd.Wait() //nolint:errcheck // see above
|
2022-07-11 11:11:36 +02:00
|
|
|
|
|
|
|
completions, err := readCompletions(stdout)
|
|
|
|
if err != nil {
|
2022-08-17 16:19:45 +02:00
|
|
|
buf, _ := io.ReadAll(stderr)
|
2022-07-11 11:11:36 +02:00
|
|
|
msg := strings.TrimSpace(string(buf))
|
|
|
|
if msg != "" {
|
|
|
|
msg = ": " + msg
|
|
|
|
}
|
2022-07-31 15:15:27 +02:00
|
|
|
return nil, "", fmt.Errorf("read completions%s: %w", msg, err)
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 05:54:28 +01:00
|
|
|
return completions, prefix, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAddress will break an address header into a prefix (containing
|
|
|
|
// the already valid addresses) and an input for completion
|
|
|
|
func (c *Completer) parseAddress(s string) (string, string) {
|
|
|
|
pattern := regexp.MustCompile(`^(.*),\s+([^,]*)$`)
|
|
|
|
matches := pattern.FindStringSubmatch(s)
|
|
|
|
if matches == nil {
|
|
|
|
return "", s
|
|
|
|
}
|
|
|
|
return matches[1] + ", ", matches[2]
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getAddressCmd constructs an exec.Cmd based on the configured command and
|
|
|
|
// specified query.
|
|
|
|
func (c *Completer) getAddressCmd(s string) (*exec.Cmd, error) {
|
|
|
|
if strings.TrimSpace(c.AddressBookCmd) == "" {
|
|
|
|
return nil, fmt.Errorf("no command configured")
|
|
|
|
}
|
2022-07-31 14:32:48 +02:00
|
|
|
queryCmd := strings.ReplaceAll(c.AddressBookCmd, "%s", s)
|
2019-12-20 19:21:35 +01:00
|
|
|
parts, err := shlex.Split(queryCmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not lex command")
|
|
|
|
}
|
|
|
|
if len(parts) < 1 {
|
|
|
|
return nil, fmt.Errorf("empty command")
|
|
|
|
}
|
|
|
|
if len(parts) > 1 {
|
|
|
|
return exec.Command(parts[0], parts[1:]...), nil
|
|
|
|
}
|
|
|
|
return exec.Command(parts[0]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readCompletions reads a slice of completions from r line by line. Each line
|
|
|
|
// must consist of tab-delimited fields. Only the first field (the email
|
|
|
|
// address field) is required, the second field (the contact name) is optional,
|
|
|
|
// and subsequent fields are ignored.
|
|
|
|
func readCompletions(r io.Reader) ([]string, error) {
|
|
|
|
buf := bufio.NewReader(r)
|
|
|
|
completions := []string{}
|
|
|
|
for {
|
|
|
|
line, err := buf.ReadString('\n')
|
2022-07-31 15:15:27 +02:00
|
|
|
if errors.Is(err, io.EOF) {
|
2019-12-20 19:21:35 +01:00
|
|
|
return completions, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parts := strings.SplitN(line, "\t", 3)
|
2020-01-09 15:42:12 +01:00
|
|
|
addr, err := mail.ParseAddress(strings.TrimSpace(parts[0]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(parts) > 1 {
|
|
|
|
addr.Name = strings.TrimSpace(parts[1])
|
|
|
|
}
|
|
|
|
decoded, err := decodeMIME(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not decode MIME string: %w", err)
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
2020-01-09 15:42:12 +01:00
|
|
|
completions = append(completions, decoded)
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 15:41:32 +01:00
|
|
|
func decodeMIME(s string) (string, error) {
|
|
|
|
var d mime.WordDecoder
|
|
|
|
return d.DecodeHeader(s)
|
|
|
|
}
|
|
|
|
|
2019-12-20 19:21:35 +01:00
|
|
|
func (c *Completer) handleErr(err error) {
|
|
|
|
if c.errHandler != nil {
|
|
|
|
c.errHandler(err)
|
|
|
|
}
|
|
|
|
}
|