Пример #1
0
func shrugCommand(c *Context, command *model.Command) bool {
	cmd := "/shrug"

	if !command.Suggest && strings.Index(command.Command, cmd) == 0 {
		message := "¯\\_(ツ)_/¯"

		parameters := strings.SplitN(command.Command, " ", 2)
		if len(parameters) > 1 {
			message += " " + parameters[1]
		}

		post := &model.Post{}
		post.Message = message
		post.ChannelId = command.ChannelId
		if _, err := CreatePost(c, post, false); err != nil {
			l4g.Error("Unable to create /shrug post post, err=%v", err)
			return false
		}
		command.Response = model.RESP_EXECUTED
		return true

	} else if strings.Index(cmd, command.Command) == 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Adds ¯\\_(ツ)_/¯ to your message, /shrug [message]"})
	}

	return false
}
Пример #2
0
func meCommand(c *Context, command *model.Command) bool {
	cmd := cmds["meCommand"]

	if !command.Suggest && strings.Index(command.Command, cmd) == 0 {
		message := ""

		parameters := strings.SplitN(command.Command, " ", 2)
		if len(parameters) > 1 {
			message += "*" + parameters[1] + "*"
		}

		post := &model.Post{}
		post.Message = message
		post.ChannelId = command.ChannelId
		if _, err := CreatePost(c, post, false); err != nil {
			l4g.Error("Unable to create /me post post, err=%v", err)
			return false
		}
		command.Response = model.RESP_EXECUTED
		return true

	} else if strings.Index(cmd, command.Command) == 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Do an action, /me [message]"})
	}

	return false
}
Пример #3
0
func shrugCommand(c *Context, command *model.Command) bool {
	cmd := cmds["shrugCommand"]

	if !command.Suggest && strings.Index(command.Command, cmd) == 0 {
		message := `¯\\\_(ツ)_/¯`

		parameters := strings.SplitN(command.Command, " ", 2)
		if len(parameters) > 1 {
			message += " " + parameters[1]
		}

		post := &model.Post{}
		post.Message = message
		post.ChannelId = command.ChannelId
		if _, err := CreatePost(c, post, false); err != nil {
			l4g.Error(utils.T("api.command.shrug_command.create.error"), err)
			return false
		}
		command.Response = model.RESP_EXECUTED
		return true

	} else if strings.Index(cmd, command.Command) == 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: c.T("api.command.shrug_command.description")})
	}

	return false
}
Пример #4
0
func echoCommand(c *Context, command *model.Command) bool {

	cmd := "/echo"

	if strings.Index(command.Command, cmd) == 0 {
		parts := strings.SplitN(command.Command, " ", 3)

		channelName := ""
		if len(parts) >= 2 {
			channelName = parts[1]
		}

		message := ""
		if len(parts) >= 3 {
			message = parts[2]
		}

		if result := <-Srv.Store.Channel().GetChannels(c.Session.TeamId, c.Session.UserId); result.Err != nil {
			c.Err = result.Err
			return false
		} else {
			channels := result.Data.(*model.ChannelList)

			for _, v := range channels.Channels {
				if v.Type == model.CHANNEL_DIRECT {
					continue
				}

				if v.Name == channelName && !command.Suggest {
					post := &model.Post{}
					post.ChannelId = v.Id
					post.Message = message

					if _, err := CreateValetPost(c, post); err != nil {
						c.Err = err
						return false
					}

					command.Response = model.RESP_EXECUTED
					return true
				}

				if len(channelName) == 0 || (strings.Index(v.Name, channelName) == 0 && len(parts) < 3) {
					command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd + " " + v.Name, Description: "Echo a message using Valet in a channel"})
				}
			}
		}

	} else if strings.Index(cmd, command.Command) == 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Echo a message using Valet in a channel"})
	}

	return false
}
Пример #5
0
func joinCommand(c *Context, command *model.Command) bool {

	// looks for "/join channel-name"
	cmd := "/join"

	if strings.Index(command.Command, cmd) == 0 {

		parts := strings.Split(command.Command, " ")

		startsWith := ""

		if len(parts) == 2 {
			startsWith = parts[1]
		}

		if result := <-Srv.Store.Channel().GetMoreChannels(c.Session.TeamId, c.Session.UserId); result.Err != nil {
			c.Err = result.Err
			return false
		} else {
			channels := result.Data.(*model.ChannelList)

			for _, v := range channels.Channels {

				if v.Name == startsWith && !command.Suggest {

					if v.Type == model.CHANNEL_DIRECT {
						return false
					}

					JoinChannel(c, v.Id, "")

					if c.Err != nil {
						return false
					}

					command.GotoLocation = c.GetTeamURL() + "/channels/" + v.Name
					command.Response = model.RESP_EXECUTED
					return true
				}

				if len(startsWith) == 0 || strings.Index(v.Name, startsWith) == 0 {
					command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd + " " + v.Name, Description: "Join the open channel"})
				}
			}
		}
	} else if strings.Index(cmd, command.Command) == 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Join an open channel"})
	}

	return false
}
Пример #6
0
func logoutCommand(c *Context, command *model.Command) bool {

	cmd := "/logout"

	if strings.Index(command.Command, cmd) == 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Logout"})

		if !command.Suggest {
			command.GotoLocation = "/logout"
			command.Response = model.RESP_EXECUTED
			return true
		}

	} else if strings.Index(cmd, command.Command) == 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Logout"})
	}

	return false
}
Пример #7
0
func echoCommand(c *Context, command *model.Command) bool {
	cmd := "/echo"
	maxThreads := 100

	if !command.Suggest && strings.Index(command.Command, cmd) == 0 {
		parameters := strings.SplitN(command.Command, " ", 2)
		if len(parameters) != 2 || len(parameters[1]) == 0 {
			return false
		}
		message := strings.Trim(parameters[1], " ")
		delay := 0
		if endMsg := strings.LastIndex(message, "\""); string(message[0]) == "\"" && endMsg > 1 {
			if checkDelay, err := strconv.Atoi(strings.Trim(message[endMsg:], " \"")); err == nil {
				delay = checkDelay
			}
			message = message[1:endMsg]
		} else if strings.Index(message, " ") > -1 {
			delayIdx := strings.LastIndex(message, " ")
			delayStr := strings.Trim(message[delayIdx:], " ")

			if checkDelay, err := strconv.Atoi(delayStr); err == nil {
				delay = checkDelay
				message = message[:delayIdx]
			}
		}

		if delay > 10000 {
			c.Err = model.NewAppError("echoCommand", "Delays must be under 10000 seconds", "")
			return false
		}

		if echoSem == nil {
			// We want one additional thread allowed so we never reach channel lockup
			echoSem = make(chan bool, maxThreads+1)
		}

		if len(echoSem) >= maxThreads {
			c.Err = model.NewAppError("echoCommand", "High volume of echo request, cannot process request", "")
			return false
		}

		echoSem <- true
		go func() {
			defer func() { <-echoSem }()
			post := &model.Post{}
			post.ChannelId = command.ChannelId
			post.Message = message

			time.Sleep(time.Duration(delay) * time.Second)

			if _, err := CreatePost(c, post, false); err != nil {
				l4g.Error("Unable to create /echo post, err=%v", err)
			}
		}()

		command.Response = model.RESP_EXECUTED
		return true

	} else if strings.Index(cmd, command.Command) == 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Echo back text from your account, /echo \"message\" [delay in seconds]"})
	}

	return false
}
Пример #8
0
func loadTestUrlCommand(c *Context, command *model.Command) bool {
	cmd := cmds["loadTestCommand"] + " url"

	if strings.Index(command.Command, cmd) == 0 && !command.Suggest {
		url := ""

		parameters := strings.SplitN(command.Command, " ", 3)
		if len(parameters) != 3 {
			c.Err = model.NewAppError("loadTestUrlCommand", "Command must contain a url", "")
			return true
		} else {
			url = parameters[2]
		}

		// provide a shortcut to easily access tests stored in doc/developer/tests
		if !strings.HasPrefix(url, "http") {
			url = "https://raw.githubusercontent.com/mattermost/platform/master/doc/developer/tests/" + url

			if path.Ext(url) == "" {
				url += ".md"
			}
		}

		var contents io.ReadCloser
		if r, err := http.Get(url); err != nil {
			c.Err = model.NewAppError("loadTestUrlCommand", "Unable to get file", err.Error())
			return false
		} else if r.StatusCode > 400 {
			c.Err = model.NewAppError("loadTestUrlCommand", "Unable to get file", r.Status)
			return false
		} else {
			contents = r.Body
		}

		bytes := make([]byte, 4000)

		// break contents into 4000 byte posts
		for {
			length, err := contents.Read(bytes)
			if err != nil && err != io.EOF {
				c.Err = model.NewAppError("loadTestUrlCommand", "Encountered error reading file", err.Error())
				return false
			}

			if length == 0 {
				break
			}

			post := &model.Post{}
			post.Message = string(bytes[:length])
			post.ChannelId = command.ChannelId

			if _, err := CreatePost(c, post, false); err != nil {
				l4g.Error("Unable to create post, err=%v", err)
				return false
			}
		}

		command.Response = model.RESP_EXECUTED

		return true
	} else if strings.Index(cmd, command.Command) == 0 && strings.Index(command.Command, "/loadtest posts") != 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Add a post containing the text from a given url to current channel <Url>"})
	}

	return false
}
Пример #9
0
func echoCommand(c *Context, command *model.Command) bool {
	cmd := cmds["echoCommand"]
	maxThreads := 100

	if !command.Suggest && strings.Index(command.Command, cmd) == 0 {
		parameters := strings.SplitN(command.Command, " ", 2)
		if len(parameters) != 2 || len(parameters[1]) == 0 {
			return false
		}
		message := strings.Trim(parameters[1], " ")
		delay := 0
		if endMsg := strings.LastIndex(message, "\""); string(message[0]) == "\"" && endMsg > 1 {
			if checkDelay, err := strconv.Atoi(strings.Trim(message[endMsg:], " \"")); err == nil {
				delay = checkDelay
			}
			message = message[1:endMsg]
		} else if strings.Index(message, " ") > -1 {
			delayIdx := strings.LastIndex(message, " ")
			delayStr := strings.Trim(message[delayIdx:], " ")

			if checkDelay, err := strconv.Atoi(delayStr); err == nil {
				delay = checkDelay
				message = message[:delayIdx]
			}
		}

		if delay > 10000 {
			c.Err = model.NewLocAppError("echoCommand", "api.command.echo_command.under.app_error", nil, "")
			return false
		}

		if echoSem == nil {
			// We want one additional thread allowed so we never reach channel lockup
			echoSem = make(chan bool, maxThreads+1)
		}

		if len(echoSem) >= maxThreads {
			c.Err = model.NewLocAppError("echoCommand", "api.command.echo_command.high_volume.app_error", nil, "")
			return false
		}

		echoSem <- true
		go func() {
			defer func() { <-echoSem }()
			post := &model.Post{}
			post.ChannelId = command.ChannelId
			post.Message = message

			time.Sleep(time.Duration(delay) * time.Second)

			if _, err := CreatePost(c, post, true); err != nil {
				l4g.Error(utils.T("api.command.echo_command.create.error"), err)
			}
		}()

		command.Response = model.RESP_EXECUTED
		return true

	} else if strings.Index(cmd, command.Command) == 0 {
		command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: c.T("api.command.echo_command.description")})
	}

	return false
}