Add /cancelauth command

This commit is contained in:
Bohdan Horbeshko 2023-06-16 00:34:49 -04:00
parent edf2c08a5b
commit fdd867cf7a
2 changed files with 34 additions and 6 deletions

View file

@ -44,6 +44,7 @@ var permissionsReadonly = client.ChatPermissions{}
var transportCommands = map[string]command{ var transportCommands = map[string]command{
"login": command{"phone", "sign in"}, "login": command{"phone", "sign in"},
"logout": command{"", "sign out"}, "logout": command{"", "sign out"},
"cancelauth": command{"", "quit the signin wizard"},
"code": command{"", "check one-time code"}, "code": command{"", "check one-time code"},
"password": command{"", "check 2fa password"}, "password": command{"", "check 2fa password"},
"setusername": command{"", "update @username"}, "setusername": command{"", "update @username"},
@ -230,7 +231,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
switch cmd { switch cmd {
case "login", "code", "password": case "login", "code", "password":
if cmd == "login" && c.Session.Login != "" { if cmd == "login" && c.Session.Login != "" {
return "" return "Phone number already provided, use /cancelauth to start over"
} }
if len(args) < 1 { if len(args) < 1 {
@ -286,6 +287,13 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
} }
c.Session.Login = "" c.Session.Login = ""
// cancel auth
case "cancelauth":
if c.Online() {
return "Not allowed when online"
}
c.cancelAuth()
return "Cancelled"
// set @username // set @username
case "setusername": case "setusername":
if !c.Online() { if !c.Online() {

View file

@ -24,6 +24,9 @@ type clientAuthorizer struct {
} }
func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.AuthorizationState) error { func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.AuthorizationState) error {
if stateHandler.isClosed {
return errors.New("Channel is closed")
}
stateHandler.State <- state stateHandler.State <- state
switch state.AuthorizationStateType() { switch state.AuthorizationStateType() {
@ -84,6 +87,9 @@ func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.Auth
} }
func (stateHandler *clientAuthorizer) Close() { func (stateHandler *clientAuthorizer) Close() {
if stateHandler.isClosed {
return
}
stateHandler.isClosed = true stateHandler.isClosed = true
close(stateHandler.TdlibParameters) close(stateHandler.TdlibParameters)
close(stateHandler.PhoneNumber) close(stateHandler.PhoneNumber)
@ -191,11 +197,7 @@ func (c *Client) Disconnect(resource string, quit bool) bool {
) )
} }
_, err := c.client.Close() c.close()
if err != nil {
log.Errorf("Couldn't close the Telegram instance: %v; %#v", err, c)
}
c.forceClose()
return true return true
} }
@ -242,6 +244,24 @@ func (c *Client) forceClose() {
c.authorizer = nil c.authorizer = nil
} }
func (c *Client) close() {
if c.authorizer != nil && !c.authorizer.isClosed {
c.authorizer.Close()
}
if c.client != nil {
_, err := c.client.Close()
if err != nil {
log.Errorf("Couldn't close the Telegram instance: %v; %#v", err, c)
}
}
c.forceClose()
}
func (c *Client) cancelAuth() {
c.close()
c.Session.Login = ""
}
// Online checks if the updates listener is alive // Online checks if the updates listener is alive
func (c *Client) Online() bool { func (c *Client) Online() bool {
return c.online return c.online