projects/sievemgr

annotate cmd/sievemgr/put.go @ 1:0cd5a454dfb4

Add README file

Adjust email address.
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Mon Oct 26 14:46:39 2020 +0100 (11 months ago)
parents b00673734e58
children 3cd182bff992
rev   line source
guido+sievemgr@1 1 // Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@berhoerster.name>
guido+sievemgr@0 2 //
guido+sievemgr@0 3 // Permission is hereby granted, free of charge, to any person obtaining
guido+sievemgr@0 4 // a copy of this software and associated documentation files (the
guido+sievemgr@0 5 // "Software"), to deal in the Software without restriction, including
guido+sievemgr@0 6 // without limitation the rights to use, copy, modify, merge, publish,
guido+sievemgr@0 7 // distribute, sublicense, and/or sell copies of the Software, and to
guido+sievemgr@0 8 // permit persons to whom the Software is furnished to do so, subject to
guido+sievemgr@0 9 // the following conditions:
guido+sievemgr@0 10 //
guido+sievemgr@0 11 // The above copyright notice and this permission notice shall be included
guido+sievemgr@0 12 // in all copies or substantial portions of the Software.
guido+sievemgr@0 13 //
guido+sievemgr@0 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
guido+sievemgr@0 15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
guido+sievemgr@0 16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
guido+sievemgr@0 17 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
guido+sievemgr@0 18 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
guido+sievemgr@0 19 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
guido+sievemgr@0 20 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
guido+sievemgr@0 21
guido+sievemgr@0 22 package main
guido+sievemgr@0 23
guido+sievemgr@0 24 import (
guido+sievemgr@0 25 "fmt"
guido+sievemgr@0 26 "io"
guido+sievemgr@0 27 "net"
guido+sievemgr@0 28 "os"
guido+sievemgr@0 29
guido+sievemgr@0 30 "go.guido-berhoerster.org/managesieve"
guido+sievemgr@0 31 )
guido+sievemgr@0 32
guido+sievemgr@0 33 func init() {
guido+sievemgr@0 34 cmdPut.Flag.StringVar(&username, "u", "", "Set the username")
guido+sievemgr@0 35 cmdPut.Flag.StringVar(&passwordFilename, "P", "",
guido+sievemgr@0 36 "Set the name of the password file")
guido+sievemgr@0 37 }
guido+sievemgr@0 38
guido+sievemgr@0 39 var cmdPut = &command{
guido+sievemgr@0 40 UsageLine: "put [options] host[:port] name [file]",
guido+sievemgr@0 41 Run: runPut,
guido+sievemgr@0 42 }
guido+sievemgr@0 43
guido+sievemgr@0 44 func runPut(cmd *command, args []string) error {
guido+sievemgr@0 45 var scriptName string
guido+sievemgr@0 46 var r io.Reader = os.Stdin
guido+sievemgr@0 47 var host, port string
guido+sievemgr@0 48 var err error
guido+sievemgr@0 49 switch len(args) {
guido+sievemgr@0 50 case 3: // name and filename
guido+sievemgr@0 51 scriptFile, err := os.Open(args[2])
guido+sievemgr@0 52 if err != nil {
guido+sievemgr@0 53 return fmt.Errorf("failed to open script file: %s\n",
guido+sievemgr@0 54 err)
guido+sievemgr@0 55 }
guido+sievemgr@0 56 defer scriptFile.Close()
guido+sievemgr@0 57 r = scriptFile
guido+sievemgr@0 58 fallthrough
guido+sievemgr@0 59 case 2: // only name
guido+sievemgr@0 60 host, port, err = parseHostPort(args[0])
guido+sievemgr@0 61 if err != nil {
guido+sievemgr@0 62 return err
guido+sievemgr@0 63 }
guido+sievemgr@0 64
guido+sievemgr@0 65 scriptName = args[1]
guido+sievemgr@0 66 default:
guido+sievemgr@0 67 return usageError("invalid number of arguments")
guido+sievemgr@0 68 }
guido+sievemgr@0 69
guido+sievemgr@0 70 script, err := readLimitedString(r, managesieve.ReadLimit)
guido+sievemgr@0 71 if err != nil {
guido+sievemgr@0 72 return fmt.Errorf("failed to read script: %s\n", err)
guido+sievemgr@0 73 }
guido+sievemgr@0 74
guido+sievemgr@0 75 username, password, err := usernamePassword(host, port, username,
guido+sievemgr@0 76 passwordFilename)
guido+sievemgr@0 77 if err != nil {
guido+sievemgr@0 78 return err
guido+sievemgr@0 79 }
guido+sievemgr@0 80
guido+sievemgr@0 81 c, err := dialPlainAuth(net.JoinHostPort(host, port), username,
guido+sievemgr@0 82 password)
guido+sievemgr@0 83 if err != nil {
guido+sievemgr@0 84 return err
guido+sievemgr@0 85 }
guido+sievemgr@0 86 defer c.Logout()
guido+sievemgr@0 87
guido+sievemgr@0 88 if err := c.PutScript(scriptName, script); err != nil {
guido+sievemgr@0 89 return err
guido+sievemgr@0 90 }
guido+sievemgr@0 91
guido+sievemgr@0 92 return nil
guido+sievemgr@0 93 }