/
team_helper.go
157 lines (142 loc) · 4.31 KB
/
team_helper.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
package client
import (
"errors"
"fmt"
"strings"
"github.com/keybase/cli"
"github.com/adamwalz/keybase-client/go/chat/utils"
"github.com/adamwalz/keybase-client/go/libkb"
"github.com/adamwalz/keybase-client/go/protocol/chat1"
"github.com/adamwalz/keybase-client/go/protocol/keybase1"
context "golang.org/x/net/context"
)
func ParseOneTeamName(ctx *cli.Context) (string, error) {
if len(ctx.Args()) == 0 {
return "", errors.New("team name argument required")
}
if len(ctx.Args()) > 1 {
return "", errors.New("one team name argument required, multiple found")
}
return ctx.Args()[0], nil
}
func ParseOneTeamNameK1(ctx *cli.Context) (res keybase1.TeamName, err error) {
teamNameStr, err := ParseOneTeamName(ctx)
if err != nil {
return res, err
}
return keybase1.TeamNameFromString(teamNameStr)
}
func ParseOneTeamID(ctx *cli.Context) (res keybase1.TeamID, err error) {
if len(ctx.Args()) == 0 {
return "", errors.New("team ID argument required")
}
if len(ctx.Args()) > 1 {
return "", errors.New("one team ID argument required, multiple found")
}
return keybase1.TeamIDFromString(ctx.Args()[0])
}
func ParseUser(ctx *cli.Context) (string, error) {
username := ctx.String("user")
if len(username) == 0 {
return "", errors.New("username required via --user flag")
}
return username, nil
}
func ParseRole(ctx *cli.Context) (keybase1.TeamRole, error) {
srole := ctx.String("role")
if srole == "" {
return 0, errors.New("team role required via --role flag")
}
role, ok := keybase1.TeamRoleMap[strings.ToUpper(srole)]
if !ok {
return 0, errors.New("invalid team role, please use owner, admin, writer, reader, bot, or restrictedbot")
}
return role, nil
}
func ParseUserAndRole(ctx *cli.Context) (string, keybase1.TeamRole, error) {
username, err := ParseUser(ctx)
if err != nil {
return "", 0, err
}
role, err := ParseRole(ctx)
if err != nil {
return "", 0, err
}
return username, role, nil
}
var botSettingsFlags = []cli.Flag{
cli.BoolFlag{
Name: "allow-commands",
Usage: "Restricted bots will receive messages that begin with commands they support.",
},
cli.BoolFlag{
Name: "allow-mentions",
Usage: "Restricted bots will receive messages when they are @-mentioned.",
},
cli.StringSliceFlag{
Name: "allow-trigger",
Usage: `Restricted bots will receive messages that match the given text.
Can be a regular expression. Can be specified multiple times.`,
},
cli.StringSliceFlag{
Name: "allow-conversation",
Usage: `Restricted bots will only be able to send/receive messages in the given conversations.
If not specified all conversations are allowed. Can be specified multiple times.`,
},
}
func ParseBotSettings(ctx *cli.Context) *keybase1.TeamBotSettings {
if !(ctx.IsSet("allow-commands") ||
ctx.IsSet("allow-mentions") ||
ctx.IsSet("allow-trigger") ||
ctx.IsSet("allow-conversation")) {
return nil
}
return &keybase1.TeamBotSettings{
Cmds: ctx.Bool("allow-commands"),
Mentions: ctx.Bool("allow-mentions"),
Triggers: ctx.StringSlice("allow-trigger"),
Convs: ctx.StringSlice("allow-conversation"),
}
}
func ValidateBotSettingsConvs(g *libkb.GlobalContext, tlfName string,
mt chat1.ConversationMembersType, botSettings *keybase1.TeamBotSettings) error {
if botSettings == nil {
return nil
}
convIDs, err := lookupConvIDsByTopicName(g, tlfName, mt, botSettings.Convs)
if err != nil {
return err
}
var convs []string
for _, convID := range convIDs {
convs = append(convs, convID.String())
}
botSettings.Convs = convs
return nil
}
func lookupConvIDsByTopicName(g *libkb.GlobalContext, tlfName string,
mt chat1.ConversationMembersType, convs []string) (convIDs []chat1.ConvIDStr, err error) {
resolver, err := newChatConversationResolver(g)
if err != nil {
return nil, err
}
for _, topicName := range convs {
topicName = utils.SanitizeTopicName(topicName)
conv, _, err := resolver.Resolve(context.TODO(), chatConversationResolvingRequest{
TlfName: tlfName,
TopicName: topicName,
TopicType: chat1.TopicType_CHAT,
MembersType: mt,
}, chatConversationResolvingBehavior{
IdentifyBehavior: keybase1.TLFIdentifyBehavior_CHAT_CLI,
})
if err != nil {
return nil, err
}
if conv == nil {
return nil, fmt.Errorf("conversation %s not found", topicName)
}
convIDs = append(convIDs, conv.GetConvID().ConvIDStr())
}
return convIDs, nil
}