aboutsummaryrefslogtreecommitdiff
path: root/telegram/connect.go
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-11-25 22:42:11 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-11-25 22:42:11 +0300
commita89629ab20c7a3176a70cda49b1dac44a561f935 (patch)
treeea151aaec1c48a0b4d85a800ec377076763d5165 /telegram/connect.go
parentd6f6207ebb3d5256256a7810a3d3d0bdc8ba04a5 (diff)
Implement an own authorizer
Diffstat (limited to 'telegram/connect.go')
-rw-r--r--telegram/connect.go80
1 files changed, 79 insertions, 1 deletions
diff --git a/telegram/connect.go b/telegram/connect.go
index 7e92bc8..4f509d2 100644
--- a/telegram/connect.go
+++ b/telegram/connect.go
@@ -9,6 +9,78 @@ import (
"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 {
@@ -17,7 +89,13 @@ func (c *Client) Connect() error {
log.Warn("Connecting to Telegram network...")
- c.authorizer = client.ClientAuthorizer()
+ 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()