101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"cwtch.im/cwtch/app"
|
|
"cwtch.im/cwtch/app/plugins"
|
|
"cwtch.im/cwtch/event"
|
|
"cwtch.im/ui/go/constants"
|
|
"cwtch.im/ui/go/the"
|
|
"cwtch.im/ui/go/ui"
|
|
"git.openprivacy.ca/openprivacy/log"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
func App(gcd *ui.GrandCentralDispatcher, subscribed chan bool, reloadingAccounts 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
|
|
|
|
gcd.Loaded()
|
|
|
|
for {
|
|
e := q.Next()
|
|
|
|
switch e.EventType {
|
|
|
|
case event.ACNStatus:
|
|
progStr := e.Data[event.Progreess]
|
|
progress, _ := strconv.Atoi(progStr)
|
|
message := e.Data[event.Status]
|
|
var statuscode int
|
|
log.Debugf("ACNStatus: %d %v\n", progress, message)
|
|
if progress >= -2 && progress < 0 {
|
|
statuscode = 0
|
|
} else if progress >= 0 && progress < 50 {
|
|
statuscode = 1
|
|
} else if progress >= 50 && progress < 100 {
|
|
statuscode = 2
|
|
} else {
|
|
statuscode = 3
|
|
}
|
|
|
|
gcd.TorStatus(statuscode)
|
|
|
|
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 {
|
|
if reloadingAccounts {
|
|
reloadingAccounts = false
|
|
} else {
|
|
gcd.ErrorLoaded0()
|
|
}
|
|
}
|
|
|
|
case event.ReloadDone:
|
|
reloadingAccounts = false
|
|
if len(the.CwtchApp.ListPeers()) == 0 {
|
|
the.CwtchApp.LoadProfiles(the.AppPassword)
|
|
}
|
|
case event.NewPeer:
|
|
onion := e.Data[event.Identity]
|
|
p := the.CwtchApp.GetPeer(onion)
|
|
|
|
if tag, exists := p.GetAttribute(app.AttributeTag); !exists || tag == "" {
|
|
p.SetAttribute(app.AttributeTag, constants.ProfileTypeV1DefaultPassword)
|
|
}
|
|
|
|
log.Infof("NewPeer for %v\n", onion)
|
|
ui.AddProfile(gcd, onion)
|
|
|
|
the.CwtchApp.AddPeerPlugin(onion, plugins.CONNECTIONRETRY)
|
|
the.CwtchApp.AddPeerPlugin(onion, plugins.NETWORKCHECK)
|
|
|
|
incSubscribed := make(chan bool)
|
|
go PeerHandler(onion, gcd.GetUiManager(p.GetOnion()), incSubscribed)
|
|
<-incSubscribed
|
|
|
|
if e.Data[event.Status] != "running" {
|
|
p.Listen()
|
|
p.StartPeersConnections()
|
|
p.StartGroupConnections()
|
|
}
|
|
|
|
blockUnkownPeers, exists := p.GetAttribute(constants.BlockUnknownPeersSetting)
|
|
if exists && blockUnkownPeers == "true" {
|
|
the.EventBus.Publish(event.NewEvent(event.BlockUnknownPeers, map[event.Field]string{}))
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|