aboutsummaryrefslogtreecommitdiff
path: root/telegram
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-11-24 20:10:29 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-11-24 20:10:29 +0300
commit653b1bde94b89d91862007a18b8730ff58673f59 (patch)
tree5b0c8674f918eb49dd27e53918ff10529c0ff55c /telegram
parentc0c21a35a4cfd326423ad530926a0e96c1b07dcf (diff)
Telegram authorization
Diffstat (limited to 'telegram')
-rw-r--r--telegram/client.go14
-rw-r--r--telegram/commands.go33
-rw-r--r--telegram/connect.go57
3 files changed, 80 insertions, 24 deletions
diff --git a/telegram/client.go b/telegram/client.go
index 833d883..c8187fc 100644
--- a/telegram/client.go
+++ b/telegram/client.go
@@ -10,6 +10,7 @@ import (
"dev.narayana.im/narayana/telegabber/persistence"
"github.com/zelenin/go-tdlib/client"
+ "gosrc.io/xmpp"
)
var logConstants = map[string]int32{
@@ -34,6 +35,8 @@ func stringToLogConstant(c string) int32 {
// Client stores the metadata for lazily invoked TDlib instance
type Client struct {
client *client.Client
+ authorizer *client.ClientAuthorizerType
+ xmpp *xmpp.Component
jid string
parameters *client.TdlibParameters
Session *persistence.Session
@@ -42,14 +45,14 @@ type Client struct {
}
// NewClient instantiates a Telegram App
-func NewClient(conf config.TelegramConfig, jid string, session *persistence.Session) (Client, error) {
+func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component, session *persistence.Session) (*Client, error) {
logVerbosity := client.WithLogVerbosity(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: stringToLogConstant(conf.Loglevel),
})
apiID, err := strconv.Atoi(conf.Tdlib.Client.APIID)
if err != nil {
- return Client{}, errors.Wrap(err, "Wrong api_id")
+ return &Client{}, errors.Wrap(err, "Wrong api_id")
}
parameters := client.TdlibParameters{
@@ -75,8 +78,9 @@ func NewClient(conf config.TelegramConfig, jid string, session *persistence.Sess
IgnoreFileNames: false,
}
- return Client{
+ return &Client{
parameters: &parameters,
+ xmpp: component,
jid: jid,
Session: session,
logVerbosity: logVerbosity,
@@ -93,3 +97,7 @@ func updateHandler(tdlibClient *client.Client) {
}
}
}
+
+func (c *Client) ProcessOutgoingMessage(chatID int, text string, messageID int) {
+ // TODO
+}
diff --git a/telegram/commands.go b/telegram/commands.go
new file mode 100644
index 0000000..64a5120
--- /dev/null
+++ b/telegram/commands.go
@@ -0,0 +1,33 @@
+package telegram
+
+const notEnoughArguments string = "Not enough arguments"
+const telegramNotInitialized string = "Telegram connection is not initialized yet"
+
+// ProcessTransportCommand executes commands sent directly to the component
+func (c *Client) ProcessTransportCommand(cmd string, args []string) string {
+ switch cmd {
+ case "login", "code", "password":
+ if cmd == "login" && c.Session.Login != "" {
+ return ""
+ }
+
+ if len(args) < 1 {
+ return notEnoughArguments
+ }
+ if c.authorizer == nil {
+ return telegramNotInitialized
+ }
+
+ switch cmd {
+ case "login":
+ c.authorizer.PhoneNumber <- args[0]
+ c.Session.Login = args[0]
+ case "code":
+ c.authorizer.Code <- args[0]
+ case "password":
+ c.authorizer.Password <- args[0]
+ }
+ }
+
+ return ""
+}
diff --git a/telegram/connect.go b/telegram/connect.go
index c30f9a0..7e92bc8 100644
--- a/telegram/connect.go
+++ b/telegram/connect.go
@@ -3,6 +3,8 @@ 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"
)
@@ -15,26 +17,15 @@ func (c *Client) Connect() error {
log.Warn("Connecting to Telegram network...")
- authorizer := client.ClientAuthorizer()
- go func() {
- for {
- state, ok := <-authorizer.State
- if !ok {
- return
- }
+ c.authorizer = client.ClientAuthorizer()
- ok = authorizationStateHandler(state)
- if !ok {
- return
- }
- }
- }()
+ go c.interactor()
- authorizer.TdlibParameters <- c.parameters
+ c.authorizer.TdlibParameters <- c.parameters
- tdlibClient, err := client.NewClient(authorizer, c.logVerbosity)
+ tdlibClient, err := client.NewClient(c.authorizer, c.logVerbosity)
if err != nil {
- return errors.Wrap(err, "Coudn't initialize a Telegram client instance")
+ return errors.Wrap(err, "Couldn't initialize a Telegram client instance")
}
c.client = tdlibClient
@@ -59,10 +50,34 @@ func (c *Client) Disconnect() {
c.online = false
}
-func authorizationStateHandler(state client.AuthorizationState) bool {
- switch state.AuthorizationStateType() {
- // TODO
- }
+func (c *Client) interactor() {
+ for {
+ state, ok := <-c.authorizer.State
+ if !ok {
+ return
+ }
+
+ stateType := state.AuthorizationStateType()
+ log.Infof("Telegram authorization state: %#v", stateType)
- return true
+ 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
+ }
+ }
}