fix history for limit >100, improve newline input, redo codestyle

This commit is contained in:
Laura Hausmann 2019-12-15 15:03:20 +01:00
parent 132334bedd
commit 93eda081db
Signed by: zotan
GPG key ID: 5EC1D38FFC321311
3 changed files with 1538 additions and 1904 deletions

View file

@ -5,10 +5,8 @@ using TdLib;
using static telegram.tgcli;
using static telegram.Util;
namespace telegram
{
public abstract class Command
{
namespace telegram {
public abstract class Command {
public string trigger;
public string shortcut;
public string description;
@ -16,8 +14,7 @@ namespace telegram
public int paramCount;
public abstract void Handler(List<string> inputParams);
protected Command(string trigger, string shortcut, string description, string syntax, int paramCount)
{
protected Command(string trigger, string shortcut, string description, string syntax, int paramCount) {
this.trigger = trigger;
this.shortcut = shortcut;
this.description = description;
@ -26,10 +23,8 @@ namespace telegram
}
}
public static class CommandManager
{
public static readonly List<Command> Commands = new List<Command>
{
public static class CommandManager {
public static readonly List<Command> Commands = new List<Command> {
new ClearCommand(),
new CloseCommand(),
new EditCommand(),
@ -52,59 +47,51 @@ namespace telegram
new LogoutCommand(),
};
public static void HandleCommand(string input)
{
public static void HandleCommand(string input) {
var split = input.Split(" ").ToList();
var trigger = split.First();
var command = Commands.Find(p => p.trigger == trigger || p.shortcut == trigger);
if (command == null)
{
if (command == null) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid command. Check /help for all available commands.");
return;
}
split.RemoveAt(0);
if (command.paramCount == -1)
{
if (command.paramCount == -1) {
command.Handler(split);
}
else if (split.Count == command.paramCount)
{
else if (split.Count == command.paramCount) {
command.Handler(split);
}
else
{
else {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid command syntax. Check /help for more information.");
}
}
}
public class OpenCommand : Command
{
public OpenCommand() : base("o", "^O", "opens a chat. queries chat list", "<query>", -1)
{
}
public class OpenCommand : Command {
public OpenCommand() : base("o", "^O", "opens a chat. queries chat list", "<query>", -1) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
if (inputParams.Count == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid command syntax. Check /help for more information.");
return;
}
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
var chatId = SearchChatId(query);
if (chatId == 0) return;
if (chatId == 0)
return;
currentChatId = 0;
currentChatUserId = 0;
currentUserRead = false;
var chat = GetChat(chatId);
if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat)
{
if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat) {
currentChatUserId = privChat.UserId;
}
@ -113,30 +100,24 @@ namespace telegram
chat.Title = TruncateString(chat.Title, 20);
prefix = $"[{chat.Title}";
lock (@lock)
{
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening chat: {chat.Title}");
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" +
$"{(chat.UnreadCount == 1 ? "." : "s.")}");
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" + $"{(chat.UnreadCount == 1 ? "." : "s.")}");
if (chat.UnreadCount >= 5)
{
if (chat.UnreadCount >= 5) {
var capped = chat.UnreadCount > 50;
GetHistory(chatId, capped ? 50 : chat.UnreadCount).ForEach(AddMessageToQueue);
if (capped)
messageQueue.Add(
$"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages.");
messageQueue.Add($"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages.");
}
else if (chat.UnreadCount > 0)
{
else if (chat.UnreadCount > 0) {
var unreads = GetHistory(chatId, chat.UnreadCount);
var rest = GetHistory(chatId, 5 - unreads.Count, unreads.First().Id);
rest.ForEach(AddMessageToQueue);
messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---");
unreads.ForEach(AddMessageToQueue);
}
else
{
else {
GetHistory(chatId).ForEach(AddMessageToQueue);
}
}
@ -145,8 +126,7 @@ namespace telegram
if (history.Count != 0)
MarkRead(chat.Id, history.First().Id);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
if (last == null) {
currentUserRead = true;
return;
}
@ -156,17 +136,12 @@ namespace telegram
}
}
public class NewChatCommand : Command
{
public NewChatCommand() : base("n", "", "starts a new chat.", "<username>", 1)
{
}
public class NewChatCommand : Command {
public NewChatCommand() : base("n", "", "starts a new chat.", "<username>", 1) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
var chat = GetChatByUsernameGlobal(inputParams[0]);
if (chat == null)
{
if (chat == null) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] User not found. Try /s <query> to find valid usernames.");
return;
@ -176,8 +151,7 @@ namespace telegram
currentChatUserId = 0;
currentUserRead = false;
if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat)
{
if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat) {
currentChatUserId = privChat.UserId;
}
@ -186,30 +160,24 @@ namespace telegram
chat.Title = TruncateString(chat.Title, 20);
prefix = $"[{chat.Title}";
lock (@lock)
{
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening chat: {chat.Title}");
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" +
$"{(chat.UnreadCount == 1 ? "." : "s.")}");
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" + $"{(chat.UnreadCount == 1 ? "." : "s.")}");
if (chat.UnreadCount >= 5)
{
if (chat.UnreadCount >= 5) {
var capped = chat.UnreadCount > 50;
GetHistory(chat.Id, capped ? 50 : chat.UnreadCount).ForEach(AddMessageToQueue);
if (capped)
messageQueue.Add(
$"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages.");
messageQueue.Add($"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages.");
}
else if (chat.UnreadCount > 0)
{
else if (chat.UnreadCount > 0) {
var unreads = GetHistory(chat.Id, chat.UnreadCount);
var rest = GetHistory(chat.Id, 5 - unreads.Count, unreads.First().Id);
rest.ForEach(AddMessageToQueue);
messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---");
unreads.ForEach(AddMessageToQueue);
}
else
{
else {
GetHistory(chat.Id).ForEach(AddMessageToQueue);
}
}
@ -218,8 +186,7 @@ namespace telegram
if (history.Count != 0)
MarkRead(chat.Id, history.First().Id);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
if (last == null) {
currentUserRead = true;
return;
}
@ -229,47 +196,35 @@ namespace telegram
}
}
public class CloseSecretChatCommand : Command
{
public CloseSecretChatCommand() : base("cs", "", "closes a secret chat (permanently)", "", 0)
{
}
public class CloseSecretChatCommand : Command {
public CloseSecretChatCommand() : base("cs", "", "closes a secret chat (permanently)", "", 0) { }
public override void Handler(List<string> _)
{
if (currentChatId != 0 && GetChat(currentChatId).Type is TdApi.ChatType.ChatTypeSecret type)
{
public override void Handler(List<string> _) {
if (currentChatId != 0 && GetChat(currentChatId).Type is TdApi.ChatType.ChatTypeSecret type) {
CloseSecretChat(type.SecretChatId);
DeleteChatHistory(currentChatId);
CommandManager.HandleCommand("c");
}
else
{
else {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No secret chat selected, cannot continue.");
}
}
}
public class NewSecretChatCommand : Command
{
public NewSecretChatCommand() : base("ns", "", "creates a new secret chat.", "<username>", 1)
{
}
public class NewSecretChatCommand : Command {
public NewSecretChatCommand() : base("ns", "", "creates a new secret chat.", "<username>", 1) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
var userId = GetUserIdByUsername(inputParams[0]);
if (userId == 0)
{
if (userId == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] User not found. Try /s <query> to find valid usernames.");
return;
}
if (GetSecretChats().Count(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId) > 0)
{
if (GetSecretChats().Count(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId) > 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] You already have a secret chat with the specified user.");
return;
@ -280,40 +235,32 @@ namespace telegram
}
}
public class OpenSecretDirectCommand : Command
{
public OpenSecretDirectCommand() : base("osd", "", "opens a secret chat by chat id", "<chat_id>", 1)
{
}
public class OpenSecretDirectCommand : Command {
public OpenSecretDirectCommand() : base("osd", "", "opens a secret chat by chat id", "<chat_id>", 1) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
var id = inputParams[0];
if (!long.TryParse(id, out var chatId))
{
if (!long.TryParse(id, out var chatId)) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid chat id.");
return;
}
var chat = GetChat(chatId);
if (chat == null)
{
if (chat == null) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid chat id.");
return;
}
TdApi.SecretChat secChat;
if (chat.Type is TdApi.ChatType.ChatTypeSecret secretChat)
{
if (chat.Type is TdApi.ChatType.ChatTypeSecret secretChat) {
currentChatUserId = secretChat.UserId;
currentChatId = chat.Id;
currentUserRead = false;
secChat = GetSecretChat(secretChat.SecretChatId);
}
else
{
else {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] The specified chat isn't a secret chat.");
return;
@ -322,38 +269,30 @@ namespace telegram
chat.Title = TruncateString(chat.Title, 20);
prefix = $"[{Ansi.Red}sec {Ansi.ResetAll}{chat.Title}]";
lock (@lock)
{
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening secret chat: {chat.Title}");
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" +
$"{(chat.UnreadCount == 1 ? "." : "s.")}");
if (secChat.State is TdApi.SecretChatState.SecretChatStateClosed)
{
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" + $"{(chat.UnreadCount == 1 ? "." : "s.")}");
if (secChat.State is TdApi.SecretChatState.SecretChatStateClosed) {
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat has ended. No messages can be sent.");
}
else if (secChat.State is TdApi.SecretChatState.SecretChatStatePending)
{
else if (secChat.State is TdApi.SecretChatState.SecretChatStatePending) {
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat is pending. No messages can be sent.");
}
if (chat.UnreadCount >= 5)
{
if (chat.UnreadCount >= 5) {
var capped = chat.UnreadCount > 50;
GetHistory(chatId, capped ? 50 : chat.UnreadCount, isSecret: true).ForEach(AddMessageToQueue);
if (capped)
messageQueue.Add(
$"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages.");
messageQueue.Add($"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages.");
}
else if (chat.UnreadCount > 0)
{
else if (chat.UnreadCount > 0) {
var unreads = GetHistory(chatId, chat.UnreadCount, isSecret: true);
var rest = GetHistory(chatId, 5 - unreads.Count, unreads.First().Id, isSecret: true);
rest.ForEach(AddMessageToQueue);
messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---");
unreads.ForEach(AddMessageToQueue);
}
else
{
else {
GetHistory(chatId, isSecret: true).ForEach(AddMessageToQueue);
}
}
@ -362,8 +301,7 @@ namespace telegram
if (history.Count != 0)
MarkRead(chat.Id, history.First().Id);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
if (last == null) {
currentUserRead = true;
return;
}
@ -373,40 +311,33 @@ namespace telegram
}
}
public class OpenSecretCommand : Command
{
public OpenSecretCommand() : base("os", "", "opens a secret chat. queries chat list.", "<query>", -1)
{
}
public class OpenSecretCommand : Command {
public OpenSecretCommand() : base("os", "", "opens a secret chat. queries chat list.", "<query>", -1) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
var userId = SearchUserInChats(query);
if (userId == 0 || query.Length == 0)
{
if (userId == 0 || query.Length == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No matching chat found.");
return;
}
var chat = GetSecretChats().Find(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId);
if (chat == null)
{
if (chat == null) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No matching secret chat found.");
return;
}
TdApi.SecretChat secChat;
if (chat.Type is TdApi.ChatType.ChatTypeSecret secretChat)
{
if (chat.Type is TdApi.ChatType.ChatTypeSecret secretChat) {
currentChatUserId = secretChat.UserId;
currentChatId = chat.Id;
currentUserRead = false;
secChat = GetSecretChat(secretChat.SecretChatId);
}
else
{
else {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No matching secret chat found. (this error should be impossible to produce)");
return;
@ -415,38 +346,30 @@ namespace telegram
chat.Title = TruncateString(chat.Title, 20);
prefix = $"[{Ansi.Red}sec {Ansi.ResetAll}{chat.Title}]";
lock (@lock)
{
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] Opening secret chat: {chat.Title}");
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" +
$"{(chat.UnreadCount == 1 ? "." : "s.")}");
if (secChat.State is TdApi.SecretChatState.SecretChatStateClosed)
{
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {chat.UnreadCount} unread message" + $"{(chat.UnreadCount == 1 ? "." : "s.")}");
if (secChat.State is TdApi.SecretChatState.SecretChatStateClosed) {
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat has ended. No messages can be sent.");
}
else if (secChat.State is TdApi.SecretChatState.SecretChatStatePending)
{
else if (secChat.State is TdApi.SecretChatState.SecretChatStatePending) {
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat is pending. No messages can be sent.");
}
if (chat.UnreadCount >= 5)
{
if (chat.UnreadCount >= 5) {
var capped = chat.UnreadCount > 50;
GetHistory(chat.Id, capped ? 50 : chat.UnreadCount, isSecret: true).ForEach(AddMessageToQueue);
if (capped)
messageQueue.Add(
$"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages.");
messageQueue.Add($"{Ansi.Yellow}[tgcli] " + $"Showing 50 of {chat.UnreadCount} unread messages.");
}
else if (chat.UnreadCount > 0)
{
else if (chat.UnreadCount > 0) {
var unreads = GetHistory(chat.Id, chat.UnreadCount, isSecret: true);
var rest = GetHistory(chat.Id, 5 - unreads.Count, unreads.First().Id, isSecret: true);
rest.ForEach(AddMessageToQueue);
messageQueue.Add($"{Ansi.Yellow}[tgcli] ---UNREAD---");
unreads.ForEach(AddMessageToQueue);
}
else
{
else {
GetHistory(chat.Id, isSecret: true).ForEach(AddMessageToQueue);
}
}
@ -455,8 +378,7 @@ namespace telegram
if (history.Count != 0)
MarkRead(chat.Id, history.First().Id);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
if (last == null) {
currentUserRead = true;
return;
}
@ -466,35 +388,26 @@ namespace telegram
}
}
public class CloseUnreadCommand : Command
{
public CloseUnreadCommand() : base("cu", "", "closes a chat, marking it as unread", "", 0)
{
}
public class CloseUnreadCommand : Command {
public CloseUnreadCommand() : base("cu", "", "closes a chat, marking it as unread", "", 0) { }
public override void Handler(List<string> inputParams)
{
if (currentChatId == 0)
{
public override void Handler(List<string> inputParams) {
if (currentChatId == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
MarkUnread(currentChatId);
CommandManager.HandleCommand("c");
}
}
public class CloseCommand : Command
{
public CloseCommand() : base("c", "^E", "closes a chat", "", 0)
{
}
public class CloseCommand : Command {
public CloseCommand() : base("c", "^E", "closes a chat", "", 0) { }
public override void Handler(List<string> inputParams)
{
if (currentChatId == 0)
{
public override void Handler(List<string> inputParams) {
if (currentChatId == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
@ -506,81 +419,72 @@ namespace telegram
lastMessage = null;
prefix = "[tgcli";
lock (@lock)
{
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] Closing chat.");
var count = missedMessages.Count;
if (count == 0) return;
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {count} missed message" +
$"{(count == 1 ? "." : "s.")}");
if (count == 0)
return;
messageQueue.Add($"{Ansi.Yellow}" + $"[tgcli] You have {count} missed message" + $"{(count == 1 ? "." : "s.")}");
messageQueue.AddRange(missedMessages);
missedMessages.Clear();
}
}
}
public class HistoryCommand : Command
{
public HistoryCommand() : base("h", "", "shows chat history. default limit is 5", "[1-50]", -1)
{
}
public class HistoryCommand : Command {
public HistoryCommand() : base("h", "", "shows chat history. default limit is 5", "[1-50]", -1) { }
public override void Handler(List<string> inputParams)
{
if (currentChatId == 0)
{
public override void Handler(List<string> inputParams) {
if (currentChatId == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
var history = inputParams.Count == 1 && int.TryParse(inputParams[0], out var limit)
? GetHistory(currentChatId, limit)
: GetHistory(currentChatId);
List<TdApi.Message> history;
lock (@lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] Last {history.Count} messages in " +
$"{GetChat(currentChatId).Title}");
if (inputParams.Count == 1 && int.TryParse(inputParams[0], out var limit)) {
history = GetHistory(currentChatId, Math.Min(limit, 50));
while (limit > 50) {
limit -= 50;
history.InsertRange(0, GetHistory(currentChatId, Math.Min(limit, 50), history.First().Id));
}
}
foreach (var msg in history)
{
else {
history = GetHistory(currentChatId);
}
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] Last {history.Count} messages in " + $"{GetChat(currentChatId).Title}");
}
foreach (var msg in history) {
AddMessageToQueue(msg);
}
}
}
public class ClearCommand : Command
{
public ClearCommand() : base("cl", "^L", "clears console", "", 0)
{
}
public class ClearCommand : Command {
public ClearCommand() : base("cl", "^L", "clears console", "", 0) { }
public override void Handler(List<string> inputParams)
{
lock (@lock)
{
public override void Handler(List<string> inputParams) {
lock (@lock) {
Console.Clear();
}
}
}
public class UnreadsCommand : Command
{
public UnreadsCommand() : base("u", "^U", "displays unread chat", "[all]", -1)
{
}
public class UnreadsCommand : Command {
public UnreadsCommand() : base("u", "^U", "displays unread chat", "[all]", -1) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
var unreads = GetUnreadChats(inputParams.Count == 1 && inputParams[0].Equals("all"));
lock (@lock)
{
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] You have {unreads.Count} unread chats.");
unreads.ForEach(chat =>
{
unreads.ForEach(chat => {
string line;
if (chat.UnreadCount == 0)
line = $"{Ansi.Bold}{Ansi.Yellow}[M] {chat.Title}";
@ -594,27 +498,20 @@ namespace telegram
}
}
public class ListChatsCommand : Command
{
public ListChatsCommand() : base("lc", "", "lists all chats, optionally filtered", "[query]", -1)
{
}
public class ListChatsCommand : Command {
public ListChatsCommand() : base("lc", "", "lists all chats, optionally filtered", "[query]", -1) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
var chats = GetChats();
lock (@lock)
{
if (inputParams.Count > 0)
{
lock (@lock) {
if (inputParams.Count > 0) {
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
chats = chats.FindAll(p => p.Title.ToLower().Contains(query.ToLower()));
}
messageQueue.Add($"{Ansi.Yellow}[tgcli] Listing {chats.Count} chats.");
chats.ForEach(chat =>
{
chats.ForEach(chat => {
string line;
if (chat.UnreadCount == 0)
line = $"{Ansi.Bold}{Ansi.Blue}[0] {chat.Title}";
@ -626,16 +523,11 @@ namespace telegram
}
}
public class SearchUserCommand : Command
{
public SearchUserCommand() : base("s", "", "searches for users globally", "<query>", -1)
{
}
public class SearchUserCommand : Command {
public SearchUserCommand() : base("s", "", "searches for users globally", "<query>", -1) { }
public override void Handler(List<string> inputParams)
{
if (inputParams.Count == 0)
{
public override void Handler(List<string> inputParams) {
if (inputParams.Count == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information.");
return;
@ -646,11 +538,9 @@ namespace telegram
var chats = SearchChatsGlobal(query);
chats = chats.FindAll(p => p.Type is TdApi.ChatType.ChatTypePrivate);
lock (@lock)
{
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] Listing {chats.Count} chats.");
chats.ForEach(chat =>
{
chats.ForEach(chat => {
string line;
var type = (TdApi.ChatType.ChatTypePrivate) chat.Type;
var user = GetUser(type.UserId);
@ -661,14 +551,10 @@ namespace telegram
}
}
public class AddContactCommand : Command
{
public AddContactCommand() : base("ac", "", "adds user to contact list", "<username>", 1)
{
}
public class AddContactCommand : Command {
public AddContactCommand() : base("ac", "", "adds user to contact list", "<username>", 1) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
/*
var query = inputParams[0];
@ -687,39 +573,25 @@ namespace telegram
}
}
public class ListSecretChatsCommand : Command
{
public ListSecretChatsCommand() : base("ls", "", "displays all open secret chats", "", 0)
{
}
public class ListSecretChatsCommand : Command {
public ListSecretChatsCommand() : base("ls", "", "displays all open secret chats", "", 0) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
var secretChats = GetSecretChats();
lock (@lock)
{
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] Listing {secretChats.Count} secret chats:");
secretChats.ForEach(chat =>
{
messageQueue.Add($"{Ansi.Bold}{Ansi.Red}[sec] {chat.Title} -> {chat.Id}");
});
secretChats.ForEach(chat => { messageQueue.Add($"{Ansi.Bold}{Ansi.Red}[sec] {chat.Title} -> {chat.Id}"); });
}
}
}
public class HelpCommand : Command
{
public HelpCommand() : base("help", "", "lists all commands", "", 0)
{
}
public class HelpCommand : Command {
public HelpCommand() : base("help", "", "lists all commands", "", 0) { }
public override void Handler(List<string> inputParams)
{
lock (@lock)
{
public override void Handler(List<string> inputParams) {
lock (@lock) {
messageQueue.Add($"{Ansi.Yellow}[tgcli] Listing {CommandManager.Commands.Count} commands:");
CommandManager.Commands.ForEach(command =>
{
CommandManager.Commands.ForEach(command => {
var commandText = $"/{command.trigger}";
if (!string.IsNullOrWhiteSpace(command.syntax))
commandText += $" {command.syntax}";
@ -733,31 +605,20 @@ namespace telegram
}
}
public class QuitCommand : Command
{
public QuitCommand() : base("q", "^D", "quits the program", "", 0)
{
}
public class QuitCommand : Command {
public QuitCommand() : base("q", "^D", "quits the program", "", 0) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
quitting = true;
}
}
public class EditCommand : Command
{
public EditCommand() : base("e", "", "edits last message. param empty adds last message to inputline",
"[message]", -1)
{
}
public class EditCommand : Command {
public EditCommand() : base("e", "", "edits last message. param empty adds last message to inputline", "[message]", -1) { }
public override void Handler(List<string> inputParams)
{
try
{
if (inputParams.Count == 0)
{
public override void Handler(List<string> inputParams) {
try {
if (inputParams.Count == 0) {
currentInputLine = "/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text;
Emojis.ForEach(em => currentInputLine = currentInputLine.Replace(em.Item2, em.Item1));
return;
@ -765,55 +626,46 @@ namespace telegram
var message = inputParams.Aggregate((current, param) => current + " " + param).Trim();
if (currentChatId == 0)
{
if (currentChatId == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
if (lastMessage == null)
{
if (lastMessage == null) {
//try to find last message
var history = GetHistory(currentChatId, 50);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
if (last == null) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No message to edit found, cannot continue.");
return;
}
lastMessage = last;
}
if (string.IsNullOrWhiteSpace(message))
{
if (string.IsNullOrWhiteSpace(message)) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No message specified, cannot continue.");
return;
}
EditMessage(message, lastMessage);
}
catch
{
catch {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error editing message.");
}
}
}
public class ReplyCommand : Command
{
public ReplyCommand() : base("r", "", "replies to message", "<offset> <message>", -1)
{
}
public class ReplyCommand : Command {
public ReplyCommand() : base("r", "", "replies to message", "<offset> <message>", -1) { }
public override void Handler(List<string> inputParams)
{
try
{
if (inputParams.Count < 2)
{
public override void Handler(List<string> inputParams) {
try {
if (inputParams.Count < 2) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information.");
return;
@ -825,8 +677,7 @@ namespace telegram
history.Reverse();
var message = inputParams.Aggregate((current, param) => current + " " + param).Trim();
if (!parsed || string.IsNullOrWhiteSpace(message) || history.Count < offset)
{
if (!parsed || string.IsNullOrWhiteSpace(message) || history.Count < offset) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}Invalid syntax, check /help for more information.");
return;
@ -834,8 +685,7 @@ namespace telegram
var replyMessage = history[offset - 1];
if (currentChatId == 0)
{
if (currentChatId == 0) {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
@ -843,22 +693,17 @@ namespace telegram
SendMessage(message, currentChatId, replyMessage.Id);
}
catch
{
catch {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error sending message.");
}
}
}
public class LogoutCommand : Command
{
public LogoutCommand() : base("logout", "", "log out this session (destroys all local data)", "", 0)
{
}
public class LogoutCommand : Command {
public LogoutCommand() : base("logout", "", "log out this session (destroys all local data)", "", 0) { }
public override void Handler(List<string> inputParams)
{
public override void Handler(List<string> inputParams) {
LogOut();
}
}

View file

@ -6,12 +6,9 @@ using NeoSmart.Unicode;
using static TdLib.TdApi;
using static telegram.tgcli;
namespace telegram
{
public class Util
{
public static class Ansi
{
namespace telegram {
public class Util {
public static class Ansi {
public const string ResetAll = "\x1B[0m";
public const string Red = "\x1b[31m";
public const string Green = "\x1b[32m";
@ -23,18 +20,12 @@ namespace telegram
public const string BoldOff = "\x1b[22m";
}
public static User GetUser(int uid)
{
try
{
var uinfo = client.ExecuteAsync(new GetUser
{
UserId = uid
}).Result;
public static User GetUser(int uid) {
try {
var uinfo = client.ExecuteAsync(new GetUser {UserId = uid}).Result;
return uinfo;
}
catch
{
catch {
var user = new User();
user.FirstName = "null";
user.LastName = "null";
@ -42,76 +33,54 @@ namespace telegram
}
}
public static Chat GetChat(long chatId)
{
try
{
return client.ExecuteAsync(new GetChat
{
ChatId = chatId
}).Result;
public static Chat GetChat(long chatId) {
try {
return client.ExecuteAsync(new GetChat {ChatId = chatId}).Result;
}
catch
{
catch {
return null;
}
}
public static User GetMe()
{
public static User GetMe() {
return client.ExecuteAsync(new GetMe()).Result;
}
public static Message GetMessage(long chatId, long messageId)
{
return client.ExecuteAsync(new GetMessage
{
ChatId = chatId,
MessageId = messageId
}).Result;
public static Message GetMessage(long chatId, long messageId) {
return client.ExecuteAsync(new GetMessage {ChatId = chatId, MessageId = messageId}).Result;
}
public static int GetTotalMessages(long chatId)
{
try
{
var response = client.ExecuteAsync(new SearchChatMessages
{
ChatId = chatId,
Query = "+",
Limit = 1
});
public static int GetTotalMessages(long chatId) {
try {
var response = client.ExecuteAsync(new SearchChatMessages {ChatId = chatId, Query = "+", Limit = 1});
return response.Result.TotalCount;
}
catch
{
catch {
return 9999;
}
}
public static List<Message> GetHistory(long chatId, int limit = 5, long fromMessageId = 0, int offset = 0,
bool isSecret = false, bool skipTotal = false)
{
public static List<Message> GetHistory(long chatId, int limit = 5, long fromMessageId = 0, int offset = 0, bool isSecret = false,
bool skipTotal = false) {
var history = new List<Message>();
var total = GetTotalMessages(chatId);
var chat = GetChat(chatId);
if (chat.Type is ChatType.ChatTypeSupergroup || isSecret)
skipTotal = true;
if (limit > total && !skipTotal) limit = total;
if (limit > total && !skipTotal)
limit = total;
for (var i = 5; i > 0; i--) {
if (limit <= 0) {
if (total == 0)
return history;
for (var i = 5; i > 0; i--)
{
if (limit <= 0)
{
if (total == 0) return history;
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] " +
$"Limit cannot be less than one. Usage: /history <count>");
messageQueue.Add($"{Ansi.Red}[tgcli] " + $"Limit cannot be less than one. Usage: /history <count>");
return history;
}
var response = client.ExecuteAsync(new GetChatHistory
{
var response = client.ExecuteAsync(new GetChatHistory {
ChatId = chatId,
FromMessageId = fromMessageId,
Limit = limit,
@ -120,8 +89,7 @@ namespace telegram
})
.Result;
if (response.Messages_.Length < limit && i > 1 && !isSecret)
{
if (response.Messages_.Length < limit && i > 1 && !isSecret) {
Thread.Sleep(100);
continue;
}
@ -134,140 +102,83 @@ namespace telegram
return history;
}
public static List<Chat> GetUnreadChats(bool all = false)
{
var response = client.ExecuteAsync(new GetChats
{
OffsetOrder = long.MaxValue,
Limit = int.MaxValue
}).Result;
public static List<Chat> GetUnreadChats(bool all = false) {
var response = client.ExecuteAsync(new GetChats {OffsetOrder = long.MaxValue, Limit = int.MaxValue}).Result;
return all
? response.ChatIds.Select(GetChat).Where(c => c.UnreadCount > 0 || c.IsMarkedAsUnread).ToList()
: response.ChatIds.Select(GetChat).Where(c => (c.UnreadCount > 0 || c.IsMarkedAsUnread)
&& c.NotificationSettings.MuteFor == 0)
.ToList();
: response.ChatIds.Select(GetChat).Where(c => (c.UnreadCount > 0 || c.IsMarkedAsUnread) && c.NotificationSettings.MuteFor == 0).ToList();
}
public static List<Chat> GetChats()
{
var response = client.ExecuteAsync(new GetChats
{
OffsetOrder = long.MaxValue,
Limit = int.MaxValue
}).Result;
public static List<Chat> GetChats() {
var response = client.ExecuteAsync(new GetChats {OffsetOrder = long.MaxValue, Limit = int.MaxValue}).Result;
return response.ChatIds.Select(GetChat).ToList();
}
public static List<Chat> SearchChatsGlobal(string query)
{
if (query.TrimStart('@').Length < 5)
{
public static List<Chat> SearchChatsGlobal(string query) {
if (query.TrimStart('@').Length < 5) {
return new List<Chat>();
}
var response = client.ExecuteAsync(new SearchPublicChats
{
Query = query
}).Result;
var response = client.ExecuteAsync(new SearchPublicChats {Query = query}).Result;
var chats = response.ChatIds.Select(GetChat).ToList();
chats.AddRange(client.ExecuteAsync(new SearchChats
{
Query = query,
Limit = int.MaxValue
}).Result.ChatIds.Select(GetChat));
chats.AddRange(client.ExecuteAsync(new SearchChats {Query = query, Limit = int.MaxValue}).Result.ChatIds.Select(GetChat));
return chats;
}
public static Chat GetChatByUsernameGlobal(string username)
{
try
{
var response = client.ExecuteAsync(new SearchPublicChat
{
Username = username
}).Result;
public static Chat GetChatByUsernameGlobal(string username) {
try {
var response = client.ExecuteAsync(new SearchPublicChat {Username = username}).Result;
return response;
}
catch
{
catch {
return null;
}
}
public static int GetUserIdByUsername(string username)
{
try
{
var response = client.ExecuteAsync(new SearchPublicChat
{
Username = username
}).Result;
public static int GetUserIdByUsername(string username) {
try {
var response = client.ExecuteAsync(new SearchPublicChat {Username = username}).Result;
if (response.Type is ChatType.ChatTypePrivate priv)
return priv.UserId;
return 0;
}
catch
{
catch {
return 0;
}
}
public static void AddUserToContacts(int userId, string name)
{
public static void AddUserToContacts(int userId, string name) {
//TODO implement when TDLib 1.6 is released
}
public static List<Chat> GetSecretChats()
{
var response = client.ExecuteAsync(new GetChats
{
OffsetOrder = long.MaxValue,
Limit = int.MaxValue
}).Result;
public static List<Chat> GetSecretChats() {
var response = client.ExecuteAsync(new GetChats {OffsetOrder = long.MaxValue, Limit = int.MaxValue}).Result;
return response.ChatIds.Select(GetChat).Where(c => c.Type is ChatType.ChatTypeSecret).ToList();
}
public static void CloseSecretChat(int secretChatId)
{
client.ExecuteAsync(new CloseSecretChat()
{
SecretChatId = secretChatId
}).Wait();
public static void CloseSecretChat(int secretChatId) {
client.ExecuteAsync(new CloseSecretChat() {SecretChatId = secretChatId}).Wait();
}
public static Chat CreateSecretChat(int userId)
{
return client.ExecuteAsync(new CreateNewSecretChat
{
UserId = userId
}).Result;
public static Chat CreateSecretChat(int userId) {
return client.ExecuteAsync(new CreateNewSecretChat {UserId = userId}).Result;
}
public static void DeleteChatHistory(long chatId)
{
client.ExecuteAsync(new DeleteChatHistory
{
ChatId = chatId,
RemoveFromChatList = true,
Revoke = true
}).Wait();
public static void DeleteChatHistory(long chatId) {
client.ExecuteAsync(new DeleteChatHistory {ChatId = chatId, RemoveFromChatList = true, Revoke = true}).Wait();
}
public static SecretChat GetSecretChat(int secretChatId)
{
var response = client.ExecuteAsync(new GetSecretChat
{
SecretChatId = secretChatId
}).Result;
public static SecretChat GetSecretChat(int secretChatId) {
var response = client.ExecuteAsync(new GetSecretChat {SecretChatId = secretChatId}).Result;
return response;
}
public static void ClearCurrentConsoleLine()
{
public static void ClearCurrentConsoleLine() {
Console.Write("\u001b[2K\r");
//Console.SetCursorPosition(0, Console.WindowHeight);
@ -275,26 +186,20 @@ namespace telegram
//Console.SetCursorPosition(0, Console.WindowHeight);
}
public static string ReadConsolePassword()
{
public static string ReadConsolePassword() {
string pass = "";
do
{
do {
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter) {
pass += key.KeyChar;
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
{
else {
if (key.Key == ConsoleKey.Backspace && pass.Length > 0) {
pass = pass.Substring(0, (pass.Length - 1));
Console.Write("\b \b");
}
else if (key.Key == ConsoleKey.Enter)
{
else if (key.Key == ConsoleKey.Enter) {
break;
}
}
@ -304,165 +209,109 @@ namespace telegram
return pass;
}
public static void SendMessage(string message, long chatId, long replyTo = 0)
{
public static void SendMessage(string message, long chatId, long replyTo = 0) {
if (string.IsNullOrWhiteSpace(message))
return;
Emojis.ForEach(em => message = message.Replace(em.Item1, em.Item2));
client.ExecuteAsync(new SendMessage
{
client.ExecuteAsync(new SendMessage {
ChatId = chatId,
InputMessageContent = new InputMessageContent.InputMessageText
{
Text = new FormattedText()
{
Text = message
}
},
InputMessageContent = new InputMessageContent.InputMessageText {Text = new FormattedText() {Text = message}},
ReplyToMessageId = replyTo,
});
currentUserRead = false;
}
public static Message EditMessage(string newText, Message message)
{
public static Message EditMessage(string newText, Message message) {
Emojis.ForEach(em => newText = newText.Replace(em.Item1, em.Item2));
var msg = client.ExecuteAsync(new EditMessageText
{
var msg = client.ExecuteAsync(new EditMessageText {
ChatId = message.ChatId,
MessageId = message.Id,
InputMessageContent = new InputMessageContent.InputMessageText
{
Text = new FormattedText()
{
Text = newText
}
}
}).Result;
InputMessageContent = new InputMessageContent.InputMessageText {Text = new FormattedText() {Text = newText}}
})
.Result;
return msg;
}
public static void MarkRead(long chatId, long messageId)
{
client.ExecuteAsync(new ViewMessages
{
ChatId = chatId,
MessageIds = new[]
{
messageId
},
ForceRead = true
});
public static void MarkRead(long chatId, long messageId) {
client.ExecuteAsync(new ViewMessages {ChatId = chatId, MessageIds = new[] {messageId}, ForceRead = true});
}
public static void MarkUnread(long chatId)
{
client.ExecuteAsync(new ToggleChatIsMarkedAsUnread
{
ChatId = chatId,
IsMarkedAsUnread = true,
});
public static void MarkUnread(long chatId) {
client.ExecuteAsync(new ToggleChatIsMarkedAsUnread {ChatId = chatId, IsMarkedAsUnread = true,});
}
public static long SearchChatId(string query)
{
try
{
var results = client.ExecuteAsync(new SearchChats
{
Query = query,
Limit = 5
}).Result;
public static long SearchChatId(string query) {
try {
var results = client.ExecuteAsync(new SearchChats {Query = query, Limit = 5}).Result;
if (query.StartsWith("@"))
return results.ChatIds.First(p =>
GetChat(p).Type is ChatType.ChatTypePrivate type &&
GetUser(type.UserId).Username == query.Substring(1));
return results.ChatIds.First(p => GetChat(p).Type is ChatType.ChatTypePrivate type && GetUser(type.UserId).Username == query.Substring(1));
return results.ChatIds.First(p => !(GetChat(p).Type is ChatType.ChatTypeSecret));
}
catch
{
catch {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No results found.");
return 0;
}
}
public static int SearchUserInChats(string query)
{
var results = client.ExecuteAsync(new SearchChatsOnServer
{
Query = query,
Limit = 5
}).Result;
public static int SearchUserInChats(string query) {
var results = client.ExecuteAsync(new SearchChatsOnServer {Query = query, Limit = 5}).Result;
if (results.ChatIds.Length == 0)
return 0;
return results.ChatIds
.Select(GetChat)
return results.ChatIds.Select(GetChat)
.Where(p => p.Type is ChatType.ChatTypePrivate)
.Select(p => ((ChatType.ChatTypePrivate) p.Type).UserId)
.First();
}
public static int SearchContacts(string query)
{
public static int SearchContacts(string query) {
//TODO implement when TDLib 1.6 is released
try
{
var results = client.ExecuteAsync(new SearchContacts
{
Query = query,
Limit = 5
}).Result;
try {
var results = client.ExecuteAsync(new SearchContacts {Query = query, Limit = 5}).Result;
return query.StartsWith("@")
? results.UserIds.First(p => GetUser(p).Username == query.Substring(1))
: results.UserIds.First();
return query.StartsWith("@") ? results.UserIds.First(p => GetUser(p).Username == query.Substring(1)) : results.UserIds.First();
}
catch
{
catch {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No results found.");
return 0;
}
}
public static void LogOut()
{
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;
if (string.IsNullOrWhiteSpace(username))
username = sender.FirstName + " " +
sender.LastName;
username = sender.FirstName + " " + sender.LastName;
else
username = "@" + username;
return username;
}
public static string FormatTime(long unix)
{
public static string FormatTime(long unix) {
var time = DateTimeOffset.FromUnixTimeSeconds(unix).DateTime.ToLocalTime();
var currentTime = DateTime.Now.ToLocalTime();
return time.ToString(time.Date.Ticks == currentTime.Date.Ticks ? "HH:mm" : "yyyy-MM-dd HH:mm");
}
public static bool IsMessageRead(long chatId, long messageId)
{
public static bool IsMessageRead(long chatId, long messageId) {
var chat = GetChat(chatId);
return chat.LastReadOutboxMessageId >= messageId;
}
public static int GetActualStringWidth(string input)
{
public static int GetActualStringWidth(string input) {
input = input.Replace(Ansi.Blue, "");
input = input.Replace(Ansi.Bold, "");
input = input.Replace(Ansi.Cyan, "");
@ -475,22 +324,19 @@ namespace telegram
return input.Length;
}
public static string GetFormattedStatus(bool isRead)
{
public static string GetFormattedStatus(bool isRead) {
var output = " ";
output += (isRead ? Ansi.Green : Ansi.Red) + "r";
return output + $"{Ansi.ResetAll}]";
}
public static string TruncateString(string input, int maxLen)
{
public static string TruncateString(string input, int maxLen) {
if (maxLen < 2)
maxLen = 2;
return input.Length <= maxLen ? input : input.Substring(0, maxLen - 1) + "~";
}
public static string TruncateMessageStart(string input, int maxLen)
{
public static string TruncateMessageStart(string input, int maxLen) {
if (maxLen < 2)
maxLen = 2;
if (input.Contains("⏎ "))
@ -498,8 +344,7 @@ namespace telegram
return input.Length < maxLen ? input : "<" + input.Substring(input.Length - maxLen + 2);
}
public static readonly List<Tuple<string, string>> Emojis = new List<Tuple<string, string>>
{
public static readonly List<Tuple<string, string>> Emojis = new List<Tuple<string, string>> {
new Tuple<string, string>("⏎ ", "\n"),
new Tuple<string, string>(":xd:", Emoji.FaceWithTearsOfJoy.Sequence.AsString),
new Tuple<string, string>(":check:", Emoji.WhiteHeavyCheckMark.Sequence.AsString),
@ -511,8 +356,7 @@ namespace telegram
new Tuple<string, string>(":shrugm:", Emoji.ManShrugging.Sequence.AsString)
};
public static readonly List<ConsoleKey> SpecialKeys = new List<ConsoleKey>
{
public static readonly List<ConsoleKey> SpecialKeys = new List<ConsoleKey> {
ConsoleKey.Backspace,
ConsoleKey.Tab,
ConsoleKey.Clear,

View file

@ -10,8 +10,7 @@ using static telegram.CommandManager;
// ReSharper disable SwitchStatementMissingSomeEnumCasesNoDefault
namespace telegram
{
namespace telegram {
/*
* TODO:
* fuzzy matching for replies?
@ -28,8 +27,7 @@ namespace telegram
*/
// ReSharper disable once InconsistentNaming
public static class tgcli
{
public static class tgcli {
public static volatile Td.TdClient client = new Td.TdClient();
public static string dbdir = "";
public static volatile bool authorized;
@ -47,42 +45,30 @@ namespace telegram
public static volatile object @lock = new object();
private static void Main(string[] args)
{
private static void Main(string[] args) {
if (args.Length == 1 && args[0] == "-s")
silent = true;
dbdir =
$"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}{Path.DirectorySeparatorChar}.tgcli";
dbdir = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}{Path.DirectorySeparatorChar}.tgcli";
if (!Directory.Exists(dbdir))
Directory.CreateDirectory(dbdir);
client.Send(new Td.TdApi.SetLogStream
{
LogStream = new Td.TdApi.LogStream.LogStreamFile
{
Path = Path.Combine(dbdir, "tdlib.log"),
MaxFileSize = 10000000
}
client.Send(new Td.TdApi.SetLogStream {
LogStream = new Td.TdApi.LogStream.LogStreamFile {Path = Path.Combine(dbdir, "tdlib.log"), MaxFileSize = 10000000}
});
client.Send(new Td.TdApi.SetLogVerbosityLevel
{
NewVerbosityLevel = 2
});
client.Send(new Td.TdApi.SetLogVerbosityLevel {NewVerbosityLevel = 2});
Console.Clear();
ClearCurrentConsoleLine();
client.UpdateReceived += HandleUpdate;
OnAuthUpdate(new Td.TdApi.Update.UpdateAuthorizationState()
{
OnAuthUpdate(new Td.TdApi.Update.UpdateAuthorizationState() {
AuthorizationState = new Td.TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters()
});
while (!authorized)
{
while (!authorized) {
Thread.Sleep(1);
}
@ -93,31 +79,25 @@ namespace telegram
Console.WriteLine($"{Ansi.Yellow}[tgcli] Shutting down...{Ansi.ResetAll}");
}
private static void MainLoop()
{
private static void MainLoop() {
var key = Console.ReadKey(true);
OnKeyPressed(key);
}
private static void HandleUpdate(object sender, Td.TdApi.Update e)
{
switch (e)
{
private static void HandleUpdate(object sender, Td.TdApi.Update e) {
switch (e) {
case Td.TdApi.Update.UpdateAuthorizationState state:
OnAuthUpdate(state);
break;
case Td.TdApi.Update.UpdateNewMessage message:
{
case Td.TdApi.Update.UpdateNewMessage message: {
Task.Run(() => AddMessageToQueue(message.Message));
break;
}
case Td.TdApi.Update.UpdateMessageContent message:
Task.Run(() => AddMessageToQueue(message));
Task.Run(() =>
{
Task.Run(() => {
var msg = GetMessage(message.ChatId, message.MessageId);
if (msg.IsOutgoing && currentChatId == msg.ChatId)
{
if (msg.IsOutgoing && currentChatId == msg.ChatId) {
lastMessage = msg;
}
});
@ -126,33 +106,36 @@ namespace telegram
lastMessage = sentMsg.Message;
break;
case Td.TdApi.Update.UpdateChatReadOutbox update:
if (lastMessage != null && lastMessage.ChatId == update.ChatId)
{
if (lastMessage != null && lastMessage.ChatId == update.ChatId) {
currentUserRead = true;
ScreenUpdate();
}
break;
case Td.TdApi.Update.UpdateConnectionState state:
switch (state.State)
{
switch (state.State) {
case Td.TdApi.ConnectionState.ConnectionStateConnecting _:
connectionState = "Connecting";
if (!authorized) return;
if (!authorized)
return;
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connecting to Telegram servers...");
ScreenUpdate();
break;
case Td.TdApi.ConnectionState.ConnectionStateConnectingToProxy _:
connectionState = "Connecting";
if (!authorized) return;
if (!authorized)
return;
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connecting to Proxy...");
ScreenUpdate();
break;
case Td.TdApi.ConnectionState.ConnectionStateReady _:
if (!authorized) return;
if (!authorized)
return;
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connected.");
Task.Run(() =>
{
Task.Run(() => {
HandleCommand("u");
connectionState = "Ready";
ScreenUpdate();
@ -161,13 +144,17 @@ namespace telegram
break;
case Td.TdApi.ConnectionState.ConnectionStateUpdating _:
connectionState = "Updating";
if (!authorized) return;
if (!authorized)
return;
messageQueue.Add($"{Ansi.Yellow}[tgcli] Updating message cache...");
ScreenUpdate();
break;
case Td.TdApi.ConnectionState.ConnectionStateWaitingForNetwork _:
connectionState = "Waiting for Network";
if (!authorized) return;
if (!authorized)
return;
messageQueue.Add($"{Ansi.Yellow}[tgcli] Lost connection. Waiting for network...");
ScreenUpdate();
break;
@ -176,15 +163,13 @@ namespace telegram
break;
case Td.TdApi.Update.UpdateSecretChat update:
var chat = update.SecretChat;
switch (chat.State)
{
switch (chat.State) {
case Td.TdApi.SecretChatState.SecretChatStateClosed _:
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat with {chat.Id} was closed.");
ScreenUpdate();
break;
case Td.TdApi.SecretChatState.SecretChatStatePending _:
break;
case Td.TdApi.SecretChatState.SecretChatStatePending _: break;
case Td.TdApi.SecretChatState.SecretChatStateReady _:
lock (@lock)
messageQueue.Add($"{Ansi.Green}[tgcli] Secret chat {chat.Id} connected.");
@ -196,10 +181,8 @@ namespace telegram
}
}
public static void ScreenUpdate()
{
lock (@lock)
{
public static void ScreenUpdate() {
lock (@lock) {
ClearCurrentConsoleLine();
messageQueue.ForEach(p => Console.WriteLine(p + Ansi.ResetAll));
if (messageQueue.Count > 0 && !silent)
@ -219,29 +202,23 @@ namespace telegram
}
}
private static void OnKeyPressed(ConsoleKeyInfo key)
{
switch (key.Key)
{
private static void OnKeyPressed(ConsoleKeyInfo key) {
switch (key.Key) {
case ConsoleKey.Enter when connectionState != "Ready":
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] " +
"Connection unstable. Check your network connection and try again.");
messageQueue.Add($"{Ansi.Red}[tgcli] " + "Connection unstable. Check your network connection and try again.");
ScreenUpdate();
break;
case ConsoleKey.Enter when currentInputLine.StartsWith("/"):
{
case ConsoleKey.Enter when currentInputLine.StartsWith("/"): {
var command = currentInputLine.Substring(1);
currentInputLine = "";
HandleCommand(command);
ScreenUpdate();
return;
}
case ConsoleKey.Enter when currentChatId == 0:
{
case ConsoleKey.Enter when currentChatId == 0: {
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] " +
"No chat selected. Select a chat with /open <query>");
messageQueue.Add($"{Ansi.Red}[tgcli] " + "No chat selected. Select a chat with /open <query>");
ScreenUpdate();
return;
}
@ -251,8 +228,7 @@ namespace telegram
ScreenUpdate();
break;
case ConsoleKey.Backspace when currentInputLine.Length >= 1:
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt))
{
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt)) {
var lastIndex = currentInputLine.TrimEnd().LastIndexOf(" ", StringComparison.Ordinal);
if (lastIndex < 0)
lastIndex = 0;
@ -266,10 +242,8 @@ namespace telegram
currentInputLine = currentInputLine.Substring(0, currentInputLine.Length - 1);
ScreenUpdate();
break;
default:
{
switch (key.Key)
{
default: {
switch (key.Key) {
case ConsoleKey.N when key.Modifiers.HasFlag(ConsoleModifiers.Control):
currentInputLine += "⏎ ";
ScreenUpdate();
@ -301,8 +275,7 @@ namespace telegram
return;
}
if (!SpecialKeys.Contains(key.Key))
{
if (!SpecialKeys.Contains(key.Key)) {
currentInputLine += key.KeyChar;
ScreenUpdate();
}
@ -312,15 +285,11 @@ namespace telegram
}
}
private static void OnAuthUpdate(Td.TdApi.Update.UpdateAuthorizationState state)
{
switch (state.AuthorizationState)
{
private static void OnAuthUpdate(Td.TdApi.Update.UpdateAuthorizationState state) {
switch (state.AuthorizationState) {
case Td.TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters _:
client.Send(new Td.TdApi.SetTdlibParameters
{
Parameters = new Td.TdApi.TdlibParameters
{
client.Send(new Td.TdApi.SetTdlibParameters {
Parameters = new Td.TdApi.TdlibParameters {
ApiId = 600606,
ApiHash = "c973f46778be4b35481ce45e93271e82",
DatabaseDirectory = dbdir,
@ -337,34 +306,22 @@ namespace telegram
case Td.TdApi.AuthorizationState.AuthorizationStateWaitEncryptionKey _:
client.Send(new Td.TdApi.CheckDatabaseEncryptionKey());
break;
case Td.TdApi.AuthorizationState.AuthorizationStateWaitPhoneNumber _:
{
case Td.TdApi.AuthorizationState.AuthorizationStateWaitPhoneNumber _: {
Console.Write("[tgcli] login> ");
var phone = Console.ReadLine();
client.Send(new Td.TdApi.SetAuthenticationPhoneNumber
{
PhoneNumber = phone
});
client.Send(new Td.TdApi.SetAuthenticationPhoneNumber {PhoneNumber = phone});
break;
}
case Td.TdApi.AuthorizationState.AuthorizationStateWaitCode _:
{
case Td.TdApi.AuthorizationState.AuthorizationStateWaitCode _: {
Console.Write("[tgcli] code> ");
var code = Console.ReadLine();
client.Send(new Td.TdApi.CheckAuthenticationCode
{
Code = code
});
client.Send(new Td.TdApi.CheckAuthenticationCode {Code = code});
break;
}
case Td.TdApi.AuthorizationState.AuthorizationStateWaitPassword _:
{
case Td.TdApi.AuthorizationState.AuthorizationStateWaitPassword _: {
Console.Write("[tgcli] 2fa password> ");
var pass = ReadConsolePassword();
client.Send(new Td.TdApi.CheckAuthenticationPassword
{
Password = pass
});
client.Send(new Td.TdApi.CheckAuthenticationPassword {Password = pass});
break;
}
case Td.TdApi.AuthorizationState.AuthorizationStateReady _:
@ -382,9 +339,10 @@ namespace telegram
ScreenUpdate();
break;
case Td.TdApi.AuthorizationState.AuthorizationStateLoggingOut _:
if (authorized) return;
Console.WriteLine(
"[tgcli] This session has been destroyed externally, to fix this delete ~/.tgcli");
if (authorized)
return;
Console.WriteLine("[tgcli] This session has been destroyed externally, to fix this delete ~/.tgcli");
Environment.Exit(1);
break;
default:
@ -394,8 +352,7 @@ namespace telegram
}
}
public static string FormatMessage(Td.TdApi.Message msg)
{
public static string FormatMessage(Td.TdApi.Message msg) {
string text;
if (msg.Content is Td.TdApi.MessageContent.MessageText messageText)
text = messageText.Text.Text;
@ -414,8 +371,7 @@ namespace telegram
var username = TruncateString(GetFormattedUsername(sender), 10);
var time = FormatTime(msg.Date);
var isChannel = msg.IsChannelPost;
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate ||
chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate || chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var isSecret = chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var isReply = msg.ReplyToMessageId != 0;
@ -423,31 +379,26 @@ namespace telegram
Td.TdApi.Message replyMessage;
var msgPrefix =
$"{Ansi.Bold}{Ansi.Green}[{time}] {(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} " +
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
var msgPrefix = $"{Ansi.Bold}{Ansi.Green}[{time}] {(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} "
+ $"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
var finalOutput = msgPrefix;
var indent = new string(' ', GetActualStringWidth(msgPrefix));
var arrows = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} ";
if (isReply)
{
try
{
if (isReply) {
try {
replyMessage = GetMessage(chat.Id, msg.ReplyToMessageId);
finalOutput = $"{FormatMessageReply(replyMessage, msgPrefix)}";
}
catch
{
catch {
//ignored; reply to deleted msg
}
}
var rest = $"{text}{(msg.EditDate == 0 ? "" : $"{Ansi.Yellow}*")}";
var lines = rest.Split("\n").ToList();
if (!isReply)
{
if (!isReply) {
finalOutput += arrows + lines.First();
lines.RemoveAt(0);
}
@ -457,8 +408,7 @@ namespace telegram
return finalOutput;
}
public static string FormatMessageReply(Td.TdApi.Message msg, string origPrefix)
{
public static string FormatMessageReply(Td.TdApi.Message msg, string origPrefix) {
string text;
if (msg.Content is Td.TdApi.MessageContent.MessageText messageText)
text = messageText.Text.Text;
@ -469,16 +419,15 @@ namespace telegram
var username = GetFormattedUsername(sender);
var time = FormatTime(msg.Date);
var isChannel = msg.IsChannelPost;
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate ||
chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate || 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} " +
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
var replyPrefix = $"{origPrefix}{Ansi.Yellow}Re: {Ansi.Bold}{Ansi.Green}[{time}] "
+ $"{(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} "
+ $"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
var indent = new string(' ', GetActualStringWidth(replyPrefix));
var arrows = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} ";
@ -495,8 +444,7 @@ namespace telegram
return finalOutput;
}
private static string FormatMessage(Td.TdApi.Update.UpdateMessageContent msg)
{
private static string FormatMessage(Td.TdApi.Update.UpdateMessageContent msg) {
string text;
if (msg.NewContent is Td.TdApi.MessageContent.MessageText messageText)
text = messageText.Text.Text;
@ -510,15 +458,14 @@ namespace telegram
var isChannel = message.IsChannelPost;
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate;
return $"{Ansi.Bold}{Ansi.Green}[{time}] {Ansi.Cyan}{chat.Title} " +
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}" +
$"{(message.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} " +
$"{text}" +
$"{Ansi.Yellow}*";
return $"{Ansi.Bold}{Ansi.Green}[{time}] {Ansi.Cyan}{chat.Title} "
+ $"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}"
+ $"{(message.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} "
+ $"{text}"
+ $"{Ansi.Yellow}*";
}
public static void AddMessageToQueue(Td.TdApi.Message msg)
{
public static void AddMessageToQueue(Td.TdApi.Message msg) {
//handle muted
if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId)
return;
@ -541,11 +488,9 @@ namespace telegram
ScreenUpdate();
}
public static void AddMessageToQueue(Td.TdApi.Update.UpdateMessageContent msg)
{
public static void AddMessageToQueue(Td.TdApi.Update.UpdateMessageContent msg) {
//handle muted
if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId
|| GetMessage(msg.ChatId, msg.MessageId).EditDate == 0)
if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId || GetMessage(msg.ChatId, msg.MessageId).EditDate == 0)
return;
var formattedMessage = FormatMessage(msg);