aboutsummaryrefslogtreecommitdiff
path: root/scripts/mailing-list-member-retirement
diff options
context:
space:
mode:
authorMichael Weiss2020-08-02 15:40:28 +0200
committerMichael Weiss2020-08-08 16:44:18 +0200
commitc29889a09803986f647c2d1fd0a5bf88d8efc5e7 (patch)
tree59af1e5ec5895157b1c73fcc992cfbda8cc81dc4 /scripts/mailing-list-member-retirement
parentb812dd932aaec17dc81dbd80d902291edfa5ca93 (diff)
scripts/mailing-list-member-retirement: Improve the error messages and simplify the handling
Diffstat (limited to 'scripts/mailing-list-member-retirement')
-rw-r--r--scripts/mailing-list-member-retirement/main.go35
1 files changed, 11 insertions, 24 deletions
diff --git a/scripts/mailing-list-member-retirement/main.go b/scripts/mailing-list-member-retirement/main.go
index 7ce7867..7388cd6 100644
--- a/scripts/mailing-list-member-retirement/main.go
+++ b/scripts/mailing-list-member-retirement/main.go
@@ -7,7 +7,6 @@ import (
"io"
"log"
"os"
- "strings"
"github.com/emersion/go-mbox"
"github.com/emersion/go-message/mail"
_ "github.com/emersion/go-message/charset"
@@ -47,34 +46,22 @@ func PrintAllMboxSenders(fileName string) {
func PrintMessageSender(r io.Reader) {
mr, err := mail.CreateReader(r)
if err != nil {
- if err.Error() == "charset \"cp-850\": ianaindex: invalid encoding name" {
- fmt.Fprintf(os.Stderr, "Ignored a mail due to an invalid charset\n")
- return // TODO: Print message ID
- }
- log.Fatal(err)
+ IgnoreMail(err, mr)
}
addr, err := mr.Header.AddressList("From")
if err != nil {
- if strings.Contains(err.Error(), "invalid utf-8 in quoted-string") {
- addr := strings.TrimPrefix(err.Error(), "mail: missing word in phrase: mail: invalid utf-8 in quoted-string: ")
- fmt.Fprintf(os.Stderr, "Ignored due to invalid UTF-8 encoding: %v\n", addr)
- return
- } else if err.Error() == "mail: missing @ in addr-spec" {
- fmt.Fprintf(os.Stderr, "Ignored a sender due to missing @\n")
- return // TODO: Print invalid address
- } else if err.Error() == "mail: no angle-addr" {
- fmt.Fprintf(os.Stderr, "Ignored a mail due to no angle-address\n")
- return // TODO: Print message ID
- }
- log.Fatal(err)
+ IgnoreMail(err, mr)
+ return
} else if len(addr) != 1 {
- if len(addr) == 0 {
- fmt.Fprintf(os.Stderr, "Ignored a mail due to a missing sender\n")
- return // TODO: Print message ID
- }
- fmt.Fprintf(os.Stderr, "A mail has an unexpected number of senders: %v\n", len(addr))
- return // TODO: Print message ID
+ IgnoreMail(fmt.Errorf("unexpected number of senders: %v", len(addr)), mr)
+ return
}
fmt.Println(addr[0].Address)
}
+
+func IgnoreMail(err error, mailReader *mail.Reader) {
+ reason := err.Error()
+ id, _ := mailReader.Header.MessageID()
+ fmt.Fprintf(os.Stderr, "Warning: Ignored a mail with ID \"%v\" for the reason \"%v\".\n", id, reason)
+}