From 0e10aa46351bcbc72e37f5e8810533a7b9a89867 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 13 Dec 2019 21:27:26 +0100 Subject: [PATCH] refactor auth flow --- telegram/Command.cs | 17 ++++++++++++++++- telegram/Util.cs | 7 +++++++ telegram/tgcli.cs | 44 +++++++++++++++++++++++++++++++++----------- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/telegram/Command.cs b/telegram/Command.cs index 64c6309..1eca999 100644 --- a/telegram/Command.cs +++ b/telegram/Command.cs @@ -45,7 +45,8 @@ namespace telegram new NewSecretChatCommand(), new CloseSecretChatCommand(), new QuitCommand(), - new HelpCommand() + new HelpCommand(), + new LogoutCommand(), }; public static void HandleCommand(string input) @@ -83,6 +84,8 @@ namespace telegram public override void Handler(List inputParams) { + if (inputParams.Count == 0) + return; //TODO do something var query = inputParams.Aggregate((current, param) => current + " " + param).Trim(); var chatId = SearchChatId(query); if (chatId == 0) return; @@ -611,4 +614,16 @@ namespace telegram } } } + + public class LogoutCommand : Command + { + public LogoutCommand() : base("logout", "", "log out this session (destroys all local data)", "", 0) + { + } + + public override void Handler(List inputParams) + { + LogOut(); + } + } } \ No newline at end of file diff --git a/telegram/Util.cs b/telegram/Util.cs index 00eb3bd..49886d3 100644 --- a/telegram/Util.cs +++ b/telegram/Util.cs @@ -333,6 +333,13 @@ namespace telegram } } + public static void LogOut() + { + lock (@lock) + messageQueue.Add($"{Ansi.Yellow}[tgcli] Logging out..."); + client.ExecuteAsync(new LogOut()).Wait(); + } + public static string GetFormattedUsername(User sender) { var username = sender.Username; diff --git a/telegram/tgcli.cs b/telegram/tgcli.cs index 45e458e..15f9466 100644 --- a/telegram/tgcli.cs +++ b/telegram/tgcli.cs @@ -72,7 +72,11 @@ namespace telegram client.UpdateReceived += HandleUpdate; - OnAuthUpdate("authorizationStateWaitTdlibParameters"); + OnAuthUpdate(new Td.TdApi.Update.UpdateAuthorizationState() + { + AuthorizationState = new Td.TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters() + }); + while (!authorized) { Thread.Sleep(1); @@ -96,7 +100,7 @@ namespace telegram switch (e) { case Td.TdApi.Update.UpdateAuthorizationState state: - OnAuthUpdate(state.AuthorizationState.DataType); + OnAuthUpdate(state); break; case Td.TdApi.Update.UpdateNewMessage message: { @@ -305,11 +309,11 @@ namespace telegram } } - private static void OnAuthUpdate(string state) + private static void OnAuthUpdate(Td.TdApi.Update.UpdateAuthorizationState state) { - switch (state) + switch (state.AuthorizationState) { - case "authorizationStateWaitTdlibParameters": + case Td.TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters _: client.Send(new Td.TdApi.SetTdlibParameters { Parameters = new Td.TdApi.TdlibParameters @@ -327,10 +331,10 @@ namespace telegram } }); break; - case "authorizationStateWaitEncryptionKey": + case Td.TdApi.AuthorizationState.AuthorizationStateWaitEncryptionKey _: client.Send(new Td.TdApi.CheckDatabaseEncryptionKey()); break; - case "authorizationStateWaitPhoneNumber": + case Td.TdApi.AuthorizationState.AuthorizationStateWaitPhoneNumber _: { Console.Write("[tgcli] login> "); var phone = Console.ReadLine(); @@ -340,7 +344,7 @@ namespace telegram }); break; } - case "authorizationStateWaitCode": + case Td.TdApi.AuthorizationState.AuthorizationStateWaitCode _: { Console.Write("[tgcli] code> "); var code = Console.ReadLine(); @@ -350,7 +354,7 @@ namespace telegram }); break; } - case "authorizationStateWaitPassword": + case Td.TdApi.AuthorizationState.AuthorizationStateWaitPassword _: { Console.Write("[tgcli] 2fa password> "); var pass = ReadConsolePassword(); @@ -360,12 +364,28 @@ namespace telegram }); break; } - case "authorizationStateReady": + case Td.TdApi.AuthorizationState.AuthorizationStateReady _: Console.WriteLine("[tgcli] logged in."); authorized = true; + connectionState = "Ready"; + break; + case Td.TdApi.AuthorizationState.AuthorizationStateClosed _: + messageQueue.Add($"{Ansi.Yellow}[tgcli] Logged out successfully. All local data has been deleted."); + ScreenUpdate(); + Environment.Exit(0); + break; + case Td.TdApi.AuthorizationState.AuthorizationStateClosing _: + messageQueue.Add($"{Ansi.Yellow}[tgcli] Logging out..."); + ScreenUpdate(); + break; + case Td.TdApi.AuthorizationState.AuthorizationStateLoggingOut _: + if (authorized) return; + Console.WriteLine( + "[tgcli] This session has been destroyed externally, to fix this delete ~/.tgcli"); + Environment.Exit(1); break; default: - Console.WriteLine($"unknown state: {state}"); + Console.WriteLine($"unknown state: {state.AuthorizationState.DataType}"); Environment.Exit(1); break; } @@ -450,6 +470,8 @@ namespace telegram chat.Type is Td.TdApi.ChatType.ChatTypeSecret; var isSecret = chat.Type is Td.TdApi.ChatType.ChatTypeSecret; + chat.Title = TruncateString(chat.Title, 20); + var finalOutput = ""; var replyPrefix = $"{origPrefix}{Ansi.Yellow}Re: {Ansi.Bold}{Ansi.Green}[{time}] " + $"{(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} " +