Browse Source

dxb@keybase's changes

pull/1/head
Gregory Rudolph 5 years ago
parent
commit
bebb14a1dc
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 120
      main.go

120
main.go

@ -2,28 +2,31 @@ package main
import ( import (
"fmt" "fmt"
"github.com/jroimartin/gocui"
"log" "log"
"samhofi.us/x/keybase"
"strings" "strings"
"time" "time"
"github.com/jroimartin/gocui"
"samhofi.us/x/keybase"
) )
var k = keybase.NewKeybase() var k = keybase.NewKeybase()
var teamOrUser = "home" var channel keybase.Channel
var channel = ""
var myUsername = ""
func main() { func main() {
if k.LoggedIn == true { if !k.LoggedIn {
fmt.Println("You are not logged in.")
return
}
kbtui, err := gocui.NewGui(gocui.OutputNormal) kbtui, err := gocui.NewGui(gocui.OutputNormal)
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
defer kbtui.Close() defer kbtui.Close()
kbtui.SetManagerFunc(layout) kbtui.SetManagerFunc(layout)
go loginGreeter(kbtui)
printToView(kbtui, "Chat", fmt.Sprintf("Welcome %s!", k.Username))
go populateList(kbtui) go populateList(kbtui)
go updateChatWindow(kbtui) go updateChatWindow(kbtui)
if err := initKeybindings(kbtui); err != nil { if err := initKeybindings(kbtui); err != nil {
@ -32,30 +35,13 @@ func main() {
if err := kbtui.MainLoop(); err != nil && err != gocui.ErrQuit { if err := kbtui.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err) log.Panicln(err)
} }
} else {
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)
} }
func sendToTeam(msg string) { func sendChat(message string) {
chat := k.NewChat(keybase.Channel{ chat := k.NewChat(channel)
Name: teamOrUser, chat.Send(message)
MembersType: "team",
TopicName: channel,
})
chat.Send(msg)
} }
func populateList(g *gocui.Gui) { func populateList(g *gocui.Gui) {
for { for {
if testVar, err := k.ChatList(); err != nil { if testVar, err := k.ChatList(); err != nil {
@ -91,7 +77,7 @@ func printToView(kbtui *gocui.Gui, viewName string, message string) {
if err != nil { if err != nil {
return err return err
} else { } else {
fmt.Fprintf(updatingView, message + "\n") fmt.Fprintf(updatingView, message+"\n")
} }
return nil return nil
}) })
@ -138,10 +124,12 @@ func layout(g *gocui.Gui) error {
} }
return nil return nil
} }
func getInputString(g *gocui.Gui) (string, error) { func getInputString(g *gocui.Gui) (string, error) {
inputView, _ := g.View("Input") inputView, _ := g.View("Input")
return inputView.Line(0) return inputView.Line(0)
} }
func initKeybindings(g *gocui.Gui) error { func initKeybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error { func(g *gocui.Gui, v *gocui.View) error {
@ -157,6 +145,7 @@ func initKeybindings(g *gocui.Gui) error {
} }
return nil return nil
} }
func updateChatWindow(g *gocui.Gui) { func updateChatWindow(g *gocui.Gui) {
k.Run(func(api keybase.ChatAPI) { k.Run(func(api keybase.ChatAPI) {
handleMessage(api, g) 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) { func handleMessage(api keybase.ChatAPI, g *gocui.Gui) {
if api.Msg.Content.Type == "text" { if api.Msg.Content.Type == "text" {
if strings.Contains(api.Msg.Content.Text.Body, myUsername) || strings.Contains(api.Msg.Channel.Name, myUsername) { msgBody := api.Msg.Content.Text.Body
if api.Msg.Sender.Username != myUsername { msgSender := api.Msg.Sender.Username
message := api.Msg.Content.Text.Body channelName := api.Msg.Channel.Name
sender := api.Msg.Sender.Username if api.Msg.Channel.MembersType == keybase.TEAM {
team := api.Msg.Channel.Name topicName := api.Msg.Channel.TopicName
channel := api.Msg.Channel.TopicName for _, m := range api.Msg.Content.Text.UserMentions {
printMe := "@" + team + "#" + channel + " " + sender + ": " + message if m.Text == k.Username {
printToView(g, "Feed", printMe) printToView(g, "Feed", fmt.Sprintf("[ %s#%s ] %s: %s", channelName, topicName, msgSender, msgBody))
break
} }
} }
if strings.Contains(api.Msg.Channel.Name, teamOrUser) {
if channel != "" && api.Msg.Channel.TopicName == channel {
} else { } else {
message := api.Msg.Content.Text.Body printToView(g, "Feed", fmt.Sprintf("[ %s ] %s: %s", channelName, msgSender, msgBody))
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 { func handleInput(g *gocui.Gui) error {
inputString, _ := getInputString(g) inputString, _ := getInputString(g)
command := "" command := strings.Split(inputString, " ")
if len(inputString) > 2 {
command = inputString[:2] switch strings.ToLower(command[0]) {
} case "/q":
if "/q" == command {
return gocui.ErrQuit return gocui.ErrQuit
} else if "/j" == command { case "/j":
clearView(g, "Chat") clearView(g, "Chat")
arr := strings.Split(inputString, " ") if len(command) == 3 {
if len(arr) > 2 { channel.MembersType = keybase.TEAM
teamOrUser = arr[1] channel.Name = command[1]
channel = arr[2] channel.TopicName = command[2]
printToView(g, "Feed", "You have joined: @" + teamOrUser + "#" + channel) 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 { } else {
teamOrUser = arr[1] printToView(g, "Feed", "To join a team use /j <team> <channel>")
channel = "" printToView(g, "Feed", "To join a PM use /j <user>")
printToView(g, "Feed", "You have joined: @" + teamOrUser)
}
} else {
if channel == "" {
sendToUser(inputString)
} else {
sendToTeam(inputString)
} }
default:
sendChat(inputString)
} }
clearView(g, "Input") clearView(g, "Input")
return nil return nil
} }
func quit(g *gocui.Gui, v *gocui.View) error { func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit return gocui.ErrQuit
} }

Loading…
Cancel
Save