diff --git a/go/characters/appEventListener.go b/go/characters/appEventListener.go new file mode 100644 index 00000000..94098860 --- /dev/null +++ b/go/characters/appEventListener.go @@ -0,0 +1,98 @@ +package characters + +import ( + "cwtch.im/cwtch/event" + "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" + "os" +) + +func AppEventListener(gcd *gothings.GrandCentralDispatcher) { + q := event.NewEventQueue(1000) + the.AppBus.Subscribe(event.NewPeer, q.EventChannel) + the.AppBus.Subscribe(event.PeerError, q.EventChannel) + the.AppBus.Subscribe(event.AppError, q.EventChannel) + + for { + e := q.Next() + + switch e.EventType { + case event.PeerError: + // current only case + log.Errorf("couldn't load profiles: %v", e.Data[event.Error]) + os.Exit(1) + + case event.AppError: + + if e.Data[event.Error] == event.AppErrLoaded0 { + log.Infoln("couldn't load your config file. attempting to create one now") + + the.CwtchApp.CreatePeer("alice", "be gay do crime") + } + + case event.NewPeer: + if the.Peer != nil { + continue + } + onion := e.Data[event.Identity] + + the.Peer = the.CwtchApp.GetPeer(onion) + the.EventBus = the.CwtchApp.GetEventBus(onion) + + gcd.UpdateMyProfile(the.Peer.GetProfile().Name, the.Peer.GetProfile().Onion, cwutil.RandomProfileImage(the.Peer.GetProfile().Onion)) + the.CwtchApp.LaunchPeers() + + contacts := the.Peer.GetContacts() + for i := range contacts { + contact, _ := the.Peer.GetProfile().GetContact(contacts[i]) + displayName, _ := contact.GetAttribute("nick") + deleted, _ := contact.GetAttribute("deleted") + if deleted != "deleted" { + gcd.UIState.AddContact(&gobjects.Contact{ + Handle: contacts[i], + DisplayName: displayName, + Image: cwutil.RandomProfileImage(contacts[i]), + Trusted: contact.Trusted, + Loading: false, + }) + } + } + + groups := the.Peer.GetGroups() + for i := range groups { + group := the.Peer.GetGroup(groups[i]) + nick, exists := group.GetAttribute("nick") + if !exists { + nick = group.GroupID[:12] + } + deleted, _ := group.GetAttribute("deleted") + // Only join servers for active and explicitly accepted groups. + if deleted != "deleted" { + + gcd.UIState.AddContact(&gobjects.Contact{ + Handle: group.GroupID, + DisplayName: nick, + Image: cwutil.RandomGroupImage(group.GroupID), + Server: group.GroupServer, + Trusted: group.Accepted, + Loading: true, + }) + } + } + go IncomingListener(&gcd.UIState) + go TorStatusPoller(gcd.TorStatus, the.ACN) + go PresencePoller(&gcd.UIState) + + // load ui preferences + gcd.RequestSettings() + locale, exists := the.Peer.GetProfile().GetAttribute("settings.locale") + if exists { + gcd.SetLocale_helper(locale) + } + } + } + +} diff --git a/go/the/globals.go b/go/the/globals.go index 72171dc2..2d75bd7c 100644 --- a/go/the/globals.go +++ b/go/the/globals.go @@ -8,7 +8,8 @@ import ( ) var CwtchApp app.Application -var EventBus *event.Manager +var EventBus event.Manager +var AppBus event.Manager var ACN connectivity.ACN var Peer libPeer.CwtchPeer var CwtchDir string diff --git a/main.go b/main.go index a2caea21..36009b73 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( libapp "cwtch.im/cwtch/app" "cwtch.im/ui/go/characters" - "cwtch.im/ui/go/cwutil" "cwtch.im/ui/go/gobjects" "cwtch.im/ui/go/gothings" "cwtch.im/ui/go/the" @@ -165,92 +164,11 @@ func loadNetworkingAndFiles(gcd *gothings.GrandCentralDispatcher) { os.Exit(1) } - // these are long-lived pollers/listeners for incoming messages and status changes - loadCwtchData(gcd, the.ACN) - go characters.IncomingListener(&gcd.UIState) - go characters.TorStatusPoller(gcd.TorStatus, the.ACN) - go characters.PresencePoller(&gcd.UIState) - - // load ui preferences - gcd.RequestSettings() - locale, exists := the.Peer.GetProfile().GetAttribute("settings.locale") - if exists { - gcd.SetLocale_helper(locale) - } -} - -// this is mostly going to get factored out when we add profile support -// for now, it loads a single peer and fills the ui with its data -func loadCwtchData(gcd *gothings.GrandCentralDispatcher, acn connectivity.ACN) { - var err error - - /*_, err := app2.NewApp(dirname, "/data/data/org.qtproject.example.go/lib/libtor.so") - if err != nil { - log.Errorf("ERROR CREATING CWTCH APP: %v", err) - } - time.Sleep(time.Second * 10) - */ + the.Peer = nil os.MkdirAll(the.CwtchDir, 0700) - the.CwtchApp = libapp.NewApp(acn, the.CwtchDir) - - err = the.CwtchApp.LoadProfiles("be gay do crime") - if err != nil { - //TODO no more fatalfs - log.Errorf("couldn't load profiles: %v", err) - os.Exit(1) - } - - if len(the.CwtchApp.ListPeers()) == 0 { - log.Infoln("couldn't load your config file. attempting to create one now") - the.Peer, err = the.CwtchApp.CreatePeer("alice", "be gay do crime") - if err != nil { - log.Errorf("couldn't create one. is your cwtch folder writable?") - os.Exit(1) - } - } else { - the.Peer = the.CwtchApp.PrimaryIdentity() - } - the.EventBus = the.CwtchApp.GetEventBus(the.Peer.GetProfile().Onion) - - gcd.UpdateMyProfile(the.Peer.GetProfile().Name, the.Peer.GetProfile().Onion, cwutil.RandomProfileImage(the.Peer.GetProfile().Onion)) - the.CwtchApp.LaunchPeers() - - contacts := the.Peer.GetContacts() - for i := range contacts { - contact, _ := the.Peer.GetProfile().GetContact(contacts[i]) - displayName, _ := contact.GetAttribute("nick") - deleted, _ := contact.GetAttribute("deleted") - if deleted != "deleted" { - gcd.UIState.AddContact(&gobjects.Contact{ - Handle: contacts[i], - DisplayName: displayName, - Image: cwutil.RandomProfileImage(contacts[i]), - Trusted: contact.Trusted, - Loading: false, - }) - } - } - - groups := the.Peer.GetGroups() - for i := range groups { - group := the.Peer.GetGroup(groups[i]) - nick, exists := group.GetAttribute("nick") - if !exists { - nick = group.GroupID[:12] - } - deleted, _ := group.GetAttribute("deleted") - // Only join servers for active and explicitly accepted groups. - if deleted != "deleted" { - - gcd.UIState.AddContact(&gobjects.Contact{ - Handle: group.GroupID, - DisplayName: nick, - Image: cwutil.RandomGroupImage(group.GroupID), - Server: group.GroupServer, - Trusted: group.Accepted, - Loading: true, - }) - } - } + the.CwtchApp = libapp.NewApp(the.ACN, the.CwtchDir) + the.AppBus = the.CwtchApp.GetPrimaryBus() + go characters.AppEventListener(gcd) + the.CwtchApp.LoadProfiles("be gay do crime") }