]> Cypherpunks.ru repositories - nncp.git/blob - src/cmd/nncp-freq/main.go
25328875426a525d23035044449ccab3b784d286
[nncp.git] / src / cmd / nncp-freq / 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 request via NNCP.
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "log"
25         "os"
26         "path/filepath"
27         "strings"
28
29         "go.cypherpunks.ru/nncp/v5"
30 )
31
32 func usage() {
33         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
34         fmt.Fprintf(os.Stderr, "nncp-freq -- send file request\n\n")
35         fmt.Fprintf(os.Stderr, "Usage: %s [options] NODE:SRC [DST]\nOptions:\n", os.Args[0])
36         flag.PrintDefaults()
37 }
38
39 func main() {
40         var (
41                 cfgPath      = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
42                 niceRaw      = flag.String("nice", nncp.NicenessFmt(nncp.DefaultNiceFreq), "Outbound packet niceness")
43                 replyNiceRaw = flag.String("replynice", nncp.NicenessFmt(nncp.DefaultNiceFile), "Reply file packet niceness")
44                 minSize      = flag.Uint64("minsize", 0, "Minimal required resulting packet size, in KiB")
45                 viaOverride  = flag.String("via", "", "Override Via path to destination node")
46                 spoolPath    = flag.String("spool", "", "Override path to spool")
47                 logPath      = flag.String("log", "", "Override path to logfile")
48                 quiet        = flag.Bool("quiet", false, "Print only errors")
49                 debug        = flag.Bool("debug", false, "Print debug messages")
50                 version      = flag.Bool("version", false, "Print version information")
51                 warranty     = flag.Bool("warranty", false, "Print warranty information")
52         )
53         flag.Usage = usage
54         flag.Parse()
55         if *warranty {
56                 fmt.Println(nncp.Warranty)
57                 return
58         }
59         if *version {
60                 fmt.Println(nncp.VersionGet())
61                 return
62         }
63         if flag.NArg() == 0 {
64                 usage()
65                 os.Exit(1)
66         }
67         nice, err := nncp.NicenessParse(*niceRaw)
68         if err != nil {
69                 log.Fatalln(err)
70         }
71         replyNice, err := nncp.NicenessParse(*replyNiceRaw)
72         if err != nil {
73                 log.Fatalln(err)
74         }
75
76         ctx, err := nncp.CtxFromCmdline(*cfgPath, *spoolPath, *logPath, *quiet, *debug)
77         if err != nil {
78                 log.Fatalln("Error during initialization:", err)
79         }
80         if ctx.Self == nil {
81                 log.Fatalln("Config lacks private keys")
82         }
83
84         splitted := strings.SplitN(flag.Arg(0), ":", 2)
85         if len(splitted) != 2 {
86                 usage()
87                 os.Exit(1)
88         }
89         node, err := ctx.FindNode(splitted[0])
90         if err != nil {
91                 log.Fatalln("Invalid NODE specified:", err)
92         }
93
94         nncp.ViaOverride(*viaOverride, ctx, node)
95         ctx.Umask()
96
97         var dst string
98         if flag.NArg() == 2 {
99                 dst = flag.Arg(1)
100         } else {
101                 dst = filepath.Base(splitted[1])
102         }
103
104         if err = ctx.TxFreq(
105                 node,
106                 nice,
107                 replyNice,
108                 splitted[1],
109                 dst,
110                 int64(*minSize)*1024,
111         ); err != nil {
112                 log.Fatalln(err)
113         }
114 }