This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
config.go
105 lines (89 loc) · 2.1 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package textsecure
import (
"io/ioutil"
"os"
"github.com/go-yaml/yaml"
"github.com/signal-golang/textsecure/config"
log "github.com/sirupsen/logrus"
)
var configFile string
// TODO: some race conditions to be solved
func checkUUID(cfg *config.Config) *config.Config {
if len(cfg.UUID) != 36 {
log.Debugln(cfg.UUID)
defer func(cfg *config.Config) *config.Config {
recover()
UUID, err := GetMyUUID()
if err != nil {
log.Debugln("[textsecure] missing my uuid", err)
return cfg
}
cfg.UUID = UUID
return cfg
}(cfg)
if cfg.UUID == "notset" {
log.Debugln("[textsecure] missing my uuid notset")
}
}
return cfg
}
// ReadConfig reads a YAML config file
func ReadConfig(fileName string) (*config.Config, error) {
b, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
configFile = fileName
cfg := &config.Config{}
err = yaml.Unmarshal(b, cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
// WriteConfig saves a config to a file
func WriteConfig(filename string, cfg *config.Config) error {
b, err := yaml.Marshal(cfg)
if err != nil {
return err
}
return ioutil.WriteFile(filename, b, 0600)
}
func saveConfig(cfg *config.Config) error {
log.Debugln("[textsecure] saving config", cfg.Tel)
err := WriteConfig(configFile, cfg)
if err != nil {
log.Errorln("[textsecure] failed to save config", err)
return err
}
return nil
}
func RefreshConfig() {
cfg, err := loadConfig()
if err != nil {
log.Errorln("[textsecure] failed to load config", err)
return
}
saveConfig(cfg)
}
// loadConfig gets the config via the client and makes sure
// that for unset values sane defaults are used
func loadConfig() (*config.Config, error) {
log.Debugln("[textsecure] loading config")
cfg, err := client.GetConfig()
if err != nil {
return nil, err
}
if cfg.Server == "" {
cfg.Server = "https://chat.signal.org:443"
}
if cfg.VerificationType == "" {
cfg.VerificationType = "sms"
}
if cfg.StorageDir == "" {
cfg.StorageDir = ".storage"
}
return cfg, nil
}