]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-file/main.go
793a6ef22c3741fa950e39ff2224a3cee4962d8a
[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/v6"
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]\nOptions:\n", os.Args[0])
35         flag.PrintDefaults()
36         fmt.Fprint(os.Stderr, `
37 If SRC equals to -, then read data from stdin to temporary file.
38
39 -minsize/-chunked take NODE's freq.minsize/freq.chunked configuration
40 options by default. You can forcefully turn them off by specifying 0 value.
41 `)
42 }
43
44 func main() {
45         var (
46                 cfgPath      = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
47                 niceRaw      = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFile), "Outbound packet niceness")
48                 argMinSize   = flag.Int64("minsize", -1, "Minimal required resulting packet size, in KiB")
49                 argChunkSize = flag.Int64("chunked", -1, "Split file on specified size chunks, in KiB")
50                 viaOverride  = flag.String("via", "", "Override Via path to destination node")
51                 spoolPath    = flag.String("spool", "", "Override path to spool")
52                 logPath      = flag.String("log", "", "Override path to logfile")
53                 quiet        = flag.Bool("quiet", false, "Print only errors")
54                 showPrgrs    = flag.Bool("progress", false, "Force progress showing")
55                 omitPrgrs    = flag.Bool("noprogress", false, "Omit progress showing")
56                 debug        = flag.Bool("debug", false, "Print debug messages")
57                 version      = flag.Bool("version", false, "Print version information")
58                 warranty     = flag.Bool("warranty", false, "Print warranty information")
59         )
60         log.SetFlags(log.Lshortfile)
61         flag.Usage = usage
62         flag.Parse()
63         if *warranty {
64                 fmt.Println(nncp.Warranty)
65                 return
66         }
67         if *version {
68                 fmt.Println(nncp.VersionGet())
69                 return
70         }
71         if flag.NArg() != 2 {
72                 usage()
73                 os.Exit(1)
74         }
75         nice, err := nncp.NicenessParse(*niceRaw)
76         if err != nil {
77                 log.Fatalln(err)
78         }
79
80         ctx, err := nncp.CtxFromCmdline(
81                 *cfgPath,
82                 *spoolPath,
83                 *logPath,
84                 *quiet,
85                 *showPrgrs,
86                 *omitPrgrs,
87                 *debug,
88         )
89         if err != nil {
90                 log.Fatalln("Error during initialization:", err)
91         }
92         if ctx.Self == nil {
93                 log.Fatalln("Config lacks private keys")
94         }
95
96         splitted := strings.SplitN(flag.Arg(1), ":", 2)
97         if len(splitted) != 2 {
98                 usage()
99                 os.Exit(1)
100         }
101         node, err := ctx.FindNode(splitted[0])
102         if err != nil {
103                 log.Fatalln("Invalid NODE specified:", err)
104         }
105
106         nncp.ViaOverride(*viaOverride, ctx, node)
107         ctx.Umask()
108
109         var minSize int64
110         if *argMinSize < 0 {
111                 minSize = node.FreqMinSize
112         } else if *argMinSize > 0 {
113                 minSize = *argMinSize * 1024
114         }
115
116         var chunkSize int64
117         if *argChunkSize < 0 {
118                 chunkSize = node.FreqChunked
119         } else if *argChunkSize > 0 {
120                 chunkSize = *argChunkSize * 1024
121         }
122         if chunkSize == 0 {
123                 chunkSize = nncp.MaxFileSize
124         }
125
126         if err = ctx.TxFile(
127                 node,
128                 nice,
129                 flag.Arg(0),
130                 splitted[1],
131                 chunkSize,
132                 minSize,
133                 nncp.MaxFileSize,
134         ); err != nil {
135                 log.Fatalln(err)
136         }
137 }