func (c *Config) handleModule(b core.Bot, m *slack.Message) error { moduleText := "currently loaded modules:\n" for key := range b.LoadedModules() { moduleText = moduleText + fmt.Sprintf("> `%s`\n", key) } return b.Say(m.Channel, moduleText) }
func (c *Config) handleConfig(b core.Bot, m *slack.Message) error { configText := "current config:\n" for key, value := range b.Configuration() { if strings.HasPrefix(key, "option.") { configText = configText + fmt.Sprintf("> `%s` = %s\n", key, value) } } return b.Say(m.Channel, configText) }
func (j *Jobs) handleJobsStatus(b core.Bot, m *slack.Message) error { statusText := "current job statuses:\n" for _, status := range b.JobManager().Status() { if len(status.RunningFor) != 0 { statusText = statusText + fmt.Sprintf(">`%s` - state: %s running for: %s\n", status.Name, status.State, status.RunningFor) } else { statusText = statusText + fmt.Sprintf(">`%s` - state: %s\n", status.Name, status.State) } } return b.Say(m.Channel, statusText) }
func (c *Core) handleChannels(b core.Bot, m *slack.Message) error { if len(b.ActiveChannels()) == 0 { return b.Say(m.Channel, "currently listening to *no* channels.") } activeChannelsText := "currently listening to the following channels:\n" for _, channelID := range b.ActiveChannels() { if channel := b.FindChannel(channelID); channel != nil { activeChannelsText = activeChannelsText + fmt.Sprintf(">#%s (id:%s)\n", channel.Name, channel.ID) } } return b.Say(m.Channel, activeChannelsText) }
func (u Util) handleUserID(b core.Bot, m *slack.Message) error { messageText := core.LessSpecificMention(m.Text, b.ID()) mentionedUserIDs := core.Mentions(messageText) outputText := "I looked up the following users:\n" for _, userID := range mentionedUserIDs { user := b.FindUser(userID) outputText = outputText + fmt.Sprintf("> %s : %s %s", userID, user.Profile.FirstName, user.Profile.LastName) } return b.Say(m.Channel, outputText) }
func (j *Jobs) handleJobRun(b core.Bot, m *slack.Message) error { messageWithoutMentions := util.TrimWhitespace(core.LessMentions(m.Text)) pieces := strings.Split(messageWithoutMentions, " ") if len(pieces) > 1 { jobName := pieces[len(pieces)-1] b.JobManager().RunJob(jobName) return b.Sayf(m.Channel, "ran job `%s`", jobName) } b.JobManager().RunAllJobs() return b.Say(m.Channel, "ran all jobs") }
func (c *Core) handleTell(b core.Bot, m *slack.Message) error { messageText := core.LessSpecificMention(m.Text, b.ID()) words := strings.Split(messageText, " ") destinationUser := "" tellMessage := "" for x := 0; x < len(words); x++ { word := words[x] if core.Like(word, "tell") { continue } else if core.IsMention(word) { destinationUser = word tellMessage = strings.Join(words[x+1:], " ") } } tellMessage = core.ReplaceAny(tellMessage, "you are", "shes", "she's", "she is", "hes", "he's", "he is", "theyre", "they're", "they are") resultMessage := fmt.Sprintf("%s %s", destinationUser, tellMessage) return b.Say(m.Channel, resultMessage) }
func (c *Core) handleHelp(b core.Bot, m *slack.Message) error { responseText := "Here are the commands that are currently configured:" for _, actionHandler := range b.Actions() { if !actionHandler.Passive { if len(actionHandler.MessagePattern) != 0 { responseText = responseText + fmt.Sprintf("\n>`%s` - %s", actionHandler.MessagePattern, actionHandler.Description) } else { responseText = responseText + fmt.Sprintf("\n>`*` - %s", actionHandler.Description) } } } responseText = responseText + "\nWith the following passive commands:" for _, actionHandler := range b.Actions() { if actionHandler.Passive { if len(actionHandler.MessagePattern) != 0 { responseText = responseText + fmt.Sprintf("\n>`%s` - %s", actionHandler.MessagePattern, actionHandler.Description) } else { responseText = responseText + fmt.Sprintf("\n>`*` - %s", actionHandler.Description) } } } return b.Say(m.Channel, responseText) }
func (cr *ConsoleRunner) handleConsoleRunnerRun(b core.Bot, m *slack.Message) error { messageWithoutMentions := util.TrimWhitespace(core.LessSpecificMention(m.Text, b.ID())) cleanedMessage := core.FixLinks(messageWithoutMentions) pieces := strings.Split(cleanedMessage, " ") if len(pieces) < 2 { return exception.Newf("invalid arguments for `%s`", ActionConsoleRunnerRun) } commandWithArguments := pieces[1:] command := commandWithArguments[0] args := []string{} if len(commandWithArguments) > 1 { args = commandWithArguments[1:] } if !cr.isWhitelistedCommand(command) { return exception.Newf("`%s` cannot run %s", ActionConsoleRunnerRun, command) } cmdFullPath, lookErr := exec.LookPath(command) if lookErr != nil { return exception.Wrap(lookErr) } stdoutBuffer := bytes.NewBuffer([]byte{}) subCmd := exec.Command(cmdFullPath, args...) subCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} subCmd.StdoutPipe() subCmd.StderrPipe() subCmd.Stdout = stdoutBuffer subCmd.Stderr = stdoutBuffer startErr := subCmd.Start() if startErr != nil { return startErr } started := time.Now().UTC() didTimeout := false go func() { for { now := time.Now().UTC() if now.Sub(started) > ConsoleRunnerTimeout { didTimeout = true pgid, err := syscall.Getpgid(subCmd.Process.Pid) if err != nil { return } syscall.Kill(-pgid, 15) } time.Sleep(50 * time.Millisecond) } }() subCmd.Wait() timedOutText := "" if didTimeout { timedOutText = " (timed out)" } stdout := stdoutBuffer.String() outputText := fmt.Sprintf("console runner stdout%s:\n", timedOutText) if len(stdout) != 0 { prefixed := strings.Replace(stdout, "\n", "\n>", -1) outputText = outputText + fmt.Sprintf(">%s", prefixed) } else { outputText = "> empty" } return b.Say(m.Channel, outputText) }