2018-06-25 12:33:13 -07:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type PluginCommand struct {
|
|
|
|
|
Command *model.Command
|
|
|
|
|
PluginId string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) RegisterPluginCommand(pluginId string, command *model.Command) error {
|
|
|
|
|
if command.Trigger == "" {
|
|
|
|
|
return fmt.Errorf("invalid command")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
command = &model.Command{
|
|
|
|
|
Trigger: strings.ToLower(command.Trigger),
|
|
|
|
|
TeamId: command.TeamId,
|
|
|
|
|
AutoComplete: command.AutoComplete,
|
|
|
|
|
AutoCompleteDesc: command.AutoCompleteDesc,
|
|
|
|
|
AutoCompleteHint: command.AutoCompleteHint,
|
|
|
|
|
DisplayName: command.DisplayName,
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.pluginCommandsLock.Lock()
|
|
|
|
|
defer a.Srv.pluginCommandsLock.Unlock()
|
2018-06-25 12:33:13 -07:00
|
|
|
|
2018-11-07 10:20:07 -08:00
|
|
|
for _, pc := range a.Srv.pluginCommands {
|
2018-06-25 12:33:13 -07:00
|
|
|
if pc.Command.Trigger == command.Trigger && pc.Command.TeamId == command.TeamId {
|
|
|
|
|
if pc.PluginId == pluginId {
|
|
|
|
|
pc.Command = command
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.pluginCommands = append(a.Srv.pluginCommands, &PluginCommand{
|
2018-06-25 12:33:13 -07:00
|
|
|
Command: command,
|
|
|
|
|
PluginId: pluginId,
|
|
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) UnregisterPluginCommand(pluginId, teamId, trigger string) {
|
|
|
|
|
trigger = strings.ToLower(trigger)
|
|
|
|
|
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.pluginCommandsLock.Lock()
|
|
|
|
|
defer a.Srv.pluginCommandsLock.Unlock()
|
2018-06-25 12:33:13 -07:00
|
|
|
|
|
|
|
|
var remaining []*PluginCommand
|
2018-11-07 10:20:07 -08:00
|
|
|
for _, pc := range a.Srv.pluginCommands {
|
2018-06-25 12:33:13 -07:00
|
|
|
if pc.Command.TeamId != teamId || pc.Command.Trigger != trigger {
|
|
|
|
|
remaining = append(remaining, pc)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.pluginCommands = remaining
|
2018-06-25 12:33:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) UnregisterPluginCommands(pluginId string) {
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.pluginCommandsLock.Lock()
|
|
|
|
|
defer a.Srv.pluginCommandsLock.Unlock()
|
2018-06-25 12:33:13 -07:00
|
|
|
|
|
|
|
|
var remaining []*PluginCommand
|
2018-11-07 10:20:07 -08:00
|
|
|
for _, pc := range a.Srv.pluginCommands {
|
2018-06-25 12:33:13 -07:00
|
|
|
if pc.PluginId != pluginId {
|
|
|
|
|
remaining = append(remaining, pc)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.pluginCommands = remaining
|
2018-06-25 12:33:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) PluginCommandsForTeam(teamId string) []*model.Command {
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.pluginCommandsLock.RLock()
|
|
|
|
|
defer a.Srv.pluginCommandsLock.RUnlock()
|
2018-06-25 12:33:13 -07:00
|
|
|
|
|
|
|
|
var commands []*model.Command
|
2018-11-07 10:20:07 -08:00
|
|
|
for _, pc := range a.Srv.pluginCommands {
|
2018-06-25 12:33:13 -07:00
|
|
|
if pc.Command.TeamId == "" || pc.Command.TeamId == teamId {
|
|
|
|
|
commands = append(commands, pc.Command)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return commands
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 17:07:08 -05:00
|
|
|
// tryExecutePluginCommand attempts to run a command provided by a plugin based on the given arguments. If no such
|
|
|
|
|
// command can be found, returns nil for all arguments.
|
|
|
|
|
func (a *App) tryExecutePluginCommand(args *model.CommandArgs) (*model.Command, *model.CommandResponse, *model.AppError) {
|
2018-06-25 12:33:13 -07:00
|
|
|
parts := strings.Split(args.Command, " ")
|
|
|
|
|
trigger := parts[0][1:]
|
|
|
|
|
trigger = strings.ToLower(trigger)
|
|
|
|
|
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.pluginCommandsLock.RLock()
|
|
|
|
|
defer a.Srv.pluginCommandsLock.RUnlock()
|
2018-06-25 12:33:13 -07:00
|
|
|
|
2018-11-07 10:20:07 -08:00
|
|
|
for _, pc := range a.Srv.pluginCommands {
|
2018-06-25 12:33:13 -07:00
|
|
|
if (pc.Command.TeamId == "" || pc.Command.TeamId == args.TeamId) && pc.Command.Trigger == trigger {
|
2018-11-20 08:52:51 -05:00
|
|
|
if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil {
|
|
|
|
|
pluginHooks, err := pluginsEnvironment.HooksForPlugin(pc.PluginId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return pc.Command, nil, model.NewAppError("ExecutePluginCommand", "model.plugin_command.error.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
|
|
|
|
|
}
|
2018-12-05 10:46:08 -08:00
|
|
|
response, appErr := pluginHooks.ExecuteCommand(a.PluginContext(), args)
|
2018-11-20 08:52:51 -05:00
|
|
|
return pc.Command, response, appErr
|
2018-06-25 12:33:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, nil, nil
|
|
|
|
|
}
|