This repository has been archived on 2021-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
ui/go/characters/appEventListener.go

130 lines
3.5 KiB
Go

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"
"strconv"
)
func AppEventListener(gcd *gothings.GrandCentralDispatcher, subscribed chan bool) {
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)
the.AppBus.Subscribe(event.ACNStatus, q.EventChannel)
the.AppBus.Subscribe(event.ReloadDone, q.EventChannel)
subscribed <- true
for {
e := q.Next()
switch e.EventType {
case event.ACNStatus:
progStr := e.Data[event.Progreess]
percent, _ := strconv.Atoi(progStr)
message := e.Data[event.Status]
var statuscode int
if percent == 0 {
statuscode = 0
message = "can't find tor. is it running? is the controlport configured?"
} else if percent == 100 {
statuscode = 3
message = "tor appears to be running just fine!"
} else if percent < 80 {
statuscode = 1
} else {
statuscode = 2
}
gcd.TorStatus(statuscode, message)
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 {
// TODO: only an error if other profiles are not loaded
log.Infoln("couldn't load your config file. attempting to create one now")
the.CwtchApp.CreatePeer("alice", the.AppPassword)
}
case event.ReloadDone:
if the.Peer == nil {
the.CwtchApp.LoadProfiles(the.AppPassword)
}
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)
incSubscribed := make(chan bool)
go IncomingListener(&gcd.UIState, incSubscribed)
<-incSubscribed
go PresencePoller(&gcd.UIState)
gcd.UpdateMyProfile(the.Peer.GetProfile().Name, the.Peer.GetProfile().Onion, cwutil.RandomProfileImage(the.Peer.GetProfile().Onion))
if e.Data[event.Status] != "running" {
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: false,
})
}
}
// load ui preferences
gcd.RequestSettings()
locale, exists := the.Peer.GetProfile().GetAttribute("settings.locale")
if exists {
gcd.SetLocale_helper(locale)
}
}
}
}