From eeba07eeb098fcb75a69a0f8cd056bf4bb6d3919 Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Thu, 5 Dec 2019 01:10:08 +0200 Subject: [PATCH] Add logout command --- telegram/commands.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/telegram/commands.go b/telegram/commands.go index 606e28d..dc02ff4 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -1,15 +1,20 @@ package telegram import ( + "strconv" "strings" + + "dev.narayana.im/narayana/telegabber/xmpp/gateway" + + log "github.com/sirupsen/logrus" ) const notEnoughArguments string = "Not enough arguments" const telegramNotInitialized string = "Telegram connection is not initialized yet" var transportCommands = map[string]command{ - "login": command{"phone", "sign in"}, - //"logout": command{"", "sign out"}, + "login": command{"phone", "sign in"}, + "logout": command{"", "sign out"}, "code": command{"", "check one-time code"}, "password": command{"", "check 2fa password"}, //"setusername": command{"", "update @username"}, @@ -120,14 +125,34 @@ func (c *Client) ProcessTransportCommand(cmdline string) string { } switch cmd { + // sign in case "login": c.authorizer.PhoneNumber <- args[0] c.Session.Login = args[0] + // check auth code case "code": c.authorizer.Code <- args[0] + // check auth password case "password": c.authorizer.Password <- args[0] } + // sign out + case "logout": + _, err := c.client.LogOut() + if err != nil { + log.Errorf("Logout error: %v", err) + } + + for id := range c.cache.chats { + gateway.SendPresence( + c.xmpp, + c.jid, + gateway.SPFrom(strconv.FormatInt(id, 10)), + gateway.SPType("unsubscribed"), + ) + } + + c.Session.Login = "" case "help": return helpString(helpTypeTransport) }