-
Notifications
You must be signed in to change notification settings - Fork 0
/
agi.go
220 lines (185 loc) · 4.26 KB
/
agi.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
package goagi
import (
"bufio"
"strings"
)
// ErrAGI goagi error
var ErrAGI = newError("AGI session")
// Reader interface for AGI object. Can be net.Conn, os.File or crafted
type Reader interface {
Read(b []byte) (int, error)
}
// Writer interface for AGI object. Can be net.Conn, os.File or crafted
type Writer interface {
Write(b []byte) (int, error)
}
// Debugger for AGI instance. Any interface that provides Printf method.
// It should be used only for debugging as it give lots of output.
type Debugger interface {
Printf(format string, v ...interface{})
}
// AGI object
type AGI struct {
env map[string]string
arg []string
reader Reader
writer Writer
isHUP bool
debugger Debugger
}
const (
codeUnknown int = 0
codeEarly = 100
codeSucc = 200
codeE503 = 503
codeE510 = 510
codeE511 = 511
codeE520 = 520
)
var codeMap = map[string]int{
"100 ": codeEarly,
"200 ": codeSucc,
"503 ": codeE503,
"510 ": codeE510,
"511 ": codeE511,
"520 ": codeE520,
}
/*
New creates and returns AGI object.
Can be used to create agi and fastagi sessions.
Parameters:
- Reader that implements Read method
- Writer that implements Write method
- Debugger that allows to deep library debugging. Nil for production.
*/
func New(r Reader, w Writer, dbg Debugger) (*AGI, error) {
agi := &AGI{
reader: r,
writer: w,
debugger: dbg,
}
agi.dbg("[>] New AGI")
sessData, err := agi.sessionInit()
if err != nil {
return nil, ErrAGI.Msg("Failed to read setup: %s", err)
}
agi.sessionSetup(sessData)
return agi, nil
}
func (agi *AGI) Close() {
agi.env = nil
agi.arg = nil
}
// Env returns AGI environment variable by key
func (agi *AGI) Env(key string) string {
agi.dbg("[>] Env for %q", key)
val, ok := agi.env[key]
if ok {
return val
}
return ""
}
// EnvArgs returns list of environment arguments
func (agi *AGI) EnvArgs() []string {
agi.dbg("[>] EnvArgs")
return agi.arg
}
// IsHungup returns true if AGI channel received HANGUP signal
func (agi *AGI) IsHungup() bool {
return agi.isHUP
}
func (agi *AGI) sessionInit() ([]string, error) {
agi.dbg("[>] sessionInit")
buf := bufio.NewReader(agi.reader)
data := make([]string, 0)
for {
line, err := buf.ReadString('\n')
if err != nil {
return nil, err
}
if line == "\n" {
break
}
agi.dbg(" [v] read line: %q", line)
data = append(data, line[:len(line)-1])
}
return data, nil
}
func (agi *AGI) dbg(pattern string, vargs ...interface{}) {
if agi.debugger != nil {
pattern += "\n"
agi.debugger.Printf(pattern, vargs...)
}
}
// low level response from device and response as string, matched response
// code, true if channel reported as hangup and error.
func (agi *AGI) read() (resp string, code int, err error) {
agi.dbg("[>] readResponse")
buf := bufio.NewReader(agi.reader)
var builder strings.Builder
moreInputExpected := false
for {
line, fail := buf.ReadString('\n')
if fail != nil {
err = fail
return
}
agi.dbg(" [v] got line: %q", line)
builder.WriteString(line)
resp = builder.String()
if codeMatch, ok := matchCode(line); ok {
code = codeMatch
builder.Reset()
return
}
if matchPrefix(line, "520-") {
moreInputExpected = true
}
if matchPrefix(line, "HANGUP") {
agi.isHUP = true
builder.Reset()
continue
}
if !moreInputExpected {
err = ErrAGI.Msg("Invalid input while reading response: %q", resp)
return
}
}
}
func matchPrefix(line, pattern string) bool {
if len(line) < len(pattern) {
return false
}
return line[:len(pattern)] == pattern
}
func matchCode(data string) (int, bool) {
if len(data) < 4 {
return 0, false
}
if codeMatch, ok := codeMap[data[:4]]; ok {
return codeMatch, true
}
return 0, false
}
func (agi *AGI) write(command []byte) error {
agi.dbg("[>] readResponse")
agi.dbg(" [v] writing command: %q", string(command))
_, err := agi.writer.Write(command)
if err != nil {
return err
}
agi.dbg(" [v] write successfully done")
return nil
}
// write command, read and parse response
func (agi *AGI) execute(cmd string) (Response, error) {
agi.dbg("[>] execute cmd: %q", cmd)
if err := agi.write([]byte(cmd)); err != nil {
return nil, err
}
resp, code, err := agi.read()
if err != nil {
return nil, err
}
return agi.parseResponse(resp, code)
}