diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2019-12-19 23:29:36 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2019-12-19 23:29:36 +0300 |
commit | ab914b0ff764de13d5496a12a1315c72dd3376fc (patch) | |
tree | 665ce28f6ee101b7c58121390dcae217e57e8786 /telegram/commands.go | |
parent | 18b5bc09356dd80024ecfd411145b3312b3fa7a1 (diff) |
More relogin fixes, prevent crashing by commands when offline
Diffstat (limited to 'telegram/commands.go')
-rw-r--r-- | telegram/commands.go | 52 |
1 files changed, 45 insertions, 7 deletions
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) |