mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-08-05 12:17:06 +00:00
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package telegram
|
|
|
|
import (
|
|
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/zelenin/go-tdlib/client"
|
|
)
|
|
|
|
// StartLoginWizard initiates a loginWizard object
|
|
func (c *Client) StartLoginWizard(inCommand bool) {
|
|
if c.loginWizard == nil {
|
|
c.loginWizard = &loginWizardMetadata{
|
|
nextStage: make(chan string, 1),
|
|
commandSent: inCommand,
|
|
}
|
|
} else {
|
|
c.loginWizard.commandSent = inCommand
|
|
}
|
|
}
|
|
|
|
// StopLoginWizard safely destroys the loginWizard object
|
|
func (c *Client) StopLoginWizard() {
|
|
c.locks.loginWizardReadLock.Lock()
|
|
c.locks.loginWizardWriteLock.Lock()
|
|
if c.loginWizard != nil {
|
|
close(c.loginWizard.nextStage)
|
|
c.loginWizard = nil
|
|
}
|
|
c.locks.loginWizardReadLock.Unlock()
|
|
c.locks.loginWizardWriteLock.Unlock()
|
|
}
|
|
|
|
// GetLoginWizardNextStage waits for the next stage from the channel
|
|
func (c *Client) GetLoginWizardNextStage() string {
|
|
c.locks.loginWizardReadLock.Lock()
|
|
defer c.locks.loginWizardReadLock.Unlock()
|
|
|
|
if c.loginWizard != nil {
|
|
if c.loginWizard.commandSent {
|
|
log.Debugf("waiting for nextStage...")
|
|
nextStage := <-c.loginWizard.nextStage
|
|
c.loginWizard.commandSent = false
|
|
c.loginWizard.chanBusy = false
|
|
log.Debugf("yielded stage %v", nextStage)
|
|
return nextStage
|
|
} else {
|
|
if c.lastAuthorizationStateType == client.TypeAuthorizationStateWaitPhoneNumber ||
|
|
c.lastAuthorizationStateType == client.TypeAuthorizationStateClosing ||
|
|
c.Session.Login == "" {
|
|
return "login"
|
|
}
|
|
switch c.lastAuthorizationStateType {
|
|
case client.TypeAuthorizationStateWaitCode:
|
|
return "code"
|
|
case client.TypeAuthorizationStateWaitPassword:
|
|
return "password"
|
|
}
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
func (c *Client) wizardStageOrPrompt(stage, message string) {
|
|
c.locks.loginWizardWriteLock.Lock()
|
|
if c.loginWizard == nil {
|
|
c.locks.loginWizardWriteLock.Unlock()
|
|
if message != "" {
|
|
gateway.SendServiceMessage(c.jid, message, c.xmpp)
|
|
}
|
|
} else {
|
|
if !c.loginWizard.chanBusy {
|
|
log.Debugf("writing wizard stage %v", stage)
|
|
c.loginWizard.nextStage <- stage
|
|
} else {
|
|
log.Warn("Skipping stage %v, wizard cannot keep up", stage)
|
|
}
|
|
c.loginWizard.chanBusy = true
|
|
c.locks.loginWizardWriteLock.Unlock()
|
|
}
|
|
}
|