162 lines
4.2 KiB
Go
162 lines
4.2 KiB
Go
package telegram
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
|
|
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/zelenin/go-tdlib/client"
|
|
)
|
|
|
|
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)
|
|
}
|
|
|
|
// Connect starts TDlib connection
|
|
func (c *Client) Connect() error {
|
|
if c.online {
|
|
return nil
|
|
}
|
|
|
|
log.Warn("Connecting to Telegram network...")
|
|
|
|
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),
|
|
}
|
|
|
|
go c.interactor()
|
|
|
|
c.authorizer.TdlibParameters <- c.parameters
|
|
|
|
tdlibClient, err := client.NewClient(c.authorizer, c.logVerbosity)
|
|
if err != nil {
|
|
return errors.Wrap(err, "Couldn't initialize a Telegram client instance")
|
|
}
|
|
|
|
c.client = tdlibClient
|
|
c.online = true
|
|
|
|
go updateHandler(c.client)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Disconnect drops TDlib connection
|
|
func (c *Client) Disconnect() {
|
|
if !c.online {
|
|
return
|
|
}
|
|
|
|
log.Warn("Disconnecting from Telegram network...")
|
|
|
|
// TODO: send unavailable presence to cached chats
|
|
|
|
c.client.Stop()
|
|
c.online = false
|
|
}
|
|
|
|
func (c *Client) interactor() {
|
|
for {
|
|
state, ok := <-c.authorizer.State
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
stateType := state.AuthorizationStateType()
|
|
log.Infof("Telegram authorization state: %#v", stateType)
|
|
|
|
switch stateType {
|
|
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)
|
|
}
|
|
case client.TypeAuthorizationStateWaitCode:
|
|
log.Warn("Waiting for authorization code...")
|
|
gateway.SendMessage(c.jid, "", "Please, enter authorization code via /code 12345", c.xmpp)
|
|
case client.TypeAuthorizationStateWaitPassword:
|
|
log.Warn("Waiting for 2FA password...")
|
|
gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp)
|
|
case client.TypeAuthorizationStateReady:
|
|
log.Warn("Authorization successful!")
|
|
// TODO
|
|
return
|
|
}
|
|
}
|
|
}
|