143 lines
3.9 KiB
Go
143 lines
3.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"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/libricochet-go/log"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func App(gcd *ui.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.NetworkStatus, q)
|
|
the.AppBus.Subscribe(event.ReloadDone, q)
|
|
subscribed <- true
|
|
|
|
networkOffline := false
|
|
timeSinceLastSuccess := time.Unix(0,0)
|
|
|
|
for {
|
|
e := q.Next()
|
|
|
|
switch e.EventType {
|
|
case event.NetworkStatus:
|
|
status := e.Data[event.Status]
|
|
if status == "Error" && !networkOffline {
|
|
networkOffline = true
|
|
// if it has been more that 5 minutes since we received any kind of success, then we should kill tor
|
|
// anything less that this i.e. transient networking failures, should allow us to reconnect without issue
|
|
if time.Now().Sub(timeSinceLastSuccess) > (time.Minute * 5) {
|
|
the.ACN.Restart()
|
|
}
|
|
}
|
|
|
|
if status == "Success" && networkOffline {
|
|
timeSinceLastSuccess = time.Now()
|
|
networkOffline = false
|
|
}
|
|
|
|
case event.ACNStatus:
|
|
progStr := e.Data[event.Progreess]
|
|
percent, _ := strconv.Atoi(progStr)
|
|
message := e.Data[event.Status]
|
|
var statuscode int
|
|
if percent >= 0 && percent <= 25 {
|
|
statuscode = 1
|
|
message = "Connecting to network"
|
|
} else if percent < 100 {
|
|
statuscode = 2
|
|
message = "Establishing Tor circuit"
|
|
} else if percent == 100 {
|
|
statuscode = 3
|
|
message = "tor appears to be running just fine!"
|
|
} else {
|
|
statuscode = 0
|
|
message = "can't find tor. is it running? is the controlport configured?"
|
|
}
|
|
|
|
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")
|
|
if gcd.Version() == "development" {
|
|
the.CwtchApp.CreatePeer("tester", the.AppPassword)
|
|
} else {
|
|
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.CONNECTIONRETRY)
|
|
the.CwtchApp.AddPeerPlugin(onion, plugins.NETWORKCHECK)
|
|
|
|
the.Peer = the.CwtchApp.GetPeer(onion)
|
|
the.EventBus = the.CwtchApp.GetEventBus(onion)
|
|
|
|
incSubscribed := make(chan bool)
|
|
go PeerHandler(&gcd.UIManager, incSubscribed)
|
|
<-incSubscribed
|
|
|
|
pic, exists := the.Peer.GetAttribute(constants.Picture)
|
|
if !exists {
|
|
pic = ui.RandomProfileImage(the.Peer.GetProfile().Onion)
|
|
the.Peer.SetAttribute(constants.Picture, pic)
|
|
}
|
|
gcd.UpdateMyProfile(the.Peer.GetProfile().Name, the.Peer.GetProfile().Onion, pic)
|
|
|
|
contacts := the.Peer.GetContacts()
|
|
for i := range contacts {
|
|
gcd.UIManager.AddContact(contacts[i])
|
|
}
|
|
|
|
groups := the.Peer.GetGroups()
|
|
for i := range groups {
|
|
// Only join servers for active and explicitly accepted groups.
|
|
gcd.UIManager.AddContact(groups[i])
|
|
}
|
|
|
|
if e.Data[event.Status] != "running" {
|
|
the.CwtchApp.LaunchPeers()
|
|
}
|
|
|
|
// load ui preferences
|
|
gcd.RequestSettings()
|
|
locale, exists := the.Peer.GetProfile().GetAttribute(constants.LocaleSetting)
|
|
if exists {
|
|
gcd.SetLocale_helper(locale)
|
|
}
|
|
|
|
blockUnkownPeers, exists := the.Peer.GetProfile().GetAttribute(constants.BlockUnknownPeersSetting)
|
|
if exists && blockUnkownPeers == "true" {
|
|
the.EventBus.Publish(event.NewEvent(event.BlockUnknownPeers, map[event.Field]string{}))
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|