generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
command.go
152 lines (131 loc) · 4.17 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package command
import (
"fmt"
"strings"
"github.com/bakurits/mattermost-plugin-anonymous/server/crypto"
"github.com/bakurits/mattermost-plugin-anonymous/server/anonymous"
"github.com/mattermost/mattermost-server/v5/model"
)
const (
helpTextHeader = "###### Mattermost Anonymous Plugin - Slash command help\n"
helpText = `
* |/anonymous help| - print this help message
* |/anonymous a [message]| - send message in encrypted form
* |/anonymous keypair [action]| - do one of the following actions regarding encryption keypair
* |action| is one of the following:
* |--generate| - generates and stores new keypair for encryption
* |--overwrite [private key]| - you enter new 32byte private key, the plugin stores it along with the updated public key
* |--export| - exports your existing keypair
`
autoCompleteDescription = "Available commands: keypair [--generate, --export, --overwrite]"
autoCompleteHint = "[command][subcommands]"
pluginDescription = "End to end message encryption"
slashCommandName = "anonymous"
)
// Handler returns API for interacting with plugin commands
type Handler interface {
Handle(args ...string) (*model.CommandResponse, error)
}
// command stores command specific information
type command struct {
args *model.CommandArgs
anonymous anonymous.Anonymous
handler HandlerMap
}
// HandlerFunc command handler function type
type HandlerFunc func(args ...string) (*model.CommandResponse, error)
// HandlerMap map of command handler functions
type HandlerMap struct {
handlers map[string]HandlerFunc
defaultHandler HandlerFunc
}
func newCommand(args *model.CommandArgs, a anonymous.Anonymous) *command {
c := &command{
args: args,
anonymous: a,
}
c.handler = HandlerMap{
handlers: map[string]HandlerFunc{
"help": c.help,
"keypair/--overwrite": c.executeKeyOverwrite,
},
defaultHandler: c.help,
}
return c
}
// NewHandler returns new Handler with given dependencies
func NewHandler(args *model.CommandArgs, a anonymous.Anonymous) Handler {
return newCommand(args, a)
}
func (c *command) Handle(args ...string) (*model.CommandResponse, error) {
ch := c.handler
if len(args) == 0 || args[0] != "/"+slashCommandName {
return ch.defaultHandler(args...)
}
args = args[1:]
for n := len(args); n > 0; n-- {
h := ch.handlers[strings.Join(args[:n], "/")]
if h != nil {
return h(args[n:]...)
}
}
return ch.defaultHandler(args...)
}
func (c *command) executeKeyOverwrite(args ...string) (*model.CommandResponse, error) {
if len(args) > 1 {
return &model.CommandResponse{}, &model.AppError{
Message: "Too many arguments passed to e",
}
}
if len(args) == 0 {
return &model.CommandResponse{}, &model.AppError{
Message: "public key not passed to function",
}
}
pub, err := crypto.PublicKeyFromString(args[0])
if err != nil {
return &model.CommandResponse{}, &model.AppError{
Message: err.Error(),
}
}
err = c.anonymous.StorePublicKey(c.args.UserId, pub)
if err != nil {
return &model.CommandResponse{}, &model.AppError{
Message: err.Error(),
}
}
c.postCommandResponse("Keys were successfully overwritten")
return &model.CommandResponse{}, nil
}
func (c *command) help(_ ...string) (*model.CommandResponse, error) {
helpText := helpTextHeader + helpText
c.postCommandResponse(helpText)
return &model.CommandResponse{}, nil
}
func (c *command) postCommandResponse(text string) {
post := &model.Post{
ChannelId: c.args.ChannelId,
Message: text,
}
_ = c.anonymous.SendEphemeralPost(c.args.UserId, post)
}
func (c *command) responsef(format string, args ...interface{}) *model.CommandResponse {
c.postCommandResponse(fmt.Sprintf(format, args...))
return &model.CommandResponse{}
}
func (c *command) responseRedirect(redirectURL string) *model.CommandResponse {
return &model.CommandResponse{
GotoLocation: redirectURL,
}
}
// GetSlashCommand returns command to register
func GetSlashCommand() *model.Command {
return &model.Command{
Trigger: slashCommandName,
DisplayName: slashCommandName,
Description: pluginDescription,
AutoComplete: true,
AutoCompleteDesc: autoCompleteDescription,
AutoCompleteHint: autoCompleteHint,
}
}