]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-exec/main.go
Raise copyright years
[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-2022 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.Fprintf(os.Stderr, nncp.UsageHeader())
34         fmt.Fprintf(os.Stderr, "nncp-exec -- send execution command\n\n")
35         fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE HANDLE [ARG0 ARG1 ...]\n", os.Args[0])
36         fmt.Fprintf(os.Stderr, "       %s [options] %s:AREA HANDLE [ARG0 ARG1 ...]\nOptions:\n",
37                 os.Args[0], nncp.AreaDir)
38         flag.PrintDefaults()
39 }
40
41 func main() {
42         var (
43                 noCompress   = flag.Bool("nocompress", false, "Do not compress input data")
44                 cfgPath      = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
45                 niceRaw      = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceExec), "Outbound packet niceness")
46                 replyNiceRaw = flag.String("replynice", nncp.NicenessFmt(nncp.DefaultNiceFile), "Possible reply packet niceness")
47                 minSize      = flag.Uint64("minsize", 0, "Minimal required resulting packet size, in KiB")
48                 argMaxSize   = flag.Uint64("maxsize", 0, "Maximal allowable resulting packet size, in KiB")
49                 viaOverride  = flag.String("via", "", "Override Via path to destination node")
50                 spoolPath    = flag.String("spool", "", "Override path to spool")
51                 logPath      = flag.String("log", "", "Override path to logfile")
52                 quiet        = flag.Bool("quiet", false, "Print only errors")
53                 showPrgrs    = flag.Bool("progress", false, "Force progress showing")
54                 omitPrgrs    = flag.Bool("noprogress", false, "Omit progress showing")
55                 debug        = flag.Bool("debug", false, "Print debug messages")
56                 version      = flag.Bool("version", false, "Print version information")
57                 warranty     = flag.Bool("warranty", false, "Print warranty information")
58         )
59         log.SetFlags(log.Lshortfile)
60         flag.Usage = usage
61         flag.Parse()
62         if *warranty {
63                 fmt.Println(nncp.Warranty)
64                 return
65         }
66         if *version {
67                 fmt.Println(nncp.VersionGet())
68                 return
69         }
70         if flag.NArg() < 2 {
71                 usage()
72                 os.Exit(1)
73         }
74         nice, err := nncp.NicenessParse(*niceRaw)
75         if err != nil {
76                 log.Fatalln(err)
77         }
78         replyNice, err := nncp.NicenessParse(*replyNiceRaw)
79         if err != nil {
80                 log.Fatalln(err)
81         }
82
83         ctx, err := nncp.CtxFromCmdline(
84                 *cfgPath,
85                 *spoolPath,
86                 *logPath,
87                 *quiet,
88                 *showPrgrs,
89                 *omitPrgrs,
90                 *debug,
91         )
92         if err != nil {
93                 log.Fatalln("Error during initialization:", err)
94         }
95         if ctx.Self == nil {
96                 log.Fatalln("Config lacks private keys")
97         }
98
99         var areaId *nncp.AreaId
100         var node *nncp.Node
101         if strings.HasPrefix(flag.Arg(0), nncp.AreaDir+":") {
102                 areaId = ctx.AreaName2Id[flag.Arg(0)[len(nncp.AreaDir)+1:]]
103                 if areaId == nil {
104                         log.Fatalln("Unknown area specified")
105                 }
106                 node = ctx.Neigh[*ctx.SelfId]
107         } else {
108                 node, err = ctx.FindNode(flag.Arg(0))
109                 if err != nil {
110                         log.Fatalln("Invalid NODE specified:", err)
111                 }
112         }
113
114         maxSize := int64(nncp.MaxFileSize)
115         if *argMaxSize > 0 {
116                 maxSize = int64(*argMaxSize) * 1024
117         }
118
119         nncp.ViaOverride(*viaOverride, ctx, node)
120         ctx.Umask()
121
122         if err = ctx.TxExec(
123                 node,
124                 nice,
125                 replyNice,
126                 flag.Args()[1],
127                 flag.Args()[2:],
128                 bufio.NewReader(os.Stdin),
129                 int64(*minSize)*1024,
130                 maxSize,
131                 *noCompress,
132                 areaId,
133         ); err != nil {
134                 log.Fatalln(err)
135         }
136 }