From 18b5bc09356dd80024ecfd411145b3312b3fa7a1 Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Wed, 18 Dec 2019 23:00:23 +0200 Subject: [PATCH] Relogin fix --- .gitignore | 1 + persistence/sessions.go | 11 ++++++++++- telegram/commands.go | 17 +++++++++++------ telegram/connect.go | 1 + 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index e2dbbf2..58426dd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ config.yml telegabber sessions/ session.dat +session.dat.new diff --git a/persistence/sessions.go b/persistence/sessions.go index 66857f1..fd520d1 100644 --- a/persistence/sessions.go +++ b/persistence/sessions.go @@ -42,7 +42,16 @@ var sessionDB *SessionsYamlDB // SessionMarshaller implementation for YamlDB func SessionMarshaller() ([]byte, error) { - return yaml.Marshal(sessionDB.Data) + cleanedMap := SessionsMap{} + emptySessionsMap(&cleanedMap) + + for jid, session := range sessionDB.Data.Sessions { + if session.Login != "" { + cleanedMap.Sessions[jid] = session + } + } + + return yaml.Marshal(&cleanedMap) } // LoadSessions restores TDlib sessions from the previous run diff --git a/telegram/commands.go b/telegram/commands.go index 04c6e3a..82e6d38 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -159,8 +159,12 @@ func (c *Client) ProcessTransportCommand(cmdline string) string { cmd, args := parseCommand(cmdline) switch cmd { case "login", "code", "password": - if cmd == "login" && c.Session.Login != "" { - return "" + if cmd == "login" { + if c.Session.Login != "" { + return "" + } else if !c.Online() { + c.Connect() + } } if len(args) < 1 { @@ -184,15 +188,16 @@ func (c *Client) ProcessTransportCommand(cmdline string) string { } // sign out case "logout": + for id := range c.cache.chats { + c.unsubscribe(id) + } + _, err := c.client.LogOut() if err != nil { + c.forceClose() return errors.Wrap(err, "Logout error").Error() } - for id := range c.cache.chats { - c.unsubscribe(id) - } - c.Session.Login = "" // set @username case "setusername": diff --git a/telegram/connect.go b/telegram/connect.go index 3a711c2..af97b02 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -207,6 +207,7 @@ func (c *Client) interactor() { func (c *Client) forceClose() { c.listener.Close() + c.authorizer = nil } // Online checks if the updates listener is alive