2019-11-05 00:25:15 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2019-12-04 22:14:56 +00:00
|
|
|
"strconv"
|
2023-08-28 14:16:57 +00:00
|
|
|
"time"
|
2019-11-05 00:25:15 +00:00
|
|
|
|
2019-11-24 17:10:29 +00:00
|
|
|
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
|
|
|
|
2019-11-20 21:45:30 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-01-17 20:45:40 +00:00
|
|
|
"github.com/zelenin/go-tdlib/client"
|
2019-11-05 00:25:15 +00:00
|
|
|
)
|
|
|
|
|
2019-11-26 22:14:06 +00:00
|
|
|
const chatsLimit int32 = 999
|
|
|
|
|
2019-11-25 19:42:11 +00:00
|
|
|
type clientAuthorizer struct {
|
2023-08-01 01:25:24 +00:00
|
|
|
TdlibParameters chan *client.SetTdlibParametersRequest
|
2019-11-25 19:42:11 +00:00
|
|
|
PhoneNumber chan string
|
|
|
|
Code chan string
|
|
|
|
State chan client.AuthorizationState
|
|
|
|
Password chan string
|
2022-08-15 10:10:29 +00:00
|
|
|
FirstName chan string
|
|
|
|
LastName chan string
|
|
|
|
isClosed bool
|
2019-11-25 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.AuthorizationState) error {
|
2023-06-16 04:34:49 +00:00
|
|
|
if stateHandler.isClosed {
|
|
|
|
return errors.New("Channel is closed")
|
|
|
|
}
|
2019-11-25 19:42:11 +00:00
|
|
|
stateHandler.State <- state
|
|
|
|
|
|
|
|
switch state.AuthorizationStateType() {
|
|
|
|
case client.TypeAuthorizationStateWaitTdlibParameters:
|
2023-08-01 01:25:24 +00:00
|
|
|
_, err := c.SetTdlibParameters(<-stateHandler.TdlibParameters)
|
2019-11-25 19:42:11 +00:00
|
|
|
return err
|
|
|
|
|
|
|
|
case client.TypeAuthorizationStateWaitPhoneNumber:
|
|
|
|
_, err := c.SetAuthenticationPhoneNumber(&client.SetAuthenticationPhoneNumberRequest{
|
|
|
|
PhoneNumber: <-stateHandler.PhoneNumber,
|
|
|
|
Settings: &client.PhoneNumberAuthenticationSettings{
|
|
|
|
AllowFlashCall: false,
|
|
|
|
IsCurrentPhoneNumber: false,
|
|
|
|
AllowSmsRetrieverApi: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
|
|
|
|
case client.TypeAuthorizationStateWaitCode:
|
|
|
|
_, err := c.CheckAuthenticationCode(&client.CheckAuthenticationCodeRequest{
|
|
|
|
Code: <-stateHandler.Code,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
|
|
|
|
case client.TypeAuthorizationStateWaitRegistration:
|
2022-08-15 10:10:29 +00:00
|
|
|
_, err := c.RegisterUser(&client.RegisterUserRequest{
|
|
|
|
FirstName: <-stateHandler.FirstName,
|
|
|
|
LastName: <-stateHandler.LastName,
|
|
|
|
})
|
|
|
|
return err
|
2019-11-25 19:42:11 +00:00
|
|
|
|
|
|
|
case client.TypeAuthorizationStateWaitPassword:
|
|
|
|
_, err := c.CheckAuthenticationPassword(&client.CheckAuthenticationPasswordRequest{
|
|
|
|
Password: <-stateHandler.Password,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
|
|
|
|
case client.TypeAuthorizationStateReady:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case client.TypeAuthorizationStateLoggingOut:
|
|
|
|
return client.ErrNotSupportedAuthorizationState
|
|
|
|
|
|
|
|
case client.TypeAuthorizationStateClosing:
|
|
|
|
return client.ErrNotSupportedAuthorizationState
|
|
|
|
|
|
|
|
case client.TypeAuthorizationStateClosed:
|
|
|
|
return client.ErrNotSupportedAuthorizationState
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.ErrNotSupportedAuthorizationState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stateHandler *clientAuthorizer) Close() {
|
2023-06-16 04:34:49 +00:00
|
|
|
if stateHandler.isClosed {
|
|
|
|
return
|
|
|
|
}
|
2022-08-15 10:10:29 +00:00
|
|
|
stateHandler.isClosed = true
|
2019-11-25 19:42:11 +00:00
|
|
|
close(stateHandler.TdlibParameters)
|
|
|
|
close(stateHandler.PhoneNumber)
|
|
|
|
close(stateHandler.Code)
|
|
|
|
close(stateHandler.State)
|
|
|
|
close(stateHandler.Password)
|
2022-08-15 10:10:29 +00:00
|
|
|
close(stateHandler.FirstName)
|
|
|
|
close(stateHandler.LastName)
|
2019-11-25 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 00:25:15 +00:00
|
|
|
// Connect starts TDlib connection
|
2022-01-03 03:54:13 +00:00
|
|
|
func (c *Client) Connect(resource string) error {
|
2023-01-23 07:30:02 +00:00
|
|
|
log.Warn("Attempting to connect to Telegram network...")
|
|
|
|
|
2019-12-15 19:30:54 +00:00
|
|
|
// avoid conflict if another authorization is pending already
|
2023-01-23 07:30:02 +00:00
|
|
|
c.locks.authorizationReady.Lock()
|
2019-12-15 19:30:54 +00:00
|
|
|
|
2019-12-15 02:26:07 +00:00
|
|
|
if c.Online() {
|
2022-01-05 21:04:22 +00:00
|
|
|
c.roster(resource)
|
2023-01-23 07:30:02 +00:00
|
|
|
c.locks.authorizationReady.Unlock()
|
2019-11-05 00:25:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-20 21:45:30 +00:00
|
|
|
log.Warn("Connecting to Telegram network...")
|
|
|
|
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerWriteLock.Lock()
|
2019-11-25 19:42:11 +00:00
|
|
|
c.authorizer = &clientAuthorizer{
|
2023-08-01 01:25:24 +00:00
|
|
|
TdlibParameters: make(chan *client.SetTdlibParametersRequest, 1),
|
2019-11-25 19:42:11 +00:00
|
|
|
PhoneNumber: make(chan string, 1),
|
|
|
|
Code: make(chan string, 1),
|
|
|
|
State: make(chan client.AuthorizationState, 10),
|
|
|
|
Password: make(chan string, 1),
|
2022-08-15 10:10:29 +00:00
|
|
|
FirstName: make(chan string, 1),
|
|
|
|
LastName: make(chan string, 1),
|
2019-11-25 19:42:11 +00:00
|
|
|
}
|
2019-11-07 21:09:53 +00:00
|
|
|
|
2019-11-24 17:10:29 +00:00
|
|
|
go c.interactor()
|
2019-11-07 21:09:53 +00:00
|
|
|
|
2019-11-24 17:10:29 +00:00
|
|
|
c.authorizer.TdlibParameters <- c.parameters
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerWriteLock.Unlock()
|
2019-11-05 00:25:15 +00:00
|
|
|
|
2019-12-16 01:02:53 +00:00
|
|
|
tdlibClient, err := client.NewClient(c.authorizer, c.options...)
|
2019-11-05 00:25:15 +00:00
|
|
|
if err != nil {
|
2023-01-23 07:30:02 +00:00
|
|
|
c.locks.authorizationReady.Unlock()
|
2019-11-24 17:10:29 +00:00
|
|
|
return errors.Wrap(err, "Couldn't initialize a Telegram client instance")
|
2019-11-05 00:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.client = tdlibClient
|
2021-12-31 23:34:04 +00:00
|
|
|
|
|
|
|
// stage 3: if a client is succesfully created, AuthorizationStateReady is already reached
|
|
|
|
log.Warn("Authorization successful!")
|
|
|
|
|
|
|
|
c.me, err = c.client.GetMe()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not retrieve me info")
|
|
|
|
} else if c.Session.Login == "" {
|
|
|
|
c.Session.Login = c.me.PhoneNumber
|
|
|
|
}
|
2019-11-05 00:25:15 +00:00
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
go c.updateHandler()
|
2021-12-31 23:34:04 +00:00
|
|
|
c.online = true
|
2023-01-23 07:30:02 +00:00
|
|
|
c.locks.authorizationReady.Unlock()
|
2022-01-03 03:54:13 +00:00
|
|
|
c.addResource(resource)
|
2021-12-31 23:34:04 +00:00
|
|
|
|
2022-01-03 03:54:13 +00:00
|
|
|
go func() {
|
|
|
|
_, err = c.client.GetChats(&client.GetChatsRequest{
|
2022-02-08 20:25:58 +00:00
|
|
|
Limit: chatsLimit,
|
2022-01-03 03:54:13 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not retrieve chats: %v", err)
|
|
|
|
}
|
2021-12-31 23:34:04 +00:00
|
|
|
|
2023-08-28 14:16:57 +00:00
|
|
|
gateway.SubscribeToTransport(c.xmpp, c.jid)
|
2022-01-03 03:54:13 +00:00
|
|
|
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login))
|
|
|
|
}()
|
2019-11-05 00:25:15 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-28 14:16:57 +00:00
|
|
|
func (c *Client) TryLogin(resource string, login string) error {
|
|
|
|
wasSessionLoginEmpty := c.Session.Login == ""
|
|
|
|
c.Session.Login = login
|
|
|
|
|
|
|
|
if wasSessionLoginEmpty && c.authorizer == nil {
|
|
|
|
go func() {
|
|
|
|
err := c.Connect(resource)
|
|
|
|
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: just re-login again
|
|
|
|
time.Sleep(1e5)
|
|
|
|
}
|
|
|
|
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerReadLock.Lock()
|
|
|
|
defer c.locks.authorizerReadLock.Unlock()
|
|
|
|
|
2023-08-28 14:16:57 +00:00
|
|
|
if c.authorizer == nil {
|
|
|
|
return errors.New(TelegramNotInitialized)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.authorizer.isClosed {
|
|
|
|
return errors.New(TelegramAuthDone)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetPhoneNumber(login string) error {
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerWriteLock.Lock()
|
|
|
|
defer c.locks.authorizerWriteLock.Unlock()
|
|
|
|
|
2023-08-28 14:16:57 +00:00
|
|
|
if c.authorizer == nil || c.authorizer.isClosed {
|
|
|
|
return errors.New("Authorization not needed")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.authorizer.PhoneNumber <- login
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 03:54:13 +00:00
|
|
|
// 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
|
2022-01-05 21:04:22 +00:00
|
|
|
if (len(c.resources) > 0 || c.Session.KeepOnline) && !quit {
|
2022-01-03 03:54:13 +00:00
|
|
|
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
|
|
|
|
}
|
2019-12-04 22:14:56 +00:00
|
|
|
// already disconnected
|
2019-12-15 02:26:07 +00:00
|
|
|
if !c.Online() {
|
2022-01-03 03:54:13 +00:00
|
|
|
return false
|
2019-11-05 00:25:15 +00:00
|
|
|
}
|
2019-11-07 21:09:53 +00:00
|
|
|
|
2019-11-20 21:45:30 +00:00
|
|
|
log.Warn("Disconnecting from Telegram network...")
|
|
|
|
|
2019-12-04 22:14:56 +00:00
|
|
|
// we're offline (unsubscribe if logout)
|
2019-12-28 02:35:40 +00:00
|
|
|
for _, id := range c.cache.ChatsKeys() {
|
2019-12-04 22:14:56 +00:00
|
|
|
gateway.SendPresence(
|
|
|
|
c.xmpp,
|
|
|
|
c.jid,
|
|
|
|
gateway.SPFrom(strconv.FormatInt(id, 10)),
|
|
|
|
gateway.SPType("unavailable"),
|
|
|
|
)
|
|
|
|
}
|
2019-11-20 21:45:30 +00:00
|
|
|
|
2023-06-16 04:34:49 +00:00
|
|
|
c.close()
|
2022-01-03 03:54:13 +00:00
|
|
|
|
|
|
|
return true
|
2019-11-07 21:09:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 17:10:29 +00:00
|
|
|
func (c *Client) interactor() {
|
|
|
|
for {
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerReadLock.Lock()
|
|
|
|
if c.authorizer == nil {
|
|
|
|
log.Warn("Authorizer is lost, halting the interactor")
|
|
|
|
c.locks.authorizerReadLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2019-11-24 17:10:29 +00:00
|
|
|
state, ok := <-c.authorizer.State
|
|
|
|
if !ok {
|
2021-12-05 03:32:12 +00:00
|
|
|
log.Warn("Interactor is disconnected")
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerReadLock.Unlock()
|
2019-11-24 17:10:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stateType := state.AuthorizationStateType()
|
|
|
|
log.Infof("Telegram authorization state: %#v", stateType)
|
2019-11-29 00:51:41 +00:00
|
|
|
log.Debugf("%#v", state)
|
2019-11-07 21:09:53 +00:00
|
|
|
|
2019-11-24 17:10:29 +00:00
|
|
|
switch stateType {
|
2019-12-21 00:04:47 +00:00
|
|
|
// stage 0: set login
|
2019-11-24 17:10:29 +00:00
|
|
|
case client.TypeAuthorizationStateWaitPhoneNumber:
|
|
|
|
log.Warn("Logging in...")
|
|
|
|
if c.Session.Login != "" {
|
|
|
|
c.authorizer.PhoneNumber <- c.Session.Login
|
|
|
|
} else {
|
2023-03-05 08:00:53 +00:00
|
|
|
gateway.SendServiceMessage(c.jid, "Please, enter your Telegram login via /login 12345", c.xmpp)
|
2019-11-24 17:10:29 +00:00
|
|
|
}
|
2019-12-21 00:04:47 +00:00
|
|
|
// stage 1: wait for auth code
|
2019-11-24 17:10:29 +00:00
|
|
|
case client.TypeAuthorizationStateWaitCode:
|
|
|
|
log.Warn("Waiting for authorization code...")
|
2023-03-05 08:00:53 +00:00
|
|
|
gateway.SendServiceMessage(c.jid, "Please, enter authorization code via /code 12345", c.xmpp)
|
2022-08-15 10:10:29 +00:00
|
|
|
// stage 1b: wait for registration
|
|
|
|
case client.TypeAuthorizationStateWaitRegistration:
|
|
|
|
log.Warn("Waiting for full name...")
|
2023-03-05 08:00:53 +00:00
|
|
|
gateway.SendServiceMessage(c.jid, "This number is not registered yet! Please, enter your name via /setname John Doe", c.xmpp)
|
2019-12-21 00:04:47 +00:00
|
|
|
// stage 2: wait for 2fa
|
2019-11-24 17:10:29 +00:00
|
|
|
case client.TypeAuthorizationStateWaitPassword:
|
|
|
|
log.Warn("Waiting for 2FA password...")
|
2023-03-05 08:00:53 +00:00
|
|
|
gateway.SendServiceMessage(c.jid, "Please, enter 2FA passphrase via /password 12345", c.xmpp)
|
2019-11-24 17:10:29 +00:00
|
|
|
}
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerReadLock.Unlock()
|
2019-11-24 17:10:29 +00:00
|
|
|
}
|
2019-11-05 00:25:15 +00:00
|
|
|
}
|
2019-12-15 02:26:07 +00:00
|
|
|
|
2019-12-17 01:56:11 +00:00
|
|
|
func (c *Client) forceClose() {
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerReadLock.Lock()
|
|
|
|
c.locks.authorizerWriteLock.Lock()
|
|
|
|
defer c.locks.authorizerReadLock.Unlock()
|
|
|
|
defer c.locks.authorizerWriteLock.Unlock()
|
|
|
|
|
2019-12-19 20:29:36 +00:00
|
|
|
c.online = false
|
2019-12-18 21:00:23 +00:00
|
|
|
c.authorizer = nil
|
2019-12-17 01:56:11 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 04:34:49 +00:00
|
|
|
func (c *Client) close() {
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerWriteLock.Lock()
|
2023-06-16 04:34:49 +00:00
|
|
|
if c.authorizer != nil && !c.authorizer.isClosed {
|
|
|
|
c.authorizer.Close()
|
|
|
|
}
|
2023-08-31 21:26:35 +00:00
|
|
|
c.locks.authorizerWriteLock.Unlock()
|
|
|
|
|
2023-06-16 04:34:49 +00:00
|
|
|
if c.client != nil {
|
|
|
|
_, err := c.client.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Couldn't close the Telegram instance: %v; %#v", err, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.forceClose()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) cancelAuth() {
|
|
|
|
c.close()
|
|
|
|
c.Session.Login = ""
|
|
|
|
}
|
|
|
|
|
2019-12-15 02:26:07 +00:00
|
|
|
// Online checks if the updates listener is alive
|
|
|
|
func (c *Client) Online() bool {
|
2019-12-30 21:26:23 +00:00
|
|
|
return c.online
|
2019-12-15 02:26:07 +00:00
|
|
|
}
|