2016-07-01 03:18:55 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-08-30 02:46:41 +00:00
|
|
|
"fmt"
|
2016-10-17 04:26:35 +00:00
|
|
|
"github.com/ricochet-im/ricochet-go/core/utils"
|
|
|
|
"github.com/ricochet-im/ricochet-go/rpc"
|
2016-09-02 16:51:24 +00:00
|
|
|
"sync"
|
2016-10-27 19:57:04 +00:00
|
|
|
"time"
|
2016-07-01 03:18:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ContactList struct {
|
2016-10-27 19:57:04 +00:00
|
|
|
core *Ricochet
|
|
|
|
|
2016-09-02 16:51:24 +00:00
|
|
|
mutex sync.RWMutex
|
|
|
|
events *utils.Publisher
|
|
|
|
|
2017-09-24 21:55:47 +00:00
|
|
|
contacts map[string]*Contact
|
2017-08-10 17:25:50 +00:00
|
|
|
inboundRequests map[string]*InboundContactRequest
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
func LoadContactList(core *Ricochet) (*ContactList, error) {
|
2016-09-02 16:51:24 +00:00
|
|
|
list := &ContactList{
|
2017-08-12 00:04:28 +00:00
|
|
|
core: core,
|
|
|
|
events: utils.CreatePublisher(),
|
|
|
|
inboundRequests: make(map[string]*InboundContactRequest),
|
2016-09-02 16:51:24 +00:00
|
|
|
}
|
|
|
|
|
2017-09-24 20:15:53 +00:00
|
|
|
config := core.Config.Read()
|
2017-09-24 21:55:47 +00:00
|
|
|
list.contacts = make(map[string]*Contact, len(config.Contacts))
|
|
|
|
for addr, data := range config.Contacts {
|
|
|
|
if _, exists := list.contacts[addr]; exists {
|
|
|
|
return nil, fmt.Errorf("Duplicate contact %s", addr)
|
2016-08-30 02:46:41 +00:00
|
|
|
}
|
2017-09-24 21:55:47 +00:00
|
|
|
if addr != data.Address {
|
|
|
|
return nil, fmt.Errorf("Contact address/key do not match ('%s' and '%s')", addr, data.Address)
|
2016-08-30 02:46:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-24 21:55:47 +00:00
|
|
|
contact, err := ContactFromConfig(core, data, list.events)
|
2016-08-30 02:46:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-24 21:55:47 +00:00
|
|
|
list.contacts[addr] = contact
|
2016-08-30 02:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2016-09-02 16:51:24 +00:00
|
|
|
func (this *ContactList) EventMonitor() utils.Subscribable {
|
|
|
|
return this.events
|
|
|
|
}
|
|
|
|
|
2016-07-01 03:18:55 +00:00
|
|
|
func (this *ContactList) Contacts() []*Contact {
|
2016-09-02 16:51:24 +00:00
|
|
|
this.mutex.RLock()
|
|
|
|
defer this.mutex.RUnlock()
|
2016-07-01 03:18:55 +00:00
|
|
|
re := make([]*Contact, 0, len(this.contacts))
|
|
|
|
for _, contact := range this.contacts {
|
|
|
|
re = append(re, contact)
|
|
|
|
}
|
|
|
|
return re
|
|
|
|
}
|
|
|
|
|
2017-09-24 21:55:47 +00:00
|
|
|
func (cl *ContactList) ContactByAddress(address string) *Contact {
|
|
|
|
cl.mutex.RLock()
|
|
|
|
defer cl.mutex.RUnlock()
|
|
|
|
return cl.contacts[address]
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 17:25:50 +00:00
|
|
|
func (cl *ContactList) InboundRequestByAddress(address string) *InboundContactRequest {
|
|
|
|
cl.mutex.RLock()
|
|
|
|
defer cl.mutex.RUnlock()
|
2017-09-24 21:55:47 +00:00
|
|
|
return cl.inboundRequests[address]
|
2017-08-10 17:25:50 +00:00
|
|
|
}
|
2016-11-06 03:27:13 +00:00
|
|
|
|
2017-08-10 17:25:50 +00:00
|
|
|
// AddNewContact adds a new contact to the persistent contact list, broadcasts a
|
|
|
|
// contact add RPC event, and returns a newly constructed Contact. AddNewContact
|
|
|
|
// does not create contact requests or trigger any other protocol behavior.
|
|
|
|
//
|
|
|
|
// Generally, you will use AddContactRequest (for outbound requests) and
|
|
|
|
// AddOrUpdateInboundContactRequest plus InboundContactRequest.Accept() instead of
|
|
|
|
// using this function directly.
|
2017-09-24 20:15:53 +00:00
|
|
|
func (this *ContactList) AddNewContact(data *ricochet.Contact) (*Contact, error) {
|
2016-10-27 19:57:04 +00:00
|
|
|
this.mutex.Lock()
|
|
|
|
defer this.mutex.Unlock()
|
|
|
|
|
2017-09-24 21:55:47 +00:00
|
|
|
if this.contacts[data.Address] != nil {
|
|
|
|
return nil, errors.New("Contact already exists with this address")
|
|
|
|
}
|
2016-10-27 19:57:04 +00:00
|
|
|
for _, contact := range this.contacts {
|
2017-09-24 20:15:53 +00:00
|
|
|
if contact.Nickname() == data.Nickname {
|
2016-10-27 19:57:04 +00:00
|
|
|
return nil, errors.New("Contact already exists with this nickname")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:25:50 +00:00
|
|
|
// XXX check inbound requests (but this can be called for an inbound req too)
|
2016-10-27 19:57:04 +00:00
|
|
|
|
|
|
|
// Write new contact into config
|
2017-09-24 20:15:53 +00:00
|
|
|
config := this.core.Config.Lock()
|
|
|
|
if config.Contacts == nil {
|
|
|
|
config.Contacts = make(map[string]*ricochet.Contact)
|
2016-10-27 19:57:04 +00:00
|
|
|
}
|
2017-09-24 21:55:47 +00:00
|
|
|
config.Contacts[data.Address] = data
|
2017-09-24 20:15:53 +00:00
|
|
|
this.core.Config.Unlock()
|
2016-10-27 19:57:04 +00:00
|
|
|
|
|
|
|
// Create Contact
|
2017-09-24 21:55:47 +00:00
|
|
|
contact, err := ContactFromConfig(this.core, data, this.events)
|
2016-10-27 19:57:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-24 21:55:47 +00:00
|
|
|
this.contacts[data.Address] = contact
|
2016-10-27 19:57:04 +00:00
|
|
|
|
|
|
|
event := ricochet.ContactEvent{
|
|
|
|
Type: ricochet.ContactEvent_ADD,
|
|
|
|
Subject: &ricochet.ContactEvent_Contact{
|
|
|
|
Contact: contact.Data(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
this.events.Publish(event)
|
|
|
|
|
2017-08-10 17:25:50 +00:00
|
|
|
// XXX Should this be here? Is it ok for inbound where we might pass conn over momentarily?
|
2016-10-28 22:08:16 +00:00
|
|
|
contact.StartConnection()
|
2016-10-27 19:57:04 +00:00
|
|
|
return contact, nil
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 17:25:50 +00:00
|
|
|
// AddContactRequest creates a new outbound contact request with the given parameters,
|
|
|
|
// adds it to the contact list, and returns the newly constructed Contact.
|
|
|
|
//
|
|
|
|
// If an inbound request already exists for this address, that request will be automatically
|
|
|
|
// accepted, and the returned contact will already be fully established.
|
|
|
|
func (cl *ContactList) AddContactRequest(address, name, fromName, text string) (*Contact, error) {
|
2017-09-24 20:15:53 +00:00
|
|
|
if !IsAddressValid(address) {
|
2017-08-10 17:25:50 +00:00
|
|
|
return nil, errors.New("Invalid ricochet address")
|
|
|
|
}
|
|
|
|
if !IsNicknameAcceptable(name) {
|
|
|
|
return nil, errors.New("Invalid nickname")
|
|
|
|
}
|
|
|
|
if len(fromName) > 0 && !IsNicknameAcceptable(fromName) {
|
|
|
|
return nil, errors.New("Invalid 'from' nickname")
|
|
|
|
}
|
|
|
|
if len(text) > 0 && !IsMessageAcceptable(text) {
|
|
|
|
return nil, errors.New("Invalid message")
|
|
|
|
}
|
|
|
|
|
2017-09-24 20:15:53 +00:00
|
|
|
data := &ricochet.Contact{
|
|
|
|
Address: address,
|
2017-08-10 17:25:50 +00:00
|
|
|
Nickname: name,
|
|
|
|
WhenCreated: time.Now().Format(time.RFC3339),
|
2017-09-24 20:15:53 +00:00
|
|
|
Request: &ricochet.ContactRequest{
|
|
|
|
Direction: ricochet.ContactRequest_OUTBOUND,
|
|
|
|
Address: address,
|
|
|
|
Nickname: name,
|
|
|
|
FromNickname: fromName,
|
|
|
|
Text: text,
|
|
|
|
WhenCreated: time.Now().Format(time.RFC3339),
|
2017-08-10 17:25:50 +00:00
|
|
|
},
|
|
|
|
}
|
2017-09-24 20:15:53 +00:00
|
|
|
contact, err := cl.AddNewContact(data)
|
2017-08-10 17:25:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if inboundRequest := cl.InboundRequestByAddress(address); inboundRequest != nil {
|
|
|
|
contact.UpdateContactRequest("Accepted")
|
|
|
|
inboundRequest.AcceptWithContact(contact)
|
|
|
|
}
|
|
|
|
|
|
|
|
return contact, nil
|
|
|
|
}
|
|
|
|
|
2016-09-02 16:51:24 +00:00
|
|
|
func (this *ContactList) RemoveContact(contact *Contact) error {
|
|
|
|
this.mutex.Lock()
|
|
|
|
defer this.mutex.Unlock()
|
|
|
|
|
2017-09-24 21:55:47 +00:00
|
|
|
address := contact.Address()
|
|
|
|
if this.contacts[address] != contact {
|
2016-09-02 16:51:24 +00:00
|
|
|
return errors.New("Not in contact list")
|
|
|
|
}
|
|
|
|
|
2017-08-14 02:04:35 +00:00
|
|
|
// XXX How do we safely make sure that the contact has stopped everything, and that
|
|
|
|
// nobody is going to block on it or keep referencing it..? This is insufficient, it
|
|
|
|
// leaves a goroutine up among other things.
|
2016-10-28 22:08:16 +00:00
|
|
|
contact.StopConnection()
|
|
|
|
|
2017-09-24 20:15:53 +00:00
|
|
|
config := this.core.Config.Lock()
|
2017-09-24 21:55:47 +00:00
|
|
|
delete(config.Contacts, address)
|
2017-09-24 20:15:53 +00:00
|
|
|
this.core.Config.Unlock()
|
2016-10-28 22:08:16 +00:00
|
|
|
|
2017-09-24 21:55:47 +00:00
|
|
|
delete(this.contacts, address)
|
2016-09-02 16:51:24 +00:00
|
|
|
|
|
|
|
event := ricochet.ContactEvent{
|
|
|
|
Type: ricochet.ContactEvent_DELETE,
|
|
|
|
Subject: &ricochet.ContactEvent_Contact{
|
|
|
|
Contact: &ricochet.Contact{
|
2017-09-24 21:55:47 +00:00
|
|
|
Address: address,
|
2016-09-02 16:51:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
this.events.Publish(event)
|
|
|
|
|
|
|
|
return nil
|
2016-07-01 03:18:55 +00:00
|
|
|
}
|
2016-10-28 22:08:16 +00:00
|
|
|
|
2017-08-10 17:25:50 +00:00
|
|
|
// AddOrUpdateInboundContactRequest creates or modifies an inbound contact request for
|
|
|
|
// the hostname, with an optional nickname suggestion and introduction message
|
|
|
|
//
|
|
|
|
// The nickname, message, and address must be validated before calling this function.
|
|
|
|
//
|
|
|
|
// This function may change the state of the request, and the caller is responsible for
|
|
|
|
// sending a reply message and closing the channel or connection as appropriate. If the
|
|
|
|
// request is still active when the state changes spontaneously later (e.g. it's accepted
|
|
|
|
// by the user), replies will be sent by InboundContactRequest.
|
|
|
|
//
|
|
|
|
// This function may return either an InboundContactRequest (which may be pending or already
|
|
|
|
// rejected), an existing contact (which should be treated as accepting the request), or
|
|
|
|
// neither, which is considered a rejection.
|
|
|
|
func (cl *ContactList) AddOrUpdateInboundContactRequest(address, nickname, message string) (*InboundContactRequest, *Contact) {
|
|
|
|
cl.mutex.Lock()
|
|
|
|
defer cl.mutex.Unlock()
|
|
|
|
|
|
|
|
// Look up existing request
|
|
|
|
if request := cl.inboundRequests[address]; request != nil {
|
|
|
|
// Errors in Update will change the state of the request, which the caller sends as a reply
|
|
|
|
request.Update(nickname, message)
|
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for existing contacts or outbound contact requests
|
|
|
|
for _, contact := range cl.contacts {
|
|
|
|
if contact.Address() == address {
|
|
|
|
if contact.IsRequest() {
|
|
|
|
contact.UpdateContactRequest("Accepted")
|
|
|
|
}
|
|
|
|
return nil, contact
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new request
|
|
|
|
request := CreateInboundContactRequest(cl.core, address, nickname, message)
|
|
|
|
request.StatusChanged = cl.inboundRequestChanged
|
|
|
|
cl.inboundRequests[address] = request
|
|
|
|
// XXX update config
|
|
|
|
requestData := request.Data()
|
|
|
|
event := ricochet.ContactEvent{
|
|
|
|
Type: ricochet.ContactEvent_ADD,
|
|
|
|
Subject: &ricochet.ContactEvent_Request{
|
|
|
|
Request: &requestData,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cl.events.Publish(event)
|
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveInboundContactRequest removes the record of an inbound contact request,
|
|
|
|
// without taking any other actions. Generally you will want to act on a request with
|
|
|
|
// Accept() or Reject(), rather than call this function directly, but it is valid to
|
|
|
|
// call on any inbound request regardless.
|
|
|
|
func (cl *ContactList) RemoveInboundContactRequest(request *InboundContactRequest) error {
|
|
|
|
cl.mutex.Lock()
|
|
|
|
defer cl.mutex.Unlock()
|
|
|
|
requestData := request.Data()
|
|
|
|
|
|
|
|
if cl.inboundRequests[requestData.Address] != request {
|
|
|
|
return errors.New("Request is not in contact list")
|
|
|
|
}
|
|
|
|
|
2017-09-23 21:56:29 +00:00
|
|
|
// Close connection asynchronously to avoid potential deadlocking
|
|
|
|
go request.CloseConnection()
|
2017-08-10 17:25:50 +00:00
|
|
|
// XXX Remove from config
|
|
|
|
|
|
|
|
delete(cl.inboundRequests, requestData.Address)
|
|
|
|
|
|
|
|
event := ricochet.ContactEvent{
|
|
|
|
Type: ricochet.ContactEvent_DELETE,
|
|
|
|
Subject: &ricochet.ContactEvent_Request{
|
|
|
|
Request: &requestData,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cl.events.Publish(event)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// inboundRequestChanged is called by the StatusChanged callback of InboundContactRequest
|
|
|
|
// after the request has been modified, such as through Update() or Reject(). Accepted
|
|
|
|
// requests do not pass through this function, because they're immediately removed.
|
|
|
|
func (cl *ContactList) inboundRequestChanged(request *InboundContactRequest) {
|
|
|
|
// XXX update config
|
|
|
|
requestData := request.Data()
|
|
|
|
event := ricochet.ContactEvent{
|
|
|
|
Type: ricochet.ContactEvent_UPDATE,
|
|
|
|
Subject: &ricochet.ContactEvent_Request{
|
|
|
|
Request: &requestData,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cl.events.Publish(event)
|
|
|
|
}
|
|
|
|
|
2016-10-28 22:08:16 +00:00
|
|
|
func (this *ContactList) StartConnections() {
|
|
|
|
for _, contact := range this.Contacts() {
|
|
|
|
contact.StartConnection()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ContactList) StopConnections() {
|
|
|
|
for _, contact := range this.Contacts() {
|
|
|
|
contact.StopConnection()
|
|
|
|
}
|
|
|
|
}
|