projects/sievemgr

view cmd/sievemgr/put.go @ 22:fc5e6970a0d5

Add support for specifying an authorization identity on the command line
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Wed Feb 17 07:50:55 2021 +0100 (7 months ago)
parents 4dff4c3f0fbb
children
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 "os"
29 "go.guido-berhoerster.org/managesieve"
30 )
32 func init() {
33 cmdPut.Flag.StringVar(&acctName, "a", "", "Select the account")
34 cmdPut.Flag.StringVar(&authzID, "A", "", "Specify the authorization identity")
35 cmdCheck.Flag.StringVar(&acctName, "a", "", "Select the account")
36 cmdCheck.Flag.StringVar(&authzID, "A", "", "Specify the authorization identity")
37 }
39 var cmdPut = &command{
40 UsageLine: "put [options] name [file]",
41 Run: runPut,
42 }
44 var cmdCheck = &command{
45 UsageLine: "check [options] [file]",
46 Run: runPut,
47 }
49 func runPut(cmd *command, args []string) error {
50 var err error
51 acct, err := getAccount(&conf, acctName)
52 if err != nil {
53 return err
54 }
56 var scriptName string
57 var r io.Reader = os.Stdin
58 if cmd.Name() == "put" {
59 switch len(args) {
60 case 2: // name and filename
61 scriptFile, err := os.Open(args[1])
62 if err != nil {
63 return fmt.Errorf("failed to open script file: %s\n",
64 err)
65 }
66 defer scriptFile.Close()
67 r = scriptFile
68 fallthrough
69 case 1: // only name
70 scriptName = args[0]
71 default:
72 return usageError("invalid number of arguments")
73 }
74 } else if cmd.Name() == "check" {
75 switch len(args) {
76 case 1: // filename
77 scriptFile, err := os.Open(args[0])
78 if err != nil {
79 return fmt.Errorf("failed to open script file: %s\n",
80 err)
81 }
82 defer scriptFile.Close()
83 r = scriptFile
84 case 0:
85 default:
86 return usageError("invalid number of arguments")
87 }
88 }
90 script, err := readLimitedString(r, managesieve.ReadLimit)
91 if err != nil {
92 return fmt.Errorf("failed to read script: %s\n", err)
93 }
95 if err := lookupHostPort(acct); err != nil {
96 return err
97 }
99 if err := readPassword(acct); err != nil {
100 return err
101 }
103 c, err := dialPlainAuth(acct)
104 if err != nil {
105 return err
106 }
107 defer c.Logout()
109 var warnings string
110 if cmd.Name() == "put" {
111 warnings, err = c.PutScript(scriptName, script)
112 } else if cmd.Name() == "check" {
113 warnings, err = c.CheckScript(script)
114 }
115 if err != nil {
116 return err
117 } else if warnings != "" {
118 fmt.Fprintln(os.Stderr, warnings)
119 }
121 return nil
122 }