tgcli/telegram/Command.cs

865 lines
30 KiB
C#
Raw Normal View History

2019-12-10 09:07:46 +01:00
using System;
2019-12-13 11:18:08 +01:00
using System.Collections.Generic;
2019-12-10 09:07:46 +01:00
using System.Linq;
using TdLib;
using static telegram.tgcli;
using static telegram.Util;
namespace telegram
{
2019-12-13 11:18:08 +01:00
public abstract class Command
2019-12-10 09:07:46 +01:00
{
2019-12-14 16:44:52 +01:00
public string trigger;
public string shortcut;
public string description;
public string syntax;
public int paramCount;
2019-12-13 11:18:08 +01:00
public abstract void Handler(List<string> inputParams);
2019-12-13 18:10:08 +01:00
protected Command(string trigger, string shortcut, string description, string syntax, int paramCount)
2019-12-10 09:07:46 +01:00
{
2019-12-14 16:44:52 +01:00
this.trigger = trigger;
this.shortcut = shortcut;
this.description = description;
this.paramCount = paramCount;
this.syntax = syntax;
2019-12-13 11:18:08 +01:00
}
}
public static class CommandManager
{
2019-12-14 16:44:52 +01:00
public static readonly List<Command> Commands = new List<Command>
2019-12-13 11:18:08 +01:00
{
new ClearCommand(),
new CloseCommand(),
new EditCommand(),
2019-12-13 19:42:56 +01:00
new ReplyCommand(),
2019-12-13 11:18:08 +01:00
new HistoryCommand(),
new OpenCommand(),
new UnreadsCommand(),
new CloseUnreadCommand(),
2019-12-14 01:20:37 +01:00
new ListChatsCommand(),
2019-12-14 16:11:35 +01:00
new NewChatCommand(),
2019-12-13 13:11:06 +01:00
new ListSecretChatsCommand(),
new OpenSecretCommand(),
2019-12-13 20:18:49 +01:00
new OpenSecretDirectCommand(),
2019-12-13 13:11:06 +01:00
new NewSecretChatCommand(),
new CloseSecretChatCommand(),
2019-12-14 01:20:37 +01:00
new SearchUserCommand(),
2019-12-14 16:11:35 +01:00
//new AddContactCommand(),
2019-12-13 13:11:06 +01:00
new QuitCommand(),
2019-12-13 21:27:26 +01:00
new HelpCommand(),
new LogoutCommand(),
2019-12-13 11:18:08 +01:00
};
public static void HandleCommand(string input)
{
var split = input.Split(" ").ToList();
var trigger = split.First();
2019-12-14 16:44:52 +01:00
var command = Commands.Find(p => p.trigger == trigger || p.shortcut == trigger);
2019-12-13 11:18:08 +01:00
if (command == null)
2019-12-10 09:07:46 +01:00
{
2019-12-14 16:44:52 +01:00
lock(@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid command. Check /help for all available commands.");
2019-12-13 11:18:08 +01:00
return;
}
2019-12-10 09:07:46 +01:00
2019-12-13 11:18:08 +01:00
split.RemoveAt(0);
2019-12-14 16:44:52 +01:00
if (command.paramCount == -1)
2019-12-13 11:18:08 +01:00
{
command.Handler(split);
}
2019-12-14 16:44:52 +01:00
else if (split.Count == command.paramCount)
2019-12-13 11:18:08 +01:00
{
command.Handler(split);
}
else
{
2019-12-14 16:44:52 +01:00
lock(@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid command syntax. Check /help for more information.");
2019-12-13 11:18:08 +01:00
}
}
}
2019-12-10 09:49:58 +01:00
2019-12-13 11:18:08 +01:00
public class OpenCommand : Command
{
2019-12-13 18:34:56 +01:00
public OpenCommand() : base("o", "^O", "opens a chat. queries chat list", "<query>", -1)
2019-12-13 11:18:08 +01:00
{
}
2019-12-10 09:07:46 +01:00
2019-12-13 11:18:08 +01:00
public override void Handler(List<string> inputParams)
{
2019-12-14 16:44:52 +01:00
if (inputParams.Count == 0){
lock(@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid command syntax. Check /help for more information.");
return;
}
2019-12-13 11:18:08 +01:00
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
var chatId = SearchChatId(query);
if (chatId == 0) return;
2019-12-10 09:49:58 +01:00
2019-12-13 11:18:08 +01:00
currentChatId = 0;
currentChatUserId = 0;
currentUserRead = false;
2019-12-10 09:49:58 +01:00
2019-12-13 11:18:08 +01:00
var chat = GetChat(chatId);
if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat)
{
currentChatUserId = privChat.UserId;
}
2019-12-10 09:49:58 +01:00
2019-12-13 11:18:08 +01:00
currentChatId = chat.Id;
2019-12-13 18:10:08 +01:00
chat.Title = TruncateString(chat.Title, 20);
2019-12-13 11:18:08 +01:00
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.")}");
2019-12-13 20:18:49 +01:00
2019-12-13 11:18:08 +01:00
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.");
2019-12-10 09:07:46 +01:00
}
2019-12-13 11:18:08 +01:00
else if (chat.UnreadCount > 0)
2019-12-10 09:07:46 +01:00
{
2019-12-13 11:18:08 +01:00
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);
2019-12-10 09:07:46 +01:00
}
2019-12-13 11:18:08 +01:00
else
2019-12-10 09:07:46 +01:00
{
2019-12-13 11:18:08 +01:00
GetHistory(chatId).ForEach(AddMessageToQueue);
}
}
2019-12-10 09:49:58 +01:00
2019-12-13 11:18:08 +01:00
var history = GetHistory(currentChatId, 50);
if (history.Count != 0)
MarkRead(chat.Id, history.First().Id);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
currentUserRead = true;
return;
}
2019-12-10 09:49:58 +01:00
2019-12-13 11:18:08 +01:00
lastMessage = last;
currentUserRead = IsMessageRead(last.ChatId, last.Id);
}
}
2019-12-10 09:07:46 +01:00
2019-12-14 16:11:35 +01:00
public class NewChatCommand : Command
{
public NewChatCommand() : base("n", "", "starts a new chat.", "<username>", 1)
{
}
public override void Handler(List<string> inputParams)
{
var chat = GetChatByUsernameGlobal(inputParams[0]);
2019-12-14 16:44:52 +01:00
if (chat == null)
{
lock(@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] User not found. Try /s <query> to find valid usernames.");
return;
}
2019-12-14 16:11:35 +01:00
currentChatId = 0;
currentChatUserId = 0;
currentUserRead = false;
if (chat.Type is TdApi.ChatType.ChatTypePrivate privChat)
{
currentChatUserId = privChat.UserId;
}
currentChatId = chat.Id;
chat.Title = TruncateString(chat.Title, 20);
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.")}");
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.");
}
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
{
GetHistory(chat.Id).ForEach(AddMessageToQueue);
}
}
var history = GetHistory(currentChatId, 50);
if (history.Count != 0)
MarkRead(chat.Id, history.First().Id);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
currentUserRead = true;
return;
}
lastMessage = last;
currentUserRead = IsMessageRead(last.ChatId, last.Id);
}
}
2019-12-13 13:11:06 +01:00
public class CloseSecretChatCommand : Command
{
2019-12-13 18:10:08 +01:00
public CloseSecretChatCommand() : base("cs", "", "closes a secret chat (permanently)", "", 0)
2019-12-13 13:11:06 +01:00
{
}
public override void Handler(List<string> _)
{
2019-12-14 16:44:52 +01:00
if (currentChatId != 0 && GetChat(currentChatId).Type is TdApi.ChatType.ChatTypeSecret type)
2019-12-13 13:11:06 +01:00
{
CloseSecretChat(type.SecretChatId);
DeleteChatHistory(currentChatId);
CommandManager.HandleCommand("c");
}
else
{
2019-12-14 16:44:52 +01:00
lock(@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No secret chat selected, cannot continue.");
2019-12-13 13:11:06 +01:00
}
}
}
2019-12-13 20:18:49 +01:00
2019-12-13 13:11:06 +01:00
public class NewSecretChatCommand : Command
{
2019-12-14 16:11:35 +01:00
public NewSecretChatCommand() : base("ns", "", "creates a new secret chat.", "<username>", 1)
2019-12-13 13:11:06 +01:00
{
}
public override void Handler(List<string> inputParams)
{
2019-12-14 16:11:35 +01:00
var userId = GetUserIdByUsername(inputParams[0]);
2019-12-13 20:18:49 +01:00
2019-12-13 13:11:06 +01:00
if (userId == 0)
2019-12-14 16:44:52 +01:00
{
lock(@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] User not found. Try /s <query> to find valid usernames.");
return;
}
2019-12-13 20:18:49 +01:00
if (GetSecretChats().Count(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId) > 0)
2019-12-14 16:44:52 +01:00
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] You already have a secret chat with the specified user.");
return;
}
2019-12-13 13:11:06 +01:00
var chat = CreateSecretChat(userId);
2019-12-13 20:18:49 +01:00
CommandManager.HandleCommand("osd " + chat.Id);
2019-12-13 13:11:06 +01:00
}
}
2019-12-13 20:18:49 +01:00
public class OpenSecretDirectCommand : Command
2019-12-13 13:11:06 +01:00
{
2019-12-13 20:18:49 +01:00
public OpenSecretDirectCommand() : base("osd", "", "opens a secret chat by chat id", "<chat_id>", 1)
2019-12-13 13:11:06 +01:00
{
}
public override void Handler(List<string> inputParams)
{
var id = inputParams[0];
if (!long.TryParse(id, out var chatId))
{
2019-12-14 16:44:52 +01:00
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid chat id.");
return;
2019-12-13 13:11:06 +01:00
}
2019-12-13 20:18:49 +01:00
2019-12-13 13:11:06 +01:00
var chat = GetChat(chatId);
if (chat == null)
2019-12-14 16:44:52 +01:00
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid chat id.");
return;
}
2019-12-13 13:11:06 +01:00
TdApi.SecretChat secChat;
if (chat.Type is TdApi.ChatType.ChatTypeSecret secretChat)
{
currentChatUserId = secretChat.UserId;
currentChatId = chat.Id;
currentUserRead = false;
secChat = GetSecretChat(secretChat.SecretChatId);
}
else
{
2019-12-14 16:44:52 +01:00
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] The specified chat isn't a secret chat.");
return;
2019-12-13 13:11:06 +01:00
}
2019-12-13 20:18:49 +01:00
2019-12-13 18:10:08 +01:00
chat.Title = TruncateString(chat.Title, 20);
2019-12-13 13:11:06 +01:00
2019-12-13 20:18:49 +01:00
prefix = $"[{Ansi.Red}sec {Ansi.ResetAll}{chat.Title}]";
2019-12-13 13:11:06 +01:00
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.Red}[tgcli] Secret chat has ended. No messages can be sent.");
}
else if (secChat.State is TdApi.SecretChatState.SecretChatStatePending)
{
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat is pending. No messages can be sent.");
}
2019-12-13 20:18:49 +01:00
2019-12-13 13:11:06 +01:00
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.");
}
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
{
GetHistory(chatId, isSecret: true).ForEach(AddMessageToQueue);
}
}
var history = GetHistory(currentChatId, 50, isSecret: true);
if (history.Count != 0)
MarkRead(chat.Id, history.First().Id);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
currentUserRead = true;
return;
}
lastMessage = last;
currentUserRead = IsMessageRead(last.ChatId, last.Id);
}
}
2019-12-13 20:18:49 +01:00
public class OpenSecretCommand : Command
{
2019-12-14 16:11:35 +01:00
public OpenSecretCommand() : base("os", "", "opens a secret chat. queries chat list.", "<query>", -1)
2019-12-13 20:18:49 +01:00
{
}
public override void Handler(List<string> inputParams)
{
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
2019-12-14 16:11:35 +01:00
var userId = SearchUserInChats(query);
2019-12-13 20:18:49 +01:00
if (userId == 0 || query.Length == 0)
2019-12-14 16:44:52 +01:00
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No matching chat found.");
return;
}
2019-12-13 20:18:49 +01:00
var chat = GetSecretChats().Find(p => ((TdApi.ChatType.ChatTypeSecret) p.Type).UserId == userId);
if (chat == null)
{
2019-12-14 16:44:52 +01:00
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No matching secret chat found.");
return;
2019-12-13 20:18:49 +01:00
}
TdApi.SecretChat secChat;
if (chat.Type is TdApi.ChatType.ChatTypeSecret secretChat)
{
currentChatUserId = secretChat.UserId;
currentChatId = chat.Id;
currentUserRead = false;
secChat = GetSecretChat(secretChat.SecretChatId);
}
else
{
2019-12-14 16:44:52 +01:00
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No matching secret chat found. (this error should be impossible to produce)");
return;
2019-12-13 20:18:49 +01:00
}
chat.Title = TruncateString(chat.Title, 20);
prefix = $"[{Ansi.Red}sec {Ansi.ResetAll}{chat.Title}]";
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.Red}[tgcli] Secret chat has ended. No messages can be sent.");
}
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)
{
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.");
}
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
{
GetHistory(chat.Id, isSecret: true).ForEach(AddMessageToQueue);
}
}
var history = GetHistory(currentChatId, 50, isSecret: true);
if (history.Count != 0)
MarkRead(chat.Id, history.First().Id);
var last = history.LastOrDefault(p => p.IsOutgoing);
if (last == null)
{
currentUserRead = true;
return;
}
lastMessage = last;
currentUserRead = IsMessageRead(last.ChatId, last.Id);
}
}
2019-12-13 11:18:08 +01:00
public class CloseUnreadCommand : Command
{
2019-12-13 18:10:08 +01:00
public CloseUnreadCommand() : base("cu", "", "closes a chat, marking it as unread", "", 0)
2019-12-13 11:18:08 +01:00
{
}
2019-12-10 09:07:46 +01:00
2019-12-13 11:18:08 +01:00
public override void Handler(List<string> inputParams)
{
2019-12-14 16:44:52 +01:00
if (currentChatId == 0)
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
2019-12-13 11:18:08 +01:00
MarkUnread(currentChatId);
2019-12-13 17:16:55 +01:00
CommandManager.HandleCommand("c");
2019-12-13 11:18:08 +01:00
}
}
2019-12-10 09:07:46 +01:00
2019-12-13 11:18:08 +01:00
public class CloseCommand : Command
{
2019-12-13 18:10:08 +01:00
public CloseCommand() : base("c", "^E", "closes a chat", "", 0)
2019-12-13 11:18:08 +01:00
{
}
2019-12-10 09:49:58 +01:00
2019-12-13 11:18:08 +01:00
public override void Handler(List<string> inputParams)
{
2019-12-14 16:44:52 +01:00
if (currentChatId == 0)
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
2019-12-13 11:18:08 +01:00
currentChatId = 0;
currentChatUserId = 0;
currentUserRead = false;
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();
}
}
}
public class HistoryCommand : Command
{
2019-12-13 18:34:56 +01:00
public HistoryCommand() : base("h", "", "shows chat history. default limit is 5", "[1-50]", -1)
2019-12-13 11:18:08 +01:00
{
}
public override void Handler(List<string> inputParams)
{
2019-12-14 16:44:52 +01:00
if (currentChatId == 0)
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
2019-12-13 11:18:08 +01:00
var history = inputParams.Count == 1 && int.TryParse(inputParams[0], 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);
}
}
}
public class ClearCommand : Command
{
2019-12-13 18:10:08 +01:00
public ClearCommand() : base("cl", "^L", "clears console", "", 0)
2019-12-13 11:18:08 +01:00
{
}
public override void Handler(List<string> inputParams)
{
lock (@lock)
{
Console.Clear();
}
}
}
public class UnreadsCommand : Command
{
2019-12-13 18:34:56 +01:00
public UnreadsCommand() : base("u", "^U", "displays unread chat", "[all]", -1)
2019-12-13 11:18:08 +01:00
{
}
public override void Handler(List<string> inputParams)
{
var unreads = GetUnreadChats(inputParams.Count == 1 && inputParams[0].Equals("all"));
lock (@lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] You have {unreads.Count} unread chats.");
unreads.ForEach(chat =>
2019-12-10 09:07:46 +01:00
{
2019-12-13 11:18:08 +01:00
string line;
if (chat.UnreadCount == 0)
line = $"{Ansi.Bold}{Ansi.Yellow}[M] {chat.Title}";
2019-12-13 13:11:06 +01:00
else if (chat.Type is TdApi.ChatType.ChatTypeSecret)
line = $"{Ansi.Bold}{Ansi.Red}[{chat.UnreadCount}] [sec] {chat.Title}";
2019-12-13 11:18:08 +01:00
else
line = $"{Ansi.Bold}{Ansi.Green}[{chat.UnreadCount}] {chat.Title}";
messageQueue.Add(line);
});
}
}
}
2019-12-13 20:18:49 +01:00
2019-12-14 01:20:37 +01:00
public class ListChatsCommand : Command
{
public ListChatsCommand() : base("lc", "", "lists all chats, optionally filtered", "[query]", -1)
{
}
public override void Handler(List<string> inputParams)
{
var chats = GetChats();
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 =>
{
string line;
if (chat.UnreadCount == 0)
line = $"{Ansi.Bold}{Ansi.Blue}[0] {chat.Title}";
else
line = $"{Ansi.Bold}{Ansi.Green}[{chat.UnreadCount}] {chat.Title}";
messageQueue.Add(line);
});
}
}
}
public class SearchUserCommand : Command
{
2019-12-14 16:11:35 +01:00
public SearchUserCommand() : base("s", "", "searches for users globally", "<query>", -1)
2019-12-14 01:20:37 +01:00
{
}
public override void Handler(List<string> inputParams)
{
if (inputParams.Count == 0)
2019-12-14 16:44:52 +01:00
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information.");
return;
}
2019-12-14 01:20:37 +01:00
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
var chats = SearchChatsGlobal(query);
chats = chats.FindAll(p => p.Type is TdApi.ChatType.ChatTypePrivate);
lock (@lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] Listing {chats.Count} chats.");
chats.ForEach(chat =>
{
string line;
var type = (TdApi.ChatType.ChatTypePrivate) chat.Type;
var user = GetUser(type.UserId);
line = $"{Ansi.Bold}{Ansi.Yellow}@{user.Username} {Ansi.Magenta}{chat.Title}";
messageQueue.Add(line);
});
}
}
}
public class AddContactCommand : Command
{
public AddContactCommand() : base("ac", "", "adds user to contact list", "<username>", 1)
{
}
public override void Handler(List<string> inputParams)
{
2019-12-14 16:44:52 +01:00
/*
2019-12-14 01:20:37 +01:00
var query = inputParams[0];
var chat = GetChatByUsernameGlobal(query);
if (chat.Type is TdApi.ChatType.ChatTypePrivate type)
{
2019-12-14 16:11:35 +01:00
//TODO implement when TDLib 1.6 is released
2019-12-14 01:20:37 +01:00
}
else
2019-12-14 16:44:52 +01:00
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Username does not refer to a user.");
}
*/
2019-12-14 01:20:37 +01:00
}
}
2019-12-13 13:11:06 +01:00
public class ListSecretChatsCommand : Command
{
2019-12-13 18:10:08 +01:00
public ListSecretChatsCommand() : base("ls", "", "displays all open secret chats", "", 0)
2019-12-13 13:11:06 +01:00
{
}
public override void Handler(List<string> inputParams)
{
var secretChats = GetSecretChats();
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}");
});
}
}
}
2019-12-13 11:18:08 +01:00
public class HelpCommand : Command
{
2019-12-13 18:10:08 +01:00
public HelpCommand() : base("help", "", "lists all commands", "", 0)
2019-12-13 11:18:08 +01:00
{
}
2019-12-10 09:07:46 +01:00
2019-12-13 11:18:08 +01:00
public override void Handler(List<string> inputParams)
{
lock (@lock)
{
messageQueue.Add($"{Ansi.Yellow}[tgcli] Listing {CommandManager.Commands.Count} commands:");
CommandManager.Commands.ForEach(command =>
{
2019-12-14 16:44:52 +01:00
var commandText = $"/{command.trigger}";
if (!string.IsNullOrWhiteSpace(command.syntax))
commandText += $" {command.syntax}";
commandText += $": {command.description}";
if (!string.IsNullOrWhiteSpace(command.shortcut))
commandText += $" ({command.shortcut})";
2019-12-13 18:34:56 +01:00
messageQueue.Add($"{Ansi.Yellow}{commandText}");
2019-12-13 11:18:08 +01:00
});
}
}
}
2019-12-13 20:18:49 +01:00
2019-12-13 13:11:06 +01:00
public class QuitCommand : Command
{
2019-12-13 18:10:08 +01:00
public QuitCommand() : base("q", "^D", "quits the program", "", 0)
2019-12-13 13:11:06 +01:00
{
}
public override void Handler(List<string> inputParams)
{
quitting = true;
}
}
2019-12-10 09:49:58 +01:00
2019-12-13 11:18:08 +01:00
public class EditCommand : Command
{
2019-12-13 20:18:49 +01:00
public EditCommand() : base("e", "", "edits last message. param empty adds last message to inputline",
"[message]", -1)
2019-12-13 11:18:08 +01:00
{
}
2019-12-10 09:07:46 +01:00
2019-12-13 11:18:08 +01:00
public override void Handler(List<string> inputParams)
{
2019-12-13 13:11:06 +01:00
try
{
2019-12-13 17:16:55 +01:00
if (inputParams.Count == 0)
2019-12-13 13:11:06 +01:00
{
currentInputLine = "/e " + ((TdApi.MessageContent.MessageText) lastMessage?.Content)?.Text?.Text;
2019-12-13 20:59:46 +01:00
Emojis.ForEach(em => currentInputLine = currentInputLine.Replace(em.Item2, em.Item1));
2019-12-13 13:11:06 +01:00
return;
}
2019-12-13 17:16:55 +01:00
var message = inputParams.Aggregate((current, param) => current + " " + param).Trim();
2019-12-14 16:44:52 +01:00
if (currentChatId == 0)
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
2019-12-13 17:16:55 +01:00
2019-12-13 13:11:06 +01:00
if (lastMessage == null)
{
//try to find last message
var history = GetHistory(currentChatId, 50);
var last = history.LastOrDefault(p => p.IsOutgoing);
2019-12-14 16:44:52 +01:00
if (last == null)
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No message to edit found, cannot continue.");
return;
}
2019-12-13 13:11:06 +01:00
lastMessage = last;
}
2019-12-13 11:18:08 +01:00
2019-12-13 13:11:06 +01:00
if (string.IsNullOrWhiteSpace(message))
2019-12-14 16:44:52 +01:00
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No message specified, cannot continue.");
2019-12-13 13:11:06 +01:00
return;
2019-12-14 16:44:52 +01:00
}
2019-12-13 13:11:06 +01:00
EditMessage(message, lastMessage);
}
catch
2019-12-13 11:18:08 +01:00
{
2019-12-14 16:44:52 +01:00
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error editing message.");
2019-12-13 11:18:08 +01:00
}
2019-12-10 09:07:46 +01:00
}
}
2019-12-13 20:18:49 +01:00
2019-12-13 19:42:56 +01:00
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)
{
2019-12-14 16:44:52 +01:00
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information.");
return;
2019-12-13 19:42:56 +01:00
}
var history = GetHistory(currentChatId, 50);
var parsed = int.TryParse(inputParams[0], out var offset);
inputParams.RemoveAt(0);
history.Reverse();
var message = inputParams.Aggregate((current, param) => current + " " + param).Trim();
if (!parsed || string.IsNullOrWhiteSpace(message) || history.Count < offset)
{
2019-12-14 16:44:52 +01:00
lock (@lock)
messageQueue.Add($"{Ansi.Red}Invalid syntax, check /help for more information.");
return;
2019-12-13 19:42:56 +01:00
}
var replyMessage = history[offset - 1];
2019-12-13 20:18:49 +01:00
2019-12-14 16:44:52 +01:00
if (currentChatId == 0)
{
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
return;
}
2019-12-13 19:42:56 +01:00
SendMessage(message, currentChatId, replyMessage.Id);
}
catch
{
2019-12-14 16:44:52 +01:00
lock (@lock)
messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error sending message.");
2019-12-13 19:42:56 +01:00
}
}
}
2019-12-13 21:27:26 +01:00
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();
}
}
2019-12-10 09:07:46 +01:00
}