projects/sievemgr

view cmd/sievemgr/put.go @ 4:f925f15d8ce5

Add check subcommand for checking the script validity
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue Oct 27 19:17:56 2020 +0100 (10 months ago)
parents 3cd182bff992
children 4dff4c3f0fbb
line source
1 // Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@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.
22 package main
24 import (
25 "fmt"
26 "io"
27 "net"
28 "os"
30 "go.guido-berhoerster.org/managesieve"
31 )
33 func init() {
34 cmdPut.Flag.StringVar(&username, "u", "", "Set the username")
35 cmdPut.Flag.StringVar(&passwordFilename, "P", "",
36 "Set the name of the password file")
37 cmdCheck.Flag.StringVar(&username, "u", "", "Set the username")
38 cmdCheck.Flag.StringVar(&passwordFilename, "P", "",
39 "Set the name of the password file")
40 }
42 var cmdPut = &command{
43 UsageLine: "put [options] host[:port] name [file]",
44 Run: runPut,
45 }
47 var cmdCheck = &command{
48 UsageLine: "check [options] host[:port] [file]",
49 Run: runPut,
50 }
52 func runPut(cmd *command, args []string) error {
53 var scriptName string
54 var r io.Reader = os.Stdin
55 var host, port string
56 var err error
57 if cmd.Name() == "put" {
58 switch len(args) {
59 case 3: // name and filename
60 scriptFile, err := os.Open(args[2])
61 if err != nil {
62 return fmt.Errorf("failed to open script file: %s\n",
63 err)
64 }
65 defer scriptFile.Close()
66 r = scriptFile
67 fallthrough
68 case 2: // only name
69 host, port, err = parseHostPort(args[0])
70 if err != nil {
71 return err
72 }
74 scriptName = args[1]
75 default:
76 return usageError("invalid number of arguments")
77 }
78 } else if cmd.Name() == "check" {
79 switch len(args) {
80 case 2: // filename
81 scriptFile, err := os.Open(args[1])
82 if err != nil {
83 return fmt.Errorf("failed to open script file: %s\n",
84 err)
85 }
86 defer scriptFile.Close()
87 r = scriptFile
88 fallthrough
89 case 1:
90 host, port, err = parseHostPort(args[0])
91 if err != nil {
92 return err
93 }
94 default:
95 return usageError("invalid number of arguments")
96 }
97 } else if cmd.Name() == "check" {
98 switch len(args) {
99 case 1: // filename
100 scriptFile, err := os.Open(args[0])
101 if err != nil {
102 return fmt.Errorf("failed to open script file: %s\n",
103 err)
104 }
105 defer scriptFile.Close()
106 r = scriptFile
107 case 0:
108 default:
109 return usageError("invalid number of arguments")
110 }
111 }
113 script, err := readLimitedString(r, managesieve.ReadLimit)
114 if err != nil {
115 return fmt.Errorf("failed to read script: %s\n", err)
116 }
118 username, password, err := usernamePassword(host, port, username,
119 passwordFilename)
120 if err != nil {
121 return err
122 }
124 c, err := dialPlainAuth(net.JoinHostPort(host, port), username,
125 password)
126 if err != nil {
127 return err
128 }
129 defer c.Logout()
131 var warnings string
132 if cmd.Name() == "put" {
133 warnings, err = c.PutScript(scriptName, script)
134 } else if cmd.Name() == "check" {
135 warnings, err = c.CheckScript(script)
136 }
137 if err != nil {
138 return err
139 } else if warnings != "" {
140 fmt.Fprintln(os.Stderr, warnings)
141 }
143 return nil
144 }