Lua -> Go: Priv command family
parent
f1a060d3fe
commit
2948bb0f64
|
@ -68,6 +68,12 @@ func init() {
|
|||
privs["end"] = make(map[string]bool)
|
||||
privs["end"]["end"] = true
|
||||
|
||||
privs["grant"] = make(map[string]bool)
|
||||
privs["grant"]["privs"] = true
|
||||
|
||||
privs["revoke"] = make(map[string]bool)
|
||||
privs["revoke"]["privs"] = true
|
||||
|
||||
multiserver.RegisterChatCommand("send", privs["send"], cmdSend)
|
||||
|
||||
multiserver.RegisterChatCommand("sendcurrent", privs["sendcurrent"],
|
||||
|
@ -167,7 +173,7 @@ func init() {
|
|||
allow, err := p.CheckPrivs(reqprivs)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
p.SendChatMsg("An internal error occured while trying to check your privileges.")
|
||||
p.SendChatMsg("An internal error occured while attempting to check your privileges.")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -216,4 +222,116 @@ func init() {
|
|||
func(p *multiserver.Peer, param string) {
|
||||
go multiserver.End(false, false)
|
||||
})
|
||||
|
||||
multiserver.RegisterChatCommand("privs", nil,
|
||||
func(p *multiserver.Peer, param string) {
|
||||
var r string
|
||||
|
||||
name := param
|
||||
var p2 *multiserver.Peer
|
||||
if name == "" {
|
||||
p2 = p
|
||||
r += "Your privileges: "
|
||||
} else {
|
||||
p2 = multiserver.GetListener().GetPeerByName(name)
|
||||
r += name + "'s privileges: "
|
||||
}
|
||||
|
||||
if name != "" && !multiserver.IsOnline(name) {
|
||||
p.SendChatMsg(name + " is not online.")
|
||||
return
|
||||
}
|
||||
|
||||
privs, err := p2.GetPrivs()
|
||||
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, " "))
|
||||
})
|
||||
|
||||
multiserver.RegisterChatCommand("grant", privs["grant"],
|
||||
func(p *multiserver.Peer, param string) {
|
||||
name := strings.Split(param, " ")[0]
|
||||
var privnames string
|
||||
var p2 *multiserver.Peer
|
||||
if len(strings.Split(param, " ")) < 2 {
|
||||
p2 = p
|
||||
privnames = name
|
||||
} else {
|
||||
p2 = multiserver.GetListener().GetPeerByName(name)
|
||||
privnames = strings.Split(param, " ")[1]
|
||||
}
|
||||
|
||||
if len(strings.Split(param, " ")) >= 2 && !multiserver.IsOnline(name) {
|
||||
p.SendChatMsg(name + " is not online.")
|
||||
return
|
||||
}
|
||||
|
||||
privs, err := p2.GetPrivs()
|
||||
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.")
|
||||
})
|
||||
|
||||
multiserver.RegisterChatCommand("revoke", privs["revoke"],
|
||||
func(p *multiserver.Peer, param string) {
|
||||
name := strings.Split(param, " ")[0]
|
||||
var privnames string
|
||||
var p2 *multiserver.Peer
|
||||
if len(strings.Split(param, " ")) < 2 {
|
||||
p2 = p
|
||||
privnames = name
|
||||
} else {
|
||||
p2 = multiserver.GetListener().GetPeerByName(name)
|
||||
privnames = strings.Split(param, " ")[1]
|
||||
}
|
||||
|
||||
if len(strings.Split(param, " ")) >= 2 && !multiserver.IsOnline(name) {
|
||||
p.SendChatMsg(name + " is not online.")
|
||||
return
|
||||
}
|
||||
|
||||
privs, err := p2.GetPrivs()
|
||||
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.")
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,71 +1,3 @@
|
|||
multiserver.register_chatcommand("p_privs", {
|
||||
privs = {},
|
||||
func = function(id, param)
|
||||
local name = param
|
||||
if not name or name == "" then
|
||||
name = multiserver.get_player_name(id)
|
||||
end
|
||||
|
||||
local privs = multiserver.get_player_privs(name)
|
||||
local privnames = {}
|
||||
for priv, v in pairs(privs) do
|
||||
table.insert(privnames, priv)
|
||||
end
|
||||
|
||||
return name .. "'s privileges: " .. table.concat(privnames, " ")
|
||||
end,
|
||||
})
|
||||
|
||||
multiserver.register_chatcommand("p_grant", {
|
||||
privs = {privs = true},
|
||||
func = function(id, param)
|
||||
local name = multiserver.split(param, " ")[1]
|
||||
local privnames = multiserver.split(param, " ")[2]
|
||||
|
||||
if not privnames or privnames == "" then
|
||||
privnames = name
|
||||
name = multiserver.get_player_name(id)
|
||||
end
|
||||
|
||||
if not multiserver.get_peer_id(name) then
|
||||
return name .. " is not online."
|
||||
end
|
||||
|
||||
local privs = multiserver.get_player_privs(name)
|
||||
for _, newpriv in ipairs(multiserver.split(privnames:gsub(" ", ""), ",")) do
|
||||
privs[newpriv] = true
|
||||
end
|
||||
multiserver.set_player_privs(name, privs)
|
||||
|
||||
return "Privileges updated."
|
||||
end,
|
||||
})
|
||||
|
||||
multiserver.register_chatcommand("p_revoke", {
|
||||
privs = {privs = true},
|
||||
func = function(id, param)
|
||||
local name = multiserver.split(param, " ")[1]
|
||||
local privnames = multiserver.split(param, " ")[2]
|
||||
|
||||
if not privnames or privnames == "" then
|
||||
privnames = name
|
||||
name = multiserver.get_player_name(id)
|
||||
end
|
||||
|
||||
if not multiserver.get_peer_id(name) then
|
||||
return name .. " is not online."
|
||||
end
|
||||
|
||||
local privs = multiserver.get_player_privs(name)
|
||||
for _, rmpriv in ipairs(multiserver.split(privnames:gsub(" ", ""), ",")) do
|
||||
privs[rmpriv] = nil
|
||||
end
|
||||
multiserver.set_player_privs(name, privs)
|
||||
|
||||
return "Privileges updated."
|
||||
end,
|
||||
})
|
||||
|
||||
multiserver.register_on_redirect_done(function(id, newsrv, success)
|
||||
if not success then
|
||||
multiserver.chat_send_player(id, "Could not connect you to " .. newsrv .. "!")
|
||||
|
|
48
privs.go
48
privs.go
|
@ -115,30 +115,6 @@ func readPrivItem(db *sql.DB, name string) (string, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
admin := GetConfKey("admin")
|
||||
if admin != nil || fmt.Sprintf("%T", admin) == "string" {
|
||||
db, err := initAuthDB()
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
eprivs, err := readPrivItem(db, admin.(string))
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
privs := decodePrivs(eprivs)
|
||||
privs["privs"] = true
|
||||
|
||||
newprivs := encodePrivs(privs)
|
||||
|
||||
modPrivItem(db, admin.(string), newprivs)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Peer) GetPrivs() (map[string]bool, error) {
|
||||
db, err := initAuthDB()
|
||||
if err != nil {
|
||||
|
@ -185,3 +161,27 @@ func (p *Peer) CheckPrivs(req map[string]bool) (bool, error) {
|
|||
|
||||
return allow, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
admin := GetConfKey("admin")
|
||||
if admin != nil || fmt.Sprintf("%T", admin) == "string" {
|
||||
db, err := initAuthDB()
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
eprivs, err := readPrivItem(db, admin.(string))
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return
|
||||
}
|
||||
|
||||
privs := decodePrivs(eprivs)
|
||||
privs["privs"] = true
|
||||
|
||||
newprivs := encodePrivs(privs)
|
||||
|
||||
modPrivItem(db, admin.(string), newprivs)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue