forked from keybase/client
/
cmd_chat_send.go
250 lines (216 loc) · 6.93 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2016 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"fmt"
"io/ioutil"
"os"
"golang.org/x/net/context"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
)
type cmdChatSend struct {
libkb.Contextified
resolver chatCLIConversationResolver
// Only one of these should be set
message string
setTopicName string
setHeadline string
clearHeadline bool
}
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(&cmdChatSend{Contextified: libkb.NewContextified(g)}, "send", c)
},
Flags: mustGetChatFlags(
"topic-type", "topic-name", "set-topic-name", "set-headline", "clear-headline", "stdin"),
}
}
func (c *cmdChatSend) Run() (err error) {
chatClient, err := GetChatLocalClient(c.G())
if err != nil {
return err
}
tlfClient, err := GetTlfClient(c.G())
if err != nil {
return err
}
ctx := context.TODO()
var conversationInfo chat1.ConversationInfoLocal
resolved, userChosen, err := c.resolver.Resolve(context.TODO(), c.G(), chatClient, tlfClient)
if err != nil {
return err
}
if resolved == nil {
if len(c.resolver.TlfName) == 0 {
c.G().UI.GetTerminalUI().Printf("No conversation found. Type `keybase chat send <tlf> [message]` to create a new one.\n")
return nil
}
// creating a new conversation!
if len(c.resolver.TopicName) > 0 && c.resolver.TopicType == chat1.TopicType_CHAT {
c.G().UI.GetTerminalUI().Printf("We are not supporting setting topic name for chat conversations yet.\n")
return nil
}
if c.resolver.TopicType == chat1.TopicType_CHAT {
c.G().UI.GetTerminalUI().Printf("Creating new %s conversation: %s\n", c.resolver.TopicType.String(), c.resolver.TlfName)
} else {
c.G().UI.GetTerminalUI().Printf("Creating new %s conversation [%s]: %s\n", c.resolver.TopicType.String(), c.resolver.TopicName, c.resolver.TlfName)
}
var tnp *string
if len(c.resolver.TopicName) > 0 {
tnp = &c.resolver.TopicName
}
ncres, err := chatClient.NewConversationLocal(ctx, chat1.NewConversationLocalArg{
TlfName: c.resolver.TlfName,
TopicName: tnp,
TopicType: c.resolver.TopicType,
TlfVisibility: c.resolver.Visibility,
})
if err != nil {
return fmt.Errorf("creating conversation error: %v\n", err)
}
conversationInfo = ncres.Conv.Info
} else {
conversationInfo = *resolved
}
var args chat1.PostLocalArg
args.ConversationID = conversationInfo.Id
var msg chat1.MessagePlaintext
// msgV1.ClientHeader.{Sender,SenderDevice} are filled by service
msg.ClientHeader.Conv = conversationInfo.Triple
msg.ClientHeader.TlfName = conversationInfo.TlfName
// Whether the user is really sure they want to send to the selected conversation.
// We require an additional confirmation if the choose menu was used.
confirmed := !userChosen
// Do one of set topic name, set headline, or send message
switch {
case c.setTopicName != "":
if conversationInfo.Triple.TopicType == chat1.TopicType_CHAT {
c.G().UI.GetTerminalUI().Printf("We are not supporting setting topic name for chat conversations yet. Ignoring --set-topic-name >.<\n")
return nil
}
msg.ClientHeader.MessageType = chat1.MessageType_METADATA
msg.MessageBody = chat1.NewMessageBodyWithMetadata(chat1.MessageConversationMetadata{ConversationTitle: c.setTopicName})
case c.setHeadline != "":
msg.ClientHeader.MessageType = chat1.MessageType_HEADLINE
msg.MessageBody = chat1.NewMessageBodyWithHeadline(chat1.MessageHeadline{Headline: c.setHeadline})
case c.clearHeadline:
msg.ClientHeader.MessageType = chat1.MessageType_HEADLINE
msg.MessageBody = chat1.NewMessageBodyWithHeadline(chat1.MessageHeadline{Headline: ""})
default:
// Ask for message contents
if len(c.message) == 0 {
promptText := "Please enter message content: "
if !confirmed {
promptText = fmt.Sprintf("Send to [%s]? Hit Ctrl-C to cancel, or enter message content to send: ", conversationInfo.TlfName)
}
c.message, err = c.G().UI.GetTerminalUI().Prompt(PromptDescriptorEnterChatMessage, promptText)
if err != nil {
return err
}
confirmed = true
}
msg.ClientHeader.MessageType = chat1.MessageType_TEXT
msg.MessageBody = chat1.NewMessageBodyWithText(chat1.MessageText{Body: c.message})
}
if !confirmed {
promptText := fmt.Sprintf("Send to [%s]? Hit Ctrl-C to cancel, or enter to send.", conversationInfo.TlfName)
c.message, err = c.G().UI.GetTerminalUI().Prompt(PromptDescriptorEnterChatMessage, promptText)
if err != nil {
return err
}
confirmed = true
}
args.Msg = msg
if _, err = chatClient.PostLocal(ctx, args); err != nil {
return err
}
return nil
}
func (c *cmdChatSend) ParseArgv(ctx *cli.Context) (err error) {
c.setTopicName = ctx.String("set-topic-name")
c.setHeadline = ctx.String("set-headline")
c.clearHeadline = ctx.Bool("clear-headline")
useStdin := ctx.Bool("stdin")
var tlfName string
// Get the TLF name from the first position arg
if len(ctx.Args()) >= 1 {
tlfName = ctx.Args().Get(0)
}
if c.resolver, err = parseConversationResolver(ctx, tlfName); err != nil {
return err
}
nActions := 0
if c.setTopicName != "" {
nActions++
if useStdin {
return fmt.Errorf("stdin not supported when setting topic name")
}
if len(ctx.Args()) > 1 {
return fmt.Errorf("cannot send message and set topic name simultaneously")
}
}
if c.setHeadline != "" {
nActions++
if useStdin {
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 useStdin {
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++
if useStdin {
if len(ctx.Args()) > 1 {
return fmt.Errorf("too many args for sending from stdin")
}
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
c.message = string(bytes)
} else {
switch len(ctx.Args()) {
case 0, 1:
c.message = ""
case 2:
c.message = ctx.Args().Get(1)
default:
cli.ShowCommandHelp(ctx, "send")
return fmt.Errorf("chat send takes 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, or --set-topic-name allowed")
}
return nil
}
func (c *cmdChatSend) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
}
}