diff --git a/core/contact.go b/core/contact.go index c06998c..d198460 100644 --- a/core/contact.go +++ b/core/contact.go @@ -191,3 +191,30 @@ func (c *Contact) shouldReplaceConnection(conn *protocol.OpenConnection) bool { } return false } + +func (c *Contact) OnConnectionClosed(conn *protocol.OpenConnection) { + c.mutex.Lock() + + if c.connection != conn { + c.mutex.Unlock() + return + } + + c.connection = nil + c.status = ricochet.Contact_OFFLINE + + config := c.core.Config.OpenWrite() + c.data.LastConnected = time.Now().Format(time.RFC3339) + config.Contacts[strconv.Itoa(c.id)] = c.data + config.Save() + + c.mutex.Unlock() + + event := ricochet.ContactEvent{ + Type: ricochet.ContactEvent_UPDATE, + Subject: &ricochet.ContactEvent_Contact{ + Contact: c.Data(), + }, + } + c.events.Publish(event) +} diff --git a/core/protocol.go b/core/protocol.go index 8e6721d..8d81b2c 100644 --- a/core/protocol.go +++ b/core/protocol.go @@ -61,6 +61,16 @@ func (handler *protocolHandler) OnConnect(oc *protocol.OpenConnection) { } } +func (handler *protocolHandler) OnDisconnect(oc *protocol.OpenConnection) { + log.Printf("protocol: OnDisconnect: %v", oc) + if oc.OtherHostname != "" { + contact := handler.p.core.Identity.ContactList().ContactByAddress("ricochet:" + oc.OtherHostname) + if contact != nil { + contact.OnConnectionClosed(oc) + } + } +} + // Authentication Management func (handler *protocolHandler) OnAuthenticationRequest(oc *protocol.OpenConnection, channelID int32, clientCookie [16]byte) { log.Printf("protocol: OnAuthenticationRequest")