From 405a022a9779be70ee6482dff9909c586714011d Mon Sep 17 00:00:00 2001
From: Knut Ahlers <knut@ahlers.me>
Date: Mon, 29 Jun 2020 14:06:03 +0200
Subject: [PATCH] Fix: Use AND matching for match-rules

Signed-off-by: Knut Ahlers <knut@ahlers.me>
---
 handler.go | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/handler.go b/handler.go
index cba5ae7..2701b5e 100644
--- a/handler.go
+++ b/handler.go
@@ -21,12 +21,20 @@ type mailHandler struct {
 }
 
 func (m mailHandler) Handles(msg *enmime.Envelope) bool {
+	if len(m.Match) == 0 {
+		// No matcher available: Should not match anything
+		return false
+	}
+
+	// Do an AND matching: As soon as one matcher fails the whole match fails
 	for _, ma := range m.Match {
-		if ma.Match(msg) {
-			return true
+		if !ma.Match(msg) {
+			return false
 		}
 	}
-	return false
+
+	// None has failed: It matches
+	return true
 }
 
 func (m mailHandler) Process(imapClient *client.Client, msg *imap.Message, envelope *enmime.Envelope) error {