| 1 |
package agi |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"context" |
| 6 |
"io" |
| 7 |
"net" |
| 8 |
"sort" |
| 9 |
"strconv" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/pkg/errors" |
| 13 |
"github.com/wenerme/astgo/agi/agimodels" |
| 14 |
"go.uber.org/zap" |
| 15 |
) |
| 16 |
|
| 17 |
const ( |
| 18 |
|
| 19 |
StatusOK = 200 |
| 20 |
|
| 21 |
|
| 22 |
StatusInvalid = 510 |
| 23 |
|
| 24 |
|
| 25 |
StatusDeadChannel = 511 |
| 26 |
|
| 27 |
|
| 28 |
StatusEndUsage = 520 |
| 29 |
) |
| 30 |
|
| 31 |
type Request struct { |
| 32 |
} |
| 33 |
type Response struct { |
| 34 |
Error error |
| 35 |
Status int |
| 36 |
Result int |
| 37 |
ResultString string |
| 38 |
Value string |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func (r *Response) Res() (string, error) { |
| 43 |
return r.ResultString, r.Error |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func (r *Response) Err() error { |
| 48 |
return r.Error |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
func (r *Response) Val() (string, error) { |
| 53 |
return r.Value, r.Error |
| 54 |
} |
| 55 |
|
| 56 |
type Session struct { |
| 57 |
Variables map[string]string |
| 58 |
r io.Reader |
| 59 |
w io.Writer |
| 60 |
ctx context.Context |
| 61 |
conn net.Conn |
| 62 |
} |
| 63 |
|
| 64 |
type HandlerFunc func(session *Session) |
| 65 |
|
| 66 |
func NewSession(c context.Context, r io.Reader, w io.Writer, conn net.Conn) (*Session, error) { |
| 67 |
session := &Session{ |
| 68 |
ctx: c, |
| 69 |
r: r, |
| 70 |
w: w, |
| 71 |
conn: conn, |
| 72 |
Variables: make(map[string]string), |
| 73 |
} |
| 74 |
return session, session.scanVariables() |
| 75 |
} |
| 76 |
func (a *Session) scanVariables() error { |
| 77 |
s := bufio.NewScanner(a.r) |
| 78 |
for s.Scan() { |
| 79 |
if s.Text() == "" { |
| 80 |
break |
| 81 |
} |
| 82 |
|
| 83 |
terms := strings.SplitN(s.Text(), ":", 2) |
| 84 |
if len(terms) == 2 { |
| 85 |
a.Variables[strings.TrimSpace(terms[0])] = strings.TrimSpace(terms[1]) |
| 86 |
} |
| 87 |
} |
| 88 |
return s.Err() |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (a *Session) Close() (err error) { |
| 93 |
if a.conn != nil { |
| 94 |
err = a.conn.Close() |
| 95 |
a.conn = nil |
| 96 |
} |
| 97 |
return |
| 98 |
} |
| 99 |
func ParseResponse(s string) *Response { |
| 100 |
sp := strings.SplitN(s, " ", 3) |
| 101 |
if len(sp) < 2 { |
| 102 |
return &Response{ |
| 103 |
Error: errors.Errorf("invalid response: %q", s), |
| 104 |
} |
| 105 |
} |
| 106 |
r := &Response{} |
| 107 |
r.Status, r.Error = strconv.Atoi(sp[0]) |
| 108 |
r.ResultString = sp[1][len("result="):] |
| 109 |
r.Result, _ = strconv.Atoi(r.ResultString) |
| 110 |
if len(sp) == 3 { |
| 111 |
r.Value = sp[2] |
| 112 |
} |
| 113 |
if r.Status != 200 { |
| 114 |
r.Error = errors.Errorf("status %v result %v extra %v", r.Status, r.ResultString, r.Value) |
| 115 |
} |
| 116 |
return r |
| 117 |
} |
| 118 |
func (a *Session) Command(cmd string) *Response { |
| 119 |
_, err := a.w.Write([]byte(cmd + "\n")) |
| 120 |
r := &Response{Error: err} |
| 121 |
if err != nil { |
| 122 |
return r |
| 123 |
} |
| 124 |
s := bufio.NewScanner(a.r) |
| 125 |
if s.Scan() { |
| 126 |
return ParseResponse(s.Text()) |
| 127 |
} |
| 128 |
return r |
| 129 |
} |
| 130 |
func (a *Session) Client() *agimodels.Client { |
| 131 |
return &agimodels.Client{ |
| 132 |
Handler: agimodels.HandlerFunc(func(cmd agimodels.Command) agimodels.Response { |
| 133 |
command, err := cmd.Command() |
| 134 |
if err != nil { |
| 135 |
return &Response{ |
| 136 |
Error: err, |
| 137 |
} |
| 138 |
} |
| 139 |
return a.Command(command) |
| 140 |
}), |
| 141 |
} |
| 142 |
} |
| 143 |
func (a *Session) RequestVariable() *RequestVariable { |
| 144 |
rc := &RequestVariable{} |
| 145 |
rc.Load(a.Variables) |
| 146 |
return rc |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
func Listen(addr string, handler HandlerFunc) error { |
| 151 |
if addr == "" { |
| 152 |
addr = "0.0.0.0:4573" |
| 153 |
} |
| 154 |
|
| 155 |
l, err := net.Listen("tcp", addr) |
| 156 |
if err != nil { |
| 157 |
return errors.Wrap(err, "failed to bind server") |
| 158 |
} |
| 159 |
defer l.Close() |
| 160 |
|
| 161 |
for { |
| 162 |
conn, err := l.Accept() |
| 163 |
if err != nil { |
| 164 |
return errors.Wrap(err, "failed to accept TCP connection") |
| 165 |
} |
| 166 |
|
| 167 |
session, err := NewSession(nil, conn, conn, conn) |
| 168 |
if err != nil { |
| 169 |
return errors.Wrap(err, "failed init session") |
| 170 |
} |
| 171 |
go func() { |
| 172 |
defer session.Close() |
| 173 |
handler(session) |
| 174 |
}() |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
type RequestVariable struct { |
| 179 |
Request string |
| 180 |
Channel string |
| 181 |
Language string |
| 182 |
Type string |
| 183 |
UniqueID string |
| 184 |
Version string |
| 185 |
CallerID string |
| 186 |
CallerIDName string |
| 187 |
CallingPres int |
| 188 |
CallingAni2 int |
| 189 |
CallingTon int |
| 190 |
CallingTns int |
| 191 |
DNID string |
| 192 |
RDNIS string |
| 193 |
Context string |
| 194 |
Extension string |
| 195 |
Priority int |
| 196 |
Enhanced bool |
| 197 |
AccountCode string |
| 198 |
ThreadID int |
| 199 |
Args []string |
| 200 |
} |
| 201 |
|
| 202 |
func (rc *RequestVariable) Load(m map[string]string) { |
| 203 |
var args []struct { |
| 204 |
i int |
| 205 |
v string |
| 206 |
} |
| 207 |
for k, v := range m { |
| 208 |
if !strings.HasPrefix(k, "agi_") { |
| 209 |
continue |
| 210 |
} |
| 211 |
name := k[4:] |
| 212 |
if strings.HasPrefix(name, "arg_") { |
| 213 |
i, err := strconv.Atoi(name[4:]) |
| 214 |
if err != nil { |
| 215 |
|
| 216 |
zap.S().With("arg", name).Warn("skip invalid request arg") |
| 217 |
} |
| 218 |
args = append(args, struct { |
| 219 |
i int |
| 220 |
v string |
| 221 |
}{i: i, v: v}) |
| 222 |
continue |
| 223 |
} |
| 224 |
if v == "unknown" { |
| 225 |
continue |
| 226 |
} |
| 227 |
var err error |
| 228 |
switch name { |
| 229 |
case "request": |
| 230 |
rc.Request = v |
| 231 |
case "channel": |
| 232 |
rc.Channel = v |
| 233 |
case "language": |
| 234 |
rc.Language = v |
| 235 |
case "type": |
| 236 |
rc.Type = v |
| 237 |
case "uniqueid": |
| 238 |
rc.UniqueID = v |
| 239 |
case "version": |
| 240 |
rc.Version = v |
| 241 |
case "callerid": |
| 242 |
rc.CallerID = v |
| 243 |
case "calleridname": |
| 244 |
rc.CallerIDName = v |
| 245 |
case "callingpres": |
| 246 |
rc.CallingPres, err = strconv.Atoi(v) |
| 247 |
case "callingani2": |
| 248 |
rc.CallingAni2, err = strconv.Atoi(v) |
| 249 |
case "callington": |
| 250 |
rc.CallingTon, err = strconv.Atoi(v) |
| 251 |
case "callingtns": |
| 252 |
rc.CallingTns, err = strconv.Atoi(v) |
| 253 |
case "dnid": |
| 254 |
rc.DNID = v |
| 255 |
case "rdnis": |
| 256 |
rc.RDNIS = v |
| 257 |
case "context": |
| 258 |
rc.Context = v |
| 259 |
case "extension": |
| 260 |
rc.Extension = v |
| 261 |
case "priority": |
| 262 |
rc.Priority, err = strconv.Atoi(v) |
| 263 |
case "enhanced": |
| 264 |
rc.Enhanced = v == "1.0" |
| 265 |
case "accountcode": |
| 266 |
rc.AccountCode = v |
| 267 |
case "threadid": |
| 268 |
rc.ThreadID, err = strconv.Atoi(v) |
| 269 |
} |
| 270 |
if err != nil { |
| 271 |
zap.S().With("err", err, "name", k, "value", v).Warn("failed to handle request variable") |
| 272 |
} |
| 273 |
} |
| 274 |
sort.Slice(args, func(i, j int) bool { |
| 275 |
return args[i].i < args[j].i |
| 276 |
}) |
| 277 |
for _, v := range args { |
| 278 |
rc.Args = append(rc.Args, v.v) |
| 279 |
} |
| 280 |
} |