aboutsummaryrefslogtreecommitdiff
path: root/telegram
diff options
context:
space:
mode:
Diffstat (limited to 'telegram')
-rw-r--r--telegram/commands.go52
-rw-r--r--telegram/connect.go40
2 files changed, 59 insertions, 33 deletions
diff --git a/telegram/commands.go b/telegram/commands.go
index 0c83945..b4920d4 100644
--- a/telegram/commands.go
+++ b/telegram/commands.go
@@ -15,7 +15,8 @@ import (
)
const notEnoughArguments string = "Not enough arguments"
-const telegramNotInitialized string = "Telegram connection is not initialized yet"
+const TelegramNotInitialized string = "Telegram connection is not initialized yet"
+const TelegramAuthDone string = "Authorization is done already"
const notOnline string = "Not online"
var permissionsAdmin = client.ChatAdministratorRights{
@@ -244,40 +245,29 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
}
if cmd == "login" {
- wasSessionLoginEmpty := c.Session.Login == ""
- c.Session.Login = args[0]
-
- 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: the command just needs to be resent again
- time.Sleep(1e5)
+ err := c.TryLogin(resource, args[0])
+ if err != nil {
+ return err.Error()
}
- }
- if c.authorizer == nil {
- return telegramNotInitialized
- }
+ c.authorizer.PhoneNumber <- args[0]
+ } else {
+ if c.authorizer == nil {
+ return TelegramNotInitialized
+ }
- if c.authorizer.isClosed {
- return "Authorization is done already"
- }
+ if c.authorizer.isClosed {
+ return TelegramAuthDone
+ }
- switch cmd {
- // sign in
- case "login":
- c.authorizer.PhoneNumber <- args[0]
- // check auth code
- case "code":
- c.authorizer.Code <- args[0]
- // check auth password
- case "password":
- c.authorizer.Password <- args[0]
+ switch cmd {
+ // check auth code
+ case "code":
+ c.authorizer.Code <- args[0]
+ // check auth password
+ case "password":
+ c.authorizer.Password <- args[0]
+ }
}
// sign out
case "logout":
diff --git a/telegram/connect.go b/telegram/connect.go
index ef03428..ab9c19c 100644
--- a/telegram/connect.go
+++ b/telegram/connect.go
@@ -3,6 +3,7 @@ package telegram
import (
"github.com/pkg/errors"
"strconv"
+ "time"
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
@@ -154,14 +155,49 @@ func (c *Client) Connect(resource string) error {
log.Errorf("Could not retrieve chats: %v", err)
}
- gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribe"))
- gateway.SendPresence(c.xmpp, c.jid, gateway.SPType("subscribed"))
+ gateway.SubscribeToTransport(c.xmpp, c.jid)
gateway.SendPresence(c.xmpp, c.jid, gateway.SPStatus("Logged in as: "+c.Session.Login))
}()
return nil
}
+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)
+ }
+
+ 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 {
+ if c.authorizer == nil || c.authorizer.isClosed {
+ return errors.New("Authorization not needed")
+ }
+
+ c.authorizer.PhoneNumber <- login
+ return nil
+}
+
// Disconnect drops TDlib connection and
// returns the flag indicating if disconnecting is permitted
func (c *Client) Disconnect(resource string, quit bool) bool {