refactor auth flow

This commit is contained in:
Laura Hausmann 2019-12-13 21:27:26 +01:00
parent f4eee05ce6
commit 0e10aa4635
Signed by: zotan
GPG key ID: 5EC1D38FFC321311
3 changed files with 56 additions and 12 deletions

View file

@ -45,7 +45,8 @@ namespace telegram
new NewSecretChatCommand(), new NewSecretChatCommand(),
new CloseSecretChatCommand(), new CloseSecretChatCommand(),
new QuitCommand(), new QuitCommand(),
new HelpCommand() new HelpCommand(),
new LogoutCommand(),
}; };
public static void HandleCommand(string input) public static void HandleCommand(string input)
@ -83,6 +84,8 @@ namespace telegram
public override void Handler(List<string> inputParams) public override void Handler(List<string> inputParams)
{ {
if (inputParams.Count == 0)
return; //TODO do something
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim(); var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
var chatId = SearchChatId(query); var chatId = SearchChatId(query);
if (chatId == 0) return; 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<string> inputParams)
{
LogOut();
}
}
} }

View file

@ -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) public static string GetFormattedUsername(User sender)
{ {
var username = sender.Username; var username = sender.Username;

View file

@ -72,7 +72,11 @@ namespace telegram
client.UpdateReceived += HandleUpdate; client.UpdateReceived += HandleUpdate;
OnAuthUpdate("authorizationStateWaitTdlibParameters"); OnAuthUpdate(new Td.TdApi.Update.UpdateAuthorizationState()
{
AuthorizationState = new Td.TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters()
});
while (!authorized) while (!authorized)
{ {
Thread.Sleep(1); Thread.Sleep(1);
@ -96,7 +100,7 @@ namespace telegram
switch (e) switch (e)
{ {
case Td.TdApi.Update.UpdateAuthorizationState state: case Td.TdApi.Update.UpdateAuthorizationState state:
OnAuthUpdate(state.AuthorizationState.DataType); OnAuthUpdate(state);
break; break;
case Td.TdApi.Update.UpdateNewMessage message: 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 client.Send(new Td.TdApi.SetTdlibParameters
{ {
Parameters = new Td.TdApi.TdlibParameters Parameters = new Td.TdApi.TdlibParameters
@ -327,10 +331,10 @@ namespace telegram
} }
}); });
break; break;
case "authorizationStateWaitEncryptionKey": case Td.TdApi.AuthorizationState.AuthorizationStateWaitEncryptionKey _:
client.Send(new Td.TdApi.CheckDatabaseEncryptionKey()); client.Send(new Td.TdApi.CheckDatabaseEncryptionKey());
break; break;
case "authorizationStateWaitPhoneNumber": case Td.TdApi.AuthorizationState.AuthorizationStateWaitPhoneNumber _:
{ {
Console.Write("[tgcli] login> "); Console.Write("[tgcli] login> ");
var phone = Console.ReadLine(); var phone = Console.ReadLine();
@ -340,7 +344,7 @@ namespace telegram
}); });
break; break;
} }
case "authorizationStateWaitCode": case Td.TdApi.AuthorizationState.AuthorizationStateWaitCode _:
{ {
Console.Write("[tgcli] code> "); Console.Write("[tgcli] code> ");
var code = Console.ReadLine(); var code = Console.ReadLine();
@ -350,7 +354,7 @@ namespace telegram
}); });
break; break;
} }
case "authorizationStateWaitPassword": case Td.TdApi.AuthorizationState.AuthorizationStateWaitPassword _:
{ {
Console.Write("[tgcli] 2fa password> "); Console.Write("[tgcli] 2fa password> ");
var pass = ReadConsolePassword(); var pass = ReadConsolePassword();
@ -360,12 +364,28 @@ namespace telegram
}); });
break; break;
} }
case "authorizationStateReady": case Td.TdApi.AuthorizationState.AuthorizationStateReady _:
Console.WriteLine("[tgcli] logged in."); Console.WriteLine("[tgcli] logged in.");
authorized = true; 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; break;
default: default:
Console.WriteLine($"unknown state: {state}"); Console.WriteLine($"unknown state: {state.AuthorizationState.DataType}");
Environment.Exit(1); Environment.Exit(1);
break; break;
} }
@ -450,6 +470,8 @@ namespace telegram
chat.Type is Td.TdApi.ChatType.ChatTypeSecret; chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var isSecret = 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 finalOutput = "";
var replyPrefix = $"{origPrefix}{Ansi.Yellow}Re: {Ansi.Bold}{Ansi.Green}[{time}] " + var replyPrefix = $"{origPrefix}{Ansi.Yellow}Re: {Ansi.Bold}{Ansi.Green}[{time}] " +
$"{(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} " + $"{(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} " +