forked from gbazil/sipd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sipconf.go
137 lines (117 loc) · 3.55 KB
/
sipconf.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
// Copyright (c) 2018 Vasily Suvorov, http://bazil.pro <[email protected]>
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//
package main
import (
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
)
func str2arr(str string) []int {
var a []int
for _, s := range strings.Split(str, ",") {
i, err := strconv.Atoi(s)
if err != nil {
sa := strings.Split(s, "-")
if len(sa) == 2 {
x, err := strconv.Atoi(sa[0])
if err != nil {
continue
}
y, err := strconv.Atoi(sa[1])
if err != nil {
continue
}
for z := x; z <= y; z++ {
a = append(a, z)
}
}
} else {
a = append(a, i)
}
}
return a
}
func Conf2DB() {
buf, err := ioutil.ReadFile("sip.conf")
if err != nil {
return
}
sqltext := "BEGIN;\nDELETE FROM register;\n"
for _, bulk := range strings.Split(string(buf), "\n[") {
name := strings.TrimSuffix(regexp.MustCompile(`^\S+]`).FindString(bulk), "]")
if name != "" {
if name == "general" {
continue
}
exten := strings.TrimPrefix(regexp.MustCompile(`(?m)^exten=\S*`).FindString(bulk), "exten=")
acode := strings.TrimPrefix(regexp.MustCompile(`(?m)^accountcode=\S*`).FindString(bulk), "accountcode=")
secret := strings.TrimPrefix(regexp.MustCompile(`(?m)^secret=\S*`).FindString(bulk), "secret=")
acl := strings.TrimPrefix(regexp.MustCompile(`(?m)^acl=\S*`).FindString(bulk), "acl=")
addr := strings.TrimPrefix(regexp.MustCompile(`(?m)^host=[0-9.:]*`).FindString(bulk), "host=")
if addr == "" {
// live user registered addr
db.QueryRow("SELECT addr FROM register WHERE name = $1 AND secret = $2", name, secret).Scan(&addr)
} else {
if !strings.Contains(addr, ":") {
addr += ":5060"
}
}
callerid := strings.TrimPrefix(regexp.MustCompile(`(?m)^callerid=.*$`).FindString(bulk), "callerid=")
if d10 := regexp.MustCompile(`\d{10}`).FindString(callerid); d10 != "" {
callerid = d10
}
dialplan := strings.TrimPrefix(regexp.MustCompile(`(?m)^dialplan=\S*`).FindString(bulk), "dialplan=")
if dialplan == "" {
switch strings.TrimPrefix(regexp.MustCompile(`(?m)^context=\S*`).FindString(bulk), "context=") {
case "domestic":
dialplan = `^.{2,10}$|^\*.{2,12}$` // 10 R CCC 1234567 = 13
case "mobile":
dialplan = `^9.|^49[59]|^800|^.{2,3}$`
case "local":
dialplan = `^49[59]|^800|^.{2,3}$`
case "trush", "em":
dialplan = `^.{2,3}$`
}
}
sqltext += fmt.Sprintf("INSERT INTO register VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');\n",
name, secret, addr, acode, exten, callerid, dialplan, acl)
}
}
var final string
if _, err := db.Exec(sqltext); err != nil {
final = "ROLLBACK;\n"
} else {
final = "COMMIT\n"
}
db.Exec(final)
if *debug {
fmt.Println(sqltext + final)
}
// compose indirect call forward chains (serial call)
sqltext = "DELETE FROM chain;\n"
rows, _ := db.Query("SELECT name, acode, exten FROM register ORDER BY acode, name")
for rows.Next() {
var name, acode, exten string
if err := rows.Scan(&name, &acode, &exten); err != nil {
continue
}
if serial := regexp.MustCompile(`\([^)]+`).FindString(exten); serial != "" {
for _, i := range str2arr(strings.TrimPrefix(serial, "(")) {
key := name
err := db.QueryRow("SELECT name FROM register WHERE acode = $1 ORDER BY acode, name LIMIT 1 OFFSET $2 - 1", acode, i).Scan(&name)
if err == nil {
sqltext += fmt.Sprintf("REPLACE INTO chain VALUES ('%s', '%s');\n", key, name)
}
}
}
}
db.Exec(sqltext)
if *debug {
fmt.Println(sqltext)
}
}