-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_team_bot_settings.go
210 lines (185 loc) · 5.35 KB
/
cmd_team_bot_settings.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package client
import (
"fmt"
"strings"
"github.com/keybase/cli"
"github.com/keybase/client/go/chatrender"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
context "golang.org/x/net/context"
)
type CmdTeamBotSettings struct {
libkb.Contextified
Team string
Username string
BotSettings *keybase1.TeamBotSettings
}
func newCmdTeamBotSettings(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
flags := botSettingsFlags
return cli.Command{
Name: "bot-settings",
ArgumentHelp: "<team name>",
Usage: "Modify the bot settings of the given user. User must be a member of the given team with role restrictedbot.",
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdTeamBotSettingsRunner(g), "bot-settings", c)
},
Flags: append(flags,
cli.StringFlag{
Name: "u, user",
Usage: "username",
}),
Description: teamBotSettingsDoc,
}
}
func NewCmdTeamBotSettingsRunner(g *libkb.GlobalContext) *CmdTeamBotSettings {
return &CmdTeamBotSettings{Contextified: libkb.NewContextified(g)}
}
func (c *CmdTeamBotSettings) ParseArgv(ctx *cli.Context) error {
var err error
c.Team, err = ParseOneTeamName(ctx)
if err != nil {
return err
}
c.Username, err = ParseUser(ctx)
if err != nil {
return err
}
c.BotSettings = ParseBotSettings(ctx)
return nil
}
func (c *CmdTeamBotSettings) Run() error {
cli, err := GetTeamsClient(c.G())
if err != nil {
return err
}
if err := ValidateBotSettingsConvs(c.G(), c.Team,
chat1.ConversationMembersType_TEAM, c.BotSettings); err != nil {
return err
}
var botSettings keybase1.TeamBotSettings
if c.BotSettings == nil {
arg := keybase1.TeamGetBotSettingsArg{
Name: c.Team,
Username: c.Username,
}
botSettings, err = cli.TeamGetBotSettings(context.Background(), arg)
if err != nil {
return err
}
} else {
botSettings = *c.BotSettings
arg := keybase1.TeamSetBotSettingsArg{
Name: c.Team,
Username: c.Username,
BotSettings: botSettings,
}
if err := cli.TeamSetBotSettings(context.Background(), arg); err != nil {
return err
}
}
if err := renderBotSettings(c.G(), c.Username, nil, botSettings); err != nil {
return err
}
return nil
}
func renderBotSettings(g *libkb.GlobalContext, username string, convID *chat1.ConversationID, botSettings keybase1.TeamBotSettings) error {
var output string
if botSettings.Cmds {
chatClient, err := GetChatLocalClient(g)
if err != nil {
return fmt.Errorf("Getting chat service client error: %s", err)
}
var cmds chat1.ListBotCommandsLocalRes
if convID == nil {
cmds, err = chatClient.ListPublicBotCommandsLocal(context.TODO(), username)
if err != nil {
return err
}
} else {
cmds, err = chatClient.ListBotCommandsLocal(context.TODO(), *convID)
if err != nil {
return err
}
}
if len(cmds.Commands) > 0 {
output += "\t- command messages for the following commands: \n"
} else {
output += "\t- command messages\n"
}
username = libkb.NewNormalizedUsername(username).String()
for _, cmd := range cmds.Commands {
if cmd.Username == username {
output += fmt.Sprintf("\t\t- !%s\n", cmd.Name)
}
}
}
if botSettings.Mentions {
output += "\t- when @-mentioned\n"
}
if len(botSettings.Triggers) > 0 {
output += "\t- messages that match the following:\n\t\t"
for _, trigger := range botSettings.Triggers {
output += fmt.Sprintf("%q\n\t\t", trigger)
}
output += "\n"
}
dui := g.UI.GetDumbOutputUI()
if len(output) == 0 {
dui.Printf("%s will not receive any messages with the current bot settings\n", username)
} else {
dui.Printf("%s will receive messages in the following cases:\n%s", username, output)
}
if len(botSettings.Convs) == 0 {
dui.Printf("%s can send/receive into all conversations", username)
} else {
dui.Printf("%s can send/receive into the following conversations:\n\t", username)
convIDs := []chat1.ConvIDStr{}
for _, conv := range botSettings.Convs {
convIDs = append(convIDs, chat1.ConvIDStr(conv))
}
convNames, err := getConvNames(g, convIDs)
if err != nil {
return err
}
dui.Printf(strings.Join(convNames, "\n\t"))
}
dui.Printf("\n")
return nil
}
func getConvNames(g *libkb.GlobalContext, convs []chat1.ConvIDStr) (convNames []string, err error) {
fetcher := chatCLIInboxFetcher{}
for _, convIDStr := range convs {
convID, err := chat1.MakeConvID(convIDStr.String())
if err != nil {
return nil, err
}
if convID.IsNil() {
continue
}
fetcher.query.ConvIDs = append(fetcher.query.ConvIDs, convID)
}
conversations, err := fetcher.fetch(context.TODO(), g)
if err != nil {
return nil, err
}
for _, conv := range conversations {
convNames = append(convNames, chatrender.ConvName(g, conv, g.Env.GetUsername().String()))
}
return convNames, nil
}
func (c *CmdTeamBotSettings) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}
const teamBotSettingsDoc = `"keybase team bot-settings" allows you to view and modify the bot settings for restrictedbot team members.
EXAMPLES:
View current bot settings:
keybase team bot-settings acme -u alice
Specify new bot settings:
keybase team bot-settings acme -u alice --allow-mentions --triggers foo --triggers bar --allowed-convs #general
`