forked from bmmcginty/barnard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
200 lines (179 loc) · 4.69 KB
/
main.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
package main
import _ "net/http/pprof"
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"strings"
"syscall"
//"gopkg.in/alessio/shellescape.v1"
//"github.com/google/shlex"
"crypto/tls"
"flag"
"github.com/tynanbe/barnard/config"
"github.com/alessio/shellescape"
"github.com/tynanbe/barnard/gumble/gumble"
_ "github.com/tynanbe/barnard/gumble/opus"
"github.com/tynanbe/barnard/uiterm"
"github.com/Windblade-GR01/go-openal/openal"
)
func showDevs(name string, args []string) {
if args == nil {
fmt.Printf("no items for %s\n", name)
}
fmt.Printf("%s\n", name)
for i := 0; i < len(args); i++ {
fmt.Printf("%s\n", args[i])
}
}
func doListDevices() {
odevs := openal.GetStrings(openal.AllDevicesSpecifier)
if odevs != nil && len(odevs) > 0 {
showDevs("All outputs:", odevs)
} else {
odevs = openal.GetStrings(openal.DeviceSpecifier)
showDevs("All outputs:", odevs)
}
idevs := openal.GetStrings(openal.CaptureDeviceSpecifier)
showDevs("Inputs:", idevs)
}
func setupNotifyRunner(notifyCommand string) chan []string {
t := make(chan []string)
var doNothing = false
if notifyCommand == "" {
doNothing = true
}
go func(events chan []string, cmdTemplate string, dummy bool) {
for {
event := <-events
if !dummy {
t := cmdTemplate
t = strings.ReplaceAll(t, "%event", shellescape.Quote(event[0]))
t = strings.ReplaceAll(t, "%who", shellescape.Quote(event[1]))
t = strings.ReplaceAll(t, "%what", shellescape.Quote(event[2]))
cmd := "/bin/sh"
args := []string{"-c", t}
x := exec.Command(cmd, args...)
x.Run()
} //if we actually have a command to run
} //for
}(t, notifyCommand, doNothing)
return t
}
func setupFifo(fn string) (chan string, error) {
t := make(chan string)
if fn == "" {
return t, nil
}
os.Remove(fn)
err := syscall.Mkfifo(fn, 0600)
if err != nil {
return t, err
}
file, err := os.OpenFile(fn, os.O_RDWR, os.ModeNamedPipe)
if err != nil {
return t, err
}
go func(fh io.Reader, out chan string) {
reader := bufio.NewReader(fh)
for {
line, err := reader.ReadBytes('\n')
if err == nil {
out <- strings.TrimSpace(string(line))
}
}
}(file, t)
return t, nil
}
func main() {
// Command line flags
server := flag.String("server", "localhost:64738", "the server to connect to")
username := flag.String("username", "", "the username of the client")
password := flag.String("password", "", "the password of the server")
insecure := flag.Bool("insecure", false, "skip server certificate verification")
certificate := flag.String("certificate", "", "PEM encoded certificate and private key")
cfgfn := flag.String("config", "~/.barnard.yaml", "Path to YAML formatted configuration file")
listDevices := flag.Bool("list_devices", false, "do not connect; instead, list available audio devices and exit")
fifo := flag.String("fifo", "", "path of a FIFO from which to read commands")
serverSet := false
usernameSet := false
buffers := flag.Int("buffers", 16, "number of audio buffers to use")
profile := flag.Bool("profile", false, "add http server to serve profiles")
flag.Parse()
if *profile == true {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
userConfig := config.NewConfig(cfgfn)
flag.CommandLine.Visit(func(theFlag *flag.Flag) {
switch theFlag.Name {
case "server":
serverSet = true
case "username":
usernameSet = true
}
})
if !serverSet {
server = userConfig.GetDefaultServer()
}
if !usernameSet {
username = userConfig.GetUsername()
}
if os.Getenv("ALSOFT_LOGLEVEL") == "" {
os.Setenv("ALSOFT_LOGLEVEL", "0")
}
if *listDevices {
doListDevices()
os.Exit(0)
}
if !strings.Contains(*server, ":") {
*server = *server + ":64738"
}
// Initialize
b := Barnard{
Config: gumble.NewConfig(),
UserConfig: userConfig,
Address: *server,
}
b.Config.Buffers = *buffers
b.Hotkeys = b.UserConfig.GetHotkeys()
b.UserConfig.SaveConfig()
b.Config.Username = *username
b.Config.Password = *password
if *insecure {
b.TLSConfig.InsecureSkipVerify = true
}
if *certificate != "" {
cert, err := tls.LoadX509KeyPair(*certificate, *certificate)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
b.TLSConfig.Certificates = append(b.TLSConfig.Certificates, cert)
}
reader, err := setupFifo(*fifo)
if err != nil {
b.exitMessage = err.Error()
b.exitStatus = 1
handleError(b)
}
b.notifyChannel = setupNotifyRunner(*b.UserConfig.GetNotifyCommand())
b.Ui = uiterm.New(&b)
b.Ui.Run(reader)
handleError(b)
}
func handleRawError(e error) {
fmt.Fprintf(os.Stderr, "%s\n", e.Error())
os.Exit(1)
}
func handleError(b Barnard) {
if b.exitMessage != "" {
fmt.Fprintf(os.Stderr, "%s\n", b.exitMessage)
}
os.Exit(b.exitStatus)
}