huge refactor
This commit is contained in:
parent
d52abd93e0
commit
74fefd9d7b
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NeoSmart.Unicode;
|
||||
using TdLib;
|
||||
using static telegram.tgcli;
|
||||
using static telegram.Util;
|
||||
|
@ -10,26 +9,26 @@ namespace telegram
|
|||
{
|
||||
public abstract class Command
|
||||
{
|
||||
public string Trigger;
|
||||
public string Shortcut;
|
||||
public string Description;
|
||||
public string Syntax;
|
||||
public int ParamCount;
|
||||
public string trigger;
|
||||
public string shortcut;
|
||||
public string description;
|
||||
public string syntax;
|
||||
public int paramCount;
|
||||
public abstract void Handler(List<string> inputParams);
|
||||
|
||||
protected Command(string trigger, string shortcut, string description, string syntax, int paramCount)
|
||||
{
|
||||
Trigger = trigger;
|
||||
Shortcut = shortcut;
|
||||
Description = description;
|
||||
ParamCount = paramCount;
|
||||
Syntax = syntax;
|
||||
this.trigger = trigger;
|
||||
this.shortcut = shortcut;
|
||||
this.description = description;
|
||||
this.paramCount = paramCount;
|
||||
this.syntax = syntax;
|
||||
}
|
||||
}
|
||||
|
||||
public static class CommandManager
|
||||
{
|
||||
public static List<Command> Commands = new List<Command>
|
||||
public static readonly List<Command> Commands = new List<Command>
|
||||
{
|
||||
new ClearCommand(),
|
||||
new CloseCommand(),
|
||||
|
@ -57,25 +56,27 @@ namespace telegram
|
|||
{
|
||||
var split = input.Split(" ").ToList();
|
||||
var trigger = split.First();
|
||||
var command = Commands.Find(p => p.Trigger == trigger || p.Shortcut == trigger);
|
||||
var command = Commands.Find(p => p.trigger == trigger || p.shortcut == trigger);
|
||||
if (command == null)
|
||||
{
|
||||
//TODO Console.WriteLine("do something");
|
||||
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
|
||||
{
|
||||
//TODO Console.WriteLine("do something");
|
||||
lock(@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid command syntax. Check /help for more information.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -88,8 +89,11 @@ namespace telegram
|
|||
|
||||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
if (inputParams.Count == 0)
|
||||
return; //TODO do something
|
||||
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;
|
||||
|
@ -162,7 +166,12 @@ namespace telegram
|
|||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
var chat = GetChatByUsernameGlobal(inputParams[0]);
|
||||
if (chat == null) return; //TODO do something
|
||||
if (chat == null)
|
||||
{
|
||||
lock(@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] User not found. Try /s <query> to find valid usernames.");
|
||||
return;
|
||||
}
|
||||
|
||||
currentChatId = 0;
|
||||
currentChatUserId = 0;
|
||||
|
@ -230,9 +239,7 @@ namespace telegram
|
|||
|
||||
public override void Handler(List<string> _)
|
||||
{
|
||||
if (currentChatId == 0)
|
||||
return; //TODO do something
|
||||
if (GetChat(currentChatId).Type is TdApi.ChatType.ChatTypeSecret type)
|
||||
if (currentChatId != 0 && GetChat(currentChatId).Type is TdApi.ChatType.ChatTypeSecret type)
|
||||
{
|
||||
CloseSecretChat(type.SecretChatId);
|
||||
DeleteChatHistory(currentChatId);
|
||||
|
@ -240,7 +247,8 @@ namespace telegram
|
|||
}
|
||||
else
|
||||
{
|
||||
//TODO do something
|
||||
lock(@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No secret chat selected, cannot continue.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -256,9 +264,18 @@ namespace telegram
|
|||
var userId = GetUserIdByUsername(inputParams[0]);
|
||||
|
||||
if (userId == 0)
|
||||
return; //TODO do something
|
||||
{
|
||||
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)
|
||||
return; //TODO do something
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] You already have a secret chat with the specified user.");
|
||||
return;
|
||||
}
|
||||
|
||||
var chat = CreateSecretChat(userId);
|
||||
CommandManager.HandleCommand("osd " + chat.Id);
|
||||
|
@ -276,12 +293,18 @@ namespace telegram
|
|||
var id = inputParams[0];
|
||||
if (!long.TryParse(id, out var chatId))
|
||||
{
|
||||
return; //todo do something
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid chat id.");
|
||||
return;
|
||||
}
|
||||
|
||||
var chat = GetChat(chatId);
|
||||
if (chat == null)
|
||||
return; //TODO do something
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid chat id.");
|
||||
return;
|
||||
}
|
||||
|
||||
TdApi.SecretChat secChat;
|
||||
if (chat.Type is TdApi.ChatType.ChatTypeSecret secretChat)
|
||||
|
@ -293,7 +316,9 @@ namespace telegram
|
|||
}
|
||||
else
|
||||
{
|
||||
return; //TODO do something
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] The specified chat isn't a secret chat.");
|
||||
return;
|
||||
}
|
||||
|
||||
chat.Title = TruncateString(chat.Title, 20);
|
||||
|
@ -361,11 +386,17 @@ namespace telegram
|
|||
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
|
||||
var userId = SearchUserInChats(query);
|
||||
if (userId == 0 || query.Length == 0)
|
||||
return; //TODO do something;
|
||||
{
|
||||
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)
|
||||
{
|
||||
return; //TODO do something
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No matching secret chat found.");
|
||||
return;
|
||||
}
|
||||
|
||||
TdApi.SecretChat secChat;
|
||||
|
@ -378,7 +409,9 @@ namespace telegram
|
|||
}
|
||||
else
|
||||
{
|
||||
return; //TODO do something
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No matching secret chat found. (this error should be impossible to produce)");
|
||||
return;
|
||||
}
|
||||
|
||||
chat.Title = TruncateString(chat.Title, 20);
|
||||
|
@ -443,7 +476,12 @@ namespace telegram
|
|||
|
||||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
if (currentChatId == 0) return; //TODO: do something
|
||||
if (currentChatId == 0)
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
|
||||
return;
|
||||
}
|
||||
MarkUnread(currentChatId);
|
||||
CommandManager.HandleCommand("c");
|
||||
}
|
||||
|
@ -457,7 +495,12 @@ namespace telegram
|
|||
|
||||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
if (currentChatId == 0) return; //TODO: do something
|
||||
if (currentChatId == 0)
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
|
||||
return;
|
||||
}
|
||||
|
||||
currentChatId = 0;
|
||||
currentChatUserId = 0;
|
||||
|
@ -486,7 +529,12 @@ namespace telegram
|
|||
|
||||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
if (currentChatId == 0) return; //TODO: do something
|
||||
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)
|
||||
|
@ -589,7 +637,11 @@ namespace telegram
|
|||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
if (inputParams.Count == 0)
|
||||
return; //TODO do something
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information.");
|
||||
return;
|
||||
}
|
||||
|
||||
var query = inputParams.Aggregate((current, param) => current + " " + param).Trim();
|
||||
|
||||
|
@ -619,6 +671,7 @@ namespace telegram
|
|||
|
||||
public override void Handler(List<string> inputParams)
|
||||
{
|
||||
/*
|
||||
var query = inputParams[0];
|
||||
|
||||
var chat = GetChatByUsernameGlobal(query);
|
||||
|
@ -628,7 +681,11 @@ namespace telegram
|
|||
//TODO implement when TDLib 1.6 is released
|
||||
}
|
||||
else
|
||||
return; //TODO do something
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Username does not refer to a user.");
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -665,12 +722,12 @@ namespace telegram
|
|||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Listing {CommandManager.Commands.Count} commands:");
|
||||
CommandManager.Commands.ForEach(command =>
|
||||
{
|
||||
var commandText = $"/{command.Trigger}";
|
||||
if (!string.IsNullOrWhiteSpace(command.Syntax))
|
||||
commandText += $" {command.Syntax}";
|
||||
commandText += $": {command.Description}";
|
||||
if (!string.IsNullOrWhiteSpace(command.Shortcut))
|
||||
commandText += $" ({command.Shortcut})";
|
||||
var commandText = $"/{command.trigger}";
|
||||
if (!string.IsNullOrWhiteSpace(command.syntax))
|
||||
commandText += $" {command.syntax}";
|
||||
commandText += $": {command.description}";
|
||||
if (!string.IsNullOrWhiteSpace(command.shortcut))
|
||||
commandText += $" ({command.shortcut})";
|
||||
|
||||
messageQueue.Add($"{Ansi.Yellow}{commandText}");
|
||||
});
|
||||
|
@ -710,24 +767,39 @@ namespace telegram
|
|||
|
||||
var message = inputParams.Aggregate((current, param) => current + " " + param).Trim();
|
||||
|
||||
if (currentChatId == 0) return; //TODO do something
|
||||
if (currentChatId == 0)
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
|
||||
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; //TODO do something
|
||||
if (last == null)
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No message to edit found, cannot continue.");
|
||||
return;
|
||||
}
|
||||
lastMessage = last;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No message specified, cannot continue.");
|
||||
return;
|
||||
}
|
||||
EditMessage(message, lastMessage);
|
||||
}
|
||||
catch
|
||||
{
|
||||
//TODO do something
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error editing message.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -744,7 +816,9 @@ namespace telegram
|
|||
{
|
||||
if (inputParams.Count < 2)
|
||||
{
|
||||
return; //TODO do something
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Invalid syntax, check /help for more information.");
|
||||
return;
|
||||
}
|
||||
|
||||
var history = GetHistory(currentChatId, 50);
|
||||
|
@ -755,18 +829,26 @@ namespace telegram
|
|||
|
||||
if (!parsed || string.IsNullOrWhiteSpace(message) || history.Count < offset)
|
||||
{
|
||||
return; //TODO do something
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}Invalid syntax, check /help for more information.");
|
||||
return;
|
||||
}
|
||||
|
||||
var replyMessage = history[offset - 1];
|
||||
|
||||
if (currentChatId == 0) return; //TODO do something
|
||||
if (currentChatId == 0)
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No open chat, cannot continue.");
|
||||
return;
|
||||
}
|
||||
|
||||
SendMessage(message, currentChatId, replyMessage.Id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
//TODO do something
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Unknown error sending message.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -269,7 +269,6 @@ namespace telegram
|
|||
public static void ClearCurrentConsoleLine()
|
||||
{
|
||||
Console.Write("\u001b[2K\r");
|
||||
//TODO is there a better solution?
|
||||
|
||||
//Console.SetCursorPosition(0, Console.WindowHeight);
|
||||
//Console.Write(new string(' ', Console.WindowWidth));
|
||||
|
|
|
@ -14,7 +14,6 @@ namespace telegram
|
|||
{
|
||||
/*
|
||||
* TODO:
|
||||
* waaay more error messages instead of just doing nothing or crashing (search for TODOs and "do something")
|
||||
* mute,unmute chats
|
||||
* photo & document download & show externally
|
||||
* publish AUR package
|
||||
|
@ -22,6 +21,8 @@ namespace telegram
|
|||
* ref: http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#cursor-navigation
|
||||
* ref: https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
|
||||
* refactor everything
|
||||
* re-evaluate ClearCurrentConsoleLine function
|
||||
* When TDLib 1.6 is released: implement contacts
|
||||
*/
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
|
@ -175,7 +176,6 @@ namespace telegram
|
|||
var chat = update.SecretChat;
|
||||
switch (chat.State)
|
||||
{
|
||||
//TODO: send notifs here!
|
||||
case Td.TdApi.SecretChatState.SecretChatStateClosed _:
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat with {chat.Id} was closed.");
|
||||
|
|
Loading…
Reference in a new issue