]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/ctx.go
Merge branch 'develop'
[nncp.git] / src / cypherpunks.ru / nncp / ctx.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2018 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, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package nncp
20
21 import (
22         "errors"
23         "io/ioutil"
24         "os"
25         "path/filepath"
26 )
27
28 type Ctx struct {
29         Self   *NodeOur
30         SelfId *NodeId
31         Neigh  map[NodeId]*Node
32         Alias  map[string]*NodeId
33
34         Spool      string
35         LogPath    string
36         Quiet      bool
37         Debug      bool
38         NotifyFile *FromToYAML
39         NotifyFreq *FromToYAML
40 }
41
42 func (ctx *Ctx) FindNode(id string) (*Node, error) {
43         nodeId, known := ctx.Alias[id]
44         if known {
45                 return ctx.Neigh[*nodeId], nil
46         }
47         nodeId, err := NodeIdFromString(id)
48         if err != nil {
49                 return nil, err
50         }
51         node, known := ctx.Neigh[*nodeId]
52         if !known {
53                 return nil, errors.New("Unknown node")
54         }
55         return node, nil
56 }
57
58 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
59         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
60         if err := os.MkdirAll(dirPath, os.FileMode(0700)); err != nil {
61                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
62                 return err
63         }
64         fd, err := os.Open(dirPath)
65         if err != nil {
66                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
67                 return err
68         }
69         fd.Close()
70         return nil
71 }
72
73 func CtxFromCmdline(cfgPath, spoolPath, logPath string, quiet, debug bool) (*Ctx, error) {
74         env := os.Getenv(CfgPathEnv)
75         if env != "" {
76                 cfgPath = env
77         }
78         cfgRaw, err := ioutil.ReadFile(cfgPath)
79         if err != nil {
80                 return nil, err
81         }
82         ctx, err := CfgParse(cfgRaw)
83         if err != nil {
84                 return nil, err
85         }
86         if spoolPath == "" {
87                 env = os.Getenv(CfgSpoolEnv)
88                 if env != "" {
89                         ctx.Spool = env
90                 }
91         } else {
92                 ctx.Spool = spoolPath
93         }
94         if logPath == "" {
95                 env = os.Getenv(CfgLogEnv)
96                 if env != "" {
97                         ctx.LogPath = env
98                 }
99         } else {
100                 ctx.LogPath = logPath
101         }
102         ctx.Quiet = quiet
103         ctx.Debug = debug
104         return ctx, nil
105 }