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