]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-exec/main.go
Remove huge usage headers, -warranty exists anyway
[nncp.git] / src / cmd / nncp-exec / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Send execution command via NNCP.
19 package main
20
21 import (
22         "bufio"
23         "flag"
24         "fmt"
25         "log"
26         "os"
27         "strings"
28
29         "go.cypherpunks.ru/nncp/v8"
30 )
31
32 func usage() {
33         fmt.Fprint(os.Stderr, "nncp-exec -- send execution command\n\n")
34         fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE HANDLE [ARG0 ARG1 ...]\n", os.Args[0])
35         fmt.Fprintf(os.Stderr, "       %s [options] %s:AREA HANDLE [ARG0 ARG1 ...]\nOptions:\n",
36                 os.Args[0], nncp.AreaDir)
37         flag.PrintDefaults()
38 }
39
40 func main() {
41         var (
42                 noCompress   = flag.Bool("nocompress", false, "Do not compress input data")
43                 cfgPath      = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
44                 niceRaw      = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceExec), "Outbound packet niceness")
45                 replyNiceRaw = flag.String("replynice", nncp.NicenessFmt(nncp.DefaultNiceFile), "Possible reply packet niceness")
46                 minSize      = flag.Uint64("minsize", 0, "Minimal required resulting packet size, in KiB")
47                 argMaxSize   = flag.Uint64("maxsize", 0, "Maximal allowable resulting packet size, in KiB")
48                 viaOverride  = flag.String("via", "", "Override Via path to destination node")
49                 spoolPath    = flag.String("spool", "", "Override path to spool")
50                 logPath      = flag.String("log", "", "Override path to logfile")
51                 quiet        = flag.Bool("quiet", false, "Print only errors")
52                 showPrgrs    = flag.Bool("progress", false, "Force progress showing")
53                 omitPrgrs    = flag.Bool("noprogress", false, "Omit progress showing")
54                 debug        = flag.Bool("debug", false, "Print debug messages")
55                 version      = flag.Bool("version", false, "Print version information")
56                 warranty     = flag.Bool("warranty", false, "Print warranty information")
57         )
58         log.SetFlags(log.Lshortfile)
59         flag.Usage = usage
60         flag.Parse()
61         if *warranty {
62                 fmt.Println(nncp.Warranty)
63                 return
64         }
65         if *version {
66                 fmt.Println(nncp.VersionGet())
67                 return
68         }
69         if flag.NArg() < 2 {
70                 usage()
71                 os.Exit(1)
72         }
73         nice, err := nncp.NicenessParse(*niceRaw)
74         if err != nil {
75                 log.Fatalln(err)
76         }
77         replyNice, err := nncp.NicenessParse(*replyNiceRaw)
78         if err != nil {
79                 log.Fatalln(err)
80         }
81
82         ctx, err := nncp.CtxFromCmdline(
83                 *cfgPath,
84                 *spoolPath,
85                 *logPath,
86                 *quiet,
87                 *showPrgrs,
88                 *omitPrgrs,
89                 *debug,
90         )
91         if err != nil {
92                 log.Fatalln("Error during initialization:", err)
93         }
94         if ctx.Self == nil {
95                 log.Fatalln("Config lacks private keys")
96         }
97
98         var areaId *nncp.AreaId
99         var node *nncp.Node
100         if strings.HasPrefix(flag.Arg(0), nncp.AreaDir+":") {
101                 areaId = ctx.AreaName2Id[flag.Arg(0)[len(nncp.AreaDir)+1:]]
102                 if areaId == nil {
103                         log.Fatalln("Unknown area specified")
104                 }
105                 node = ctx.Neigh[*ctx.SelfId]
106         } else {
107                 node, err = ctx.FindNode(flag.Arg(0))
108                 if err != nil {
109                         log.Fatalln("Invalid NODE specified:", err)
110                 }
111         }
112
113         maxSize := int64(nncp.MaxFileSize)
114         if *argMaxSize > 0 {
115                 maxSize = int64(*argMaxSize) * 1024
116         }
117
118         nncp.ViaOverride(*viaOverride, ctx, node)
119         ctx.Umask()
120
121         if err = ctx.TxExec(
122                 node,
123                 nice,
124                 replyNice,
125                 flag.Args()[1],
126                 flag.Args()[2:],
127                 bufio.NewReaderSize(os.Stdin, nncp.MTHBlockSize),
128                 int64(*minSize)*1024,
129                 maxSize,
130                 *noCompress,
131                 areaId,
132         ); err != nil {
133                 log.Fatalln(err)
134         }
135 }