comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:6369453d47a3
1 // Copyright (C) 2020 Guido Berhoerster <guido+managesieve@berhoerster.name>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 package managesieve_test
23
24 import (
25 "crypto/tls"
26 "fmt"
27 "log"
28
29 "go.guido-berhoerster.org/managesieve"
30 )
31
32 func Example() {
33 host := "mail.example.org"
34 username := "foo"
35 password := "S3cR3T"
36 script := `require "fileinto";
37
38 if address :is "from" "foo@example.com" {
39 fileinto "INBOX/foo";
40 }
41 `
42 scriptName := "newscript"
43
44 // Try to look up a SRV record for given domain and fall back to the
45 // domain and port 4190.
46 var hostport string
47 if services, err := managesieve.LookupService(host); err != nil {
48 hostport = host + ":4190"
49 } else {
50 hostport = services[0]
51 }
52
53 // Connect to the ManageSieve server.
54 c, err := managesieve.Dial(hostport)
55 if err != nil {
56 log.Fatalf("failed to connect: %s", err)
57 }
58
59 // Establish a TLS connection.
60 tlsConf := &tls.Config{ServerName: host}
61 if err := c.StartTLS(tlsConf); err != nil {
62 log.Fatalf("failed to start TLS connection: %s", err)
63 }
64
65 // Authenticate the user using the PLAIN SASL mechanism.
66 auth := managesieve.PlainAuth("", username, password, host)
67 if err := c.Authenticate(auth); err != nil {
68 log.Fatalf("failed to authenticate user %s: %s", username, err)
69 }
70
71 // Check the validity of the script.
72 if err = c.CheckScript(script); err != nil {
73 log.Fatalf("script %q is not valid: %s", scriptName, err)
74 }
75
76 // Check whether ther is sufficient space for uploading the script.
77 if ok, err := c.HaveSpace(scriptName, int64(len(script))); err != nil {
78 log.Fatalf("failed to determine whether there is enough space: %s",
79 err)
80 } else if !ok {
81 log.Fatalf("not enough space to upload script %q", scriptName)
82 }
83
84 // Upload the script.
85 if err = c.PutScript(scriptName, script); err != nil {
86 log.Fatalf("failed to upload script %q: %s", scriptName, err)
87 }
88
89 // Activate the uploaded script
90 if err = c.ActivateScript(scriptName); err != nil {
91 log.Fatalf("failed to set script %q active: %s", scriptName,
92 err)
93 }
94
95 // Get a list of the names of all scripts on the server and determine
96 // the currently active script.
97 scripts, active, err := c.ListScripts()
98 if err != nil {
99 log.Fatalf("failed to list scripts: %s", err)
100 }
101 if active != scriptName {
102 log.Fatalf("%q is not the active script", scriptName)
103 }
104 // Download each script from the list.
105 for _, name := range scripts {
106 if name == active {
107 fmt.Printf("%q:\n", name)
108 } else {
109 fmt.Printf("%q (active):\n", name)
110 }
111
112 content, err := c.GetScript(name)
113 if err != nil {
114 log.Fatalf("failed to get script %q: %s", name, err)
115 }
116 fmt.Println(content)
117 }
118
119 // Rename the script to "temp".
120 if err = c.RenameScript(scriptName, "temp"); err != nil {
121 log.Fatalf("RENAMESCRIPT failed: %s", err)
122 }
123
124 // Delete the script.
125 if err = c.DeleteScript("temp"); err != nil {
126 log.Fatalf("DELETESCRIPT failed: %s", err)
127 }
128
129 // Log out and close the connection.
130 if err = c.Logout(); err != nil {
131 log.Fatalf("failed to log out: %s", err)
132 }
133 }