cwtch/app/cli/main.go

385 lines
14 KiB
Go
Raw Normal View History

2018-04-30 21:47:21 +00:00
package main
import (
"fmt"
app2 "git.mascherari.press/cwtch/app"
2018-05-03 04:12:45 +00:00
"github.com/c-bata/go-prompt"
2018-05-03 06:01:15 +00:00
"strings"
"time"
2018-04-30 21:47:21 +00:00
)
2018-05-03 04:12:45 +00:00
var app app2.Application
2018-05-03 05:46:42 +00:00
var suggestions = []prompt.Suggest{
{Text: "new-profile", Description: "create a new profile"},
{Text: "load-profile", Description: "load a new profile"},
{Text: "quit", Description: "quit cwtch"},
2018-05-03 20:09:03 +00:00
{Text: "info", Description: "show user info"},
2018-05-03 05:46:42 +00:00
{Text: "servers", Description: "retrieve a list of servers and their connection status"},
{Text: "peers", Description: "retrieve a list of peers and their connection status"},
{Text: "contacts", Description: "retrieve a list of contacts"},
{Text: "groups", Description: "retrieve a list of groups"},
{Text: "send", Description: "send a message to a group"},
{Text: "timeline", Description: "read the timeline of a given group"},
{Text: "accept-invite", Description: "accept the invite of a group"},
{Text: "invite", Description: "invite a new contact"},
{Text: "invite-to-group", Description: "invite an existing contact to join an existing group"},
{Text: "new-group", Description: "create a new group"},
{Text: "help", Description: "print list of commands"},
{Text: "trust", Description: "trust a peer"},
{Text: "block", Description: "block a peer - you will no longer see messages or connect to this peer"},
2018-05-03 05:46:42 +00:00
}
2018-05-03 06:01:15 +00:00
var usages = map[string]string{
2018-05-03 20:09:03 +00:00
"new-profile": "new-profile [name] [filename]",
"load-profile": "load-profile [filename]",
2018-05-03 06:01:15 +00:00
"quit": "",
"servers": "",
"peers": "",
"contacts": "",
"groups": "",
"info": "",
2018-05-03 06:01:15 +00:00
"send": "send [groupid] [message]",
"timeline": "timeline [groupid]",
"accept-invite": "accept-invite [groupid]",
"invite": "invite [peerid]",
"invite-to-group": "invite-to-group [peerid] [groupid]",
2018-05-03 06:01:15 +00:00
"new-group": "new-group [server]",
"help": "",
"trust": "trust [peerid]",
"block": "block [peerid]",
2018-05-03 05:46:42 +00:00
}
2018-05-03 04:12:45 +00:00
func completer(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{}
if d.FindStartOfPreviousWord() == 0 {
2018-05-03 05:46:42 +00:00
return prompt.FilterHasPrefix(suggestions, d.GetWordBeforeCursor(), true)
2018-05-03 04:12:45 +00:00
}
w := d.CurrentLine()
if strings.HasPrefix(w, "send") || strings.HasPrefix(w, "timeline") {
s = []prompt.Suggest{}
groups := app.Peer.Profile.Groups
for _, group := range groups {
s = append(s, prompt.Suggest{Text: group.GroupID, Description: "Group owned by " + group.Owner + " on " + group.GroupServer})
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
if strings.HasPrefix(w, "block") || strings.HasPrefix(w, "trust") {
s = []prompt.Suggest{}
contacts := app.Peer.Profile.Contacts
for _, contact := range contacts {
s = append(s, prompt.Suggest{Text: contact.Onion, Description: contact.Name})
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
2018-05-03 04:12:45 +00:00
if strings.HasPrefix(w, "invite-to-group") {
if d.FindStartOfPreviousWordWithSpace() == 0 {
s = []prompt.Suggest{}
contacts := app.Peer.Profile.Contacts
for _, contact := range contacts {
s = append(s, prompt.Suggest{Text: contact.Onion, Description: contact.Name})
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
} else {
s = []prompt.Suggest{}
groups := app.Peer.Profile.Groups
for _, group := range groups {
if group.Owner == "self" {
s = append(s, prompt.Suggest{Text: group.GroupID, Description: "Group owned by " + group.Owner + " on " + group.GroupServer})
}
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
}
if strings.HasPrefix(w, "accept-invite") {
s = []prompt.Suggest{}
groups := app.Peer.Profile.Groups
for _, group := range groups {
if group.Accepted == false {
s = append(s, prompt.Suggest{Text: group.GroupID, Description: "Group owned by " + group.Owner + " on " + group.GroupServer})
}
}
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
return s
}
2018-04-30 21:47:21 +00:00
func main() {
2018-05-03 04:12:45 +00:00
cwtch :=
2018-05-03 06:01:15 +00:00
`
2018-05-03 04:12:45 +00:00
#, #'
@@@@@@:
@@@@@@.
@'@@+#' @@@@+
''''''@ #+@ :
@''''+;+' . '
@''@' :+' , ; ##, +'
,@@ ;' #'#@''. #''@''#
# ''''''#:,,#'''''@
: @''''@ :+'''@
' @;+'@ @'#
.:# '#..# '# @
@@@@@@
@@@@@@
'@@@@
@# . .
+++, #'@+'@
''', ''''''#
.#+# ''', @'''+,
@''# ''', .#@
:; '@''# .;. ''', ' : ;. ,
@+'''@ '+'+ @++ @+'@+''''+@ #+'''#: ''';#''+@ @@@@ @@@@@@@@@ :@@@@#
#''''''# +''. +'': +'''''''''+ @'''''''# '''+'''''@ @@@@ @@@@@@@@@@@@@@@@:
@'''@@'''@ @''# ,'''@ ''+ @@''+#+ :'''@@+''' ''''@@'''' @@@@ @@@@@@@@@@@@@@@@@
'''# @''# +''@ @'''# ;''@ +''+ @''@ ,+'', '''@ #'''. @@@@ @@@@ '@@@# @@@@
;''' @@; '''# #'@'' @''@ @''+ +''# .@@ ''', '''. @@@@ @@@ @@@ .@@@
@''# #'' ''#''#@''. #''# '''. '''. +'', @@@@ @@@ @@@ @@@
@''# @''@'' #'@+'+ #''# '''. ''', +'', +@@@.@@@ @@@@ @@@, @@@ ,@@@
;''+ @, +''@'# @'+''@ @''# +''; '+ ''', +'', @@@@@@@@# @@@@ @@@. .@@@ .@@@
'''# ++'+ ''''@ ,''''# #''' @''@ '@''+ ''', ''', @@@@@@@@: @@@@ @@@; .@@@' ;@@@
@'''@@'''@ #'''. +'''' ;'''#@ :'''#@+''+ ''', ''', @@@@@@# @@@@ @@@+ ,@@@. @@@@
#''''''# @''+ @''+ +'''' @'''''''# ''', ''', #@@@. @@@@ @@@+ @@@ @@@@
@+''+@ '++@ ;++@ '#''@ ##'''@: +++, +++, :@ @@@@ @@@' @@@ '@@@
:' ' '`
fmt.Printf("%v\n\n", cwtch)
2018-04-30 21:47:21 +00:00
quit := false
2018-05-03 04:12:45 +00:00
app = app2.Application{}
profilefile := ""
2018-05-03 04:12:45 +00:00
var history []string
2018-04-30 21:47:21 +00:00
for !quit {
profile := "unset"
if app.Peer != nil {
profile = app.Peer.Profile.Name
}
2018-05-03 04:12:45 +00:00
prmpt := fmt.Sprintf("cwtch [%v]> ", profile)
text := prompt.Input(prmpt, completer, prompt.OptionSuggestionBGColor(prompt.Purple),
2018-05-03 06:01:15 +00:00
prompt.OptionDescriptionBGColor(prompt.White),
prompt.OptionHistory(history))
2018-05-03 04:12:45 +00:00
2018-05-03 06:01:15 +00:00
commands := strings.Split(text[0:], " ")
history = append(history, text)
2018-04-30 21:47:21 +00:00
switch commands[0] {
case "quit":
2018-05-03 04:12:45 +00:00
app.Peer.Save(profilefile)
2018-04-30 21:47:21 +00:00
quit = true
2018-05-03 04:12:45 +00:00
case "new-profile":
2018-04-30 21:47:21 +00:00
if len(commands) == 3 {
err := app.NewProfile(commands[1], commands[2])
2018-05-12 23:13:31 +00:00
profilefile = commands[2]
2018-04-30 21:47:21 +00:00
if err == nil {
fmt.Printf("New profile created for %v\n", commands[1])
} else {
fmt.Printf("Error creating profile for %v: %v\n", commands[1], err)
}
} else {
2018-05-03 05:46:42 +00:00
fmt.Printf("Error creating NewProfile, usage: %s\n", usages["new-profile"])
2018-04-30 21:47:21 +00:00
}
2018-05-03 04:12:45 +00:00
case "load-profile":
2018-04-30 21:47:21 +00:00
if len(commands) == 2 {
err := app.SetProfile(commands[1])
profilefile = commands[1]
2018-04-30 21:47:21 +00:00
if err == nil {
fmt.Printf("Loaded profile for %v\n", commands[1])
} else {
fmt.Printf("Error loading profile for %v: %v\n", commands[1], err)
}
} else {
2018-05-03 05:46:42 +00:00
fmt.Printf("Error Loading profile, usage: %s\n", usages["load-profile"])
2018-04-30 21:47:21 +00:00
}
case "info":
if app.Peer != nil {
fmt.Printf("Address cwtch:%v\n", app.Peer.Profile.Onion)
} else {
2018-05-03 04:12:45 +00:00
fmt.Printf("Profile needs to be set\n")
2018-04-30 21:47:21 +00:00
}
case "invite":
if len(commands) == 2 {
fmt.Printf("Inviting cwtch:%v\n", commands[1])
app.PeerRequest(commands[1])
} else {
2018-05-03 05:46:42 +00:00
fmt.Printf("Error inviting peer, usage: %s\n", usages["invite"])
}
case "peers":
peers := app.Peer.GetPeers()
for p, s := range peers {
fmt.Printf("Name: %v Status: %v\n", p, s)
}
2018-05-03 04:12:45 +00:00
case "servers":
servers := app.Peer.GetServers()
for s, st := range servers {
fmt.Printf("Name: %v Status: %v\n", s, st)
}
case "contacts":
for _, c := range app.Peer.Profile.Contacts {
fmt.Printf("Name: %v, Onion: %v, Trusted: %v\n", c.Name, c.Onion, c.Trusted)
}
2018-05-01 21:36:03 +00:00
case "groups":
for gid, g := range app.Peer.Profile.Groups {
2018-05-03 04:12:45 +00:00
fmt.Printf("Group Id: %v, Owner: %v Accepted:%v \n", gid, g.Owner, g.Accepted)
}
case "trust":
if len(commands) == 2 {
app.Peer.TrustPeer(commands[1])
} else {
fmt.Printf("Error trusting peer, usage: %s\n", usages["trust"])
}
case "block":
if len(commands) == 2 {
app.Peer.BlockPeer(commands[1])
} else {
fmt.Printf("Error blocking peer, usage: %s\n", usages["trust"])
}
2018-05-03 04:12:45 +00:00
case "accept-invite":
if len(commands) == 2 {
2018-05-03 06:01:15 +00:00
groupID := commands[1]
2018-05-03 04:12:45 +00:00
err := app.Peer.AcceptInvite(groupID)
if err != nil {
2018-05-03 04:12:45 +00:00
fmt.Printf("Error: %v\n", err)
} else {
app.Peer.Save(profilefile)
group := app.Peer.Profile.GetGroupByGroupId(groupID)
if group == nil {
fmt.Printf("Error: group does not exist\n")
} else {
app.Peer.JoinServer(group.GroupServer)
}
2018-05-03 04:12:45 +00:00
}
} else {
2018-05-03 05:46:42 +00:00
fmt.Printf("Error accepting invite, usage: %s\n", usages["accept-invite"])
2018-05-01 21:36:03 +00:00
}
2018-05-03 04:12:45 +00:00
case "invite-to-group":
2018-05-01 21:36:03 +00:00
if len(commands) == 3 {
fmt.Printf("Inviting %v to %v\n", commands[1], commands[2])
err := app.Peer.InviteOnionToGroup(commands[1], commands[2])
2018-05-03 04:12:45 +00:00
if err != nil {
fmt.Printf("Error: %v\n", err)
2018-05-01 21:36:03 +00:00
}
} else {
2018-05-03 05:46:42 +00:00
fmt.Printf("Error inviting peer to group, usage: %s\n", usages["invite-to-group"])
2018-05-01 21:36:03 +00:00
}
2018-05-03 04:12:45 +00:00
case "new-group":
2018-05-01 21:36:03 +00:00
if len(commands) == 2 {
fmt.Printf("Setting up a new group on server:%v\n", commands[1])
2018-05-16 20:20:46 +00:00
id, _, err := app.Peer.Profile.StartGroup(commands[1])
2018-05-16 20:18:47 +00:00
if err == nil {
fmt.Printf("New Group [%v] created for server %v\n", id, commands[1])
app.Peer.Save(profilefile)
group := app.Peer.Profile.GetGroupByGroupId(id)
if group == nil {
fmt.Printf("Error: group does not exist\n")
} else {
app.Peer.JoinServer(group.GroupServer)
}
} else {
2018-05-16 20:18:47 +00:00
fmt.Printf("Error creating new group: %v", err)
}
2018-05-01 21:36:03 +00:00
} else {
fmt.Printf("Error creating a new group, usage: %s\n", usages["new-group"])
2018-05-01 21:36:03 +00:00
}
2018-05-03 04:12:45 +00:00
case "send":
if len(commands) > 2 {
message := strings.Join(commands[2:], " ")
err := app.Peer.SendMessageToGroup(commands[1], message)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
} else {
2018-05-03 05:46:42 +00:00
fmt.Printf("Error sending message to group, usage: %s\n", usages["send"])
2018-05-03 04:12:45 +00:00
}
case "timeline":
if len(commands) == 2 {
group := app.Peer.Profile.GetGroupByGroupId(commands[1])
if group == nil {
fmt.Printf("Error: group does not exist\n")
} else {
timeline := group.GetTimeline()
for _, m := range timeline.Messages {
2018-05-03 04:12:45 +00:00
verified := "not-verified"
if m.Verified {
verified = "verified"
}
2018-05-03 17:59:58 +00:00
p, ok := app.Peer.Profile.Contacts[m.PeerID]
name := "unknown"
if ok {
name = p.Name
} else if app.Peer.Profile.Onion == m.PeerID {
name = app.Peer.Profile.Name
}
fmt.Printf("%v %v (%v): %v [%s]\n", m.Timestamp, name, m.PeerID, m.Message, verified)
2018-05-03 04:12:45 +00:00
}
}
} else {
2018-05-03 05:46:42 +00:00
fmt.Printf("Error reading timeline from group, usage: %s\n", usages["timeline"])
2018-05-03 04:12:45 +00:00
}
case "save":
app.Peer.Save(profilefile)
2018-05-03 05:46:42 +00:00
case "help":
for _, command := range suggestions {
fmt.Printf("%-18s%-56s%s\n", command.Text, command.Description, usages[command.Text])
}
case "sendlots":
if len(commands) == 2 {
group := app.Peer.Profile.GetGroupByGroupId(commands[1])
if group == nil {
fmt.Printf("Error: group does not exist\n")
} else {
for i := 0; i < 100; i++ {
fmt.Printf("Sending message: %v\n", i)
err := app.Peer.SendMessageToGroup(commands[1], fmt.Sprintf("this is message %v", i))
if err != nil {
fmt.Printf("could not send message %v because %v\n", i, err)
}
}
fmt.Printf("Waiting 5 seconds for message to process...\n")
time.Sleep(time.Second * 5)
timeline := group.GetTimeline()
totalLatency := time.Duration(0)
maxLatency := time.Duration(0)
totalMessages := 0
for i := 0; i < 100; i++ {
found := false
for _, m := range timeline.Messages {
if m.Message == fmt.Sprintf("this is message %v", i) && m.PeerID == app.Peer.Profile.Onion {
found = true
latency := m.Received.Sub(m.Timestamp)
fmt.Printf("Latency for Message %v was %v\n", i, latency)
totalLatency = totalLatency + latency
if maxLatency < latency {
maxLatency = latency
}
totalMessages += 1
}
}
if !found {
fmt.Printf("message %v was never received\n", i)
}
}
fmt.Printf("Average Latency for %v messages was: %vms\n", totalMessages, time.Duration(int64(totalLatency)/int64(totalMessages)))
fmt.Printf("Max Latency for %v messages was: %vms\n", totalMessages, maxLatency)
}
}
2018-04-30 21:47:21 +00:00
}
2018-05-16 20:18:47 +00:00
if profilefile != "" {
app.Peer.Save(profilefile)
}
2018-04-30 21:47:21 +00:00
}
}