From 7fd73a6fdb49843d29aefa2bb8bb5de900e53811 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sat, 30 Jul 2022 23:00:34 +1200 Subject: [PATCH] UI: cater for messages without From email address --- server/ui-src/templates/Message.vue | 3 ++- storage/database.go | 6 +++++- storage/utils.go | 17 ++++++++++------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/server/ui-src/templates/Message.vue b/server/ui-src/templates/Message.vue index bee7fd1..6e7ef3a 100644 --- a/server/ui-src/templates/Message.vue +++ b/server/ui-src/templates/Message.vue @@ -80,7 +80,8 @@ export default { From - {{ message.From.Name + " <" + message.From.Address +">" }} + {{ message.From.Name }} + <{{ message.From.Address }}> [ Unknown ] diff --git a/storage/database.go b/storage/database.go index d2ab5df..edc299a 100644 --- a/storage/database.go +++ b/storage/database.go @@ -185,6 +185,8 @@ func Store(mailbox string, b []byte) (string, error) { fromData := addressToSlice(env, "From") if len(fromData) > 0 { from = fromData[0] + } else if env.GetHeader("From") != "" { + from = &mail.Address{Name: env.GetHeader("From")} } obj := CloverStore{ @@ -311,7 +313,7 @@ func List(mailbox string, start, limit int) ([]data.Summary, error) { // Search returns a summary of items mathing a search. It searched the SearchText field. func Search(mailbox, search string, start, limit int) ([]data.Summary, error) { - sq := fmt.Sprintf("(?i)%s", regexp.QuoteMeta(search)) + sq := fmt.Sprintf("(?i)%s", cleanString(regexp.QuoteMeta(search))) q, err := db.FindAll(clover.NewQuery(mailbox). Skip(start). Limit(limit). @@ -393,6 +395,8 @@ func GetMessage(mailbox, id string) (*data.Message, error) { fromData := addressToSlice(env, "From") if len(fromData) > 0 { from = fromData[0] + } else if env.GetHeader("From") != "" { + from = &mail.Address{Name: env.GetHeader("From")} } date, err := env.Date() diff --git a/storage/utils.go b/storage/utils.go index 8cfca72..f2bbb18 100644 --- a/storage/utils.go +++ b/storage/utils.go @@ -42,17 +42,20 @@ func createSearchText(env *enmime.Envelope) string { b.WriteString(a.FileName + " ") } - d := b.String() - - // remove/replace new lines - re := regexp.MustCompile(`(\r?\n|\t|>|<|"|:|\,|;)`) - d = re.ReplaceAllString(d, " ") - // remove duplicate whitespace and trim - d = strings.ToLower(strings.Join(strings.Fields(strings.TrimSpace(d)), " ")) + d := cleanString(b.String()) return d } +// cleanString removed unwanted characters from stored search text and search queries +func cleanString(str string) string { + // remove/replace new lines + re := regexp.MustCompile(`(\r?\n|\t|>|<|"|:|\,|;)`) + str = re.ReplaceAllString(str, " ") + // remove duplicate whitespace and trim + return strings.ToLower(strings.Join(strings.Fields(strings.TrimSpace(str)), " ")) +} + // Auto-prune runs every 5 minutes to automatically delete oldest messages // if total is greater than the threshold func pruneCron() {