2016-09-16 21:26:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/special/notricochet/rpc"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
Backend ricochet.RicochetCoreClient
|
2016-10-16 04:57:16 +00:00
|
|
|
Ui *UI
|
2016-09-16 21:26:14 +00:00
|
|
|
|
|
|
|
ServerStatus ricochet.ServerStatusReply
|
|
|
|
Identity ricochet.Identity
|
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
NetworkStatus ricochet.NetworkStatus
|
|
|
|
Contacts *ContactList
|
2016-10-16 03:57:07 +00:00
|
|
|
|
2016-10-16 21:50:34 +00:00
|
|
|
monitorsChannel chan interface{}
|
|
|
|
blockChannel chan struct{}
|
|
|
|
unblockChannel chan struct{}
|
|
|
|
|
|
|
|
populatedChannel chan struct{}
|
|
|
|
populatedNetwork bool
|
|
|
|
populatedContacts bool
|
|
|
|
populatedConversations bool
|
2016-09-16 21:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX need to handle backend connection loss/reconnection..
|
|
|
|
func (c *Client) Initialize() error {
|
2016-10-16 03:13:02 +00:00
|
|
|
c.Contacts = NewContactList()
|
2016-10-16 03:57:07 +00:00
|
|
|
c.monitorsChannel = make(chan interface{}, 10)
|
2016-10-16 04:57:16 +00:00
|
|
|
c.blockChannel = make(chan struct{})
|
|
|
|
c.unblockChannel = make(chan struct{})
|
2016-10-16 21:50:34 +00:00
|
|
|
c.populatedChannel = make(chan struct{})
|
2016-10-16 03:13:02 +00:00
|
|
|
|
2016-09-16 21:26:14 +00:00
|
|
|
// Query server status and version
|
|
|
|
status, err := c.Backend.GetServerStatus(context.Background(), &ricochet.ServerStatusRequest{
|
|
|
|
RpcVersion: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.ServerStatus = *status
|
|
|
|
if status.RpcVersion != 1 {
|
2016-10-16 21:50:34 +00:00
|
|
|
return fmt.Errorf("unsupported backend RPC version %d", status.RpcVersion)
|
2016-09-16 21:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query identity
|
|
|
|
identity, err := c.Backend.GetIdentity(context.Background(), &ricochet.IdentityRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Identity = *identity
|
|
|
|
|
|
|
|
// Spawn routines to query and monitor state changes
|
|
|
|
go c.monitorNetwork()
|
|
|
|
go c.monitorContacts()
|
2016-10-16 03:57:07 +00:00
|
|
|
// Conversation monitor isn't started until contacts are populated
|
2016-09-16 21:26:14 +00:00
|
|
|
|
2016-10-16 21:50:34 +00:00
|
|
|
// Spawn routine to handle all events
|
|
|
|
go c.Run()
|
|
|
|
|
|
|
|
// Block until all state is populated
|
|
|
|
<-c.populatedChannel
|
2016-09-16 21:26:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
func (c *Client) Run() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case v := <-c.monitorsChannel:
|
|
|
|
switch event := v.(type) {
|
|
|
|
case *ricochet.NetworkStatus:
|
|
|
|
c.onNetworkStatus(event)
|
|
|
|
case *ricochet.ContactEvent:
|
|
|
|
c.onContactEvent(event)
|
|
|
|
case *ricochet.ConversationEvent:
|
|
|
|
c.onConversationEvent(event)
|
|
|
|
default:
|
|
|
|
log.Panicf("Unknown event type on monitor channel: %v", event)
|
|
|
|
}
|
2016-10-16 04:57:16 +00:00
|
|
|
case <-c.blockChannel:
|
|
|
|
<-c.unblockChannel
|
2016-10-16 03:57:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
func (c *Client) Block() {
|
|
|
|
c.blockChannel <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Unblock() {
|
|
|
|
c.unblockChannel <- struct{}{}
|
2016-10-06 23:50:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 21:50:34 +00:00
|
|
|
func (c *Client) IsInitialized() bool {
|
|
|
|
return c.populatedChannel == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) checkIfPopulated() {
|
|
|
|
if c.populatedChannel != nil && c.populatedContacts &&
|
|
|
|
c.populatedConversations && c.populatedNetwork {
|
|
|
|
close(c.populatedChannel)
|
|
|
|
c.populatedChannel = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 21:26:14 +00:00
|
|
|
func (c *Client) monitorNetwork() {
|
|
|
|
stream, err := c.Backend.MonitorNetwork(context.Background(), &ricochet.MonitorNetworkRequest{})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Initializing network status monitor failed: %v", err)
|
|
|
|
// XXX handle
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
status, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Network status monitor error: %v", err)
|
|
|
|
// XXX handle
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
c.monitorsChannel <- status
|
2016-09-16 21:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) monitorContacts() {
|
|
|
|
stream, err := c.Backend.MonitorContacts(context.Background(), &ricochet.MonitorContactsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Initializing contact status monitor failed: %v", err)
|
|
|
|
// XXX handle
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := stream.Recv()
|
|
|
|
if err != nil {
|
2016-10-16 03:57:07 +00:00
|
|
|
log.Printf("Contact monitor error: %v", err)
|
2016-09-16 21:26:14 +00:00
|
|
|
// XXX handle
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
c.monitorsChannel <- event
|
2016-09-16 21:26:14 +00:00
|
|
|
}
|
2016-10-16 03:57:07 +00:00
|
|
|
}
|
2016-09-16 21:26:14 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
func (c *Client) monitorConversations() {
|
|
|
|
stream, err := c.Backend.MonitorConversations(context.Background(), &ricochet.MonitorConversationsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Initializing conversations monitor failed: %v", err)
|
|
|
|
// XXX handle
|
|
|
|
return
|
|
|
|
}
|
2016-09-16 21:26:14 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := stream.Recv()
|
|
|
|
if err != nil {
|
2016-10-16 03:57:07 +00:00
|
|
|
log.Printf("Conversations monitor error: %v", err)
|
2016-09-16 21:26:14 +00:00
|
|
|
// XXX handle
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
c.monitorsChannel <- event
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 21:26:14 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
func (c *Client) onNetworkStatus(status *ricochet.NetworkStatus) {
|
|
|
|
log.Printf("Network status changed: %v", status)
|
|
|
|
c.NetworkStatus = *status
|
2016-10-16 21:50:34 +00:00
|
|
|
c.populatedNetwork = true
|
|
|
|
c.checkIfPopulated()
|
2016-10-16 03:57:07 +00:00
|
|
|
}
|
2016-10-16 03:13:02 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
func (c *Client) onContactEvent(event *ricochet.ContactEvent) {
|
|
|
|
if !c.populatedContacts && event.Type != ricochet.ContactEvent_POPULATE {
|
|
|
|
log.Printf("Ignoring unexpected contact event during populate: %v", event)
|
|
|
|
return
|
|
|
|
}
|
2016-09-16 21:26:14 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
data := event.GetContact()
|
2016-09-16 21:26:14 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
switch event.Type {
|
|
|
|
case ricochet.ContactEvent_POPULATE:
|
2016-10-16 21:50:34 +00:00
|
|
|
if c.populatedContacts {
|
|
|
|
log.Printf("Ignoring unexpected contact populate event: %v", event)
|
|
|
|
} else if event.Subject == nil {
|
|
|
|
// Populate is terminated by a nil subject
|
2016-10-16 03:57:07 +00:00
|
|
|
c.populatedContacts = true
|
|
|
|
log.Printf("Loaded %d contacts", len(c.Contacts.Contacts))
|
2016-10-16 21:50:34 +00:00
|
|
|
c.checkIfPopulated()
|
2016-10-16 03:57:07 +00:00
|
|
|
go c.monitorConversations()
|
|
|
|
} else if data != nil {
|
|
|
|
c.Contacts.Populate(data)
|
|
|
|
} else {
|
|
|
|
log.Printf("Invalid contact populate event: %v", event)
|
|
|
|
}
|
2016-10-06 23:50:52 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
case ricochet.ContactEvent_ADD:
|
|
|
|
if data == nil {
|
|
|
|
log.Printf("Ignoring contact add event with null data")
|
|
|
|
return
|
|
|
|
}
|
2016-09-16 21:26:14 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
c.Contacts.Added(data)
|
2016-09-16 21:26:14 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
case ricochet.ContactEvent_UPDATE:
|
|
|
|
if data == nil {
|
|
|
|
log.Printf("Ignoring contact update event with null data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
contact := c.Contacts.ByIdAndAddress(data.Id, data.Address)
|
|
|
|
if contact == nil {
|
|
|
|
log.Printf("Ignoring contact update event for unknown contact: %v", data)
|
|
|
|
} else {
|
|
|
|
contact.Updated(data)
|
|
|
|
}
|
2016-10-06 23:50:52 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
case ricochet.ContactEvent_DELETE:
|
|
|
|
if data == nil {
|
|
|
|
log.Printf("Ignoring contact delete event with null data")
|
|
|
|
return
|
2016-09-16 21:26:14 +00:00
|
|
|
}
|
2016-10-16 03:57:07 +00:00
|
|
|
|
|
|
|
contact, _ := c.Contacts.Deleted(data)
|
|
|
|
|
2016-10-16 04:57:16 +00:00
|
|
|
if c.Ui.CurrentContact == contact {
|
|
|
|
c.Ui.SetCurrentContact(nil)
|
2016-10-16 03:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Printf("Ignoring unknown contact event: %v", event)
|
2016-09-16 21:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-05 21:38:41 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
func (c *Client) onConversationEvent(event *ricochet.ConversationEvent) {
|
|
|
|
if event.Type != ricochet.ConversationEvent_RECEIVE &&
|
|
|
|
event.Type != ricochet.ConversationEvent_SEND &&
|
|
|
|
event.Type != ricochet.ConversationEvent_POPULATE {
|
2016-10-05 21:38:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
message := event.Msg
|
2016-10-16 21:50:34 +00:00
|
|
|
|
|
|
|
if event.Type == ricochet.ConversationEvent_POPULATE && message == nil {
|
|
|
|
c.populatedConversations = true
|
|
|
|
c.checkIfPopulated()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
if message == nil || message.Recipient == nil || message.Sender == nil {
|
|
|
|
log.Printf("Ignoring invalid conversation event: %v", event)
|
|
|
|
return
|
|
|
|
}
|
2016-10-05 21:38:41 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
var remoteEntity *ricochet.Entity
|
|
|
|
if !message.Sender.IsSelf {
|
|
|
|
remoteEntity = message.Sender
|
|
|
|
} else {
|
|
|
|
remoteEntity = message.Recipient
|
|
|
|
}
|
2016-10-06 23:50:52 +00:00
|
|
|
|
2016-10-16 03:57:07 +00:00
|
|
|
remoteContact := c.Contacts.ByIdAndAddress(remoteEntity.ContactId, remoteEntity.Address)
|
|
|
|
if remoteContact == nil {
|
|
|
|
log.Printf("Ignoring conversation event with unknown contact: %v", event)
|
|
|
|
return
|
|
|
|
}
|
2016-10-06 23:50:52 +00:00
|
|
|
|
2016-10-16 21:50:34 +00:00
|
|
|
if event.Type != ricochet.ConversationEvent_POPULATE {
|
|
|
|
c.Ui.PrintMessage(remoteContact, message.Sender.IsSelf, message.Text)
|
|
|
|
}
|
2016-10-06 23:50:52 +00:00
|
|
|
|
2016-10-16 21:50:34 +00:00
|
|
|
// XXX Shouldn't mark until displayed
|
2016-10-16 03:57:07 +00:00
|
|
|
if !message.Sender.IsSelf {
|
|
|
|
backend := c.Backend
|
|
|
|
message := message
|
|
|
|
go func() {
|
|
|
|
_, err := backend.MarkConversationRead(context.Background(), &ricochet.MarkConversationReadRequest{
|
|
|
|
Entity: message.Sender,
|
|
|
|
LastRecvIdentifier: message.Identifier,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Mark conversation read failed: %v", err)
|
2016-10-06 23:50:52 +00:00
|
|
|
}
|
2016-10-16 03:57:07 +00:00
|
|
|
}()
|
2016-10-05 21:38:41 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-16 21:50:34 +00:00
|
|
|
|
|
|
|
func (c *Client) NetworkControlStatus() ricochet.TorControlStatus {
|
|
|
|
if c.NetworkStatus.Control != nil {
|
|
|
|
return *c.NetworkStatus.Control
|
|
|
|
} else {
|
|
|
|
return ricochet.TorControlStatus{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) NetworkConnectionStatus() ricochet.TorConnectionStatus {
|
|
|
|
if c.NetworkStatus.Connection != nil {
|
|
|
|
return *c.NetworkStatus.Connection
|
|
|
|
} else {
|
|
|
|
return ricochet.TorConnectionStatus{}
|
|
|
|
}
|
|
|
|
}
|