diff --git a/telegram/client.go b/telegram/client.go index 0ce3cc1..445281a 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -57,12 +57,19 @@ type IntPair struct { type barrier struct { mu sync.Mutex open bool + released bool releaseCh chan struct{} } // Wait blocks until the barrier is released. func (b *barrier) Wait() { b.mu.Lock() + + if b.released { + b.mu.Unlock() + return + } + if !b.open { b.open = true b.releaseCh = make(chan struct{}) // Reinitialize the channel @@ -81,6 +88,8 @@ func (b *barrier) Done() { if b.open { close(b.releaseCh) // Close the channel to release waiting goroutines b.open = false // Mark the barrier as closed + } else { + b.released = true } } diff --git a/telegram/connect.go b/telegram/connect.go index b0a2ca0..afdf5f3 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -145,9 +145,11 @@ func (c *Client) Connect(resource string) error { c.Session.Login = c.me.PhoneNumber } + log.Debug("waiting for loginFinish") c.locks.loginFinish.Wait() go c.updateHandler() + log.Warn("Going online") c.online = true c.locks.authorizationReady.Unlock() c.addResource(resource) @@ -163,6 +165,7 @@ func (c *Client) Connect(resource string) error { gateway.SubscribeToTransport(c.xmpp, c.jid) c.sendPresence(gateway.SPStatus("Logged in as: " + c.Session.Login)) }() + log.Warn("Client connected!") return nil } @@ -248,6 +251,7 @@ func (c *Client) Disconnect(resource string, quit bool) bool { } func (c *Client) interactor() { + wasSessionLoginEmpty := c.Session.Login == "" for { c.locks.authorizerReadLock.Lock() if c.authorizer == nil { @@ -293,7 +297,11 @@ func (c *Client) interactor() { c.locks.authorizerReadLock.Unlock() } if c.loginStage != LoginStageCancel { - c.wizardStageOrPrompt(LoginStagePreset, "Do you want to use a config preset? `/preset modern` enables brand new XMPP features, `/preset classic` targets legacy clients stuck in 00s. /pass proceeds to the next stage.") + if wasSessionLoginEmpty { + c.wizardStageOrPrompt(LoginStagePreset, "Do you want to use a config preset? `/preset modern` enables brand new XMPP features, `/preset classic` targets legacy clients stuck in 00s. /pass proceeds to the next stage.") + } else { + c.wizardStageOrPrompt(LoginStageSuccess, "") + } } } diff --git a/telegram/loginwizard.go b/telegram/loginwizard.go index 695507e..1eb4ac8 100644 --- a/telegram/loginwizard.go +++ b/telegram/loginwizard.go @@ -26,6 +26,7 @@ func (c *Client) setLoginStage(stage LoginStage) { case LoginStageSuccess, LoginStageCancel: c.locks.loginFinish.Done() } + log.Debugf("set loginStage %v", stage) } // StartLoginWizard initiates a loginWizard object