Check admin or operator privileges

This commit is contained in:
Andreas Neue 2016-08-07 19:41:07 +02:00
parent 5f47deeb94
commit bae871a524
2 changed files with 34 additions and 18 deletions

View File

@ -88,20 +88,17 @@ func handleCmdJoin(sv *Server, msg *irc.Message) {
sv.sendMsg(msg)
sv.sendReply(msg.Pre, RPL_TOPIC, msg.Args[0], sv.chTopics[msg.Args[0]])
sv.channelNames(msg.Pre, msg.Args[0])
_, isadm := sv.admins[clid]
if isadm {
m, isoper := sv.opers[clid]
if isoper {
mode := "o " + clid
sv.chModes[chid][mode] = true
sv.sendMsg(irc.M(sv.host, "MODE", chid+" +o "+clid, ""))
sv.sendMsg(irc.M(sv.host, "PRIVMSG", chid, clid+" is a server administrator"))
return
}
_, isop := sv.opers[clid]
if isop {
mode := "o " + clid
sv.chModes[chid][mode] = true
sv.sendMsg(irc.M(sv.host, "MODE", chid+" +o "+clid, ""))
sv.sendMsg(irc.M(sv.host, "PRIVMSG", chid, clid+" is a server operator"))
if m == "a" {
sv.sendMsg(irc.M(sv.host, "PRIVMSG", chid, clid+" is a server administrator"))
}
if m == "o" {
sv.sendMsg(irc.M(sv.host, "PRIVMSG", chid, clid+" is a server operator"))
}
return
}
if first {
@ -258,7 +255,7 @@ func handleCmdPing(sv *Server, msg *irc.Message) {
func handleCmdRehash(sv *Server, msg *irc.Message) {
sv.loadConfig()
sv.sendReply(msg.Pre, RPL_REHASHING, "", "Rehashing.")
sv.sendReply(msg.Pre, RPL_REHASHING, "", "Rehashing")
xlog.Info("Rehashing")
}

View File

@ -40,8 +40,7 @@ type Server struct {
created string
motd string
admins map[string]bool
opers map[string]bool
opers map[string]string
clients map[string]Client
clModes map[string]string
@ -70,8 +69,7 @@ func NewServer(configPath, software, version string) *Server {
sv.addq = make(chan Client, 128)
sv.delq = make(chan Client, 128)
sv.admins = make(map[string]bool)
sv.opers = make(map[string]bool)
sv.opers = make(map[string]string)
sv.clients = make(map[string]Client)
sv.clModes = make(map[string]string)
@ -92,11 +90,11 @@ func NewServer(configPath, software, version string) *Server {
nicks, _ := sv.config.GetString("control", "a")
for _, nick := range strings.Split(nicks, ",") {
sv.admins[nick] = true
sv.opers[nick] = "a"
}
nicks, _ = sv.config.GetString("control", "o")
for _, nick := range strings.Split(nicks, ",") {
sv.opers[nick] = true
sv.opers[nick] = "o"
}
sv.packetsTransferred = 0
@ -268,11 +266,24 @@ func (sv *Server) delClient(cl Client) {
func (sv *Server) recvMsg(msg *irc.Message) {
cmd := msg.Cmd
clid := strings.ToLower(msg.Pre)
hook, exists := svCommandHooks[cmd]
if !exists {
sv.sendReply(msg.Pre, ERR_UNKNOWNCOMMAND, cmd, "Unknown command")
return
}
nm := hook.NeedMode
if nm != "" {
if m, isoper := sv.opers[clid]; !isoper {
sv.sendReply(msg.Pre, ERR_NOPRIVILEGES, cmd, "Permission denied: You're not a server operator")
return
} else {
if strings.Index(nm, m) == -1 {
sv.sendReply(msg.Pre, ERR_NOPRIVILEGES, cmd, "Permission denied: You're not a server admin")
return
}
}
}
argc := len(msg.Args)
if argc < hook.MinArgs {
sv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters")
@ -342,6 +353,14 @@ func (sv *Server) sendLogon(nick string) {
sv.sendReply(nick, RPL_MOTD, "", fmt.Sprintf("- %s", line))
}
sv.sendReply(nick, RPL_ENDOFMOTD, "", "End of MOTD command")
if m, isoper := sv.opers[strings.ToLower(nick)]; isoper {
switch m {
case "a":
sv.sendReply(nick, RPL_YOUREOPER, "", "You are now a server admin")
case "o":
sv.sendReply(nick, RPL_YOUREOPER, "", "You are now a server operator")
}
}
}
// Send channel name list to client