multiserver/igutils.go

438 lines
11 KiB
Go
Raw Normal View History

2021-01-14 10:06:40 -08:00
package main
import (
2021-01-14 11:18:58 -08:00
"log"
2021-01-14 10:06:40 -08:00
"strings"
)
2021-01-27 04:07:51 -08:00
func privs(args ...string) map[string]bool {
m := make(map[string]bool)
for _, priv := range args {
m[priv] = true
2021-01-14 10:06:40 -08:00
}
2021-01-27 04:07:51 -08:00
return m
2021-01-14 10:06:40 -08:00
}
func init() {
2021-03-09 13:23:41 -08:00
disable, ok := ConfKey("disable_builtin").(bool)
2021-02-28 02:59:35 -08:00
if ok && disable {
return
}
2021-03-06 06:40:51 -08:00
RegisterChatCommand("help",
nil,
"Shows the help for a command. Shows the help for all commands if executed without arguments. Usage: help [command]",
func(p *Peer, param string) {
showHelp := func(name string) {
cmd := chatCommands[name]
if help := cmd.Help(); help != "" {
color := "#F00"
if has, err := p.CheckPrivs(cmd.privs); (err == nil && has) || cmd.privs == nil {
color = "#0F0"
}
p.SendChatMsg(Colorize(name, color) + ": " + help)
} else {
p.SendChatMsg("No help available for " + name + ".")
}
}
if param == "" {
for cmd := range chatCommands {
showHelp(cmd)
}
} else {
showHelp(param)
}
})
RegisterChatCommand("send",
privs("send"),
"Sends a player to a server. Usage: send <playername> <servername>",
2021-01-27 04:07:51 -08:00
func(p *Peer, param string) {
if param == "" {
2021-03-06 06:40:51 -08:00
p.SendChatMsg("Usage: send <playername> <servername>")
2021-01-27 04:07:51 -08:00
return
}
2021-01-27 04:07:51 -08:00
name := strings.Split(param, " ")[0]
if name == "" || len(strings.Split(param, " ")) < 2 {
2021-03-06 06:40:51 -08:00
p.SendChatMsg("Usage: send <playername> <servername>")
2021-01-27 04:07:51 -08:00
return
}
tosrv := strings.Split(param, " ")[1]
if tosrv == "" {
2021-03-06 06:40:51 -08:00
p.SendChatMsg("Usage: send <playername> <servername>")
2021-01-27 04:07:51 -08:00
return
}
2021-01-14 11:46:55 -08:00
2021-03-09 13:23:41 -08:00
servers := ConfKey("servers").(map[interface{}]interface{})
2021-01-27 04:07:51 -08:00
if servers[tosrv] == nil {
p.SendChatMsg("Unknown servername " + tosrv)
return
}
2021-01-14 11:34:42 -08:00
2021-03-09 09:06:05 -08:00
p2 := PeerByUsername(name)
2021-01-27 04:07:51 -08:00
if p2 == nil {
p.SendChatMsg(name + " is not online.")
return
}
2021-01-14 12:37:03 -08:00
2021-01-27 04:07:51 -08:00
srv := p2.ServerName()
if srv == tosrv {
p.SendChatMsg(name + " is already connected to this server!")
}
2021-01-14 12:37:03 -08:00
2021-01-27 04:07:51 -08:00
go p2.Redirect(tosrv)
})
2021-01-14 10:06:40 -08:00
2021-03-06 06:40:51 -08:00
RegisterChatCommand("sendcurrent",
privs("send"),
"Sends all players on the current server to a new server. Usage: sendcurrent <servername>",
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
2021-01-14 10:11:25 -08:00
if param == "" {
2021-03-06 06:40:51 -08:00
p.SendChatMsg("Usage: sendcurrent <servername>")
2021-01-14 10:11:25 -08:00
return
2021-01-14 10:06:40 -08:00
}
2021-03-09 13:23:41 -08:00
servers := ConfKey("servers").(map[interface{}]interface{})
2021-01-14 10:11:25 -08:00
if servers[param] == nil {
p.SendChatMsg("Unknown servername " + param)
return
2021-01-14 10:11:25 -08:00
}
2021-01-14 10:06:40 -08:00
2021-01-14 11:34:42 -08:00
srv := p.ServerName()
2021-01-14 10:11:25 -08:00
if srv == param {
p.SendChatMsg("All targets are already connected to this server!")
return
2021-01-14 10:06:40 -08:00
}
2021-01-30 03:41:26 -08:00
go func() {
2021-03-09 09:06:05 -08:00
for _, p := range Peers() {
if p.ServerName() == srv {
p.Redirect(param)
2021-01-30 03:41:26 -08:00
}
2021-01-14 10:11:25 -08:00
}
2021-01-30 03:41:26 -08:00
}()
2021-01-14 10:11:25 -08:00
})
2021-01-14 10:06:40 -08:00
2021-03-06 06:40:51 -08:00
RegisterChatCommand("sendall",
privs("send"),
"Sends all players to a server. Usage: sendall <servername>",
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
2021-01-14 10:11:25 -08:00
if param == "" {
2021-03-06 06:40:51 -08:00
p.SendChatMsg("Usage: sendall <servername>")
2021-01-14 10:11:25 -08:00
return
2021-01-14 10:06:40 -08:00
}
2021-03-09 13:23:41 -08:00
servers := ConfKey("servers").(map[interface{}]interface{})
2021-01-14 10:11:25 -08:00
if servers[param] == nil {
p.SendChatMsg("Unknown servername " + param)
return
2021-01-14 10:11:25 -08:00
}
2021-01-30 03:41:26 -08:00
go func() {
2021-03-09 09:06:05 -08:00
for _, p := range Peers() {
if psrv := p.ServerName(); psrv != param {
p.Redirect(param)
2021-01-30 03:41:26 -08:00
}
2021-01-14 10:11:25 -08:00
}
2021-01-30 03:41:26 -08:00
}()
2021-01-14 10:11:25 -08:00
})
2021-01-14 10:55:12 -08:00
2021-03-06 06:40:51 -08:00
RegisterChatCommand("alert",
privs("alert"),
"Sends a message to all players that are connected to the network. Usage: alert [message]",
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
ChatSendAll("[ALERT] " + param)
2021-01-14 10:55:12 -08:00
})
2021-01-14 11:18:58 -08:00
2021-03-06 06:40:51 -08:00
RegisterChatCommand("server",
nil,
`Prints your current server and a list of all servers if executed without arguments.
Sends you to a server if executed with arguments and the required privilege. Usage: server [servername]"`,
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
2021-01-14 11:18:58 -08:00
if param == "" {
2021-01-14 11:34:42 -08:00
var r string
2021-03-09 13:23:41 -08:00
servers := ConfKey("servers").(map[interface{}]interface{})
2021-01-14 11:18:58 -08:00
for server := range servers {
r += server.(string) + " "
}
2021-01-14 11:34:42 -08:00
srv := p.ServerName()
2021-01-14 11:18:58 -08:00
p.SendChatMsg("Current server: " + srv + " | All servers: " + r)
} else {
2021-03-09 13:23:41 -08:00
servers := ConfKey("servers").(map[interface{}]interface{})
2021-01-14 11:34:42 -08:00
srv := p.ServerName()
2021-01-14 11:18:58 -08:00
if srv == param {
p.SendChatMsg("You are already connected to this server!")
return
}
if servers[param] == nil {
p.SendChatMsg("Unknown servername " + param)
return
}
reqprivs := make(map[string]bool)
2021-03-09 13:23:41 -08:00
reqpriv, ok := ConfKey("servers:" + param + ":priv").(string)
2021-01-19 09:57:58 -08:00
if ok {
reqprivs[reqpriv] = true
2021-01-14 11:18:58 -08:00
}
allow, err := p.CheckPrivs(reqprivs)
if err != nil {
log.Print(err)
2021-01-14 12:37:03 -08:00
p.SendChatMsg("An internal error occured while attempting to check your privileges.")
2021-01-14 11:18:58 -08:00
return
}
if !allow {
2021-01-19 09:57:58 -08:00
p.SendChatMsg("You do not have permission to join this server! Required privilege: " + reqpriv)
2021-01-14 11:18:58 -08:00
return
}
go p.Redirect(param)
p.SendChatMsg("Redirecting you to " + param + ".")
}
})
2021-01-14 11:34:42 -08:00
2021-03-06 06:40:51 -08:00
RegisterChatCommand("find",
privs("find"),
"Prints the online status and the current server of a player. Usage: find <playername>",
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
2021-01-14 11:34:42 -08:00
if param == "" {
2021-03-06 06:40:51 -08:00
p.SendChatMsg("Usage: find <playername>")
2021-01-14 11:34:42 -08:00
return
}
2021-03-09 09:06:05 -08:00
p2 := PeerByUsername(param)
2021-01-14 11:34:42 -08:00
if p2 == nil {
p.SendChatMsg(param + " is not online.")
} else {
srv := p2.ServerName()
p.SendChatMsg(param + " is connected to server " + srv + ".")
}
})
2021-03-06 06:40:51 -08:00
RegisterChatCommand("addr",
privs("addr"),
"Prints the network address (including the port) of a connected player. Usage: addr <playername>",
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
if param == "" {
2021-03-06 06:40:51 -08:00
p.SendChatMsg("Usage: addr <playername>")
return
}
2021-03-09 09:06:05 -08:00
p2 := PeerByUsername(param)
if p2 == nil {
p.SendChatMsg(param + " is not online.")
} else {
p.SendChatMsg(param + "'s address is " + p2.Addr().String())
}
})
2021-01-14 11:46:55 -08:00
2021-03-06 06:40:51 -08:00
RegisterChatCommand("end",
privs("end"),
"Kicks all connected clients and stops the proxy. Usage: end",
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
go End(false, false)
2021-01-14 11:46:55 -08:00
})
2021-01-14 12:37:03 -08:00
2021-03-06 06:40:51 -08:00
RegisterChatCommand("privs",
nil,
`Prints your privileges if executed without arguments.
Prints a connected player's privileges if executed with arguments. Usage: privs [playername]`,
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
2021-01-14 12:37:03 -08:00
var r string
name := param
2021-01-24 05:00:26 -08:00
var p2 *Peer
2021-01-14 12:37:03 -08:00
if name == "" {
p2 = p
r += "Your privileges: "
} else {
2021-03-09 09:06:05 -08:00
p2 = PeerByUsername(name)
2021-01-14 12:37:03 -08:00
r += name + "'s privileges: "
}
2021-01-24 05:00:26 -08:00
if name != "" && !IsOnline(name) {
2021-01-14 12:37:03 -08:00
p.SendChatMsg(name + " is not online.")
return
}
2021-03-09 13:23:41 -08:00
privs, err := p2.Privs()
2021-01-14 12:37:03 -08:00
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to get the privileges.")
return
}
var privnames []string
for k, v := range privs {
if v {
privnames = append(privnames, k)
}
}
p.SendChatMsg(r + strings.Join(privnames, " "))
})
2021-03-06 06:40:51 -08:00
RegisterChatCommand("grant",
privs("privs"),
`Grants privileges to a connected player. The privileges need to be comma-seperated.
If the playername is omitted, privileges are granted to you. Usage: grant [playername] <privileges>`,
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
2021-01-14 12:37:03 -08:00
name := strings.Split(param, " ")[0]
var privnames string
2021-01-24 05:00:26 -08:00
var p2 *Peer
2021-01-14 12:37:03 -08:00
if len(strings.Split(param, " ")) < 2 {
p2 = p
privnames = name
} else {
2021-03-09 09:06:05 -08:00
p2 = PeerByUsername(name)
2021-01-14 12:37:03 -08:00
privnames = strings.Split(param, " ")[1]
}
2021-01-24 05:00:26 -08:00
if len(strings.Split(param, " ")) >= 2 && !IsOnline(name) {
2021-01-14 12:37:03 -08:00
p.SendChatMsg(name + " is not online.")
return
}
2021-03-09 13:23:41 -08:00
privs, err := p2.Privs()
2021-01-14 12:37:03 -08:00
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to get the privileges.")
return
}
splitprivs := strings.Split(strings.Replace(privnames, " ", "", -1), ",")
for i := range splitprivs {
privs[splitprivs[i]] = true
}
err = p2.SetPrivs(privs)
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to get the privileges.")
return
}
p.SendChatMsg("Privileges updated.")
})
2021-03-06 06:40:51 -08:00
RegisterChatCommand("revoke",
privs("privs"),
`Revokes privileges from a connected player. The privileges need to be comma-seperated.
If the playername is omitted, privileges are revoked from you. Usage: revoke [playername] <privileges>`,
2021-01-24 05:00:26 -08:00
func(p *Peer, param string) {
2021-01-14 12:37:03 -08:00
name := strings.Split(param, " ")[0]
var privnames string
2021-01-24 05:00:26 -08:00
var p2 *Peer
2021-01-14 12:37:03 -08:00
if len(strings.Split(param, " ")) < 2 {
p2 = p
privnames = name
} else {
2021-03-09 09:06:05 -08:00
p2 = PeerByUsername(name)
2021-01-14 12:37:03 -08:00
privnames = strings.Split(param, " ")[1]
}
2021-01-24 05:00:26 -08:00
if len(strings.Split(param, " ")) >= 2 && !IsOnline(name) {
2021-01-14 12:37:03 -08:00
p.SendChatMsg(name + " is not online.")
return
}
2021-03-09 13:23:41 -08:00
privs, err := p2.Privs()
2021-01-14 12:37:03 -08:00
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to get the privileges.")
return
}
splitprivs := strings.Split(strings.Replace(privnames, " ", "", -1), ",")
for i := range splitprivs {
privs[splitprivs[i]] = false
}
err = p2.SetPrivs(privs)
if err != nil {
log.Print(err)
p.SendChatMsg("An internal error occured while attempting to set the privileges.")
return
}
p.SendChatMsg("Privileges updated.")
})
2021-01-14 12:56:01 -08:00
2021-03-06 06:57:39 -08:00
RegisterChatCommand("banlist",
privs("ban"),
"Prints the list of banned IP address and associated players. Usage: banlist",
func(p *Peer, param string) {
bans, err := BanList()
if err != nil {
p.SendChatMsg("An internal error occured while attempting to read the ban list.")
return
}
msg := "Address | Name\n"
for addr, name := range bans {
msg += addr + " | " + name + "\n"
}
p.SendChatMsg(msg)
})
2021-03-06 06:40:51 -08:00
RegisterChatCommand("ban",
privs("ban"),
"Bans an IP address or a connected player. Usage: ban <playername | IP address>",
2021-02-28 04:33:30 -08:00
func(p *Peer, param string) {
2021-02-28 04:12:28 -08:00
if param == "" {
2021-03-06 06:40:51 -08:00
p.SendChatMsg("Usage: ban <playername | IP address>")
2021-02-28 04:12:28 -08:00
return
}
2021-02-28 04:33:30 -08:00
err := Ban(param)
if err != nil {
2021-03-09 09:06:05 -08:00
p2 := PeerByUsername(param)
2021-02-28 04:33:30 -08:00
if p2 == nil {
p.SendChatMsg(param + " is not online.")
return
}
2021-02-28 04:12:28 -08:00
2021-02-28 04:33:30 -08:00
if err := p2.Ban(); err != nil {
p.SendChatMsg("An internal error occured while attempting to ban the player.")
return
}
2021-02-28 04:12:28 -08:00
}
p.SendChatMsg("Banned " + param)
})
2021-03-06 06:40:51 -08:00
RegisterChatCommand("unban",
privs("ban"),
"Unbans an IP address or a playername. Usage: unban <playername | IP address>",
2021-02-28 04:33:30 -08:00
func(p *Peer, param string) {
2021-02-28 04:12:28 -08:00
if param == "" {
2021-03-06 06:40:51 -08:00
p.SendChatMsg("Usage: unban <playername | IP address>")
2021-02-28 04:12:28 -08:00
return
}
if err := Unban(param); err != nil {
p.SendChatMsg("An internal error occured while attempting to unban the player.")
return
}
p.SendChatMsg("Unbanned " + param)
})
2021-01-24 05:00:26 -08:00
RegisterOnRedirectDone(func(p *Peer, newsrv string, success bool) {
2021-01-14 12:56:01 -08:00
if success {
2021-01-24 05:00:26 -08:00
err := SetStorageKey("server:"+p.Username(), newsrv)
2021-01-14 12:56:01 -08:00
if err != nil {
log.Print(err)
return
}
} else {
p.SendChatMsg("Could not connect you to " + newsrv + "!")
}
})
2021-02-28 02:59:35 -08:00
log.Print("Loaded builtin")
2021-01-14 10:06:40 -08:00
}