diff --git a/mattermost/server/bathroom-linux-amd64 b/mattermost/server/bathroom-linux-amd64 index 0f04c02..94c99dc 100755 --- a/mattermost/server/bathroom-linux-amd64 +++ b/mattermost/server/bathroom-linux-amd64 Binary files differ diff --git a/mattermost/server/bathroom-linux-amd64 b/mattermost/server/bathroom-linux-amd64 index 0f04c02..94c99dc 100755 --- a/mattermost/server/bathroom-linux-amd64 +++ b/mattermost/server/bathroom-linux-amd64 Binary files differ diff --git a/mattermost/server/bathroom.go b/mattermost/server/bathroom.go index 551bb65..ccd6ac8 100644 --- a/mattermost/server/bathroom.go +++ b/mattermost/server/bathroom.go @@ -12,7 +12,7 @@ "os" _ "github.com/fatih/structs" "github.com/hashicorp/go-multierror" - _ "github.com/kr/pretty" + "github.com/kr/pretty" "strings" "regexp" "strconv" @@ -264,14 +264,22 @@ return doorId8, nil } +func SliceIndex(limit int, predicate func(i int) bool) int { + for i := 0; i < limit; i++ { + if predicate(i) { + return i + } + } + return -1 +} + func (p *BathroomMonitorPlugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) { auth, authOk := r.Header["Mattermost-User-Id"] var authUser string = "" if authOk && len(auth) > 0 { authUser = auth[0] } - p.log(fmt.Sprintf("Requested path: %s %s AUTH: %s", r.URL.Path, c.IpAddress, authUser)) - + p.log(fmt.Sprintf("Requested path: %s %s AUTH: %s (%t)", r.URL.Path, c.IpAddress, authUser, authOk)) if r.URL.Path == "/settings" { p.configLock.Lock() defer p.configLock.Unlock() @@ -314,6 +322,39 @@ } return } + if r.URL.Path == "/admin-update-status" && authOk { + r.ParseForm() + p.configLock.Lock() + p.doorLock.Lock() + defer p.doorLock.Unlock() + defer p.configLock.Unlock() + + if SliceIndex(len(p.config.adminUsers), func(i int) bool { return p.config.adminUsers[i].Id == authUser }) != -1 { + for n, vs := range r.Form { + matches := doorWatchFile.FindStringSubmatch(n) + if matches != nil && len(matches) >= 3 { + doorId64, err := strconv.ParseUint(matches[2], 10, 8) + doorId := uint8(doorId64) + if err == nil && doorId >= 1 && doorId <= p.config.numDoors && len(vs) > 0 { + status64, err := strconv.ParseUint(vs[0], 10, 8) + status := uint(status64) + if err == nil && status >= Unknown && status <= Closed { + p.setDoorStatus(uint8(doorId), status, true) + } else { + fmt.Fprintf(w, "Not a valid status: %d", status) + } + } else { + fmt.Fprintf(w, "Not a valid door: %# v, %d, %# v", matches, doorId, pretty.Formatter(vs)) + } + } else { + fmt.Fprintf(w, "Not a valid door key: %s", n) + } + } + } else { + fmt.Fprintf(w, "Not authorized: %s", authUser) + } + return + } if r.URL.Path == "/status-update" { r.ParseForm() p.log("Contacted by " + r.RemoteAddr)