forked from cwtch.im/cwtch
90 lines
1.9 KiB
Go
90 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
"cwtch.im/cwtch/peer"
|
|
"github.com/marcusolsson/tui-go"
|
|
"io/ioutil"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
peer := peer.NewCwtchPeer("cwtchbot")
|
|
groupID, err := peer.ImportGroup(os.Args[1])
|
|
peer.AcceptInvite(groupID)
|
|
group := peer.Profile.GetGroupByGroupID(groupID)
|
|
peer.JoinServer(group.GroupServer)
|
|
|
|
history := tui.NewVBox()
|
|
historyScroll := tui.NewScrollArea(history)
|
|
historyScroll.SetAutoscrollToBottom(true)
|
|
|
|
historyBox := tui.NewVBox(historyScroll)
|
|
historyBox.SetBorder(true)
|
|
historyBox.SetTitle("cwtchat:" + group.GroupID + "@" + group.GroupServer)
|
|
|
|
input := tui.NewEntry()
|
|
input.SetFocused(true)
|
|
input.SetSizePolicy(tui.Expanding, tui.Maximum)
|
|
|
|
inputBox := tui.NewHBox(input)
|
|
inputBox.SetBorder(true)
|
|
inputBox.SetSizePolicy(tui.Expanding, tui.Maximum)
|
|
|
|
chat := tui.NewVBox(historyBox, inputBox)
|
|
chat.SetSizePolicy(tui.Expanding, tui.Expanding)
|
|
|
|
input.OnSubmit(func(e *tui.Entry) {
|
|
if strings.Trim(e.Text(), " ") != "" {
|
|
go peer.SendMessageToGroup(groupID, e.Text())
|
|
e.SetText("")
|
|
}
|
|
})
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
root := tui.NewHBox(chat)
|
|
|
|
ui, err := tui.New(root)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
ui.SetKeybinding("Esc", func() { ui.Quit() })
|
|
|
|
go func() {
|
|
for {
|
|
group = peer.Profile.GetGroupByGroupID(groupID)
|
|
if group == nil {
|
|
log.Fatalf("group does not exist")
|
|
}
|
|
timeline := group.GetTimeline()
|
|
for _, m := range timeline {
|
|
old, _ := seen[string(m.Signature)]
|
|
if !old {
|
|
|
|
ui.Update(func() {
|
|
history.Append(tui.NewHBox(
|
|
tui.NewLabel(m.Timestamp.Format("15:04")),
|
|
tui.NewPadder(1, 0, tui.NewLabel(fmt.Sprintf("<%s>", m.PeerID))),
|
|
tui.NewLabel(m.Message),
|
|
tui.NewSpacer(),
|
|
))
|
|
})
|
|
seen[string(m.Signature)] = true
|
|
}
|
|
}
|
|
time.Sleep(time.Second * 1)
|
|
}
|
|
}()
|
|
|
|
if err := ui.Run(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|