diff options
author | Robin Jarry <robin@jarry.cc> | 2022-11-02 11:44:24 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-11-03 10:15:12 +0100 |
commit | 14ceca320065656ea31994191f9e74d254a72e04 (patch) | |
tree | 9b6851e78114a4f710ade83f60501b16fd5ce3f1 | |
parent | 7565a96525edb518129c7892843130bf0948eeb6 (diff) | |
download | aerc-14ceca320065656ea31994191f9e74d254a72e04.tar.gz |
address-book-cmd: be more lenient with errors
Instead of failing completely when address-book-cmd returns an invalid
line, ignore the line and report a warning in the logs.
Do not wait for 100 "valid" addresses before bailing out as the command
could only output garbage in large quantities. The issue of the command
not printing any new line characters still exists. We could address this
but I think it would be overkill.
Reported-by: Bence Ferdinandy <bence@ferdinandy.com>
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
-rw-r--r-- | completer/completer.go | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/completer/completer.go b/completer/completer.go index f3f7b9f7..131063e7 100644 --- a/completer/completer.go +++ b/completer/completer.go @@ -162,30 +162,38 @@ func (c *Completer) getAddressCmd(s string) (*exec.Cmd, error) { func readCompletions(r io.Reader) ([]string, error) { buf := bufio.NewReader(r) completions := []string{} - for { + for i := 0; i < maxCompletionLines; i++ { line, err := buf.ReadString('\n') if errors.Is(err, io.EOF) { return completions, nil } else if err != nil { return nil, err } + if strings.TrimSpace(line) == "" { + // skip empty lines + continue + } parts := strings.SplitN(line, "\t", 3) addr, err := mail.ParseAddress(strings.TrimSpace(parts[0])) if err != nil { - return nil, err + logging.Warnf( + "line %d: %#v: could not parse address: %v", + line, err) + continue } 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) + logging.Warnf( + "line %d: %#v: could not decode MIME string: %v", + i+1, line, err) + continue } completions = append(completions, decoded) - if len(completions) >= maxCompletionLines { - return completions, tooManyLines - } } + return completions, tooManyLines } func decodeMIME(s string) (string, error) { |