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
|
package main
import (
"fmt"
"os"
"strings"
"github.com/HimbeerserverDE/mt-multiserver-proxy"
)
func init() {
proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "shutdown",
Perm: "cmd_shutdown",
Handler: func(cc *proxy.ClientConn, args ...string) string {
proc, err := os.FindProcess(os.Getpid())
if err != nil {
return "Could not find process: " + err.Error()
}
proc.Signal(os.Interrupt)
return ""
},
})
proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "find",
Perm: "cmd_find",
Handler: func(cc *proxy.ClientConn, args ...string) string {
if len(args) != 1 {
return "Usage: find <name>"
}
clt := proxy.Find(args[0])
if clt != nil {
return fmt.Sprintf("%s is connected to %s", clt.Name(), clt.ServerName())
}
return "Player not connected."
},
})
proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "addr",
Perm: "cmd_addr",
Handler: func(cc *proxy.ClientConn, args ...string) string {
if len(args) != 1 {
return "Usage: addr <name>"
}
clt := proxy.Find(args[0])
if clt != nil {
return fmt.Sprintf("%s is at %s", clt.Name(), clt.RemoteAddr())
}
return "Player not connected."
},
})
proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "alert",
Perm: "cmd_alert",
Handler: func(cc *proxy.ClientConn, args ...string) string {
if len(args) <= 0 {
return "Usage: alert <message>"
}
msg := strings.Join(args, " ")
for clt := range proxy.Clts() {
clt.SendChatMsg(msg)
}
return ""
},
})
proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "send",
Perm: "cmd_send",
Handler: func(cc *proxy.ClientConn, args ...string) string {
if len(args) != 2 {
return "Usage: send <player <server> <name> | current <server> | all <server>>"
}
var found bool
for _, srv := range proxy.Conf().Servers {
if srv.Name == args[1] {
found = true
break
}
}
if !found {
return "Server not existent."
}
switch args[0] {
case "player":
if len(args) != 3 {
return "Usage: send <player <server> <name> | current <server> | all <server>>"
}
if args[1] == cc.ServerName() {
return "Player is already connected to this server."
}
clt := proxy.Find(args[2])
if clt == nil {
return "Player not connected."
}
if err := clt.Hop(args[1]); err != nil {
clt.SendChatMsg("Could not switch servers. Reconnect if you encounter any problems. Error:", err.Error())
}
case "current":
if args[1] == cc.ServerName() {
return "Start and destination are identical."
}
for clt := range proxy.Clts() {
if clt.ServerName() == cc.ServerName() && clt.ServerName() != args[1] {
if err := clt.Hop(args[1]); err != nil {
clt.SendChatMsg("Could not switch servers. Reconnect if you encounter any problems. Error:", err.Error())
}
}
}
case "all":
for clt := range proxy.Clts() {
if clt.ServerName() != args[1] {
if err := clt.Hop(args[1]); err != nil {
clt.SendChatMsg("Could not switch servers. Reconnect if you encounter any problems. Error:", err.Error())
}
}
}
default:
return "Usage: send <player | current | all> <server> [name]"
}
return ""
},
})
proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "players",
Perm: "cmd_players",
Handler: func(cc *proxy.ClientConn, args ...string) string {
srvs := make(map[string][]*proxy.ClientConn)
for clt := range proxy.Clts() {
srv := clt.ServerName()
srvs[srv] = append(srvs[srv], clt)
}
b := &strings.Builder{}
for srv, clts := range srvs {
b.WriteString(srv + ":\n")
for _, clt := range clts {
b.WriteString("- " + clt.Name() + "\n")
}
}
return strings.Trim(b.String(), "\n")
},
})
proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "reload",
Perm: "cmd_reload",
Handler: func(cc *proxy.ClientConn, args ...string) string {
if err := proxy.LoadConfig(); err != nil {
return "Configuration could not be reloaded. Old config is still active. Error: " + err.Error()
}
return "Configuration reloaded. You should restart if you encounter any problems."
},
})
proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "perms",
Perm: "cmd_perms",
Handler: func(cc *proxy.ClientConn, args ...string) string {
return "Your permissions: " + strings.Join(cc.Perms(), ", ")
},
})
proxy.RegisterChatCmd(proxy.ChatCmd{
Name: "server",
Perm: "cmd_server",
Handler: func(cc *proxy.ClientConn, args ...string) string {
if len(args) == 0 {
srvs := make([]string, len(proxy.Conf().Servers))
for i, srv := range proxy.Conf().Servers {
srvs[i] = srv.Name
}
return fmt.Sprintf("Connected to: %s | Servers: %s", cc.ServerName(), strings.Join(srvs, " "))
}
srv := strings.Join(args, " ")
if cc.ServerName() == srv {
return "Already connected to this server."
}
if err := cc.Hop(srv); err != nil {
return "Could not switch servers. Reconnect if you encounter any problems. Error: " + err.Error()
}
return ""
},
})
}
|