guido+sievemgr@1: // Copyright (C) 2020 Guido Berhoerster guido+sievemgr@0: // guido+sievemgr@0: // Permission is hereby granted, free of charge, to any person obtaining guido+sievemgr@0: // a copy of this software and associated documentation files (the guido+sievemgr@0: // "Software"), to deal in the Software without restriction, including guido+sievemgr@0: // without limitation the rights to use, copy, modify, merge, publish, guido+sievemgr@0: // distribute, sublicense, and/or sell copies of the Software, and to guido+sievemgr@0: // permit persons to whom the Software is furnished to do so, subject to guido+sievemgr@0: // the following conditions: guido+sievemgr@0: // guido+sievemgr@0: // The above copyright notice and this permission notice shall be included guido+sievemgr@0: // in all copies or substantial portions of the Software. guido+sievemgr@0: // guido+sievemgr@0: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, guido+sievemgr@0: // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF guido+sievemgr@0: // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. guido+sievemgr@0: // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY guido+sievemgr@0: // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, guido+sievemgr@0: // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE guido+sievemgr@0: // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. guido+sievemgr@0: guido+sievemgr@0: package main guido+sievemgr@0: guido+sievemgr@0: import ( guido+sievemgr@0: "fmt" guido+sievemgr@0: "io" guido+sievemgr@0: "net" guido+sievemgr@0: "os" guido+sievemgr@0: guido+sievemgr@0: "go.guido-berhoerster.org/managesieve" guido+sievemgr@0: ) guido+sievemgr@0: guido+sievemgr@0: func init() { guido+sievemgr@0: cmdPut.Flag.StringVar(&username, "u", "", "Set the username") guido+sievemgr@0: cmdPut.Flag.StringVar(&passwordFilename, "P", "", guido+sievemgr@0: "Set the name of the password file") guido+sievemgr@0: } guido+sievemgr@0: guido+sievemgr@0: var cmdPut = &command{ guido+sievemgr@0: UsageLine: "put [options] host[:port] name [file]", guido+sievemgr@0: Run: runPut, guido+sievemgr@0: } guido+sievemgr@0: guido+sievemgr@0: func runPut(cmd *command, args []string) error { guido+sievemgr@0: var scriptName string guido+sievemgr@0: var r io.Reader = os.Stdin guido+sievemgr@0: var host, port string guido+sievemgr@0: var err error guido+sievemgr@0: switch len(args) { guido+sievemgr@0: case 3: // name and filename guido+sievemgr@0: scriptFile, err := os.Open(args[2]) guido+sievemgr@0: if err != nil { guido+sievemgr@0: return fmt.Errorf("failed to open script file: %s\n", guido+sievemgr@0: err) guido+sievemgr@0: } guido+sievemgr@0: defer scriptFile.Close() guido+sievemgr@0: r = scriptFile guido+sievemgr@0: fallthrough guido+sievemgr@0: case 2: // only name guido+sievemgr@0: host, port, err = parseHostPort(args[0]) guido+sievemgr@0: if err != nil { guido+sievemgr@0: return err guido+sievemgr@0: } guido+sievemgr@0: guido+sievemgr@0: scriptName = args[1] guido+sievemgr@0: default: guido+sievemgr@0: return usageError("invalid number of arguments") guido+sievemgr@0: } guido+sievemgr@0: guido+sievemgr@0: script, err := readLimitedString(r, managesieve.ReadLimit) guido+sievemgr@0: if err != nil { guido+sievemgr@0: return fmt.Errorf("failed to read script: %s\n", err) guido+sievemgr@0: } guido+sievemgr@0: guido+sievemgr@0: username, password, err := usernamePassword(host, port, username, guido+sievemgr@0: passwordFilename) guido+sievemgr@0: if err != nil { guido+sievemgr@0: return err guido+sievemgr@0: } guido+sievemgr@0: guido+sievemgr@0: c, err := dialPlainAuth(net.JoinHostPort(host, port), username, guido+sievemgr@0: password) guido+sievemgr@0: if err != nil { guido+sievemgr@0: return err guido+sievemgr@0: } guido+sievemgr@0: defer c.Logout() guido+sievemgr@0: guido+sievemgr@0: if err := c.PutScript(scriptName, script); err != nil { guido+sievemgr@0: return err guido+sievemgr@0: } guido+sievemgr@0: guido+sievemgr@0: return nil guido+sievemgr@0: }