Mercurial > projects > managesieve
diff example_test.go @ 0:6369453d47a3
Initial revision
author | Guido Berhoerster <guido+managesieve@berhoerster.name> |
---|---|
date | Thu, 15 Oct 2020 09:11:05 +0200 |
parents | |
children | f9bb517e9447 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/example_test.go Thu Oct 15 09:11:05 2020 +0200 @@ -0,0 +1,133 @@ +// Copyright (C) 2020 Guido Berhoerster <guido+managesieve@berhoerster.name> +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +package managesieve_test + +import ( + "crypto/tls" + "fmt" + "log" + + "go.guido-berhoerster.org/managesieve" +) + +func Example() { + host := "mail.example.org" + username := "foo" + password := "S3cR3T" + script := `require "fileinto"; + +if address :is "from" "foo@example.com" { + fileinto "INBOX/foo"; +} +` + scriptName := "newscript" + + // Try to look up a SRV record for given domain and fall back to the + // domain and port 4190. + var hostport string + if services, err := managesieve.LookupService(host); err != nil { + hostport = host + ":4190" + } else { + hostport = services[0] + } + + // Connect to the ManageSieve server. + c, err := managesieve.Dial(hostport) + if err != nil { + log.Fatalf("failed to connect: %s", err) + } + + // Establish a TLS connection. + tlsConf := &tls.Config{ServerName: host} + if err := c.StartTLS(tlsConf); err != nil { + log.Fatalf("failed to start TLS connection: %s", err) + } + + // Authenticate the user using the PLAIN SASL mechanism. + auth := managesieve.PlainAuth("", username, password, host) + if err := c.Authenticate(auth); err != nil { + log.Fatalf("failed to authenticate user %s: %s", username, err) + } + + // Check the validity of the script. + if err = c.CheckScript(script); err != nil { + log.Fatalf("script %q is not valid: %s", scriptName, err) + } + + // Check whether ther is sufficient space for uploading the script. + if ok, err := c.HaveSpace(scriptName, int64(len(script))); err != nil { + log.Fatalf("failed to determine whether there is enough space: %s", + err) + } else if !ok { + log.Fatalf("not enough space to upload script %q", scriptName) + } + + // Upload the script. + if err = c.PutScript(scriptName, script); err != nil { + log.Fatalf("failed to upload script %q: %s", scriptName, err) + } + + // Activate the uploaded script + if err = c.ActivateScript(scriptName); err != nil { + log.Fatalf("failed to set script %q active: %s", scriptName, + err) + } + + // Get a list of the names of all scripts on the server and determine + // the currently active script. + scripts, active, err := c.ListScripts() + if err != nil { + log.Fatalf("failed to list scripts: %s", err) + } + if active != scriptName { + log.Fatalf("%q is not the active script", scriptName) + } + // Download each script from the list. + for _, name := range scripts { + if name == active { + fmt.Printf("%q:\n", name) + } else { + fmt.Printf("%q (active):\n", name) + } + + content, err := c.GetScript(name) + if err != nil { + log.Fatalf("failed to get script %q: %s", name, err) + } + fmt.Println(content) + } + + // Rename the script to "temp". + if err = c.RenameScript(scriptName, "temp"); err != nil { + log.Fatalf("RENAMESCRIPT failed: %s", err) + } + + // Delete the script. + if err = c.DeleteScript("temp"); err != nil { + log.Fatalf("DELETESCRIPT failed: %s", err) + } + + // Log out and close the connection. + if err = c.Logout(); err != nil { + log.Fatalf("failed to log out: %s", err) + } +}