From ab914b0ff764de13d5496a12a1315c72dd3376fc Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Thu, 19 Dec 2019 22:29:36 +0200 Subject: [PATCH] More relogin fixes, prevent crashing by commands when offline --- telegram/client.go | 1 + telegram/commands.go | 52 ++++++++++++++++++++++++++++++++++++++------ telegram/connect.go | 6 +++-- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/telegram/client.go b/telegram/client.go index 133f1e6..2acd75e 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -52,6 +52,7 @@ type Client struct { Session *persistence.Session content *config.TelegramContentConfig cache *cache + online bool locks clientLocks } diff --git a/telegram/commands.go b/telegram/commands.go index 82e6d38..533c627 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -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) diff --git a/telegram/connect.go b/telegram/connect.go index af97b02..3a5a91f 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -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() }