using System; using System.Collections.Generic; using System.Linq; using TdLib; using static telegram.tgcli; using static telegram.Util; namespace telegram { public class Command { public static void ParseCommand(string command) { while (true) { var split = command.Split(" "); if (split[0].Equals("open") || split[0].Equals("o")) { if (split.Length < 2) return; var query = command.Substring(split[0].Length); var chatId = searchChatId(query); if (chatId == 0) return; currentChatId = 0; currentChatUserId = 0; currentUserOnline = false; currentUserTyping = false; currentUserRead = false; ctsTyping?.Cancel(); var chat = getChat(chatId); if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat) { currentChatUserId = privChat.UserId; } currentChatId = chat.Id; prefix = $"[{chat.Title}"; 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.")}"); while (getHistory(chatId, 50).Count == 1) { getHistory(chatId, 10); } 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."); } 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 { getHistory(chatId).ForEach(AddMessageToQueue); } } markRead(chat.Id, getHistory(chat.Id).First().Id); var history = getHistory(currentChatId, 50); var last = history.LastOrDefault(p => p.IsOutgoing); if (last == null) { currentUserRead = true; return; } lastMessage = last; currentUserRead = isMessageRead(last.ChatId, last.Id); } else if (split[0].Equals("cu") || split[0].Equals("closeunread")) { if (currentChatId == 0) return; markUnread(currentChatId); command = "close"; continue; } else if (split[0].Equals("close") || split[0].Equals("c")) { if (split.Length != 1 || currentChatId == 0) return; currentChatId = 0; currentChatUserId = 0; currentUserOnline = false; currentUserTyping = false; currentUserRead = false; ctsTyping?.Cancel(); lastMessage = null; prefix = "[tgcli"; 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.")}"); messageQueue.AddRange(missedMessages); missedMessages.Clear(); } } else if (split[0].Equals("history") || split[0].Equals("h")) { if (split.Length != 1 && split.Length != 2 || currentChatId == 0) { lock (_lock) messageQueue.Add($"{Ansi.Red}[tgcli] " + "No chat selected. Select a chat with /open "); return; } var history = split.Length > 1 && int.TryParse(split[1], out var limit) ? getHistory(currentChatId, limit) : getHistory(currentChatId); lock (_lock) { messageQueue.Add($"{Ansi.Yellow}[tgcli] Last {history.Count} messages in " + $"{getChat(currentChatId).Title}"); } foreach (var msg in history) { AddMessageToQueue(msg); } } else if (split[0].Equals("clear") || split[0].Equals("cl")) { lock (_lock) { Console.Clear(); } } else if (split[0].Equals("quit") || split[0].Equals("q")) { quitting = true; } else if (split[0].Equals("unreads") || split[0].Equals("u")) { var unreads = getUnreadChats(split.Length == 2 && split[1].Equals("all")); lock (_lock) { messageQueue.Add($"{Ansi.Yellow}[tgcli] You have {unreads.Count} unread chats."); unreads.ForEach(chat => { string line; if (chat.UnreadCount == 0) line = $"{Ansi.Bold}{Ansi.Yellow}[M] {chat.Title}"; else line = $"{Ansi.Bold}{Ansi.Green}[{chat.UnreadCount}] {chat.Title}"; messageQueue.Add(line); }); } } else if (split[0].Equals("edit") || split[0].Equals("e")) { if (currentChatId == 0) return; if (split.Length == 1) { currentInputLine = "/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text; return; } if (lastMessage == null) { //try to find last message var history = getHistory(currentChatId, 50); var last = history.LastOrDefault(p => p.IsOutgoing); if (last == null) return; lastMessage = last; } var message = command.Substring(split[0].Length); editMessage(message, lastMessage); } break; } } } }