-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
108 lines (82 loc) · 2.37 KB
/
server.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
package barnard
import (
"fmt"
"github.com/layeh/gumble/gumble"
"net/http"
"strings"
)
func (b *Barnard) InitApi() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/add-channel/", b.AddChannel)
mux.HandleFunc("/add-temp-channel/", b.AddTempChannel)
mux.HandleFunc("/join-channel/", b.JoinChannel)
mux.HandleFunc("/remove-channel/", b.RemoveChannel)
mux.HandleFunc("/user-list/", b.UserList)
return mux
}
func (b *Barnard) AddTempChannel(w http.ResponseWriter, req *http.Request) {
channelName := req.URL.Path[len("/add-temp-channel/"):]
if !b.Client.Self.IsRegistered() {
b.Client.Self.Register()
}
channel := b.Client.Self.Channel
channel.Add(channelName, true)
}
func (b *Barnard) AddChannel(w http.ResponseWriter, req *http.Request) {
channelName := req.URL.Path[len("/add-channel/"):]
if !b.Client.Self.IsRegistered() {
b.Client.Self.Register()
}
channel := b.Client.Self.Channel
channel.Add(channelName, false)
}
func (b *Barnard) JoinChannel(w http.ResponseWriter, req *http.Request) {
channelName := req.URL.Path[len("/join-channel/"):]
root := b.Client.Channels[0]
var targetChannel *gumble.Channel
if channelName == "Root" && channelName == "root" {
if root.IsRoot() {
b.Client.Self.Move(root)
} else {
fmt.Fprint(w, root.IsRoot())
}
} else {
channel := strings.Split(channelName, ",")
if len(channel) == 1 {
targetChannel = root.Find(channel[0])
} else if len(channel) == 2 {
targetChannel = root.Find(channel[0], channel[1])
}
b.Client.Self.Move(targetChannel)
fmt.Fprint(w, channel)
}
}
func (b *Barnard) RemoveChannel(w http.ResponseWriter, req *http.Request) {
// channelName := req.URL.Path[len("/remove-channel/"):]
channel := b.Client.Self.Channel
channel.Remove()
}
func (b *Barnard) UserList(w http.ResponseWriter, req *http.Request) {
channelName := req.URL.Path[len("/user-list/"):]
var list []string
var channel *gumble.Channel
root := b.Client.Channels[0]
if channelName == "Root" && channelName == "root" {
if root.IsRoot() {
channel = root
} else {
fmt.Fprint(w, root.IsRoot())
}
} else {
channels := strings.Split(channelName, ",")
if len(channels) == 1 {
channel = root.Find(channels[0])
} else if len(channels) == 2 {
channel = root.Find(channels[0], channels[1])
}
}
for _, user := range channel.Users {
list = append(list, user.Name)
}
fmt.Fprint(w, list)
}