github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/app/plugin_commands.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "fmt" 8 "net/http" 9 "strings" 10 11 "github.com/mattermost/mattermost-server/model" 12 ) 13 14 type PluginCommand struct { 15 Command *model.Command 16 PluginId string 17 } 18 19 func (a *App) RegisterPluginCommand(pluginId string, command *model.Command) error { 20 if command.Trigger == "" { 21 return fmt.Errorf("invalid command") 22 } 23 24 command = &model.Command{ 25 Trigger: strings.ToLower(command.Trigger), 26 TeamId: command.TeamId, 27 AutoComplete: command.AutoComplete, 28 AutoCompleteDesc: command.AutoCompleteDesc, 29 AutoCompleteHint: command.AutoCompleteHint, 30 DisplayName: command.DisplayName, 31 } 32 33 a.Srv.pluginCommandsLock.Lock() 34 defer a.Srv.pluginCommandsLock.Unlock() 35 36 for _, pc := range a.Srv.pluginCommands { 37 if pc.Command.Trigger == command.Trigger && pc.Command.TeamId == command.TeamId { 38 if pc.PluginId == pluginId { 39 pc.Command = command 40 return nil 41 } 42 } 43 } 44 45 a.Srv.pluginCommands = append(a.Srv.pluginCommands, &PluginCommand{ 46 Command: command, 47 PluginId: pluginId, 48 }) 49 return nil 50 } 51 52 func (a *App) UnregisterPluginCommand(pluginId, teamId, trigger string) { 53 trigger = strings.ToLower(trigger) 54 55 a.Srv.pluginCommandsLock.Lock() 56 defer a.Srv.pluginCommandsLock.Unlock() 57 58 var remaining []*PluginCommand 59 for _, pc := range a.Srv.pluginCommands { 60 if pc.Command.TeamId != teamId || pc.Command.Trigger != trigger { 61 remaining = append(remaining, pc) 62 } 63 } 64 a.Srv.pluginCommands = remaining 65 } 66 67 func (a *App) UnregisterPluginCommands(pluginId string) { 68 a.Srv.pluginCommandsLock.Lock() 69 defer a.Srv.pluginCommandsLock.Unlock() 70 71 var remaining []*PluginCommand 72 for _, pc := range a.Srv.pluginCommands { 73 if pc.PluginId != pluginId { 74 remaining = append(remaining, pc) 75 } 76 } 77 a.Srv.pluginCommands = remaining 78 } 79 80 func (a *App) PluginCommandsForTeam(teamId string) []*model.Command { 81 a.Srv.pluginCommandsLock.RLock() 82 defer a.Srv.pluginCommandsLock.RUnlock() 83 84 var commands []*model.Command 85 for _, pc := range a.Srv.pluginCommands { 86 if pc.Command.TeamId == "" || pc.Command.TeamId == teamId { 87 commands = append(commands, pc.Command) 88 } 89 } 90 return commands 91 } 92 93 // tryExecutePluginCommand attempts to run a command provided by a plugin based on the given arguments. If no such 94 // command can be found, returns nil for all arguments. 95 func (a *App) tryExecutePluginCommand(args *model.CommandArgs) (*model.Command, *model.CommandResponse, *model.AppError) { 96 parts := strings.Split(args.Command, " ") 97 trigger := parts[0][1:] 98 trigger = strings.ToLower(trigger) 99 100 a.Srv.pluginCommandsLock.RLock() 101 defer a.Srv.pluginCommandsLock.RUnlock() 102 103 for _, pc := range a.Srv.pluginCommands { 104 if (pc.Command.TeamId == "" || pc.Command.TeamId == args.TeamId) && pc.Command.Trigger == trigger { 105 if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil { 106 pluginHooks, err := pluginsEnvironment.HooksForPlugin(pc.PluginId) 107 if err != nil { 108 return pc.Command, nil, model.NewAppError("ExecutePluginCommand", "model.plugin_command.error.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) 109 } 110 response, appErr := pluginHooks.ExecuteCommand(a.PluginContext(), args) 111 return pc.Command, response, appErr 112 } 113 } 114 } 115 return nil, nil, nil 116 }