mirror of https://github.com/fluffle/goirc
!remove
This commit is contained in:
parent
a44a71677b
commit
f8acc8ffea
56
auth.go
56
auth.go
|
@ -38,15 +38,56 @@ func addAccess(conn *irc.Conn, channel, nick, flags string) (string, string) {
|
|||
}
|
||||
|
||||
auth.AddOption(section, n.Host, nflags)
|
||||
if err := auth.WriteFile(authFile, 0644, ""); err != nil {
|
||||
if updateAuth() != nil {
|
||||
say(conn, channel, "Error while writing to %s", authFile)
|
||||
}
|
||||
// config.WriteFile destroys the config, so
|
||||
readAuth()
|
||||
|
||||
return n.Host, nflags
|
||||
}
|
||||
|
||||
func removeAccess(conn *irc.Conn, channel, nick, flags string) (string, string) {
|
||||
n := conn.GetNick(nick)
|
||||
if n == nil {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
section := conn.Network + " " + channel
|
||||
cflags, _ := auth.String(section, n.Host)
|
||||
|
||||
nflags := ""
|
||||
for _, flag := range cflags {
|
||||
if strings.IndexRune(flags, flag) < 0 {
|
||||
// we're not removing this flag
|
||||
nflags += string(flag)
|
||||
}
|
||||
}
|
||||
|
||||
auth.AddOption(section, n.Host, nflags)
|
||||
if updateAuth() != nil {
|
||||
say(conn, channel, "Error while writing to %s", authFile)
|
||||
}
|
||||
|
||||
return n.Host, nflags
|
||||
}
|
||||
|
||||
func removeUser(conn *irc.Conn, channel, nick string) (string, bool) {
|
||||
n := conn.GetNick(nick)
|
||||
if n == nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
section := conn.Network + " " + channel
|
||||
|
||||
if !auth.RemoveOption(section, n.Host) {
|
||||
return n.Host, false
|
||||
}
|
||||
if updateAuth() != nil {
|
||||
say(conn, channel, "Error while writing to %s", authFile)
|
||||
}
|
||||
|
||||
return n.Host, true
|
||||
}
|
||||
|
||||
// passing a flag of "" will check if the user has any access
|
||||
func hasAccess(conn *irc.Conn, channel, nick, flag string) bool {
|
||||
n := conn.GetNick(nick)
|
||||
|
@ -67,3 +108,12 @@ func hasAccess(conn *irc.Conn, channel, nick, flag string) bool {
|
|||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func updateAuth() os.Error {
|
||||
if err := auth.WriteFile(authFile, 0644, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
// config.WriteFile destroys the config, so
|
||||
readAuth()
|
||||
return nil
|
||||
}
|
||||
|
|
49
handler.go
49
handler.go
|
@ -17,6 +17,7 @@ var commands = map [string]func(*irc.Conn, string, string, string) {
|
|||
"tr": translate,
|
||||
"flags": flags,
|
||||
"add": add,
|
||||
"remove": remove,
|
||||
"topic": topic,
|
||||
"appendtopic": appendtopic,
|
||||
}
|
||||
|
@ -196,20 +197,42 @@ func sayTr(conn *irc.Conn, target string, data interface{}) {
|
|||
}
|
||||
|
||||
func add(conn *irc.Conn, nick, args, channel string) {
|
||||
if !isChannel(channel) {
|
||||
if !isChannel(channel) || !hasAccess(conn, channel, nick, "a") {
|
||||
return
|
||||
}
|
||||
if hasAccess(conn, channel, nick, "a") {
|
||||
split := strings.Fields(args)
|
||||
if len(split) != 2 {
|
||||
return
|
||||
}
|
||||
host, nflags := addAccess(conn, channel, split[0], split[1])
|
||||
split := strings.Fields(args)
|
||||
if len(split) != 2 {
|
||||
return
|
||||
}
|
||||
host, nflags := addAccess(conn, channel, split[0], strings.TrimSpace(split[1]))
|
||||
if host == "" {
|
||||
say(conn, channel, "Could not find nick %s", split[0])
|
||||
} else {
|
||||
say(conn, channel, "%s now has flags %s", host, nflags)
|
||||
}
|
||||
}
|
||||
func remove(conn *irc.Conn, nick, args, channel string) {
|
||||
if !isChannel(channel) || !hasAccess(conn, channel, nick, "a") {
|
||||
return
|
||||
}
|
||||
split := strings.Fields(args)
|
||||
|
||||
if len(split) == 2 {
|
||||
host, nflags := removeAccess(conn, channel, split[0], strings.TrimSpace(split[1]))
|
||||
if host == "" {
|
||||
say(conn, channel, "Could not find nick %s", split[0])
|
||||
} else {
|
||||
say(conn, channel, "%s now has flags %s", host, nflags)
|
||||
}
|
||||
} else if len(split) == 1 {
|
||||
host, removed := removeUser(conn, channel, split[0])
|
||||
if host == "" {
|
||||
say(conn, channel, "Could not find nick %s", split[0])
|
||||
} else if removed {
|
||||
say(conn, channel, "Removed %s", host)
|
||||
} else {
|
||||
say(conn, channel, "%s did not have any flags", host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -218,7 +241,7 @@ func flags(conn *irc.Conn, nick, args, channel string) {
|
|||
return
|
||||
}
|
||||
|
||||
query := args
|
||||
query := strings.TrimSpace(args)
|
||||
if query == "" {
|
||||
query = nick
|
||||
}
|
||||
|
@ -233,12 +256,12 @@ func flags(conn *irc.Conn, nick, args, channel string) {
|
|||
return
|
||||
}
|
||||
|
||||
flags, err := auth.String(conn.Network + " " + channel, n.Host)
|
||||
if err != nil {
|
||||
say(conn, channel, "Error while retrieving flags for %s: %s", n.Host, err)
|
||||
return
|
||||
flags, _ := auth.String(conn.Network + " " + channel, n.Host)
|
||||
if flags == "" {
|
||||
say(conn, channel, "%s has no flags", n.Host)
|
||||
} else {
|
||||
say(conn, channel, "%s: %s", n.Host, flags)
|
||||
}
|
||||
say(conn, channel, "%s: %s", n.Host, flags)
|
||||
}
|
||||
|
||||
func topic(conn *irc.Conn, nick, args, channel string) {
|
||||
|
|
Loading…
Reference in New Issue