]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-file/main.go
6870aec808c818b7c1dfb9aedcf94a2a3fc81e67
[nncp.git] / src / cmd / nncp-file / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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 file via NNCP.
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "log"
25         "os"
26         "strings"
27
28         "go.cypherpunks.ru/nncp/v7"
29 )
30
31 func usage() {
32         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
33         fmt.Fprintf(os.Stderr, "nncp-file -- send file\n\n")
34         fmt.Fprintf(os.Stderr, "Usage: %s [options] SRC NODE:[DST]\n", os.Args[0])
35         fmt.Fprintf(os.Stderr, "       %s [options] SRC %s:AREA:[DST]\nOptions:\n",
36                 os.Args[0], nncp.AreaDir)
37         flag.PrintDefaults()
38         fmt.Fprint(os.Stderr, `
39 If SRC equals to -, then read data from stdin to temporary file.
40
41 -minsize/-chunked take NODE's freq.minsize/freq.chunked configuration
42 options by default. You can forcefully turn them off by specifying 0 value.
43 `)
44 }
45
46 func main() {
47         var (
48                 cfgPath      = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
49                 niceRaw      = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFile), "Outbound packet niceness")
50                 argMinSize   = flag.Int64("minsize", -1, "Minimal required resulting packet size, in KiB")
51                 argChunkSize = flag.Int64("chunked", -1, "Split file on specified size chunks, in KiB")
52                 viaOverride  = flag.String("via", "", "Override Via path to destination node")
53                 spoolPath    = flag.String("spool", "", "Override path to spool")
54                 logPath      = flag.String("log", "", "Override path to logfile")
55                 quiet        = flag.Bool("quiet", false, "Print only errors")
56                 showPrgrs    = flag.Bool("progress", false, "Force progress showing")
57                 omitPrgrs    = flag.Bool("noprogress", false, "Omit progress showing")
58                 debug        = flag.Bool("debug", false, "Print debug messages")
59                 version      = flag.Bool("version", false, "Print version information")
60                 warranty     = flag.Bool("warranty", false, "Print warranty information")
61         )
62         log.SetFlags(log.Lshortfile)
63         flag.Usage = usage
64         flag.Parse()
65         if *warranty {
66                 fmt.Println(nncp.Warranty)
67                 return
68         }
69         if *version {
70                 fmt.Println(nncp.VersionGet())
71                 return
72         }
73         if flag.NArg() != 2 {
74                 usage()
75                 os.Exit(1)
76         }
77         nice, err := nncp.NicenessParse(*niceRaw)
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         splitted := strings.Split(flag.Arg(1), ":")
99         if len(splitted) < 2 {
100                 usage()
101                 os.Exit(1)
102         }
103         var areaId *nncp.AreaId
104         var node *nncp.Node
105         if splitted[0] == nncp.AreaDir {
106                 if len(splitted) < 3 {
107                         usage()
108                         os.Exit(1)
109                 }
110                 areaId = ctx.AreaName2Id[splitted[1]]
111                 if areaId == nil {
112                         log.Fatalln("Unknown area specified")
113                 }
114                 node = ctx.Neigh[*ctx.SelfId]
115                 splitted = splitted[2:]
116         } else {
117                 node, err = ctx.FindNode(splitted[0])
118                 if err != nil {
119                         log.Fatalln("Invalid NODE specified:", err)
120                 }
121                 splitted = splitted[1:]
122         }
123
124         nncp.ViaOverride(*viaOverride, ctx, node)
125         ctx.Umask()
126
127         var minSize int64
128         if *argMinSize < 0 {
129                 minSize = node.FreqMinSize
130         } else if *argMinSize > 0 {
131                 minSize = *argMinSize * 1024
132         }
133
134         var chunkSize int64
135         if *argChunkSize < 0 {
136                 chunkSize = node.FreqChunked
137         } else if *argChunkSize > 0 {
138                 chunkSize = *argChunkSize * 1024
139         }
140         if chunkSize == 0 {
141                 chunkSize = nncp.MaxFileSize
142         }
143
144         if err = ctx.TxFile(
145                 node,
146                 nice,
147                 flag.Arg(0),
148                 strings.Join(splitted, ":"),
149                 chunkSize,
150                 minSize,
151                 nncp.MaxFileSize,
152                 areaId,
153         ); err != nil {
154                 log.Fatalln(err)
155         }
156 }