changeset 4:f9bb517e9447

Return warning messages from the CHECKSCRIPT and PUTSCRIPT commands
author Guido Berhoerster <guido+managesieve@berhoerster.name>
date Tue, 27 Oct 2020 17:47:14 +0100
parents 8413916df2be
children e3a89d58dbff
files example_test.go managesieve.go managesieve_test.go
diffstat 3 files changed, 42 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/example_test.go	Mon Oct 26 15:24:43 2020 +0100
+++ b/example_test.go	Tue Oct 27 17:47:14 2020 +0100
@@ -69,8 +69,10 @@
 	}
 
 	// Check the validity of the script.
-	if err = c.CheckScript(script); err != nil {
+	if warnings, err := c.CheckScript(script); err != nil {
 		log.Fatalf("script %q is not valid: %s", scriptName, err)
+	} else if warnings != "" {
+		log.Printf("warning: %s", warnings)
 	}
 
 	// Check whether ther is sufficient space for uploading the script.
@@ -82,8 +84,10 @@
 	}
 
 	// Upload the script.
-	if err = c.PutScript(scriptName, script); err != nil {
+	if warnings, err := c.PutScript(scriptName, script); err != nil {
 		log.Fatalf("failed to upload script %q: %s", scriptName, err)
+	} else if warnings != "" {
+		log.Printf("warning: %s", warnings)
 	}
 
 	// Activate the uploaded script
--- a/managesieve.go	Mon Oct 26 15:24:43 2020 +0100
+++ b/managesieve.go	Tue Oct 27 17:47:14 2020 +0100
@@ -427,12 +427,19 @@
 
 // PutScript stores the script content with the given name on the server.  An
 // already existing script with the same name will be replaced.
-func (c *Client) PutScript(name, content string) error {
+func (c *Client) PutScript(name, content string) (warnings string, err error) {
 	if !IsNetUnicode(name) {
-		return ProtocolError("script name must comply with Net-Unicode")
+		err = ProtocolError("script name must comply with Net-Unicode")
+		return
 	}
-	_, err := c.cmd("PUTSCRIPT", quoteString(name), quoteString(content))
-	return err
+	r, err := c.cmd("PUTSCRIPT", quoteString(name), quoteString(content))
+	if err != nil {
+		return
+	}
+	if r.code == "WARNINGS" {
+		warnings = r.msg
+	}
+	return
 }
 
 // ListScripts returns the names of all scripts on the server and the name of
@@ -513,12 +520,19 @@
 
 // CheckScript checks if the given script contains any errors. This operation
 // is only available if the server conforms to RFC 5804.
-func (c *Client) CheckScript(content string) error {
+func (c *Client) CheckScript(content string) (warnings string, err error) {
 	if !c.SupportsRFC5804() {
-		return NotSupportedError("CHECKSCRIPT")
+		err = NotSupportedError("CHECKSCRIPT")
+		return
 	}
-	_, err := c.cmd("CHECKSCRIPT", quoteString(content))
-	return err
+	r, err := c.cmd("CHECKSCRIPT", quoteString(content))
+	if err != nil {
+		return
+	}
+	if r.code == "WARNINGS" {
+		warnings = r.msg
+	}
+	return
 }
 
 // Noop does nothing but contact the server and can be used to prevent timeouts
--- a/managesieve_test.go	Mon Oct 26 15:24:43 2020 +0100
+++ b/managesieve_test.go	Tue Oct 27 17:47:14 2020 +0100
@@ -118,6 +118,7 @@
     redirect "lists@groups.example.com";
 }
 `
+var expectedWarnings = "line 8: server redirect action limit is 2, this redirect might be ignored"
 
 // basic functionality
 var basicServer string = `"IMPlemENTATION" "Example1 ManageSieved v001"
@@ -137,9 +138,9 @@
 "MAXREdIRECTS" "5"
 "VERSION" "1.0"
 OK
-OK (WARNINGS) "line 8: server redirect action limit is 2, this redirect might be ignored"
+OK (WARNINGS) "` + expectedWarnings + `"
 OK
-OK (WARNINGS) "line 8: server redirect action limit is 2, this redirect might be ignored"
+OK (WARNINGS) "` + expectedWarnings + `"
 OK
 "default" ACTIVE
 OK
@@ -217,8 +218,11 @@
 		t.Fatalf("plain authentication failed: %s", err)
 	}
 
-	if err = c.CheckScript(validScript); err != nil {
+	if warnings, err := c.CheckScript(validScript); err != nil {
 		t.Fatalf("CHECKSCRIPT failed: %s", err)
+	} else if warnings != expectedWarnings {
+		t.Fatalf("CHECKSCRIPT expected: %s, got %s", warnings,
+			expectedWarnings)
 	}
 
 	if ok, err := c.HaveSpace("default", int64(len(validScript))); err != nil {
@@ -227,8 +231,11 @@
 		t.Fatal("HaveSpace unexpectedly returned false")
 	}
 
-	if err = c.PutScript("default", validScript); err != nil {
+	if warnings, err := c.PutScript("default", validScript); err != nil {
 		t.Fatalf("PUTSCRIPT failed: %s", err)
+	} else if warnings != expectedWarnings {
+		t.Fatalf("PUTSCRIPT expected: %s, got %s", warnings,
+			expectedWarnings)
 	}
 
 	if err = c.ActivateScript("default"); err != nil {
@@ -523,7 +530,7 @@
 	}
 
 	// invalid script name
-	err = c.PutScript("def\u2028ault", validScript)
+	_, err = c.PutScript("def\u2028ault", validScript)
 	if err == nil {
 		t.Fatalf("expected error but succeeded")
 	}
@@ -533,7 +540,7 @@
 	}
 
 	// EOF during upload
-	err = c.PutScript("default", validScript)
+	_, err = c.PutScript("default", validScript)
 	if err == nil {
 		t.Fatalf("expected error but succeeded")
 	}
@@ -738,7 +745,7 @@
 			err, err)
 	}
 
-	err = c.CheckScript(validScript)
+	_, err = c.CheckScript(validScript)
 	if err == nil {
 		t.Fatalf("expected error but succeeded")
 	}