diff --git a/mattermost/bathroom.tar.gz b/mattermost/bathroom.tar.gz index 837ffaf..130944c 100644 --- a/mattermost/bathroom.tar.gz +++ b/mattermost/bathroom.tar.gz Binary files differ diff --git a/mattermost/bathroom.tar.gz b/mattermost/bathroom.tar.gz index 837ffaf..130944c 100644 --- a/mattermost/bathroom.tar.gz +++ b/mattermost/bathroom.tar.gz Binary files differ diff --git a/mattermost/server/bathroom-linux-amd64 b/mattermost/server/bathroom-linux-amd64 index 673a918..4852eb8 100755 --- a/mattermost/server/bathroom-linux-amd64 +++ b/mattermost/server/bathroom-linux-amd64 Binary files differ diff --git a/mattermost/bathroom.tar.gz b/mattermost/bathroom.tar.gz index 837ffaf..130944c 100644 --- a/mattermost/bathroom.tar.gz +++ b/mattermost/bathroom.tar.gz Binary files differ diff --git a/mattermost/server/bathroom-linux-amd64 b/mattermost/server/bathroom-linux-amd64 index 673a918..4852eb8 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 7637e45..7613457 100644 --- a/mattermost/server/bathroom.go +++ b/mattermost/server/bathroom.go @@ -31,9 +31,11 @@ _ "math" ) +const POST_STATUS_TO_ADMIN = true const DO_LOGGING = false -var userSplit *regexp.Regexp = regexp.MustCompile(`\s+(^|[^,])|\s*,\s*`) +var splitWhitespaceOrComma *regexp.Regexp = regexp.MustCompile(`\s+(^|[^,])|\s*,\s*`) +var doorWatchFile *regexp.Regexp = regexp.MustCompile(`(^|/)door(\d+)$`) const ( Unknown = uint(iota) @@ -75,7 +77,7 @@ WatchPath string AdminUsers string - adminUsers []string + adminUsers []*model.User KeyPath string @@ -101,7 +103,7 @@ doorLock sync.RWMutex doors map[uint8]*Door - lastReport *string + bot *string } func (p *BathroomMonitorPlugin) getKeyFile(id uint8) (*rsa.PublicKey, error) { @@ -176,7 +178,9 @@ if report { statusStr, _ := statusName(status) _ = statusStr - //p.postAdminChannel(fmt.Sprintf("STATUS: door %d = %s", id, statusStr)) + if POST_STATUS_TO_ADMIN { + p.postAdminChannel(fmt.Sprintf("STATUS: door %d = %s", id, statusStr)) + } p.API.PublishWebSocketEvent("updated", map[string]interface{}{}, &model.WebsocketBroadcast{}) } } else { @@ -401,55 +405,16 @@ func (p *BathroomMonitorPlugin) postAdminChannel(text string) { p.API.LogError(text) - if p.lastReport != nil && *p.lastReport == text { - return - } - p.lastReport = &text - var users []*model.User = make([]*model.User, 0, len(p.config.adminUsers)) - for _, un := range(p.config.adminUsers) { - u, _ := p.API.GetUserByUsername(un) - if u != nil { - users = append(users, u) - } - } - if len(users) > 0 { - admin := users[0] - bots, _ := p.API.GetBots(&model.BotGetOptions{Page:0, PerPage:1000}) - var bathroom_bot *model.Bot = nil - if bots != nil { - for _, b := range(bots) { - if b.Username == "bathroom-bot" { - bathroom_bot = b - break - } - } - } - - if bathroom_bot == nil { - created_bathroom_bot, err := p.API.CreateBot(&model.Bot{Username:"bathroom-bot", OwnerId:admin.Id, DisplayName:"Bathroom Bot", Description:"Tracks Bathroom Status"}) - if err != nil { - p.API.LogError(errors.Wrap(err, "Couldn't create bathroom-bot bot").Error()) - return - } - bathroom_bot = created_bathroom_bot - } - - if bathroom_bot == nil { - p.API.LogError("Really couldn't create bathroom-bot bot") - return - } - - for _, u := range(users) { - channel, err := p.API.GetDirectChannel(bathroom_bot.UserId, u.Id); + if p.bot != nil { + for _, u := range(p.config.adminUsers) { + channel, err := p.API.GetDirectChannel(*p.bot, u.Id); if err != nil { p.API.LogError(errors.Wrap(err, fmt.Sprintf("Couldn't get direct channel to user %s", u.Username)).Error()) continue } - p.API.CreatePost(&model.Post{UserId: bathroom_bot.UserId, ChannelId:channel.Id, Message:text, MessageSource:text}) + p.API.CreatePost(&model.Post{UserId: *p.bot, ChannelId:channel.Id, Message:text, MessageSource:text}) } - } else { - p.API.LogError("No admin users?") } } @@ -494,13 +459,16 @@ newConfig.numDoors = uint8(numDoors) } - newConfig.adminUsers = make([]string, 0, 4) - - split := userSplit.Split(newConfig.AdminUsers, -1) + newConfig.adminUsers = make([]*model.User, 0, 4) + + split := splitWhitespaceOrComma.Split(newConfig.AdminUsers, -1) for _, un := range split { trimmed := strings.Trim(un, ", \t\n") if trimmed != "" { - newConfig.adminUsers = append(newConfig.adminUsers, trimmed) + u, _ := p.API.GetUserByUsername(trimmed) + if u != nil { + newConfig.adminUsers = append(newConfig.adminUsers, u) + } } } @@ -551,12 +519,6 @@ } func fileNotifyBasedLoop(p *BathroomMonitorPlugin) { - doorFile, err := regexp.Compile(`(^|/)door(\d+)$`) - if err != nil { - p.API.LogError(errors.Wrap(err, "Couldn't compile regex").Error()) - return - } - for { p.configLock.Lock() numDoors := p.config.numDoors @@ -587,7 +549,7 @@ time.Sleep(60) run = false } else { - match := doorFile.FindStringSubmatch(event.Name) + match := doorWatchFile.FindStringSubmatch(event.Name) if match == nil { continue } @@ -649,6 +611,14 @@ func (p *BathroomMonitorPlugin) OnActivate() error { + if p.bot == nil { + botUser, err := p.Helpers.EnsureBot(&model.Bot{Username:"bathroom-bot", DisplayName:"Bathroom Bot", Description:"Tracks Bathroom Status"}); + if err == nil { + p.bot = &botUser + } else { + p.bot = nil + } + } if USE_FSNOTIFY { go fileNotifyBasedLoop(p) } else {