forked from keybase/go-keybase-chat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kbchat.go
441 lines (387 loc) · 10.1 KB
/
kbchat.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package kbchat
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os/exec"
"os"
"sync"
"time"
"github.com/malware-unicorn/keybase-bot-api/kbapi"
"github.com/malware-unicorn/go-keybase-chat-bot/kbchat/types/chat1"
"github.com/malware-unicorn/go-keybase-chat-bot/kbchat/types/stellar1"
)
// API is the main object used for communicating with the Keybase JSON API
type API struct {
sync.Mutex
apiInput io.Writer
apiOutput io.Reader
kb *kbapi.Kbapi
apiCmd *exec.Cmd
username string
runOpts RunOptions
subscriptions []*NewSubscription
pipeW *os.File
pipeR *os.File
}
type OneshotOptions struct {
Username string
PaperKey string
}
type RunOptions struct {
KeybaseLocation string
HomeDir string
Oneshot *OneshotOptions
StartService bool
// Have the bot send/receive typing notifications
EnableTyping bool
// Disable bot lite mode
DisableBotLiteMode bool
}
func (r RunOptions) Location() string {
if r.KeybaseLocation == "" {
return "keybase"
}
return r.KeybaseLocation
}
func (r RunOptions) Command(args ...string) *exec.Cmd {
var cmd []string
if r.HomeDir != "" {
cmd = append(cmd, "--home", r.HomeDir)
}
cmd = append(cmd, args...)
return exec.Command(r.Location(), cmd...)
}
// Start fires up the Keybase JSON API in stdin/stdout mode
func Start(runOpts RunOptions) (*API, error) {
api := &API{
runOpts: runOpts,
kb: kbapi.NewKbApi(),
}
if err := api.startPipes(); err != nil {
api.pipeW.Close()
api.pipeR.Close()
return nil, err
}
return api, nil
}
func (a *API) Command(args ...string) *exec.Cmd {
return a.runOpts.Command(args...)
}
func (a *API) auth() (string, error) {
username := a.kb.GetUsername()
fmt.Printf("%s\n", username)
if username != "" {
return username, nil
}
if a.runOpts.Oneshot == nil {
a.runOpts.Oneshot = &OneshotOptions{
Username: os.Getenv("KEYBASE_USERNAME"),
PaperKey: os.Getenv("KEYBASE_PAPERKEY"),
}
}
username = ""
// If a paper key is specified, then login with oneshot mode (logout first)
if a.runOpts.Oneshot != nil {
if username == a.runOpts.Oneshot.Username {
// just get out if we are on the desired user already
return username, nil
}
if err:= kbapi.Logout(a.kb.GetGlobalContext(), true); err != nil {
return "", err
}
if err:= kbapi.LoginOneshot(a.kb.GetGlobalContext(), a.runOpts.Oneshot.Username, a.runOpts.Oneshot.PaperKey); err != nil {
return "", err
}
username = a.runOpts.Oneshot.Username
return username, nil
}
return "", errors.New("unable to auth")
}
func (a *API) startPipes() (err error) {
a.Lock()
defer a.Unlock()
a.apiCmd = nil
if a.runOpts.StartService {
args := []string{fmt.Sprintf("-enable-bot-lite-mode=%v", a.runOpts.DisableBotLiteMode), "service"}
if err := a.runOpts.Command(args...).Start(); err != nil {
return err
}
}
if a.username, err = a.auth(); err != nil {
return err
}
// Disable Typing
if err := kbapi.SetChatSettings(a.kb.GetGlobalContext()); err != nil {
return err
}
pipeR, pipeW, _ := os.Pipe()
a.pipeW = pipeW
a.pipeR = pipeR
return nil
}
var errAPIDisconnected = errors.New("chat API disconnected")
func (a *API) getAPIPipesLocked() (io.Writer, io.Reader, error) {
// this should only be called inside a lock
if a.apiCmd == nil {
return nil, nil, errAPIDisconnected
}
return a.apiInput, a.apiOutput, nil
}
func (a *API) GetUsername() string {
return a.username
}
func (a *API) doSend(arg interface{}) (resp SendResponse, err error) {
a.Lock()
defer a.Unlock()
bArg, err := json.Marshal(arg)
if err != nil {
return SendResponse{}, err
}
responseRaw, err := a.kb.SendChatApi(string(bArg))
if err != nil {
return SendResponse{}, err
}
if err := json.Unmarshal(responseRaw, &resp); err != nil {
return resp, fmt.Errorf("failed to decode API response: %s", err)
} else if resp.Error != nil {
return resp, errors.New(resp.Error.Message)
}
return resp, nil
}
func (a *API) doFetch(apiInput string) ([]byte, error) {
a.Lock()
defer a.Unlock()
byteOutput, err := a.kb.SendChatApi(apiInput)
if err != nil {
return nil, err
}
return byteOutput, nil
}
// SubscriptionMessage contains a message and conversation object
type SubscriptionMessage struct {
Message chat1.MsgSummary
Conversation chat1.ConvSummary
}
type SubscriptionConversation struct {
Conversation chat1.ConvSummary
}
type SubscriptionWalletEvent struct {
Payment stellar1.PaymentDetailsLocal
}
// NewSubscription has methods to control the background message fetcher loop
type NewSubscription struct {
sync.Mutex
newMsgsCh <-chan SubscriptionMessage
newConvsCh <-chan SubscriptionConversation
newWalletCh <-chan SubscriptionWalletEvent
errorCh <-chan error
running bool
shutdownCh chan struct{}
}
// Read blocks until a new message arrives
func (m *NewSubscription) Read() (SubscriptionMessage, error) {
select {
case msg := <-m.newMsgsCh:
return msg, nil
case err := <-m.errorCh:
return SubscriptionMessage{}, err
case <-m.shutdownCh:
return SubscriptionMessage{}, errors.New("Subscription shutdown")
}
}
func (m *NewSubscription) ReadNewConvs() (SubscriptionConversation, error) {
select {
case conv := <-m.newConvsCh:
return conv, nil
case err := <-m.errorCh:
return SubscriptionConversation{}, err
case <-m.shutdownCh:
return SubscriptionConversation{}, errors.New("Subscription shutdown")
}
}
// Read blocks until a new message arrives
func (m *NewSubscription) ReadWallet() (SubscriptionWalletEvent, error) {
select {
case msg := <-m.newWalletCh:
return msg, nil
case err := <-m.errorCh:
return SubscriptionWalletEvent{}, err
case <-m.shutdownCh:
return SubscriptionWalletEvent{}, errors.New("Subscription shutdown")
}
}
// Shutdown terminates the background process
func (m *NewSubscription) Shutdown() {
m.Lock()
defer m.Unlock()
if m.running {
close(m.shutdownCh)
m.running = false
}
}
type ListenOptions struct {
Wallet bool
Convs bool
}
type PaymentHolder struct {
Payment stellar1.PaymentDetailsLocal `json:"notification"`
}
type TypeHolder struct {
Type string `json:"type"`
}
// ListenForNewTextMessages proxies to Listen without wallet events
func (a *API) ListenForNewTextMessages() (*NewSubscription, error) {
opts := ListenOptions{Wallet: false}
return a.Listen(opts)
}
func (a *API) registerSubscription(sub *NewSubscription) {
a.Lock()
defer a.Unlock()
a.subscriptions = append(a.subscriptions, sub)
}
// Listen fires of a background loop and puts chat messages and wallet
// events into channels
func (a *API) Listen(opts ListenOptions) (*NewSubscription, error) {
newMsgsCh := make(chan SubscriptionMessage, 100)
newConvsCh := make(chan SubscriptionConversation, 100)
newWalletCh := make(chan SubscriptionWalletEvent, 100)
errorCh := make(chan error, 100)
shutdownCh := make(chan struct{})
done := make(chan struct{})
sub := &NewSubscription{
newMsgsCh: newMsgsCh,
newConvsCh: newConvsCh,
newWalletCh: newWalletCh,
shutdownCh: shutdownCh,
errorCh: errorCh,
running: true,
}
a.registerSubscription(sub)
pause := 2 * time.Second
readScanner := func() {
for {
buff, err := a.kb.ReadListener(a.pipeR)
if err != nil {
errorCh <- err
break
}
var typeHolder TypeHolder
if err := json.Unmarshal(buff, &typeHolder); err != nil {
errorCh <- err
break
}
switch typeHolder.Type {
case "chat":
var notification chat1.MsgNotification
if err := json.Unmarshal(buff, ¬ification); err != nil {
errorCh <- err
break
}
if notification.Error != nil {
log.Printf("error message received: %s", *notification.Error)
} else if notification.Msg != nil {
subscriptionMessage := SubscriptionMessage{
Message: *notification.Msg,
Conversation: chat1.ConvSummary{
Id: notification.Msg.ConvID,
Channel: notification.Msg.Channel,
},
}
newMsgsCh <- subscriptionMessage
}
case "chat_conv":
var notification chat1.ConvNotification
if err := json.Unmarshal(buff, ¬ification); err != nil {
errorCh <- err
break
}
if notification.Error != nil {
log.Printf("error message received: %s", *notification.Error)
} else if notification.Conv != nil {
subscriptionConv := SubscriptionConversation{
Conversation: *notification.Conv,
}
newConvsCh <- subscriptionConv
}
case "wallet":
var holder PaymentHolder
if err := json.Unmarshal(buff, &holder); err != nil {
errorCh <- err
break
}
subscriptionPayment := SubscriptionWalletEvent(holder)
newWalletCh <- subscriptionPayment
default:
continue
}
}
done <- struct{}{}
}
attempts := 0
maxAttempts := 1800
go func() {
for {
select {
case <-shutdownCh:
log.Printf("Listen: received shutdown")
return
default:
}
if attempts >= maxAttempts {
panic("Listen: failed to auth, giving up")
}
attempts++
if err := a.kb.StartChatApiListener(a.pipeW); err != nil {
log.Printf("Listen: failed to make listen scanner: %s", err)
time.Sleep(pause)
continue
}
attempts = 0
<-done
time.Sleep(pause)
}
}()
go readScanner()
return sub, nil
}
func (a *API) LogSend(feedback string) error {
feedback = "go-keybase-chat-bot log send\n" +
"username: " + a.GetUsername() + "\n" +
feedback
args := []string{
"log", "send",
"--no-confirm",
"--feedback", feedback,
}
// We're determining whether the service is already running by running status
// with autofork disabled.
if err := a.runOpts.Command("--no-auto-fork", "status"); err != nil {
// Assume that there's no service running, so log send as standalone
args = append([]string{"--standalone"}, args...)
}
return a.runOpts.Command(args...).Run()
}
func (a *API) Shutdown() error {
a.Lock()
defer a.Unlock()
a.pipeW.Close()
a.pipeR.Close()
for _, sub := range a.subscriptions {
sub.Shutdown()
}
if a.runOpts.Oneshot != nil {
if err:= kbapi.Logout(a.kb.GetGlobalContext(), true); err != nil {
return err
}
}
if a.runOpts.StartService {
err := kbapi.CtlStop(a.kb.GetGlobalContext(), true)
if err != nil {
return err
}
}
return nil
}