github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/client/cmd_team_bot_settings.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/keybase/cli" 8 "github.com/keybase/client/go/chatrender" 9 "github.com/keybase/client/go/libcmdline" 10 "github.com/keybase/client/go/libkb" 11 "github.com/keybase/client/go/protocol/chat1" 12 keybase1 "github.com/keybase/client/go/protocol/keybase1" 13 context "golang.org/x/net/context" 14 ) 15 16 type CmdTeamBotSettings struct { 17 libkb.Contextified 18 Team string 19 Username string 20 BotSettings *keybase1.TeamBotSettings 21 } 22 23 func newCmdTeamBotSettings(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command { 24 flags := botSettingsFlags 25 return cli.Command{ 26 Name: "bot-settings", 27 ArgumentHelp: "<team name>", 28 Usage: "Modify the bot settings of the given user. User must be a member of the given team with role restrictedbot.", 29 Action: func(c *cli.Context) { 30 cl.ChooseCommand(NewCmdTeamBotSettingsRunner(g), "bot-settings", c) 31 }, 32 Flags: append(flags, 33 cli.StringFlag{ 34 Name: "u, user", 35 Usage: "username", 36 }), 37 Description: teamBotSettingsDoc, 38 } 39 } 40 41 func NewCmdTeamBotSettingsRunner(g *libkb.GlobalContext) *CmdTeamBotSettings { 42 return &CmdTeamBotSettings{Contextified: libkb.NewContextified(g)} 43 } 44 45 func (c *CmdTeamBotSettings) ParseArgv(ctx *cli.Context) error { 46 var err error 47 c.Team, err = ParseOneTeamName(ctx) 48 if err != nil { 49 return err 50 } 51 52 c.Username, err = ParseUser(ctx) 53 if err != nil { 54 return err 55 } 56 57 c.BotSettings = ParseBotSettings(ctx) 58 return nil 59 } 60 61 func (c *CmdTeamBotSettings) Run() error { 62 cli, err := GetTeamsClient(c.G()) 63 if err != nil { 64 return err 65 } 66 67 if err := ValidateBotSettingsConvs(c.G(), c.Team, 68 chat1.ConversationMembersType_TEAM, c.BotSettings); err != nil { 69 return err 70 } 71 72 var botSettings keybase1.TeamBotSettings 73 if c.BotSettings == nil { 74 arg := keybase1.TeamGetBotSettingsArg{ 75 Name: c.Team, 76 Username: c.Username, 77 } 78 botSettings, err = cli.TeamGetBotSettings(context.Background(), arg) 79 if err != nil { 80 return err 81 } 82 } else { 83 botSettings = *c.BotSettings 84 arg := keybase1.TeamSetBotSettingsArg{ 85 Name: c.Team, 86 Username: c.Username, 87 BotSettings: botSettings, 88 } 89 if err := cli.TeamSetBotSettings(context.Background(), arg); err != nil { 90 return err 91 } 92 } 93 if err := renderBotSettings(c.G(), c.Username, nil, botSettings); err != nil { 94 return err 95 } 96 return nil 97 } 98 99 func renderBotSettings(g *libkb.GlobalContext, username string, convID *chat1.ConversationID, botSettings keybase1.TeamBotSettings) error { 100 var output string 101 if botSettings.Cmds { 102 chatClient, err := GetChatLocalClient(g) 103 if err != nil { 104 return fmt.Errorf("Getting chat service client error: %s", err) 105 } 106 var cmds chat1.ListBotCommandsLocalRes 107 if convID == nil { 108 cmds, err = chatClient.ListPublicBotCommandsLocal(context.TODO(), username) 109 if err != nil { 110 return err 111 } 112 } else { 113 cmds, err = chatClient.ListBotCommandsLocal(context.TODO(), *convID) 114 if err != nil { 115 return err 116 } 117 } 118 119 if len(cmds.Commands) > 0 { 120 output += "\t- command messages for the following commands: \n" 121 } else { 122 output += "\t- command messages\n" 123 } 124 username = libkb.NewNormalizedUsername(username).String() 125 for _, cmd := range cmds.Commands { 126 if cmd.Username == username { 127 output += fmt.Sprintf("\t\t- !%s\n", cmd.Name) 128 } 129 } 130 } 131 132 if botSettings.Mentions { 133 output += "\t- when @-mentioned\n" 134 } 135 136 if len(botSettings.Triggers) > 0 { 137 output += "\t- messages that match the following:\n\t\t" 138 for _, trigger := range botSettings.Triggers { 139 output += fmt.Sprintf("%q\n\t\t", trigger) 140 } 141 output += "\n" 142 } 143 144 dui := g.UI.GetDumbOutputUI() 145 if len(output) == 0 { 146 dui.Printf("%s will not receive any messages with the current bot settings\n", username) 147 } else { 148 dui.Printf("%s will receive messages in the following cases:\n%s", username, output) 149 } 150 if len(botSettings.Convs) == 0 { 151 dui.Printf("%s can send/receive into all conversations", username) 152 } else { 153 dui.Printf("%s can send/receive into the following conversations:\n\t", username) 154 convIDs := []chat1.ConvIDStr{} 155 for _, conv := range botSettings.Convs { 156 convIDs = append(convIDs, chat1.ConvIDStr(conv)) 157 } 158 convNames, err := getConvNames(g, convIDs) 159 if err != nil { 160 return err 161 } 162 dui.Printf(strings.Join(convNames, "\n\t")) 163 } 164 dui.Printf("\n") 165 return nil 166 } 167 168 func getConvNames(g *libkb.GlobalContext, convs []chat1.ConvIDStr) (convNames []string, err error) { 169 fetcher := chatCLIInboxFetcher{} 170 for _, convIDStr := range convs { 171 convID, err := chat1.MakeConvID(convIDStr.String()) 172 if err != nil { 173 return nil, err 174 } 175 if convID.IsNil() { 176 continue 177 } 178 fetcher.query.ConvIDs = append(fetcher.query.ConvIDs, convID) 179 } 180 conversations, err := fetcher.fetch(context.TODO(), g) 181 if err != nil { 182 return nil, err 183 } 184 for _, conv := range conversations { 185 convNames = append(convNames, chatrender.ConvName(g, conv, g.Env.GetUsername().String())) 186 } 187 188 return convNames, nil 189 } 190 191 func (c *CmdTeamBotSettings) GetUsage() libkb.Usage { 192 return libkb.Usage{ 193 Config: true, 194 API: true, 195 KbKeyring: true, 196 } 197 } 198 199 const teamBotSettingsDoc = `"keybase team bot-settings" allows you to view and modify the bot settings for restrictedbot team members. 200 201 EXAMPLES: 202 203 View current bot settings: 204 205 keybase team bot-settings acme -u alice 206 207 Specify new bot settings: 208 209 keybase team bot-settings acme -u alice --allow-mentions --triggers foo --triggers bar --allowed-convs #general 210 `