From bebb14a1dce08164950cebf1acbb6f0acc861bab Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Tue, 17 Sep 2019 20:48:38 -0400 Subject: [PATCH] dxb@keybase's changes --- main.go | 150 +++++++++++++++++++++++++++----------------------------- 1 file changed, 71 insertions(+), 79 deletions(-) diff --git a/main.go b/main.go index cd2e33f..f2b93b5 100644 --- a/main.go +++ b/main.go @@ -2,60 +2,46 @@ package main import ( "fmt" - "github.com/jroimartin/gocui" "log" - "samhofi.us/x/keybase" "strings" "time" + + "github.com/jroimartin/gocui" + "samhofi.us/x/keybase" ) var k = keybase.NewKeybase() -var teamOrUser = "home" -var channel = "" -var myUsername = "" +var channel keybase.Channel func main() { - if k.LoggedIn == true { - kbtui, err := gocui.NewGui(gocui.OutputNormal) - if err != nil { - log.Panicln(err) - } - defer kbtui.Close() - - kbtui.SetManagerFunc(layout) - go loginGreeter(kbtui) - go populateList(kbtui) - go updateChatWindow(kbtui) - if err := initKeybindings(kbtui); err != nil { - log.Fatalln(err) - } - if err := kbtui.MainLoop(); err != nil && err != gocui.ErrQuit { - log.Panicln(err) - } - } else { + if !k.LoggedIn { fmt.Println("You are not logged in.") return } -} -func loginGreeter(g *gocui.Gui) { - myUsername = k.Username - messg := "Welcome " + myUsername+ "!" - printToView(g, "Chat", messg) -} -func sendToUser(msg string) { - chat := k.NewChat(keybase.Channel{Name:teamOrUser}) - chat.Send(msg) + kbtui, err := gocui.NewGui(gocui.OutputNormal) + if err != nil { + log.Panicln(err) + } + defer kbtui.Close() + kbtui.SetManagerFunc(layout) + + printToView(kbtui, "Chat", fmt.Sprintf("Welcome %s!", k.Username)) + go populateList(kbtui) + go updateChatWindow(kbtui) + if err := initKeybindings(kbtui); err != nil { + log.Fatalln(err) + } + if err := kbtui.MainLoop(); err != nil && err != gocui.ErrQuit { + log.Panicln(err) + } } -func sendToTeam(msg string) { - chat := k.NewChat(keybase.Channel{ - Name: teamOrUser, - MembersType: "team", - TopicName: channel, - }) - chat.Send(msg) +func sendChat(message string) { + chat := k.NewChat(channel) + chat.Send(message) } + func populateList(g *gocui.Gui) { for { if testVar, err := k.ChatList(); err != nil { @@ -91,7 +77,7 @@ func printToView(kbtui *gocui.Gui, viewName string, message string) { if err != nil { return err } else { - fmt.Fprintf(updatingView, message + "\n") + fmt.Fprintf(updatingView, message+"\n") } return nil }) @@ -123,9 +109,9 @@ func layout(g *gocui.Gui) error { if err3 != gocui.ErrUnknownView { return err3 } - if _, err := g.SetCurrentView("Input"); err != nil { - return err - } + if _, err := g.SetCurrentView("Input"); err != nil { + return err + } inputView.Editable = true inputView.Wrap = true } @@ -138,10 +124,12 @@ func layout(g *gocui.Gui) error { } return nil } + func getInputString(g *gocui.Gui) (string, error) { inputView, _ := g.View("Input") return inputView.Line(0) } + func initKeybindings(g *gocui.Gui) error { if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error { @@ -157,6 +145,7 @@ func initKeybindings(g *gocui.Gui) error { } return nil } + func updateChatWindow(g *gocui.Gui) { k.Run(func(api keybase.ChatAPI) { handleMessage(api, g) @@ -164,59 +153,62 @@ func updateChatWindow(g *gocui.Gui) { } +func cleanChannelName(c string) string { + newChannelName := strings.Replace(c, fmt.Sprintf("%s,", k.Username), "", 1) + return strings.Replace(newChannelName, fmt.Sprintf(",%s", k.Username), "", 1) +} + func handleMessage(api keybase.ChatAPI, g *gocui.Gui) { if api.Msg.Content.Type == "text" { - if strings.Contains(api.Msg.Content.Text.Body, myUsername) || strings.Contains(api.Msg.Channel.Name, myUsername) { - if api.Msg.Sender.Username != myUsername { - message := api.Msg.Content.Text.Body - sender := api.Msg.Sender.Username - team := api.Msg.Channel.Name - channel := api.Msg.Channel.TopicName - printMe := "@" + team + "#" + channel + " " + sender + ": " + message - printToView(g, "Feed", printMe) + msgBody := api.Msg.Content.Text.Body + msgSender := api.Msg.Sender.Username + channelName := api.Msg.Channel.Name + if api.Msg.Channel.MembersType == keybase.TEAM { + topicName := api.Msg.Channel.TopicName + for _, m := range api.Msg.Content.Text.UserMentions { + if m.Text == k.Username { + printToView(g, "Feed", fmt.Sprintf("[ %s#%s ] %s: %s", channelName, topicName, msgSender, msgBody)) + break + } } + } else { + printToView(g, "Feed", fmt.Sprintf("[ %s ] %s: %s", channelName, msgSender, msgBody)) } - if strings.Contains(api.Msg.Channel.Name, teamOrUser) { - if channel != "" && api.Msg.Channel.TopicName == channel { - - } else { - message := api.Msg.Content.Text.Body - sender := api.Msg.Sender.Username - printToView(g, "Chat", sender+": "+message) - } + if api.Msg.Channel.MembersType == channel.MembersType && cleanChannelName(api.Msg.Channel.Name) == channel.Name { + printToView(g, "Chat", fmt.Sprintf("%s: %s", msgSender, msgBody)) } } } + func handleInput(g *gocui.Gui) error { inputString, _ := getInputString(g) - command := "" - if len(inputString) > 2 { - command = inputString[:2] - } - if "/q" == command { + command := strings.Split(inputString, " ") + + switch strings.ToLower(command[0]) { + case "/q": return gocui.ErrQuit - } else if "/j" == command { + case "/j": clearView(g, "Chat") - arr := strings.Split(inputString, " ") - if len(arr) > 2 { - teamOrUser = arr[1] - channel = arr[2] - printToView(g, "Feed", "You have joined: @" + teamOrUser + "#" + channel) - } else { - teamOrUser = arr[1] - channel = "" - printToView(g, "Feed", "You have joined: @" + teamOrUser) - } - } else { - if channel == "" { - sendToUser(inputString) + if len(command) == 3 { + channel.MembersType = keybase.TEAM + channel.Name = command[1] + channel.TopicName = command[2] + printToView(g, "Feed", fmt.Sprintf("You have joined: @%s#%s", channel.Name, channel.TopicName)) + } else if len(command) == 2 { + channel.MembersType = keybase.USER + channel.Name = command[1] + printToView(g, "Feed", fmt.Sprintf("You have joined: @%s", channel.Name)) } else { - sendToTeam(inputString) + printToView(g, "Feed", "To join a team use /j ") + printToView(g, "Feed", "To join a PM use /j ") } + default: + sendChat(inputString) } clearView(g, "Input") return nil } + func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit }