diff --git a/telegram/Command.cs b/telegram/Command.cs index 1b68a1d..16a20dd 100644 --- a/telegram/Command.cs +++ b/telegram/Command.cs @@ -29,6 +29,8 @@ namespace telegram { new CloseCommand(), new EditCommand(), new ReplyCommand(), + new ReplyOffsetCommand(), + new ReplyDirectCommand(), new HistoryCommand(), new OpenCommand(), new UnreadsCommand(), @@ -669,7 +671,48 @@ namespace telegram { } public class ReplyCommand : Command { - public ReplyCommand() : base("r", "", "replies to message", " ", -1) { } + public ReplyCommand() : base("r", "", "matches message in history to reply to", "", -1) { } + + public override void Handler(List inputParams) { + try { + if (inputParams.Count == 0) { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information."); + return; + } + + if (currentChatId == 0) { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue."); + return; + } + + var history = GetHistory(currentChatId, 50); + history.Reverse(); + var query = inputParams.Aggregate((current, param) => current + " " + param).Trim(); + + var result = history.Where(m => m.Content is TdApi.MessageContent.MessageText) + .FirstOrDefault(m => ((TdApi.MessageContent.MessageText) m.Content) + .Text.Text.ToLowerInvariant() + .Contains(query.ToLower())); + + if (result == null) { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] No match found."); + return; + } + + currentInputLine = $"/rd {result.Id} "; + } + catch { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error searching for message."); + } + } + } + + public class ReplyOffsetCommand : Command { + public ReplyOffsetCommand() : base("ro", "", "replies to message", " ", -1) { } public override void Handler(List inputParams) { try { @@ -679,6 +722,12 @@ namespace telegram { return; } + if (currentChatId == 0) { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue."); + return; + } + var history = GetHistory(currentChatId, 50); var parsed = int.TryParse(inputParams[0], out var offset); inputParams.RemoveAt(0); @@ -693,13 +742,43 @@ namespace telegram { var replyMessage = history[offset - 1]; + SendMessage(message, currentChatId, replyMessage.Id); + } + catch { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error sending message."); + } + } + } + + public class ReplyDirectCommand : Command { + public ReplyDirectCommand() : base("rd", "", "replies to message by id", " ", -1) { } + + public override void Handler(List inputParams) { + try { + if (inputParams.Count < 2) { + lock (@lock) + messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information."); + return; + } + if (currentChatId == 0) { lock (@lock) messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue."); return; } - SendMessage(message, currentChatId, replyMessage.Id); + var parsed = long.TryParse(inputParams[0], out var replyId); + inputParams.RemoveAt(0); + var message = inputParams.Aggregate((current, param) => current + " " + param).Trim(); + + if (!parsed || string.IsNullOrWhiteSpace(message)) { + lock (@lock) + messageQueue.Add($"{Ansi.Red}Invalid syntax, check /help for more information."); + return; + } + + SendMessage(message, currentChatId, replyId); } catch { lock (@lock) diff --git a/telegram/tgcli.cs b/telegram/tgcli.cs index 3ed8d5b..6122662 100644 --- a/telegram/tgcli.cs +++ b/telegram/tgcli.cs @@ -13,11 +13,9 @@ using static telegram.CommandManager; namespace telegram { /* * TODO: - * fuzzy matching for replies? * unreads are unreliable in secret chats! * mute,unmute chats * photo & document download & show externally - * publish AUR package * cursor input nav (up/down history, (alt +) left/right) * ref: http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#cursor-navigation * ref: https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences