129 lines
3.4 KiB
Go
129 lines
3.4 KiB
Go
package characters
|
|
|
|
import (
|
|
"cwtch.im/cwtch/app/plugins"
|
|
"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.NewQueue()
|
|
the.AppBus.Subscribe(event.NewPeer, q)
|
|
the.AppBus.Subscribe(event.PeerError, q)
|
|
the.AppBus.Subscribe(event.AppError, q)
|
|
the.AppBus.Subscribe(event.ACNStatus, q)
|
|
the.AppBus.Subscribe(event.ReloadDone, q)
|
|
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.CwtchApp.AddPeerPlugin(onion, plugins.CONTACTRETRY)
|
|
|
|
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))
|
|
|
|
contacts := the.Peer.GetContacts()
|
|
for i := range contacts {
|
|
contact, _ := the.Peer.GetProfile().GetContact(contacts[i])
|
|
displayName, _ := contact.GetAttribute("nick")
|
|
|
|
gcd.UIState.AddContact(&gobjects.Contact{
|
|
Handle: contacts[i],
|
|
DisplayName: displayName,
|
|
Image: cwutil.RandomProfileImage(contacts[i]),
|
|
Trusted: contact.Trusted,
|
|
Blocked: contact.Blocked,
|
|
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]
|
|
}
|
|
// Only join servers for active and explicitly accepted groups.
|
|
gcd.UIState.AddContact(&gobjects.Contact{
|
|
Handle: group.GroupID,
|
|
DisplayName: nick,
|
|
Image: cwutil.RandomGroupImage(group.GroupID),
|
|
Server: group.GroupServer,
|
|
Trusted: group.Accepted,
|
|
Loading: false,
|
|
})
|
|
}
|
|
|
|
if e.Data[event.Status] != "running" {
|
|
the.CwtchApp.LaunchPeers()
|
|
}
|
|
|
|
// load ui preferences
|
|
gcd.RequestSettings()
|
|
locale, exists := the.Peer.GetProfile().GetAttribute("settings.locale")
|
|
if exists {
|
|
gcd.SetLocale_helper(locale)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|