Exemplo n.º 1
0
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)
}
Exemplo n.º 2
0
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)
}
Exemplo n.º 3
0
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)
}