package characters import ( "cwtch.im/cwtch/event" "cwtch.im/cwtch/protocol/connections" "cwtch.im/ui/go/cwutil" "cwtch.im/ui/go/gobjects" "cwtch.im/ui/go/gothings" "cwtch.im/ui/go/the" "git.openprivacy.ca/openprivacy/libricochet-go/log" "time" ) func IncomingListener(uiState *gothings.InterfaceState, subscribed chan bool) { q := event.NewEventQueue(1000) the.EventBus.Subscribe(event.NewMessageFromPeer, q.EventChannel) the.EventBus.Subscribe(event.NewMessageFromGroup, q.EventChannel) the.EventBus.Subscribe(event.NewGroupInvite, q.EventChannel) the.EventBus.Subscribe(event.SendMessageToGroupError, q.EventChannel) the.EventBus.Subscribe(event.SendMessageToPeerError, q.EventChannel) the.EventBus.Subscribe(event.ServerStateChange, q.EventChannel) the.EventBus.Subscribe(event.PeerStateChange, q.EventChannel) subscribed <- true for { e := q.Next() switch e.EventType { case event.NewMessageFromPeer: //event.TimestampReceived, event.RemotePeer, event.Data ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampReceived]) uiState.AddMessage(&gobjects.Message{ Handle: e.Data[event.RemotePeer], From: e.Data[event.RemotePeer], Message: e.Data[event.Data], Image: cwutil.RandomProfileImage(e.Data[event.RemotePeer]), Timestamp: ts, }) if the.Peer.GetContact(e.Data[event.RemotePeer]) == nil { the.Peer.AddContact(e.Data[event.RemotePeer], e.Data[event.RemotePeer], false) } the.Peer.PeerWithOnion(e.Data[event.RemotePeer]) if e.Data[event.Data] != "ack" { the.Peer.SendMessageToPeer(e.Data[event.RemotePeer], "ack") } case event.NewMessageFromGroup: //event.TimestampReceived, event.TimestampSent, event.Data, event.GroupID, event.RemotePeer var name string var exists bool ctc := the.Peer.GetContact(e.Data[event.RemotePeer]) if ctc != nil { name, exists = ctc.GetAttribute("nick") if !exists || name == "" { name = e.Data[event.RemotePeer] } } else { name = e.Data[event.RemotePeer] } ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampSent]) uiState.AddMessage(&gobjects.Message{ MessageID: e.Data[event.Signature], Handle: e.Data[event.GroupID], From: e.Data[event.RemotePeer], Message: e.Data[event.Data], Image: cwutil.RandomProfileImage(e.Data[event.RemotePeer]), FromMe: e.Data[event.RemotePeer] == the.Peer.GetProfile().Onion, Timestamp: ts, Acknowledged: true, DisplayName: name, }) case event.NewGroupInvite: log.Debugf("got a group invite!") case event.SendMessageToGroupError: uiState.AddSendMessageError(e.Data[event.GroupServer], e.Data[event.Signature], e.Data[event.Error]) case event.SendMessageToPeerError: uiState.AddSendMessageError(e.Data[event.RemotePeer], e.Data[event.Signature], e.Data[event.Error]) case event.PeerStateChange: cxnState := connections.ConnectionStateType[e.Data[event.ConnectionState]] if contact, exists := the.Peer.GetProfile().Contacts[e.Data[event.RemotePeer]]; exists { uiContact := uiState.GetContact(contact.Onion) if uiContact != nil && uiContact.Status != int(cxnState) { uiContact.Status = int(cxnState) uiState.UpdateContact(contact.Onion) } } else { the.Peer.PeerWithOnion(contact.Onion) c2 := uiState.GetContact(contact.Onion) if c2 != nil && c2.Status != -2 { c2.Status = -2 uiState.UpdateContact(contact.Onion) } } case event.ServerStateChange: serverOnion := e.Data[event.GroupServer] state := connections.ConnectionStateType[e.Data[event.ConnectionState]] groups := the.Peer.GetGroups() for _, groupID := range groups { group := the.Peer.GetGroup(groupID) if group != nil && group.GroupServer == serverOnion { uiState.GetContact(group.GroupID).Status = int(state) if state == connections.AUTHENTICATED { uiState.GetContact(group.GroupID).Loading = true } else { uiState.GetContact(group.GroupID).Loading = false } uiState.UpdateContact(group.GroupID) } else { log.Errorf("found group that is nil :/") } } } } }