-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_chat_send.go
221 lines (198 loc) · 6.15 KB
/
cmd_chat_send.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
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2016 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"io"
"os"
"time"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/chat/globals"
"github.com/keybase/client/go/chat/msgchecker"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
isatty "github.com/mattn/go-isatty"
)
type CmdChatSend struct {
libkb.Contextified
resolvingRequest chatConversationResolvingRequest
// Only one of these should be set
message string
setHeadline string
ephemeralLifetime time.Duration
clearHeadline bool
hasTTY bool
nonBlock bool
team bool
}
func NewCmdChatSendRunner(g *libkb.GlobalContext) *CmdChatSend {
return &CmdChatSend{
Contextified: libkb.NewContextified(g),
}
}
func (c *CmdChatSend) SetTeamChatForTest(n string) {
c.team = true
c.resolvingRequest = chatConversationResolvingRequest{
TlfName: n,
TopicName: globals.DefaultTeamTopic,
MembersType: chat1.ConversationMembersType_TEAM,
TopicType: chat1.TopicType_CHAT,
Visibility: keybase1.TLFVisibility_PRIVATE,
}
}
func (c *CmdChatSend) SetMessage(m string) {
c.message = m
}
func newCmdChatSend(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
flags := append(getConversationResolverFlags(),
mustGetChatFlags("set-headline", "clear-headline", "nonblock", "exploding-lifetime")...,
)
return cli.Command{
Name: "send",
Usage: "Send a message to a conversation",
ArgumentHelp: "[conversation [message]]",
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdChatSendRunner(g), "send", c)
cl.SetNoStandalone()
cl.SetLogForward(libcmdline.LogForwardNone)
},
Flags: flags,
}
}
func (c *CmdChatSend) Run() (err error) {
ui := NewChatCLIUI(c.G())
protocols := []rpc.Protocol{
chat1.ChatUiProtocol(ui),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
// if no tlfname specified, request one
if c.resolvingRequest.TlfName == "" {
c.resolvingRequest.TlfName, err = c.G().UI.GetTerminalUI().Prompt(PromptDescriptorEnterChatTLFName,
"Specify a team name, a single receiving user, or a comma-separated list of users (e.g. alice,bob,charlie) to continue: ")
if err != nil {
return err
}
if c.resolvingRequest.TlfName == "" {
return fmt.Errorf("no user or team name specified")
}
}
if err = annotateResolvingRequest(c.G(), &c.resolvingRequest); err != nil {
return err
}
// TLFVisibility_ANY doesn't make any sense for send, so switch that to PRIVATE:
if c.resolvingRequest.Visibility == keybase1.TLFVisibility_ANY {
c.resolvingRequest.Visibility = keybase1.TLFVisibility_PRIVATE
}
// Verify we can continue with the current options, this will return an
// error if you try to send to a KBFS chat or have --public set and
// ephemeralLifetime.
if c.ephemeralLifetime > 0 {
if c.resolvingRequest.Visibility == keybase1.TLFVisibility_PUBLIC {
return fmt.Errorf("Cannot send ephemeral messages with --public set.")
}
if c.resolvingRequest.MembersType == chat1.ConversationMembersType_KBFS {
return fmt.Errorf("Cannot send ephemeral messages to a KBFS type chat.")
}
}
if err := CheckAndStartStandaloneChat(c.G(), c.resolvingRequest.MembersType); err != nil {
return err
}
return chatSend(context.TODO(), c.G(), ChatSendArg{
resolvingRequest: c.resolvingRequest,
message: c.message,
setHeadline: c.setHeadline,
clearHeadline: c.clearHeadline,
hasTTY: c.hasTTY,
nonBlock: c.nonBlock,
team: c.team,
setTopicName: "",
ephemeralLifetime: c.ephemeralLifetime,
})
}
func (c *CmdChatSend) ParseArgv(ctx *cli.Context) (err error) {
c.setHeadline = ctx.String("set-headline")
c.clearHeadline = ctx.Bool("clear-headline")
c.hasTTY = isatty.IsTerminal(os.Stdin.Fd())
c.nonBlock = ctx.Bool("nonblock")
c.ephemeralLifetime = ctx.Duration("exploding-lifetime")
var tlfName string
// Get the TLF name from the first position arg
if len(ctx.Args()) >= 1 {
tlfName = ctx.Args().Get(0)
}
if c.resolvingRequest, err = parseConversationResolvingRequest(ctx, tlfName); err != nil {
return err
}
nActions := 0
if c.setHeadline != "" {
nActions++
if !c.hasTTY {
return fmt.Errorf("stdin not supported with --set-headline")
}
if len(ctx.Args()) > 1 {
return fmt.Errorf("cannot send message and set headline name simultaneously")
}
}
if c.clearHeadline {
nActions++
if !c.hasTTY {
return fmt.Errorf("stdin not supported with --clear-headline")
}
if len(ctx.Args()) > 1 {
return fmt.Errorf("cannot send message and clear headline name simultaneously")
}
}
if c.ephemeralLifetime != 0 {
if c.ephemeralLifetime > libkb.MaxEphemeralContentLifetime {
return fmt.Errorf("ephemeral lifetime cannot exceed %v", libkb.MaxEphemeralContentLifetime)
}
if c.ephemeralLifetime < libkb.MinEphemeralContentLifetime {
return fmt.Errorf("ephemeral lifetime must be at least %v", libkb.MinEphemeralContentLifetime)
}
}
// Send a normal message.
if nActions == 0 {
nActions++
switch len(ctx.Args()) {
case 2:
// message is supplied, so stdin is ignored even if piped
c.message = ctx.Args().Get(1)
case 1:
if !c.hasTTY {
bytes, err := io.ReadAll(io.LimitReader(os.Stdin, msgchecker.TextMessageMaxLength))
if err != nil {
return err
}
c.message = string(bytes)
} else {
c.message = "" // get message through prompt later
}
case 0:
if !c.hasTTY {
return fmt.Errorf("need exactly 1 argument to send from stdin")
}
c.message = "" // get message through prompt later
default:
return fmt.Errorf("chat send takes 0, 1 or 2 args")
}
}
if nActions < 1 {
return fmt.Errorf("incorrect usage")
}
if nActions > 1 {
return fmt.Errorf("only one of message, --set-headline, --clear-headline allowed")
}
return nil
}
func (c *CmdChatSend) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}