/
cmd_chat_send.go
191 lines (170 loc) · 4.85 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
// Copyright 2016 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"io"
"io/ioutil"
"os"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/chat"
"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"
isatty "github.com/mattn/go-isatty"
)
type CmdChatSend struct {
libkb.Contextified
resolvingRequest chatConversationResolvingRequest
// Only one of these should be set
message string
setHeadline string
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: chat.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 {
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()
},
Flags: append(getConversationResolverFlags(),
mustGetChatFlags("set-headline", "clear-headline", "nonblock")...,
),
}
}
func (c *CmdChatSend) Run() (err error) {
if c.resolvingRequest.TlfName != "" {
err = annotateResolvingRequest(c.G(), &c.resolvingRequest)
if 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
}
// TODO: Right now this command cannot be run in standalone at
// all, even though team chats should work, but there is a bug
// in finding existing conversations.
if c.G().Standalone {
switch c.resolvingRequest.MembersType {
case chat1.ConversationMembersType_TEAM, chat1.ConversationMembersType_IMPTEAM:
c.G().StartStandaloneChat()
default:
err = CantRunInStandaloneError{}
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: "",
})
}
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")
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")
}
}
// 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 := ioutil.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:
cli.ShowCommandHelp(ctx, "send")
return fmt.Errorf("chat send takes 0, 1 or 2 args")
}
}
if nActions < 1 {
cli.ShowCommandHelp(ctx, "send")
return fmt.Errorf("Incorrect Usage.")
}
if nActions > 1 {
cli.ShowCommandHelp(ctx, "send")
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,
}
}