]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-file/main.go
3257f2b71a39cb9ac38754896989b071910b5e77
[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-2019 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/v5"
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                 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         flag.Usage = usage
59         flag.Parse()
60         if *warranty {
61                 fmt.Println(nncp.Warranty)
62                 return
63         }
64         if *version {
65                 fmt.Println(nncp.VersionGet())
66                 return
67         }
68         if flag.NArg() != 2 {
69                 usage()
70                 os.Exit(1)
71         }
72         nice, err := nncp.NicenessParse(*niceRaw)
73         if err != nil {
74                 log.Fatalln(err)
75         }
76
77         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
78         if err != nil {
79                 log.Fatalln("Error during initialization:", err)
80         }
81         if ctx.Self == nil {
82                 log.Fatalln("Config lacks private keys")
83         }
84
85         splitted := strings.SplitN(flag.Arg(1), ":", 2)
86         if len(splitted) != 2 {
87                 usage()
88                 os.Exit(1)
89         }
90         node, err := ctx.FindNode(splitted[0])
91         if err != nil {
92                 log.Fatalln("Invalid NODE specified:", err)
93         }
94
95         nncp.ViaOverride(*viaOverride, ctx, node)
96         ctx.Umask()
97
98         var minSize int64
99         if *argMinSize < 0 {
100                 minSize = node.FreqMinSize
101         } else if *argMinSize > 0 {
102                 minSize = *argMinSize * 1024
103         }
104
105         var chunkSize int64
106         if *argChunkSize < 0 {
107                 chunkSize = node.FreqChunked
108         } else if *argChunkSize > 0 {
109                 chunkSize = *argChunkSize * 1024
110         }
111         if chunkSize == 0 {
112                 chunkSize = nncp.MaxFileSize
113         }
114
115         if err = ctx.TxFile(
116                 node,
117                 nice,
118                 flag.Arg(0),
119                 splitted[1],
120                 chunkSize,
121                 minSize,
122                 nncp.MaxFileSize,
123         ); err != nil {
124                 log.Fatalln(err)
125         }
126 }