2016-10-16 04:57:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-10-23 18:37:57 +00:00
|
|
|
"github.com/chzyer/readline"
|
2016-10-17 04:26:35 +00:00
|
|
|
"github.com/ricochet-im/ricochet-go/rpc"
|
2016-10-16 04:57:16 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-10-23 19:42:45 +00:00
|
|
|
"io"
|
2016-10-16 04:57:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-10-23 19:42:45 +00:00
|
|
|
"time"
|
2016-10-16 04:57:16 +00:00
|
|
|
)
|
|
|
|
|
2016-10-23 00:50:21 +00:00
|
|
|
var Ui UI
|
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
type UI struct {
|
|
|
|
Input *readline.Instance
|
2016-10-23 19:42:45 +00:00
|
|
|
Stdout io.Writer
|
2016-10-16 04:57:16 +00:00
|
|
|
Client *Client
|
|
|
|
|
|
|
|
CurrentContact *Contact
|
2016-10-23 19:42:45 +00:00
|
|
|
|
|
|
|
baseConfig *readline.Config
|
|
|
|
baseChatConfig *readline.Config
|
2016-10-16 04:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ui *UI) CommandLoop() {
|
2016-10-23 19:42:45 +00:00
|
|
|
ui.setupInputConfigs()
|
|
|
|
ui.Input.SetConfig(ui.baseConfig)
|
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
for {
|
|
|
|
line, err := ui.Input.Readline()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ui.Execute(line); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 19:42:45 +00:00
|
|
|
func (ui *UI) setupInputConfigs() {
|
|
|
|
ui.baseConfig = ui.Input.Config.Clone()
|
|
|
|
ui.baseConfig.Prompt = "> "
|
|
|
|
ui.baseChatConfig = ui.baseConfig.Clone()
|
|
|
|
ui.baseChatConfig.Prompt = "\x1b[90m%s\x1b[39m | %s \x1b[34m<<\x1b[39m "
|
|
|
|
ui.baseChatConfig.UniqueEditLine = true
|
|
|
|
}
|
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
func (ui *UI) Execute(line string) error {
|
|
|
|
// Block client event handlers for threadsafety
|
|
|
|
ui.Client.Block()
|
|
|
|
defer ui.Client.Unblock()
|
|
|
|
|
2016-10-28 15:48:41 +00:00
|
|
|
words := strings.SplitN(line, " ", 2)
|
2016-10-16 04:57:16 +00:00
|
|
|
|
|
|
|
if ui.CurrentContact != nil {
|
|
|
|
if len(words[0]) > 0 && words[0][0] == '/' {
|
|
|
|
words[0] = words[0][1:]
|
|
|
|
} else {
|
2016-10-23 00:52:26 +00:00
|
|
|
ui.CurrentContact.Conversation.SendMessage(line)
|
2016-10-16 04:57:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if id, err := strconv.Atoi(words[0]); err == nil {
|
|
|
|
contact := ui.Client.Contacts.ById(int32(id))
|
|
|
|
if contact != nil {
|
|
|
|
ui.SetCurrentContact(contact)
|
|
|
|
} else {
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "no contact %d\n", id)
|
2016-10-16 04:57:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch words[0] {
|
|
|
|
case "clear":
|
|
|
|
readline.ClearScreen(readline.Stdout)
|
|
|
|
|
|
|
|
case "quit":
|
|
|
|
return errors.New("Quitting")
|
|
|
|
|
|
|
|
case "status":
|
2016-10-23 19:42:45 +00:00
|
|
|
ui.PrintStatus()
|
2016-10-16 04:57:16 +00:00
|
|
|
|
|
|
|
case "connect":
|
|
|
|
status, err := ui.Client.Backend.StartNetwork(context.Background(), &ricochet.StartNetworkRequest{})
|
|
|
|
if err != nil {
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "start network error: %v\n", err)
|
2016-10-16 04:57:16 +00:00
|
|
|
} else {
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "network started: %v\n", status)
|
2016-10-16 04:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "disconnect":
|
|
|
|
status, err := ui.Client.Backend.StopNetwork(context.Background(), &ricochet.StopNetworkRequest{})
|
|
|
|
if err != nil {
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "stop network error: %v\n", err)
|
2016-10-16 04:57:16 +00:00
|
|
|
} else {
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "network stopped: %v\n", status)
|
2016-10-16 04:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "contacts":
|
|
|
|
ui.ListContacts()
|
|
|
|
|
2016-10-28 15:48:41 +00:00
|
|
|
case "add-contact":
|
|
|
|
ui.AddContact(words[1:])
|
|
|
|
|
|
|
|
case "delete-contact":
|
|
|
|
ui.DeleteContact(words[1:])
|
|
|
|
|
2016-10-16 21:50:34 +00:00
|
|
|
case "log":
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprint(ui.Stdout, LogBuffer.String())
|
|
|
|
|
|
|
|
case "close":
|
|
|
|
ui.SetCurrentContact(nil)
|
2016-10-16 21:50:34 +00:00
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
case "help":
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
default:
|
2016-10-28 15:48:41 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "Commands: clear, quit, status, connect, disconnect, contacts, add-contact, delete-contact, log, close, help\n")
|
2016-10-16 04:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-16 21:50:34 +00:00
|
|
|
func (ui *UI) PrintStatus() {
|
|
|
|
controlStatus := ui.Client.NetworkControlStatus()
|
|
|
|
connectionStatus := ui.Client.NetworkConnectionStatus()
|
|
|
|
|
|
|
|
switch controlStatus.Status {
|
|
|
|
case ricochet.TorControlStatus_STOPPED:
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "Network is stopped -- type 'connect' to go online\n")
|
2016-10-16 21:50:34 +00:00
|
|
|
|
|
|
|
case ricochet.TorControlStatus_ERROR:
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "Network error: %s\n", controlStatus.ErrorMessage)
|
2016-10-16 21:50:34 +00:00
|
|
|
|
|
|
|
case ricochet.TorControlStatus_CONNECTING:
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "Network connecting...\n")
|
2016-10-16 21:50:34 +00:00
|
|
|
|
|
|
|
case ricochet.TorControlStatus_CONNECTED:
|
|
|
|
switch connectionStatus.Status {
|
|
|
|
case ricochet.TorConnectionStatus_UNKNOWN:
|
|
|
|
fallthrough
|
|
|
|
case ricochet.TorConnectionStatus_OFFLINE:
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "Network is offline\n")
|
2016-10-16 21:50:34 +00:00
|
|
|
|
|
|
|
case ricochet.TorConnectionStatus_BOOTSTRAPPING:
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "Network bootstrapping: %s\n", connectionStatus.BootstrapProgress)
|
2016-10-16 21:50:34 +00:00
|
|
|
|
|
|
|
case ricochet.TorConnectionStatus_READY:
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "Network is online\n")
|
2016-10-16 21:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 19:42:45 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "Your ricochet ID is %s\n", ui.Client.Identity.Address)
|
2016-10-16 21:50:34 +00:00
|
|
|
|
|
|
|
// no. contacts, contact reqs, online contacts
|
|
|
|
// unread messages
|
|
|
|
}
|
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
func (ui *UI) ListContacts() {
|
|
|
|
byStatus := make(map[ricochet.Contact_Status][]*Contact)
|
|
|
|
for _, contact := range ui.Client.Contacts.Contacts {
|
|
|
|
byStatus[contact.Data.Status] = append(byStatus[contact.Data.Status], contact)
|
|
|
|
}
|
|
|
|
|
|
|
|
order := []ricochet.Contact_Status{ricochet.Contact_ONLINE, ricochet.Contact_UNKNOWN, ricochet.Contact_OFFLINE, ricochet.Contact_REQUEST, ricochet.Contact_REJECTED}
|
|
|
|
for _, status := range order {
|
|
|
|
contacts := byStatus[status]
|
|
|
|
if len(contacts) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-26 21:26:21 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "%s\n", ColoredContactStatus(status))
|
2016-10-16 04:57:16 +00:00
|
|
|
for _, contact := range contacts {
|
2016-10-26 21:26:21 +00:00
|
|
|
unreadCount := contact.Conversation.UnreadCount()
|
|
|
|
if unreadCount > 0 {
|
|
|
|
fmt.Fprintf(ui.Stdout, " \x1b[1m%s\x1b[0m (\x1b[1m%d\x1b[0m) -- \x1b[34;1m%d new messages\x1b[0m\n", contact.Data.Nickname, contact.Data.Id, unreadCount)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(ui.Stdout, " %s (\x1b[1m%d\x1b[0m)\n", contact.Data.Nickname, contact.Data.Id)
|
|
|
|
}
|
2016-10-23 19:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 15:48:41 +00:00
|
|
|
func (ui *UI) AddContact(params []string) {
|
|
|
|
var address string
|
|
|
|
|
|
|
|
if len(params) > 0 {
|
|
|
|
address = params[0]
|
|
|
|
} else {
|
|
|
|
str, err := readline.Line("Contact address: ")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
address = str
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX validate address
|
|
|
|
|
|
|
|
nickname, err := readline.Line("Nickname: ")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fromNickname, err := readline.Line("From (your nickname): ")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
message, err := readline.Line("Message: ")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
contact, err := ui.Client.Backend.AddContactRequest(context.Background(),
|
|
|
|
&ricochet.ContactRequest{
|
|
|
|
Direction: ricochet.ContactRequest_OUTBOUND,
|
|
|
|
Address: address,
|
|
|
|
Nickname: nickname,
|
|
|
|
Text: message,
|
|
|
|
FromNickname: fromNickname,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(ui.Stdout, "Failed: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(ui.Stdout, "Added contact \x1b[1m%s\x1b[0m (\x1b[1m%d\x1b[0m)\n", contact.Nickname, contact.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ui *UI) DeleteContact(params []string) {
|
|
|
|
if len(params) < 1 {
|
|
|
|
fmt.Fprintf(ui.Stdout, "Usage: delete-contact [id]\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id, err := strconv.Atoi(params[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(ui.Stdout, "Invalid contact id '%s'\n", params[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
contact := ui.Client.Contacts.ById(int32(id))
|
|
|
|
if contact == nil {
|
|
|
|
fmt.Fprintf(ui.Stdout, "No contact with id %d\n", id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(ui.Stdout, "\nThis contact will be \x1b[31mdeleted\x1b[0m:\n\n")
|
|
|
|
fmt.Fprintf(ui.Stdout, " Address:\t%s\n", contact.Data.Address)
|
|
|
|
fmt.Fprintf(ui.Stdout, " Name:\t%s\n", contact.Data.Nickname)
|
|
|
|
fmt.Fprintf(ui.Stdout, " Online:\t%s\n", contact.Data.LastConnected)
|
|
|
|
fmt.Fprintf(ui.Stdout, " Created:\t%s\n\n", contact.Data.WhenCreated)
|
|
|
|
confirm, err := readline.Line("Type YES to confirm: ")
|
|
|
|
if err != nil || confirm != "YES" {
|
|
|
|
fmt.Fprintf(ui.Stdout, "Aborted\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ui.Client.Backend.DeleteContact(context.Background(),
|
|
|
|
&ricochet.DeleteContactRequest{
|
|
|
|
Id: contact.Data.Id,
|
|
|
|
Address: contact.Data.Address,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(ui.Stdout, "Failed: %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(ui.Stdout, "Contact deleted\n")
|
|
|
|
}
|
|
|
|
|
2016-10-23 19:42:45 +00:00
|
|
|
// This type acts as a readline Listener and handles special behavior for
|
|
|
|
// the prompt in a conversation. In particular, it swaps temporarily back to
|
|
|
|
// the normal prompt for command lines (starting with /), and it keeps the
|
|
|
|
// timestamp in the conversation prompt updated.
|
|
|
|
type conversationInputConfig struct {
|
|
|
|
Input *readline.Instance
|
|
|
|
Config *readline.Config
|
|
|
|
BaseConfig *readline.Config
|
|
|
|
PromptFmt string
|
|
|
|
|
2016-10-26 20:57:48 +00:00
|
|
|
usingConfig bool
|
2016-10-23 19:42:45 +00:00
|
|
|
stopPromptTimer chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *conversationInputConfig) OnChange(line []rune, pos int, key rune) ([]rune, int, bool) {
|
2016-10-26 20:57:48 +00:00
|
|
|
if len(line) == 0 && key != 0 && !cc.usingConfig {
|
|
|
|
cc.Install()
|
2016-10-23 19:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(line) > 0 && line[0] == '/' {
|
2016-10-26 20:57:48 +00:00
|
|
|
if cc.usingConfig {
|
2016-10-23 19:42:45 +00:00
|
|
|
cc.stopPromptTimer <- struct{}{}
|
2016-10-26 20:57:48 +00:00
|
|
|
cc.usingConfig = false
|
2016-10-23 19:42:45 +00:00
|
|
|
close(cc.stopPromptTimer)
|
|
|
|
cc.BaseConfig.Listener = cc.Config.Listener
|
|
|
|
cc.Input.SetConfig(cc.BaseConfig)
|
2016-10-16 04:57:16 +00:00
|
|
|
}
|
2016-10-26 20:57:48 +00:00
|
|
|
} else if !cc.usingConfig {
|
2016-10-23 19:42:45 +00:00
|
|
|
line = append([]rune{'/'}, line...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return line, pos, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *conversationInputConfig) Install() {
|
2016-10-26 20:57:48 +00:00
|
|
|
if !cc.usingConfig {
|
|
|
|
cc.usingConfig = true
|
|
|
|
cc.Input.SetConfig(cc.Config)
|
|
|
|
cc.stopPromptTimer = make(chan struct{})
|
|
|
|
go cc.updatePromptTimer()
|
|
|
|
}
|
2016-10-23 19:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *conversationInputConfig) Remove() {
|
2016-10-26 20:57:48 +00:00
|
|
|
cc.BaseConfig.Listener = nil
|
|
|
|
|
|
|
|
if cc.usingConfig {
|
2016-10-23 19:42:45 +00:00
|
|
|
cc.stopPromptTimer <- struct{}{}
|
2016-10-26 20:57:48 +00:00
|
|
|
cc.usingConfig = false
|
2016-10-23 19:42:45 +00:00
|
|
|
close(cc.stopPromptTimer)
|
|
|
|
cc.Input.SetConfig(cc.BaseConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *conversationInputConfig) updatePromptTimer() {
|
|
|
|
for {
|
|
|
|
t := time.Now()
|
|
|
|
cc.Input.SetPrompt(fmt.Sprintf(cc.PromptFmt, t.Format("15:04")))
|
|
|
|
cc.Input.Refresh()
|
|
|
|
|
|
|
|
sec := 61 - t.Second()
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Duration(sec) * time.Second):
|
|
|
|
continue
|
|
|
|
case <-cc.stopPromptTimer:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ui *UI) setupConversationPrompt() {
|
2016-10-28 15:47:46 +00:00
|
|
|
if ui.Input.Config.Listener != nil {
|
|
|
|
ui.Input.Config.Listener.(*conversationInputConfig).Remove()
|
|
|
|
}
|
|
|
|
|
2016-10-23 19:42:45 +00:00
|
|
|
listener := &conversationInputConfig{
|
|
|
|
Input: ui.Input,
|
|
|
|
Config: ui.baseChatConfig.Clone(),
|
|
|
|
BaseConfig: ui.baseConfig,
|
|
|
|
PromptFmt: fmt.Sprintf(ui.baseChatConfig.Prompt, "%s", ui.CurrentContact.Data.Nickname),
|
2016-10-16 04:57:16 +00:00
|
|
|
}
|
2016-10-23 19:42:45 +00:00
|
|
|
listener.Config.Listener = listener
|
|
|
|
listener.Install()
|
2016-10-16 04:57:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 21:26:21 +00:00
|
|
|
func ColoredContactStatus(status ricochet.Contact_Status) string {
|
|
|
|
switch status {
|
|
|
|
case ricochet.Contact_UNKNOWN:
|
|
|
|
return "\x1b[31moffline\x1b[39m"
|
|
|
|
case ricochet.Contact_OFFLINE:
|
|
|
|
return "\x1b[31moffline\x1b[39m"
|
|
|
|
case ricochet.Contact_ONLINE:
|
|
|
|
return "\x1b[32monline\x1b[39m"
|
|
|
|
case ricochet.Contact_REQUEST:
|
|
|
|
return "\x1b[33mcontact request\x1b[39m"
|
|
|
|
case ricochet.Contact_REJECTED:
|
|
|
|
return "\x1b[31mrejected\x1b[39m"
|
|
|
|
default:
|
|
|
|
return status.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
func (ui *UI) SetCurrentContact(contact *Contact) {
|
|
|
|
if ui.CurrentContact == contact {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-26 22:12:16 +00:00
|
|
|
if ui.CurrentContact != nil {
|
|
|
|
ui.CurrentContact.Conversation.SetActive(false)
|
|
|
|
}
|
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
ui.CurrentContact = contact
|
|
|
|
if ui.CurrentContact != nil {
|
2016-10-23 19:42:45 +00:00
|
|
|
ui.setupConversationPrompt()
|
2016-10-26 21:26:21 +00:00
|
|
|
fmt.Fprintf(ui.Stdout, "------- \x1b[1m%s\x1b[0m is %s -------\n", contact.Data.Nickname, ColoredContactStatus(contact.Data.Status))
|
2016-10-26 22:12:16 +00:00
|
|
|
ui.CurrentContact.Conversation.SetActive(true)
|
2016-10-16 04:57:16 +00:00
|
|
|
} else {
|
2016-10-23 19:42:45 +00:00
|
|
|
ui.Input.Config.Listener.(*conversationInputConfig).Remove()
|
|
|
|
ui.Input.SetConfig(ui.baseConfig)
|
2016-10-16 04:57:16 +00:00
|
|
|
}
|
|
|
|
}
|