diff options
Diffstat (limited to 'telegram/connect.go')
-rw-r--r-- | telegram/connect.go | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/telegram/connect.go b/telegram/connect.go index 7221453..8cee376 100644 --- a/telegram/connect.go +++ b/telegram/connect.go @@ -86,11 +86,12 @@ func (stateHandler *clientAuthorizer) Close() { } // Connect starts TDlib connection -func (c *Client) Connect() error { +func (c *Client) Connect(resource string) error { // avoid conflict if another authorization is pending already c.locks.authorizationReady.Wait() if c.Online() { + c.refresh(resource) return nil } @@ -116,7 +117,6 @@ func (c *Client) Connect() error { } c.client = tdlibClient - c.locks.authorizationReady.Done() // stage 3: if a client is succesfully created, AuthorizationStateReady is already reached log.Warn("Authorization successful!") @@ -130,27 +130,41 @@ func (c *Client) Connect() error { go c.updateHandler() c.online = true + c.locks.authorizationReady.Done() + c.addResource(resource) - _, err = c.client.GetChats(&client.GetChatsRequest{ - OffsetOrder: client.JsonInt64(math.MaxInt64), - Limit: chatsLimit, - }) - if err != nil { - log.Errorf("Could not retrieve chats: %v", err) - } + go func() { + _, err = c.client.GetChats(&client.GetChatsRequest{ + OffsetOrder: client.JsonInt64(math.MaxInt64), + Limit: chatsLimit, + }) + if err != nil { + log.Errorf("Could not retrieve chats: %v", err) + } - gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribe")) - gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribed")) - gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login)) + gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribe")) + gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribed")) + gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login)) + }() return nil } -// Disconnect drops TDlib connection -func (c *Client) Disconnect() { +// Disconnect drops TDlib connection and +// returns the flag indicating if disconnecting is permitted +func (c *Client) Disconnect(resource string, quit bool) bool { + if !quit { + c.deleteResource(resource) + } + // other resources are still active + if len(c.resources) > 0 && !quit { + log.Infof("Resource %v for account %v has disconnected, %v remaining", resource, c.Session.Login, len(c.resources)) + log.Debugf("Resources: %#v", c.resources) + return false + } // already disconnected if !c.Online() { - return + return false } log.Warn("Disconnecting from Telegram network...") @@ -168,8 +182,10 @@ func (c *Client) Disconnect() { _, err := c.client.Close() if err != nil { log.Errorf("Couldn't close the Telegram instance: %v; %#v", err, c) - c.forceClose() } + c.forceClose() + + return true } func (c *Client) interactor() { |