-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvoice_channel.go
185 lines (173 loc) · 3.51 KB
/
voice_channel.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"github.com/urfave/cli"
)
type VoiceChannel struct {
IMSI string `json:"imsi"`
Name string `json:"name"`
Number string `json:"number"`
RxGain int `json:"rx-gain,omitempty"`
TxGain int `json:"tx-gain,omitempty"`
Label string `json:"label"`
SMS string `json:"sms_out"`
Calls string `json:"calls_out"`
}
type VoiceState struct {
Enabled bool `json:"enabled"`
Config *VoiceChannel `json:"config"`
}
func VoiceChannelCMD(ctx *cli.Context) error {
if ctx.IsSet(enableFlag) {
return EnableVoiceChan(ctx)
}
if ctx.IsSet(disableFlag) {
return DisableVoiceChan(ctx)
}
if ctx.IsSet(removeFlag) {
return RemoveVoiceChan(ctx)
}
if ctx.IsSet(configFlag) {
return configVoice(ctx)
}
return nil
}
func configVoice(ctx *cli.Context) error {
src := ctx.String("config")
if src == "" {
return errors.New("fconf: missing configuration source file")
}
var b []byte
var err error
if src == "stdin" {
b, err = ReadFromStdin()
if err != nil {
return err
}
} else {
b, err = ioutil.ReadFile(src)
if err != nil {
return err
}
}
e := VoiceChannel{}
err = json.Unmarshal(b, &e)
if err != nil {
return err
}
state := &VoiceState{Config: &e}
ws, err := voiceChanState(e.IMSI)
if err == nil {
state.Enabled = ws.Enabled
}
b, _ = json.Marshal(state)
setInterface(ctx, e.IMSI)
return keepState(
fmt.Sprintf(defaultVoiceChanConfig, e.IMSI), b)
}
func voiceChanState(i string) (*VoiceState, error) {
dir := os.Getenv("FCONF_CONFIGDIR")
if dir == "" {
dir = fconfConfigDir
}
b, err := ioutil.ReadFile(filepath.Join(dir,
fmt.Sprintf(defaultVoiceChanConfig, i)))
if err != nil {
return nil, err
}
w := &VoiceState{}
err = json.Unmarshal(b, w)
if err != nil {
return nil, err
}
if w.Config == nil {
return nil, ErrWrongStateFile
}
return w, nil
}
func EnableVoiceChan(ctx *cli.Context) error {
if ctx.IsSet(configFlag) {
err := configVoice(ctx)
if err != nil {
return err
}
}
i := getInterface(ctx)
if i == "" {
return errors.New("missing imSi , you must specify imsi")
}
w, err := voiceChanState(i)
if err != nil {
return err
}
w.Enabled = true
data, err := json.Marshal(w)
if err != nil {
return err
}
err = keepState(
fmt.Sprintf(defaultVoiceChanConfig, w.Config.IMSI), data)
if err != nil {
return err
}
return notify(ctx)
}
func DisableVoiceChan(ctx *cli.Context) error {
i := getInterface(ctx)
if i == "" {
return errors.New("missing imsi , you must specify imsi")
}
w, err := voiceChanState(i)
if err != nil {
return err
}
w.Enabled = false
data, err := json.Marshal(w)
if err != nil {
return err
}
err = keepState(
fmt.Sprintf(defaultVoiceChanConfig, w.Config.IMSI), data)
if err != nil {
return err
}
return notify(ctx)
}
func RemoveVoiceChan(ctx *cli.Context) error {
i := getInterface(ctx)
if i == "" {
return errors.New("missing imsi , you must specify imsi")
}
w, err := voiceChanState(i)
if err != nil {
return err
}
dir := os.Getenv("FCONF_CONFIGDIR")
if dir == "" {
dir = fconfConfigDir
}
err = checkDir(dir)
if err != nil {
return err
}
name := filepath.Join(dir, fmt.Sprintf(defaultVoiceChanConfig, w.Config.IMSI))
err = removeFile(name)
if err != nil {
return err
}
return notify(ctx)
}
func notify(ctx *cli.Context) error {
if ctx.GlobalIsSet("pid") {
pid := ctx.GlobalInt("pid")
fmt.Printf("sending SIGHUP to %d\n", pid)
return syscall.Kill(pid, syscall.SIGHUP)
}
return nil
}