this is the main cwtch gui with the pretty interface https://cwtch.im
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

peerHandler.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package handlers
  2. import (
  3. "cwtch.im/cwtch/event"
  4. "cwtch.im/cwtch/protocol/connections"
  5. "cwtch.im/ui/go/the"
  6. "cwtch.im/ui/go/ui"
  7. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  8. "time"
  9. )
  10. func PeerHandler(uiManager *ui.Manager, subscribed chan bool) {
  11. q := event.NewQueue()
  12. the.EventBus.Subscribe(event.NewMessageFromPeer, q)
  13. the.EventBus.Subscribe(event.PeerAcknowledgement, q)
  14. the.EventBus.Subscribe(event.NewMessageFromGroup, q)
  15. the.EventBus.Subscribe(event.NewGroupInvite, q)
  16. the.EventBus.Subscribe(event.SendMessageToGroupError, q)
  17. the.EventBus.Subscribe(event.SendMessageToPeerError, q)
  18. the.EventBus.Subscribe(event.ServerStateChange, q)
  19. the.EventBus.Subscribe(event.PeerStateChange, q)
  20. the.EventBus.Subscribe(event.PeerCreated, q)
  21. the.EventBus.Subscribe(event.NetworkStatus, q)
  22. subscribed <- true
  23. networkOffline := false
  24. for {
  25. e := q.Next()
  26. switch e.EventType {
  27. case event.NetworkStatus:
  28. the.AppBus.Publish(*e)
  29. if e.Data["Status"] == "Success" && networkOffline {
  30. networkOffline = false
  31. // TODO we may have to reinitialize the peer
  32. } else {
  33. networkOffline = true
  34. }
  35. case event.NewMessageFromPeer: //event.TimestampReceived, event.RemotePeer, event.Data
  36. ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampReceived])
  37. uiManager.AddMessage(e.Data[event.RemotePeer], e.Data[event.RemotePeer], e.Data[event.Data], false, e.EventID, ts, true)
  38. if the.Peer.GetContact(e.Data[event.RemotePeer]) == nil {
  39. the.Peer.AddContact(e.Data[event.RemotePeer], e.Data[event.RemotePeer], false)
  40. }
  41. case event.PeerAcknowledgement:
  42. uiManager.Acknowledge(e.Data[event.EventID])
  43. case event.NewMessageFromGroup: //event.TimestampReceived, event.TimestampSent, event.Data, event.GroupID, event.RemotePeer
  44. ts, _ := time.Parse(time.RFC3339Nano, e.Data[event.TimestampSent])
  45. uiManager.AddMessage(e.Data[event.GroupID], e.Data[event.RemotePeer], e.Data[event.Data], e.Data[event.RemotePeer] == the.Peer.GetProfile().Onion, e.Data[event.Signature], ts, true)
  46. case event.NewGroupInvite:
  47. gid, err := the.Peer.GetProfile().ProcessInvite(e.Data[event.GroupInvite], e.Data[event.RemotePeer])
  48. group := the.Peer.GetGroup(gid)
  49. if err == nil && group != nil {
  50. uiManager.AddContact(gid)
  51. }
  52. case event.PeerCreated:
  53. onion := e.Data[event.RemotePeer]
  54. uiManager.AddContact(onion)
  55. case event.SendMessageToGroupError:
  56. uiManager.AddSendMessageError(e.Data[event.GroupServer], e.Data[event.Signature], e.Data[event.Error])
  57. case event.SendMessageToPeerError:
  58. uiManager.AddSendMessageError(e.Data[event.RemotePeer], e.Data[event.EventID], e.Data[event.Error])
  59. case event.PeerStateChange:
  60. cxnState := connections.ConnectionStateToType[e.Data[event.ConnectionState]]
  61. // if it's not in the.PeerHandler it's new. Only add once Authed
  62. if _, exists := the.Peer.GetProfile().Contacts[e.Data[event.RemotePeer]]; !exists {
  63. // Contact does not exist, we will add them but we won't know who they are until they are authenticated
  64. // So if we get any other state from an unknown contact we do nothing
  65. // (the next exists check will fail)
  66. if cxnState == connections.AUTHENTICATED {
  67. the.Peer.AddContact(e.Data[event.RemotePeer], e.Data[event.RemotePeer], false)
  68. uiManager.AddContact(e.Data[event.RemotePeer])
  69. }
  70. }
  71. // if it's in the.PeerHandler its either existing and needs an update or not in the UI and needs to be added
  72. if contact, exists := the.Peer.GetProfile().Contacts[e.Data[event.RemotePeer]]; exists {
  73. contact.State = e.Data[event.ConnectionState]
  74. uiManager.UpdateContactStatus(contact.Onion, int(cxnState), false)
  75. }
  76. case event.ServerStateChange:
  77. serverOnion := e.Data[event.GroupServer]
  78. state := connections.ConnectionStateToType[e.Data[event.ConnectionState]]
  79. groups := the.Peer.GetGroups()
  80. for _, groupID := range groups {
  81. group := the.Peer.GetGroup(groupID)
  82. if group != nil && group.GroupServer == serverOnion {
  83. group.State = e.Data[event.ConnectionState]
  84. loading := false
  85. if state == connections.AUTHENTICATED {
  86. loading = true
  87. }
  88. uiManager.UpdateContactStatus(group.GroupID, int(state), loading)
  89. } else {
  90. log.Errorf("found group that is nil :/")
  91. }
  92. }
  93. }
  94. }
  95. }