2019-11-05 00:25:15 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2019-11-26 22:14:06 +00:00
|
|
|
"math"
|
2019-12-04 22:14:56 +00:00
|
|
|
"strconv"
|
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"
|
2019-11-05 00:25:15 +00:00
|
|
|
"github.com/zelenin/go-tdlib/client"
|
|
|
|
)
|
|
|
|
|
2019-11-26 22:14:06 +00:00
|
|
|
const chatsLimit int32 = 999
|
|
|
|
|
2019-11-25 19:42:11 +00:00
|
|
|
type clientAuthorizer struct {
|
|
|
|
TdlibParameters chan *client.TdlibParameters
|
|
|
|
PhoneNumber chan string
|
|
|
|
Code chan string
|
|
|
|
State chan client.AuthorizationState
|
|
|
|
Password chan string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.AuthorizationState) error {
|
|
|
|
stateHandler.State <- state
|
|
|
|
|
|
|
|
switch state.AuthorizationStateType() {
|
|
|
|
case client.TypeAuthorizationStateWaitTdlibParameters:
|
|
|
|
_, err := c.SetTdlibParameters(&client.SetTdlibParametersRequest{
|
|
|
|
Parameters: <-stateHandler.TdlibParameters,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
|
|
|
|
case client.TypeAuthorizationStateWaitEncryptionKey:
|
|
|
|
_, err := c.CheckDatabaseEncryptionKey(&client.CheckDatabaseEncryptionKeyRequest{})
|
|
|
|
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:
|
|
|
|
return client.ErrNotSupportedAuthorizationState
|
|
|
|
|
|
|
|
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() {
|
|
|
|
close(stateHandler.TdlibParameters)
|
|
|
|
close(stateHandler.PhoneNumber)
|
|
|
|
close(stateHandler.Code)
|
|
|
|
close(stateHandler.State)
|
|
|
|
close(stateHandler.Password)
|
|
|
|
}
|
|
|
|
|
2019-11-05 00:25:15 +00:00
|
|
|
// Connect starts TDlib connection
|
|
|
|
func (c *Client) Connect() error {
|
2019-12-15 19:30:54 +00:00
|
|
|
// avoid conflict if another authorization is pending already
|
|
|
|
c.locks.authorizationReady.Wait()
|
|
|
|
|
2019-12-15 02:26:07 +00:00
|
|
|
if c.Online() {
|
2019-11-05 00:25:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-20 21:45:30 +00:00
|
|
|
log.Warn("Connecting to Telegram network...")
|
|
|
|
|
2019-11-25 19:42:11 +00:00
|
|
|
c.authorizer = &clientAuthorizer{
|
|
|
|
TdlibParameters: make(chan *client.TdlibParameters, 1),
|
|
|
|
PhoneNumber: make(chan string, 1),
|
|
|
|
Code: make(chan string, 1),
|
|
|
|
State: make(chan client.AuthorizationState, 10),
|
|
|
|
Password: make(chan string, 1),
|
|
|
|
}
|
2019-11-07 21:09:53 +00:00
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
c.locks.authorizationReady.Add(1)
|
|
|
|
|
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
|
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 {
|
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
|
2019-12-15 02:26:07 +00:00
|
|
|
c.listener = tdlibClient.GetListener()
|
2019-12-15 19:30:54 +00:00
|
|
|
c.locks.authorizationReady.Done()
|
2019-12-19 20:29:36 +00:00
|
|
|
c.online = true
|
2019-11-05 00:25:15 +00:00
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
go c.updateHandler()
|
2019-11-05 00:25:15 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect drops TDlib connection
|
|
|
|
func (c *Client) Disconnect() {
|
2019-12-04 22:14:56 +00:00
|
|
|
// already disconnected
|
2019-12-15 02:26:07 +00:00
|
|
|
if !c.Online() {
|
2019-11-05 00:25:15 +00:00
|
|
|
return
|
|
|
|
}
|
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)
|
|
|
|
for id := range c.cache.chats {
|
|
|
|
gateway.SendPresence(
|
|
|
|
c.xmpp,
|
|
|
|
c.jid,
|
|
|
|
gateway.SPFrom(strconv.FormatInt(id, 10)),
|
|
|
|
gateway.SPType("unavailable"),
|
|
|
|
)
|
|
|
|
}
|
2019-11-20 21:45:30 +00:00
|
|
|
|
2019-12-15 02:26:07 +00:00
|
|
|
_, err := c.client.Close()
|
|
|
|
if err != nil {
|
2019-12-16 01:02:53 +00:00
|
|
|
log.Errorf("Couldn't close the Telegram instance: %v; %#v", err, c)
|
2019-12-17 01:56:11 +00:00
|
|
|
c.forceClose()
|
2019-12-15 02:26:07 +00:00
|
|
|
}
|
2019-11-07 21:09:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 17:10:29 +00:00
|
|
|
func (c *Client) interactor() {
|
|
|
|
for {
|
|
|
|
state, ok := <-c.authorizer.State
|
|
|
|
if !ok {
|
2019-12-15 02:26:07 +00:00
|
|
|
log.Error("Interactor is disconnected")
|
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 {
|
|
|
|
gateway.SendMessage(c.jid, "", "Please, enter your Telegram login via /login 12345", c.xmpp)
|
|
|
|
}
|
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...")
|
|
|
|
gateway.SendMessage(c.jid, "", "Please, enter authorization code via /code 12345", 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...")
|
|
|
|
gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp)
|
2019-12-21 00:04:47 +00:00
|
|
|
// stage 3: auth completed
|
2019-11-24 17:10:29 +00:00
|
|
|
case client.TypeAuthorizationStateReady:
|
2019-11-26 22:14:06 +00:00
|
|
|
var err error
|
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
c.locks.authorizationReady.Wait()
|
2019-11-26 22:14:06 +00:00
|
|
|
|
2019-11-24 17:10:29 +00:00
|
|
|
log.Warn("Authorization successful!")
|
2019-11-26 22:14:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.client.GetChats(&client.GetChatsRequest{
|
|
|
|
OffsetOrder: client.JsonInt64(math.MaxInt64),
|
|
|
|
Limit: chatsLimit,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not retrieve chats")
|
|
|
|
}
|
|
|
|
|
2019-11-29 00:51:41 +00:00
|
|
|
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in "+c.Session.Login))
|
2019-12-17 01:56:11 +00:00
|
|
|
|
|
|
|
return
|
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() {
|
2019-12-19 20:29:36 +00:00
|
|
|
//c.listener.Close()
|
|
|
|
c.online = false
|
2019-12-18 21:00:23 +00:00
|
|
|
c.authorizer = nil
|
2019-12-17 01:56:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 02:26:07 +00:00
|
|
|
// Online checks if the updates listener is alive
|
|
|
|
func (c *Client) Online() bool {
|
2019-12-19 20:29:36 +00:00
|
|
|
return c.listener != nil && c.online //c.listener.IsActive()
|
2019-12-15 02:26:07 +00:00
|
|
|
}
|