tgcli/tgcli.core/Util.cs

178 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TdLib;
namespace tgcli.core
{
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";
public const string Yellow = "\x1b[33m";
public const string Blue = "\x1b[34m";
public const string Magenta = "\x1b[35m";
public const string Cyan = "\x1b[36m";
public const string Bold = "\x1b[1m";
public const string BoldOff = "\x1b[22m";
}
public static TdApi.User getUser(int uid)
{
try
{
var uinfo = Program.client.ExecuteAsync(new TdApi.GetUser
{
UserId = uid
}).Result;
return uinfo;
}
catch
{
var user = new TdApi.User();
user.FirstName = "null";
user.LastName = "null";
return user;
}
}
public static TdApi.Chat getChat(long chatId)
{
return Program.client.ExecuteAsync(new TdApi.GetChat
{
ChatId = chatId
}).Result;
}
public static TdApi.User getMe()
{
return Program.client.ExecuteAsync(new TdApi.GetMe()).Result;
}
public static List<TdApi.Message> getHistory(long chatId, int limit = 5 ,long fromMessageId = 0)
{
var history = new List<TdApi.Message>();
if (limit <= 0)
{
lock(Program._lock)
Program.messageQueue.Add($"{Ansi.Bold}{Ansi.Red}[tgcli] " +
$"Limit cannot be less than one. Usage: /history <count>");
return history;
}
var response = Program.client.ExecuteAsync(new TdApi.GetChatHistory
{
ChatId = chatId,
FromMessageId = fromMessageId,
Limit = limit,
}).Result;
history.AddRange(response.Messages_);
return history;
}
public static void ClearCurrentConsoleLine()
{
Console.SetCursorPosition(0, Console.WindowHeight);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.WindowHeight);
}
public static string ReadConsolePassword()
{
string pass = "";
do
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
pass += key.KeyChar;
Console.Write("*");
}
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)
{
break;
}
}
} while (true);
Console.WriteLine();
return pass;
}
public static void sendMessage(string message, long chatId)
{
Program.client.ExecuteAsync(new TdApi.SendMessage
{
ChatId = chatId,
InputMessageContent = new TdApi.InputMessageContent.InputMessageText
{
Text = new TdApi.FormattedText()
{
Text = message
}
}
});
}
public static void markRead(long chatId, long messageId)
{
Program.client.ExecuteAsync(new TdApi.ViewMessages
{
ChatId = chatId,
MessageIds = new []
{
messageId
},
ForceRead = true
});
}
public static long searchChatId(string query)
{
try
{
var results = Program.client.ExecuteAsync(new TdApi.SearchChatsOnServer
{
Query = query,
Limit = 5
}).Result;
return results.ChatIds.First();
}
catch
{
lock (Program._lock)
Program.messageQueue.Add($"{Ansi.Red}{Ansi.Bold}[tgcli] No results found.");
return 0;
}
}
public static object getFormattedUsername(TdApi.User sender)
{
var username = sender.Username;
if (string.IsNullOrWhiteSpace(username))
username = sender.FirstName + " " +
sender.LastName;
else
username = "@" + username;
return username;
}
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");
}
}
}