-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.go
378 lines (326 loc) · 9.76 KB
/
commands.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package gobaresip
import (
"bytes"
"fmt"
"strconv"
"strings"
"sync/atomic"
"time"
"unicode"
"github.com/goccy/go-json"
)
/*
/about About box
/accept a Accept incoming call
/acceptdir .. Accept incoming call with audio and videodirection.
/answermode .. Set answer mode
/aubitrate .. Set audio bitrate
/audio_debug A Audio stream
/auplay .. Switch audio player
/ausrc .. Switch audio source
/callfind .. Find call
/callstat c Call status
/contact_next > Set next contact
/contact_prev < Set previous contact
/contacts C List contacts
/dial .. d .. Dial
/dialcontact D Dial current contact
/dialdir .. Dial with audio and videodirection.
/dnd .. Set Do not Disturb
/hangup b Hangup call
/hangupall .. Hangup all calls with direction
/help h Help menu
/hold x Call hold
/insmod .. Load module
/line .. @ .. Set current call
/listcalls l List active calls
/medialdir .. Set local media direction
/message .. M .. Message current contact
/mute m Call mute/un-mute
/options .. o .. Options
/quit q Quit
/reginfo r Registration info
/reinvite I Send re-INVITE
/resume X Call resume
/rmmod .. Unload module
/setadelay .. Set answer delay for outgoing call
/sndcode .. Send Code
/statmode S Statusmode toggle
/tlsissuer TLS certificate issuer
/tlssubject TLS certificate subject
/transfer .. t .. Transfer call
/uadel .. Delete User-Agent
/uadelall .. Delete all User-Agents
/uafind .. Find User-Agent
/uanew .. Create User-Agent
/uareg .. UA register [index]
/video_debug V Video stream
/videodir .. Set video direction
/vidsrc .. Switch video source
*/
//CommandMsg struct for ctrl_tcp
type CommandMsg struct {
Command string `json:"command,omitempty"`
Params string `json:"params,omitempty"`
Token string `json:"token,omitempty"`
}
func buildCommand(command, params, token string) *CommandMsg {
return &CommandMsg{
Command: command,
Params: params,
Token: token,
}
}
// Cmd will send a raw baresip command over ctrl_tcp.
func (b *Baresip) Cmd(command, params, token string) error {
msg, err := json.Marshal(buildCommand(command, params, token))
if err != nil {
return err
}
if atomic.LoadUint32(&b.ctrlConnAlive) == 0 {
return fmt.Errorf("can't write command to closed tcp_ctrl connection")
}
b.ctrlConn.SetWriteDeadline(time.Now().Add(2 * time.Second))
_, err = b.ctrlConn.Write([]byte(fmt.Sprintf("%d:%s,", len(msg), msg)))
if err != nil {
return err
}
return nil
}
// CmdAccept will accept incoming call
func (b *Baresip) CmdAccept() error {
c := "accept"
return b.Cmd(c, "", "cmd_"+c)
}
// CmdAcceptdir will accept incoming call with audio and videodirection.
func (b *Baresip) CmdAcceptdir(s string) error {
c := "acceptdir"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdAnswermode will set answer mode
func (b *Baresip) CmdAnswermode(s string) error {
c := "answermode"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdAuplay will switch audio player
func (b *Baresip) CmdAuplay(s string) error {
c := "auplay"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdAusrc will switch audio source
func (b *Baresip) CmdAusrc(s string) error {
c := "ausrc"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdCallstat will show call status
func (b *Baresip) CmdCallstat() error {
c := "callstat"
return b.Cmd(c, "", "cmd_"+c)
}
// CmdContact_next will set next contact
func (b *Baresip) CmdContact_next() error {
c := "contact_next"
return b.Cmd(c, "", "cmd_"+c)
}
// CmdContact_prev will set previous contact
func (b *Baresip) CmdContact_prev() error {
c := "contact_prev"
return b.Cmd(c, "", "cmd_"+c)
}
// CmdAutodial will dial number automatically
func (b *Baresip) CmdAutodial(s string) error {
c := "autodial dial"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdAutodialdelay will set delay before auto dial [ms]
func (b *Baresip) CmdAutodialdelay(n int) error {
c := "autodialdelay"
return b.Cmd(c, strconv.Itoa(n), "cmd_"+c+"_"+strconv.Itoa(n))
}
// CmdDial will dial number
func (b *Baresip) CmdDial(s string) error {
c := "dial"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdDialcontact will dial current contact
func (b *Baresip) CmdDialcontact() error {
c := "dialcontact"
return b.Cmd(c, "", "cmd_"+c)
}
// CmdDialdir will dial with audio and videodirection
func (b *Baresip) CmdDialdir(s string) error {
c := "dialdir"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdAutohangup will hangup call automatically
func (b *Baresip) CmdAutohangup() error {
c := "autohangup"
return b.Cmd(c, "hangup", "cmd_"+c)
}
// CmdAutohangupdelay will set delay before hangup [ms]
func (b *Baresip) CmdAutohangupdelay(n int) error {
c := "autohangupdelay"
return b.Cmd(c, strconv.Itoa(n), "cmd_"+c+"_"+strconv.Itoa(n))
}
// CmdHangup will hangup call
func (b *Baresip) CmdHangup() error {
c := "hangup"
return b.Cmd(c, "", "cmd_"+c)
}
// CmdHangupID will hangup call with Call-ID
func (b *Baresip) CmdHangupID(callID string) error {
c := "hangup"
return b.Cmd(c, callID, "cmd_"+c)
}
// CmdHangupall will hangup all calls with direction
func (b *Baresip) CmdHangupall(s string) error {
c := "hangupall"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdInsmod will load module
func (b *Baresip) CmdInsmod(s string) error {
c := "insmod"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdListcalls will list active calls
func (b *Baresip) CmdListcalls() error {
c := "listcalls"
return b.Cmd(c, "", "cmd_"+c)
}
// CmdReginfo will list registration info
func (b *Baresip) CmdReginfo() error {
c := "reginfo"
return b.Cmd(c, "", "cmd_"+c)
}
// CmdRmmod will unload module
func (b *Baresip) CmdRmmod(s string) error {
c := "rmmod"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdSetadelay will set answer delay for outgoing call
func (b *Baresip) CmdSetadelay(n int) error {
c := "setadelay"
return b.Cmd(c, strconv.Itoa(n), "cmd_"+c+"_"+strconv.Itoa(n))
}
// CmdUadel will delete User-Agent
func (b *Baresip) CmdUadel(s string) error {
c := "uadel"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdUadelall will delete all User-Agents
func (b *Baresip) CmdUadelall() error {
c := "uadelall"
return b.Cmd(c, "", "cmd_"+c)
}
// CmdUafind will find User-Agent <aor>
func (b *Baresip) CmdUafind(s string) error {
c := "uafind"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdUanew will create User-Agent
func (b *Baresip) CmdUanew(s string) error {
c := "uanew"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdUareg will register <regint> [index]
func (b *Baresip) CmdUareg(s string) error {
c := "uareg"
return b.Cmd(c, s, "cmd_"+c+"_"+s)
}
// CmdQuit will quit baresip
func (b *Baresip) CmdQuit() error {
c := "quit"
return b.Cmd(c, "", "cmd_"+c)
}
func (b *Baresip) CmdWs(raw []byte) error {
m := strings.SplitN(string(bytes.TrimSpace(bytes.Join(bytes.Fields(raw), []byte(" ")))), " ", 2)
if len(m) < 1 {
return nil
}
m[0] = strings.ToLower(m[0])
if m[0] == "quit" || m[0] == "uadelall" {
return nil
}
if len(m) == 2 && m[0] == "autodialadd" {
b.CmdAutodialadd(m[1])
} else if len(m) == 2 && m[0] == "autodialdel" {
b.CmdAutodialdel(m[1])
} else if len(m) == 2 && m[0] == "autohangupgap" {
b.CmdAutohangupgap(m[1])
} else if m[0] == "autocmdinfo" {
b.CmdAutocmdinfo()
} else if len(m) == 1 {
b.Cmd(m[0], "", "cmd_"+m[0])
} else if len(m) == 2 {
b.Cmd(m[0], m[1], "cmd_"+m[0])
}
return nil
}
func cutSpace(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, str)
}
func (b *Baresip) CmdAutodialadd(s string) error {
in := strings.Split(cutSpace(s), ",")
for _, v := range in {
gap := 60
parts := strings.Split(v, ";autodialgap=")
if len(parts) == 2 {
if g, err := strconv.Atoi(parts[1]); err == nil {
gap = g
}
}
b.autoCmd.mux.RLock()
_, ok := b.autoCmd.num[parts[0]]
b.autoCmd.mux.RUnlock()
if !ok {
b.autoCmd.mux.Lock()
b.autoCmd.num[parts[0]] = gap
b.autoCmd.mux.Unlock()
go b.autoDialSchedule(parts[0], gap)
}
}
return b.Cmd("autodialinfo", "", "cmd_autodialadd")
}
func (b *Baresip) autoDialSchedule(num string, gap int) {
if gap < 1 {
return
}
tick := time.NewTicker(time.Duration(gap) * time.Second)
defer tick.Stop()
for ; true; <-tick.C {
b.autoCmd.mux.RLock()
_, ok := b.autoCmd.num[num]
b.autoCmd.mux.RUnlock()
if !ok {
return
}
b.CmdDial(num)
}
}
func (b *Baresip) CmdAutodialdel(s string) error {
data := strings.Split(cutSpace(s), ",")
for _, d := range data {
parts := strings.Split(d, ";autodialgap=")
b.autoCmd.mux.Lock()
delete(b.autoCmd.num, parts[0])
b.autoCmd.mux.Unlock()
}
return b.Cmd("autodialinfo", "", "cmd_autodialdel")
}
func (b *Baresip) CmdAutohangupgap(s string) error {
if n, err := strconv.Atoi(s); err == nil {
if n < 0 {
n = 0
}
atomic.StoreUint32(&b.autoCmd.hangupGap, uint32(n))
}
return b.Cmd("autodialinfo", "", "cmd_autohangupgap")
}
func (b *Baresip) CmdAutocmdinfo() error {
return b.Cmd("autodialinfo", "", "cmd_autocmdinfo")
}