More relogin fixes, prevent crashing by commands when offline
This commit is contained in:
parent
18b5bc0935
commit
ab914b0ff7
|
@ -52,6 +52,7 @@ type Client struct {
|
|||
Session *persistence.Session
|
||||
content *config.TelegramContentConfig
|
||||
cache *cache
|
||||
online bool
|
||||
|
||||
locks clientLocks
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ import (
|
|||
|
||||
const notEnoughArguments string = "Not enough arguments"
|
||||
const telegramNotInitialized string = "Telegram connection is not initialized yet"
|
||||
const notOnline string = "Not online"
|
||||
|
||||
var transportCommands = map[string]command{
|
||||
"login": command{"phone", "sign in"},
|
||||
|
@ -159,17 +160,31 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
|
|||
cmd, args := parseCommand(cmdline)
|
||||
switch cmd {
|
||||
case "login", "code", "password":
|
||||
if cmd == "login" {
|
||||
if c.Session.Login != "" {
|
||||
return ""
|
||||
} else if !c.Online() {
|
||||
c.Connect()
|
||||
}
|
||||
if cmd == "login" && c.Session.Login != "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if len(args) < 1 {
|
||||
return notEnoughArguments
|
||||
}
|
||||
|
||||
if cmd == "login" {
|
||||
wasSessionLoginEmpty := c.Session.Login == ""
|
||||
c.Session.Login = args[0]
|
||||
|
||||
if wasSessionLoginEmpty && c.authorizer == nil {
|
||||
go func() {
|
||||
err := c.Connect()
|
||||
if err != nil {
|
||||
log.Error(errors.Wrap(err, "TDlib connection failure"))
|
||||
}
|
||||
}()
|
||||
// a quirk for authorizer to become ready. If it's still not,
|
||||
// nothing bad: the command just needs to be resent again
|
||||
time.Sleep(1e5)
|
||||
}
|
||||
}
|
||||
|
||||
if c.authorizer == nil {
|
||||
return telegramNotInitialized
|
||||
}
|
||||
|
@ -178,7 +193,6 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
|
|||
// sign in
|
||||
case "login":
|
||||
c.authorizer.PhoneNumber <- args[0]
|
||||
c.Session.Login = args[0]
|
||||
// check auth code
|
||||
case "code":
|
||||
c.authorizer.Code <- args[0]
|
||||
|
@ -188,6 +202,10 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
|
|||
}
|
||||
// sign out
|
||||
case "logout":
|
||||
if !c.Online() {
|
||||
return notOnline
|
||||
}
|
||||
|
||||
for id := range c.cache.chats {
|
||||
c.unsubscribe(id)
|
||||
}
|
||||
|
@ -201,6 +219,10 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
|
|||
c.Session.Login = ""
|
||||
// set @username
|
||||
case "setusername":
|
||||
if !c.Online() {
|
||||
return notOnline
|
||||
}
|
||||
|
||||
var username string
|
||||
if len(args) > 0 {
|
||||
username = args[0]
|
||||
|
@ -214,6 +236,10 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
|
|||
}
|
||||
// set My Name
|
||||
case "setname":
|
||||
if !c.Online() {
|
||||
return notOnline
|
||||
}
|
||||
|
||||
var firstname string
|
||||
var lastname string
|
||||
if len(args) > 0 {
|
||||
|
@ -232,6 +258,10 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
|
|||
}
|
||||
// set About
|
||||
case "setbio":
|
||||
if !c.Online() {
|
||||
return notOnline
|
||||
}
|
||||
|
||||
_, err := c.client.SetBio(&client.SetBioRequest{
|
||||
Bio: strings.Join(args, " "),
|
||||
})
|
||||
|
@ -240,6 +270,10 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
|
|||
}
|
||||
// set password
|
||||
case "setpassword":
|
||||
if !c.Online() {
|
||||
return notOnline
|
||||
}
|
||||
|
||||
var oldPassword string
|
||||
var newPassword string
|
||||
// 0 or 1 argument is ignored and the password is reset
|
||||
|
@ -287,6 +321,10 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
|
|||
// ProcessChatCommand executes a command sent in a mapped chat
|
||||
// and returns a response and the status of command support
|
||||
func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) {
|
||||
if !c.Online() {
|
||||
return notOnline, true
|
||||
}
|
||||
|
||||
cmd, args := parseCommand(cmdline)
|
||||
switch cmd {
|
||||
// delete last message(s)
|
||||
|
|
|
@ -118,6 +118,7 @@ func (c *Client) Connect() error {
|
|||
c.client = tdlibClient
|
||||
c.listener = tdlibClient.GetListener()
|
||||
c.locks.authorizationReady.Done()
|
||||
c.online = true
|
||||
|
||||
go c.updateHandler()
|
||||
|
||||
|
@ -206,11 +207,12 @@ func (c *Client) interactor() {
|
|||
}
|
||||
|
||||
func (c *Client) forceClose() {
|
||||
c.listener.Close()
|
||||
//c.listener.Close()
|
||||
c.online = false
|
||||
c.authorizer = nil
|
||||
}
|
||||
|
||||
// Online checks if the updates listener is alive
|
||||
func (c *Client) Online() bool {
|
||||
return c.listener != nil && c.listener.IsActive()
|
||||
return c.listener != nil && c.online //c.listener.IsActive()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue