fix history for limit >100, improve newline input, redo codestyle
This commit is contained in:
parent
132334bedd
commit
93eda081db
1559
telegram/Command.cs
1559
telegram/Command.cs
File diff suppressed because it is too large
Load diff
938
telegram/Util.cs
938
telegram/Util.cs
|
@ -6,604 +6,448 @@ using NeoSmart.Unicode;
|
|||
using static TdLib.TdApi;
|
||||
using static telegram.tgcli;
|
||||
|
||||
namespace telegram
|
||||
{
|
||||
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";
|
||||
}
|
||||
namespace telegram {
|
||||
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 User GetUser(int uid)
|
||||
{
|
||||
try
|
||||
{
|
||||
var uinfo = client.ExecuteAsync(new GetUser
|
||||
{
|
||||
UserId = uid
|
||||
}).Result;
|
||||
return uinfo;
|
||||
}
|
||||
catch
|
||||
{
|
||||
var user = new User();
|
||||
user.FirstName = "null";
|
||||
user.LastName = "null";
|
||||
return user;
|
||||
}
|
||||
}
|
||||
public static User GetUser(int uid) {
|
||||
try {
|
||||
var uinfo = client.ExecuteAsync(new GetUser {UserId = uid}).Result;
|
||||
return uinfo;
|
||||
}
|
||||
catch {
|
||||
var user = new User();
|
||||
user.FirstName = "null";
|
||||
user.LastName = "null";
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
public static Chat GetChat(long chatId)
|
||||
{
|
||||
try
|
||||
{
|
||||
return client.ExecuteAsync(new GetChat
|
||||
{
|
||||
ChatId = chatId
|
||||
}).Result;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public static Chat GetChat(long chatId) {
|
||||
try {
|
||||
return client.ExecuteAsync(new GetChat {ChatId = chatId}).Result;
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static User GetMe()
|
||||
{
|
||||
return client.ExecuteAsync(new GetMe()).Result;
|
||||
}
|
||||
public static User GetMe() {
|
||||
return client.ExecuteAsync(new GetMe()).Result;
|
||||
}
|
||||
|
||||
public static Message GetMessage(long chatId, long messageId)
|
||||
{
|
||||
return client.ExecuteAsync(new GetMessage
|
||||
{
|
||||
ChatId = chatId,
|
||||
MessageId = messageId
|
||||
}).Result;
|
||||
}
|
||||
public static Message GetMessage(long chatId, long messageId) {
|
||||
return client.ExecuteAsync(new GetMessage {ChatId = chatId, MessageId = messageId}).Result;
|
||||
}
|
||||
|
||||
public static int GetTotalMessages(long chatId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = client.ExecuteAsync(new SearchChatMessages
|
||||
{
|
||||
ChatId = chatId,
|
||||
Query = "+",
|
||||
Limit = 1
|
||||
});
|
||||
return response.Result.TotalCount;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 9999;
|
||||
}
|
||||
}
|
||||
public static int GetTotalMessages(long chatId) {
|
||||
try {
|
||||
var response = client.ExecuteAsync(new SearchChatMessages {ChatId = chatId, Query = "+", Limit = 1});
|
||||
return response.Result.TotalCount;
|
||||
}
|
||||
catch {
|
||||
return 9999;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Message> GetHistory(long chatId, int limit = 5, long fromMessageId = 0, int offset = 0,
|
||||
bool isSecret = false, bool skipTotal = false)
|
||||
{
|
||||
var history = new List<Message>();
|
||||
var total = GetTotalMessages(chatId);
|
||||
var chat = GetChat(chatId);
|
||||
if (chat.Type is ChatType.ChatTypeSupergroup || isSecret)
|
||||
skipTotal = true;
|
||||
if (limit > total && !skipTotal) limit = total;
|
||||
public static List<Message> GetHistory(long chatId, int limit = 5, long fromMessageId = 0, int offset = 0, bool isSecret = false,
|
||||
bool skipTotal = false) {
|
||||
var history = new List<Message>();
|
||||
var total = GetTotalMessages(chatId);
|
||||
var chat = GetChat(chatId);
|
||||
if (chat.Type is ChatType.ChatTypeSupergroup || isSecret)
|
||||
skipTotal = true;
|
||||
if (limit > total && !skipTotal)
|
||||
limit = total;
|
||||
|
||||
for (var i = 5; i > 0; i--)
|
||||
{
|
||||
if (limit <= 0)
|
||||
{
|
||||
if (total == 0) return history;
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] " +
|
||||
$"Limit cannot be less than one. Usage: /history <count>");
|
||||
return history;
|
||||
}
|
||||
for (var i = 5; i > 0; i--) {
|
||||
if (limit <= 0) {
|
||||
if (total == 0)
|
||||
return history;
|
||||
|
||||
var response = client.ExecuteAsync(new GetChatHistory
|
||||
{
|
||||
ChatId = chatId,
|
||||
FromMessageId = fromMessageId,
|
||||
Limit = limit,
|
||||
Offset = offset,
|
||||
OnlyLocal = false
|
||||
})
|
||||
.Result;
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] " + $"Limit cannot be less than one. Usage: /history <count>");
|
||||
return history;
|
||||
}
|
||||
|
||||
if (response.Messages_.Length < limit && i > 1 && !isSecret)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
continue;
|
||||
}
|
||||
var response = client.ExecuteAsync(new GetChatHistory {
|
||||
ChatId = chatId,
|
||||
FromMessageId = fromMessageId,
|
||||
Limit = limit,
|
||||
Offset = offset,
|
||||
OnlyLocal = false
|
||||
})
|
||||
.Result;
|
||||
|
||||
history.AddRange(response.Messages_);
|
||||
history.Reverse();
|
||||
return history;
|
||||
}
|
||||
if (response.Messages_.Length < limit && i > 1 && !isSecret) {
|
||||
Thread.Sleep(100);
|
||||
continue;
|
||||
}
|
||||
|
||||
return history;
|
||||
}
|
||||
history.AddRange(response.Messages_);
|
||||
history.Reverse();
|
||||
return history;
|
||||
}
|
||||
|
||||
public static List<Chat> GetUnreadChats(bool all = false)
|
||||
{
|
||||
var response = client.ExecuteAsync(new GetChats
|
||||
{
|
||||
OffsetOrder = long.MaxValue,
|
||||
Limit = int.MaxValue
|
||||
}).Result;
|
||||
return all
|
||||
? response.ChatIds.Select(GetChat).Where(c => c.UnreadCount > 0 || c.IsMarkedAsUnread).ToList()
|
||||
: response.ChatIds.Select(GetChat).Where(c => (c.UnreadCount > 0 || c.IsMarkedAsUnread)
|
||||
&& c.NotificationSettings.MuteFor == 0)
|
||||
.ToList();
|
||||
}
|
||||
return history;
|
||||
}
|
||||
|
||||
public static List<Chat> GetChats()
|
||||
{
|
||||
var response = client.ExecuteAsync(new GetChats
|
||||
{
|
||||
OffsetOrder = long.MaxValue,
|
||||
Limit = int.MaxValue
|
||||
}).Result;
|
||||
return response.ChatIds.Select(GetChat).ToList();
|
||||
}
|
||||
public static List<Chat> GetUnreadChats(bool all = false) {
|
||||
var response = client.ExecuteAsync(new GetChats {OffsetOrder = long.MaxValue, Limit = int.MaxValue}).Result;
|
||||
return all
|
||||
? response.ChatIds.Select(GetChat).Where(c => c.UnreadCount > 0 || c.IsMarkedAsUnread).ToList()
|
||||
: response.ChatIds.Select(GetChat).Where(c => (c.UnreadCount > 0 || c.IsMarkedAsUnread) && c.NotificationSettings.MuteFor == 0).ToList();
|
||||
}
|
||||
|
||||
public static List<Chat> SearchChatsGlobal(string query)
|
||||
{
|
||||
if (query.TrimStart('@').Length < 5)
|
||||
{
|
||||
return new List<Chat>();
|
||||
}
|
||||
public static List<Chat> GetChats() {
|
||||
var response = client.ExecuteAsync(new GetChats {OffsetOrder = long.MaxValue, Limit = int.MaxValue}).Result;
|
||||
return response.ChatIds.Select(GetChat).ToList();
|
||||
}
|
||||
|
||||
var response = client.ExecuteAsync(new SearchPublicChats
|
||||
{
|
||||
Query = query
|
||||
}).Result;
|
||||
public static List<Chat> SearchChatsGlobal(string query) {
|
||||
if (query.TrimStart('@').Length < 5) {
|
||||
return new List<Chat>();
|
||||
}
|
||||
|
||||
var chats = response.ChatIds.Select(GetChat).ToList();
|
||||
var response = client.ExecuteAsync(new SearchPublicChats {Query = query}).Result;
|
||||
|
||||
chats.AddRange(client.ExecuteAsync(new SearchChats
|
||||
{
|
||||
Query = query,
|
||||
Limit = int.MaxValue
|
||||
}).Result.ChatIds.Select(GetChat));
|
||||
var chats = response.ChatIds.Select(GetChat).ToList();
|
||||
|
||||
return chats;
|
||||
}
|
||||
chats.AddRange(client.ExecuteAsync(new SearchChats {Query = query, Limit = int.MaxValue}).Result.ChatIds.Select(GetChat));
|
||||
|
||||
public static Chat GetChatByUsernameGlobal(string username)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = client.ExecuteAsync(new SearchPublicChat
|
||||
{
|
||||
Username = username
|
||||
}).Result;
|
||||
return response;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return chats;
|
||||
}
|
||||
|
||||
public static int GetUserIdByUsername(string username)
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = client.ExecuteAsync(new SearchPublicChat
|
||||
{
|
||||
Username = username
|
||||
}).Result;
|
||||
public static Chat GetChatByUsernameGlobal(string username) {
|
||||
try {
|
||||
var response = client.ExecuteAsync(new SearchPublicChat {Username = username}).Result;
|
||||
return response;
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (response.Type is ChatType.ChatTypePrivate priv)
|
||||
return priv.UserId;
|
||||
return 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public static int GetUserIdByUsername(string username) {
|
||||
try {
|
||||
var response = client.ExecuteAsync(new SearchPublicChat {Username = username}).Result;
|
||||
|
||||
public static void AddUserToContacts(int userId, string name)
|
||||
{
|
||||
//TODO implement when TDLib 1.6 is released
|
||||
}
|
||||
if (response.Type is ChatType.ChatTypePrivate priv)
|
||||
return priv.UserId;
|
||||
|
||||
public static List<Chat> GetSecretChats()
|
||||
{
|
||||
var response = client.ExecuteAsync(new GetChats
|
||||
{
|
||||
OffsetOrder = long.MaxValue,
|
||||
Limit = int.MaxValue
|
||||
}).Result;
|
||||
return response.ChatIds.Select(GetChat).Where(c => c.Type is ChatType.ChatTypeSecret).ToList();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void CloseSecretChat(int secretChatId)
|
||||
{
|
||||
client.ExecuteAsync(new CloseSecretChat()
|
||||
{
|
||||
SecretChatId = secretChatId
|
||||
}).Wait();
|
||||
}
|
||||
public static void AddUserToContacts(int userId, string name) {
|
||||
//TODO implement when TDLib 1.6 is released
|
||||
}
|
||||
|
||||
public static Chat CreateSecretChat(int userId)
|
||||
{
|
||||
return client.ExecuteAsync(new CreateNewSecretChat
|
||||
{
|
||||
UserId = userId
|
||||
}).Result;
|
||||
}
|
||||
public static List<Chat> GetSecretChats() {
|
||||
var response = client.ExecuteAsync(new GetChats {OffsetOrder = long.MaxValue, Limit = int.MaxValue}).Result;
|
||||
return response.ChatIds.Select(GetChat).Where(c => c.Type is ChatType.ChatTypeSecret).ToList();
|
||||
}
|
||||
|
||||
public static void DeleteChatHistory(long chatId)
|
||||
{
|
||||
client.ExecuteAsync(new DeleteChatHistory
|
||||
{
|
||||
ChatId = chatId,
|
||||
RemoveFromChatList = true,
|
||||
Revoke = true
|
||||
}).Wait();
|
||||
}
|
||||
public static void CloseSecretChat(int secretChatId) {
|
||||
client.ExecuteAsync(new CloseSecretChat() {SecretChatId = secretChatId}).Wait();
|
||||
}
|
||||
|
||||
public static SecretChat GetSecretChat(int secretChatId)
|
||||
{
|
||||
var response = client.ExecuteAsync(new GetSecretChat
|
||||
{
|
||||
SecretChatId = secretChatId
|
||||
}).Result;
|
||||
return response;
|
||||
}
|
||||
public static Chat CreateSecretChat(int userId) {
|
||||
return client.ExecuteAsync(new CreateNewSecretChat {UserId = userId}).Result;
|
||||
}
|
||||
|
||||
public static void ClearCurrentConsoleLine()
|
||||
{
|
||||
Console.Write("\u001b[2K\r");
|
||||
public static void DeleteChatHistory(long chatId) {
|
||||
client.ExecuteAsync(new DeleteChatHistory {ChatId = chatId, RemoveFromChatList = true, Revoke = true}).Wait();
|
||||
}
|
||||
|
||||
//Console.SetCursorPosition(0, Console.WindowHeight);
|
||||
//Console.Write(new string(' ', Console.WindowWidth));
|
||||
//Console.SetCursorPosition(0, Console.WindowHeight);
|
||||
}
|
||||
public static SecretChat GetSecretChat(int secretChatId) {
|
||||
var response = client.ExecuteAsync(new GetSecretChat {SecretChatId = secretChatId}).Result;
|
||||
return response;
|
||||
}
|
||||
|
||||
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);
|
||||
public static void ClearCurrentConsoleLine() {
|
||||
Console.Write("\u001b[2K\r");
|
||||
|
||||
Console.WriteLine();
|
||||
return pass;
|
||||
}
|
||||
//Console.SetCursorPosition(0, Console.WindowHeight);
|
||||
//Console.Write(new string(' ', Console.WindowWidth));
|
||||
//Console.SetCursorPosition(0, Console.WindowHeight);
|
||||
}
|
||||
|
||||
public static void SendMessage(string message, long chatId, long replyTo = 0)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
return;
|
||||
Emojis.ForEach(em => message = message.Replace(em.Item1, em.Item2));
|
||||
client.ExecuteAsync(new SendMessage
|
||||
{
|
||||
ChatId = chatId,
|
||||
InputMessageContent = new InputMessageContent.InputMessageText
|
||||
{
|
||||
Text = new FormattedText()
|
||||
{
|
||||
Text = message
|
||||
}
|
||||
},
|
||||
ReplyToMessageId = replyTo,
|
||||
});
|
||||
currentUserRead = false;
|
||||
}
|
||||
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);
|
||||
|
||||
public static Message EditMessage(string newText, Message message)
|
||||
{
|
||||
Emojis.ForEach(em => newText = newText.Replace(em.Item1, em.Item2));
|
||||
Console.WriteLine();
|
||||
return pass;
|
||||
}
|
||||
|
||||
var msg = client.ExecuteAsync(new EditMessageText
|
||||
{
|
||||
ChatId = message.ChatId,
|
||||
MessageId = message.Id,
|
||||
InputMessageContent = new InputMessageContent.InputMessageText
|
||||
{
|
||||
Text = new FormattedText()
|
||||
{
|
||||
Text = newText
|
||||
}
|
||||
}
|
||||
}).Result;
|
||||
public static void SendMessage(string message, long chatId, long replyTo = 0) {
|
||||
if (string.IsNullOrWhiteSpace(message))
|
||||
return;
|
||||
|
||||
return msg;
|
||||
}
|
||||
Emojis.ForEach(em => message = message.Replace(em.Item1, em.Item2));
|
||||
client.ExecuteAsync(new SendMessage {
|
||||
ChatId = chatId,
|
||||
InputMessageContent = new InputMessageContent.InputMessageText {Text = new FormattedText() {Text = message}},
|
||||
ReplyToMessageId = replyTo,
|
||||
});
|
||||
currentUserRead = false;
|
||||
}
|
||||
|
||||
public static void MarkRead(long chatId, long messageId)
|
||||
{
|
||||
client.ExecuteAsync(new ViewMessages
|
||||
{
|
||||
ChatId = chatId,
|
||||
MessageIds = new[]
|
||||
{
|
||||
messageId
|
||||
},
|
||||
ForceRead = true
|
||||
});
|
||||
}
|
||||
public static Message EditMessage(string newText, Message message) {
|
||||
Emojis.ForEach(em => newText = newText.Replace(em.Item1, em.Item2));
|
||||
|
||||
public static void MarkUnread(long chatId)
|
||||
{
|
||||
client.ExecuteAsync(new ToggleChatIsMarkedAsUnread
|
||||
{
|
||||
ChatId = chatId,
|
||||
IsMarkedAsUnread = true,
|
||||
});
|
||||
}
|
||||
var msg = client.ExecuteAsync(new EditMessageText {
|
||||
ChatId = message.ChatId,
|
||||
MessageId = message.Id,
|
||||
InputMessageContent = new InputMessageContent.InputMessageText {Text = new FormattedText() {Text = newText}}
|
||||
})
|
||||
.Result;
|
||||
|
||||
public static long SearchChatId(string query)
|
||||
{
|
||||
try
|
||||
{
|
||||
var results = client.ExecuteAsync(new SearchChats
|
||||
{
|
||||
Query = query,
|
||||
Limit = 5
|
||||
}).Result;
|
||||
return msg;
|
||||
}
|
||||
|
||||
if (query.StartsWith("@"))
|
||||
return results.ChatIds.First(p =>
|
||||
GetChat(p).Type is ChatType.ChatTypePrivate type &&
|
||||
GetUser(type.UserId).Username == query.Substring(1));
|
||||
return results.ChatIds.First(p => !(GetChat(p).Type is ChatType.ChatTypeSecret));
|
||||
}
|
||||
catch
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No results found.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public static void MarkRead(long chatId, long messageId) {
|
||||
client.ExecuteAsync(new ViewMessages {ChatId = chatId, MessageIds = new[] {messageId}, ForceRead = true});
|
||||
}
|
||||
|
||||
public static int SearchUserInChats(string query)
|
||||
{
|
||||
var results = client.ExecuteAsync(new SearchChatsOnServer
|
||||
{
|
||||
Query = query,
|
||||
Limit = 5
|
||||
}).Result;
|
||||
if (results.ChatIds.Length == 0)
|
||||
return 0;
|
||||
return results.ChatIds
|
||||
.Select(GetChat)
|
||||
.Where(p => p.Type is ChatType.ChatTypePrivate)
|
||||
.Select(p => ((ChatType.ChatTypePrivate) p.Type).UserId)
|
||||
.First();
|
||||
}
|
||||
public static void MarkUnread(long chatId) {
|
||||
client.ExecuteAsync(new ToggleChatIsMarkedAsUnread {ChatId = chatId, IsMarkedAsUnread = true,});
|
||||
}
|
||||
|
||||
public static int SearchContacts(string query)
|
||||
{
|
||||
//TODO implement when TDLib 1.6 is released
|
||||
try
|
||||
{
|
||||
var results = client.ExecuteAsync(new SearchContacts
|
||||
{
|
||||
Query = query,
|
||||
Limit = 5
|
||||
}).Result;
|
||||
public static long SearchChatId(string query) {
|
||||
try {
|
||||
var results = client.ExecuteAsync(new SearchChats {Query = query, Limit = 5}).Result;
|
||||
|
||||
return query.StartsWith("@")
|
||||
? results.UserIds.First(p => GetUser(p).Username == query.Substring(1))
|
||||
: results.UserIds.First();
|
||||
}
|
||||
catch
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No results found.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (query.StartsWith("@"))
|
||||
return results.ChatIds.First(p => GetChat(p).Type is ChatType.ChatTypePrivate type && GetUser(type.UserId).Username == query.Substring(1));
|
||||
|
||||
public static void LogOut()
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Logging out...");
|
||||
client.ExecuteAsync(new LogOut()).Wait();
|
||||
}
|
||||
return results.ChatIds.First(p => !(GetChat(p).Type is ChatType.ChatTypeSecret));
|
||||
}
|
||||
catch {
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No results found.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetFormattedUsername(User sender)
|
||||
{
|
||||
var username = sender.Username;
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
username = sender.FirstName + " " +
|
||||
sender.LastName;
|
||||
else
|
||||
username = "@" + username;
|
||||
public static int SearchUserInChats(string query) {
|
||||
var results = client.ExecuteAsync(new SearchChatsOnServer {Query = query, Limit = 5}).Result;
|
||||
if (results.ChatIds.Length == 0)
|
||||
return 0;
|
||||
|
||||
return username;
|
||||
}
|
||||
return results.ChatIds.Select(GetChat)
|
||||
.Where(p => p.Type is ChatType.ChatTypePrivate)
|
||||
.Select(p => ((ChatType.ChatTypePrivate) p.Type).UserId)
|
||||
.First();
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
public static int SearchContacts(string query) {
|
||||
//TODO implement when TDLib 1.6 is released
|
||||
try {
|
||||
var results = client.ExecuteAsync(new SearchContacts {Query = query, Limit = 5}).Result;
|
||||
|
||||
public static bool IsMessageRead(long chatId, long messageId)
|
||||
{
|
||||
var chat = GetChat(chatId);
|
||||
return chat.LastReadOutboxMessageId >= messageId;
|
||||
}
|
||||
return query.StartsWith("@") ? results.UserIds.First(p => GetUser(p).Username == query.Substring(1)) : results.UserIds.First();
|
||||
}
|
||||
catch {
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] No results found.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static int GetActualStringWidth(string input)
|
||||
{
|
||||
input = input.Replace(Ansi.Blue, "");
|
||||
input = input.Replace(Ansi.Bold, "");
|
||||
input = input.Replace(Ansi.Cyan, "");
|
||||
input = input.Replace(Ansi.Green, "");
|
||||
input = input.Replace(Ansi.Magenta, "");
|
||||
input = input.Replace(Ansi.Red, "");
|
||||
input = input.Replace(Ansi.Yellow, "");
|
||||
input = input.Replace(Ansi.BoldOff, "");
|
||||
input = input.Replace(Ansi.ResetAll, "");
|
||||
return input.Length;
|
||||
}
|
||||
public static void LogOut() {
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Logging out...");
|
||||
client.ExecuteAsync(new LogOut()).Wait();
|
||||
}
|
||||
|
||||
public static string GetFormattedStatus(bool isRead)
|
||||
{
|
||||
var output = " ";
|
||||
output += (isRead ? Ansi.Green : Ansi.Red) + "r";
|
||||
return output + $"{Ansi.ResetAll}]";
|
||||
}
|
||||
public static string GetFormattedUsername(User sender) {
|
||||
var username = sender.Username;
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
username = sender.FirstName + " " + sender.LastName;
|
||||
else
|
||||
username = "@" + username;
|
||||
|
||||
public static string TruncateString(string input, int maxLen)
|
||||
{
|
||||
if (maxLen < 2)
|
||||
maxLen = 2;
|
||||
return input.Length <= maxLen ? input : input.Substring(0, maxLen - 1) + "~";
|
||||
}
|
||||
return username;
|
||||
}
|
||||
|
||||
public static string TruncateMessageStart(string input, int maxLen)
|
||||
{
|
||||
if (maxLen < 2)
|
||||
maxLen = 2;
|
||||
if (input.Contains("⏎"))
|
||||
input = "⏎" + input.Split("⏎").Last();
|
||||
return input.Length < maxLen ? input : "<" + input.Substring(input.Length - maxLen + 2);
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
public static readonly List<Tuple<string, string>> Emojis = new List<Tuple<string, string>>
|
||||
{
|
||||
new Tuple<string, string>("⏎", "\n"),
|
||||
new Tuple<string, string>(":xd:", Emoji.FaceWithTearsOfJoy.Sequence.AsString),
|
||||
new Tuple<string, string>(":check:", Emoji.WhiteHeavyCheckMark.Sequence.AsString),
|
||||
new Tuple<string, string>(":thinking:", Emoji.ThinkingFace.Sequence.AsString),
|
||||
new Tuple<string, string>(":eyes:", Emoji.Eyes.Sequence.AsString),
|
||||
new Tuple<string, string>(":heart:", Emoji.RedHeart.Sequence.AsString),
|
||||
new Tuple<string, string>(":shrug:", Emoji.PersonShrugging.Sequence.AsString),
|
||||
new Tuple<string, string>(":shrugf:", Emoji.WomanShrugging.Sequence.AsString),
|
||||
new Tuple<string, string>(":shrugm:", Emoji.ManShrugging.Sequence.AsString)
|
||||
};
|
||||
public static bool IsMessageRead(long chatId, long messageId) {
|
||||
var chat = GetChat(chatId);
|
||||
return chat.LastReadOutboxMessageId >= messageId;
|
||||
}
|
||||
|
||||
public static readonly List<ConsoleKey> SpecialKeys = new List<ConsoleKey>
|
||||
{
|
||||
ConsoleKey.Backspace,
|
||||
ConsoleKey.Tab,
|
||||
ConsoleKey.Clear,
|
||||
ConsoleKey.Enter,
|
||||
ConsoleKey.Pause,
|
||||
ConsoleKey.Escape,
|
||||
ConsoleKey.PageUp,
|
||||
ConsoleKey.PageDown,
|
||||
ConsoleKey.End,
|
||||
ConsoleKey.Home,
|
||||
ConsoleKey.LeftArrow,
|
||||
ConsoleKey.UpArrow,
|
||||
ConsoleKey.RightArrow,
|
||||
ConsoleKey.DownArrow,
|
||||
ConsoleKey.Select,
|
||||
ConsoleKey.Print,
|
||||
ConsoleKey.Execute,
|
||||
ConsoleKey.PrintScreen,
|
||||
ConsoleKey.Insert,
|
||||
ConsoleKey.Delete,
|
||||
ConsoleKey.Help,
|
||||
ConsoleKey.LeftWindows,
|
||||
ConsoleKey.RightWindows,
|
||||
ConsoleKey.Applications,
|
||||
ConsoleKey.Sleep,
|
||||
ConsoleKey.F1,
|
||||
ConsoleKey.F2,
|
||||
ConsoleKey.F3,
|
||||
ConsoleKey.F4,
|
||||
ConsoleKey.F5,
|
||||
ConsoleKey.F6,
|
||||
ConsoleKey.F7,
|
||||
ConsoleKey.F8,
|
||||
ConsoleKey.F9,
|
||||
ConsoleKey.F10,
|
||||
ConsoleKey.F11,
|
||||
ConsoleKey.F12,
|
||||
ConsoleKey.F13,
|
||||
ConsoleKey.F14,
|
||||
ConsoleKey.F15,
|
||||
ConsoleKey.F16,
|
||||
ConsoleKey.F17,
|
||||
ConsoleKey.F18,
|
||||
ConsoleKey.F19,
|
||||
ConsoleKey.F20,
|
||||
ConsoleKey.F21,
|
||||
ConsoleKey.F22,
|
||||
ConsoleKey.F23,
|
||||
ConsoleKey.F24,
|
||||
ConsoleKey.BrowserBack,
|
||||
ConsoleKey.BrowserForward,
|
||||
ConsoleKey.BrowserRefresh,
|
||||
ConsoleKey.BrowserStop,
|
||||
ConsoleKey.BrowserSearch,
|
||||
ConsoleKey.BrowserFavorites,
|
||||
ConsoleKey.BrowserHome,
|
||||
ConsoleKey.VolumeMute,
|
||||
ConsoleKey.VolumeDown,
|
||||
ConsoleKey.VolumeUp,
|
||||
ConsoleKey.MediaNext,
|
||||
ConsoleKey.MediaPrevious,
|
||||
ConsoleKey.MediaStop,
|
||||
ConsoleKey.MediaPlay,
|
||||
ConsoleKey.LaunchMail,
|
||||
ConsoleKey.LaunchMediaSelect,
|
||||
ConsoleKey.LaunchApp1,
|
||||
ConsoleKey.LaunchApp2,
|
||||
ConsoleKey.Oem1,
|
||||
ConsoleKey.OemPlus,
|
||||
ConsoleKey.OemComma,
|
||||
ConsoleKey.OemMinus,
|
||||
ConsoleKey.OemPeriod,
|
||||
ConsoleKey.Oem2,
|
||||
ConsoleKey.Oem3,
|
||||
ConsoleKey.Oem4,
|
||||
ConsoleKey.Oem5,
|
||||
ConsoleKey.Oem6,
|
||||
ConsoleKey.Oem7,
|
||||
ConsoleKey.Oem8,
|
||||
ConsoleKey.Oem102,
|
||||
ConsoleKey.Process,
|
||||
ConsoleKey.Packet,
|
||||
ConsoleKey.Attention,
|
||||
ConsoleKey.CrSel,
|
||||
ConsoleKey.ExSel,
|
||||
ConsoleKey.EraseEndOfFile,
|
||||
ConsoleKey.Play,
|
||||
ConsoleKey.Zoom,
|
||||
ConsoleKey.NoName,
|
||||
ConsoleKey.Pa1,
|
||||
ConsoleKey.OemClear
|
||||
};
|
||||
}
|
||||
public static int GetActualStringWidth(string input) {
|
||||
input = input.Replace(Ansi.Blue, "");
|
||||
input = input.Replace(Ansi.Bold, "");
|
||||
input = input.Replace(Ansi.Cyan, "");
|
||||
input = input.Replace(Ansi.Green, "");
|
||||
input = input.Replace(Ansi.Magenta, "");
|
||||
input = input.Replace(Ansi.Red, "");
|
||||
input = input.Replace(Ansi.Yellow, "");
|
||||
input = input.Replace(Ansi.BoldOff, "");
|
||||
input = input.Replace(Ansi.ResetAll, "");
|
||||
return input.Length;
|
||||
}
|
||||
|
||||
public static string GetFormattedStatus(bool isRead) {
|
||||
var output = " ";
|
||||
output += (isRead ? Ansi.Green : Ansi.Red) + "r";
|
||||
return output + $"{Ansi.ResetAll}]";
|
||||
}
|
||||
|
||||
public static string TruncateString(string input, int maxLen) {
|
||||
if (maxLen < 2)
|
||||
maxLen = 2;
|
||||
return input.Length <= maxLen ? input : input.Substring(0, maxLen - 1) + "~";
|
||||
}
|
||||
|
||||
public static string TruncateMessageStart(string input, int maxLen) {
|
||||
if (maxLen < 2)
|
||||
maxLen = 2;
|
||||
if (input.Contains("⏎ "))
|
||||
input = "⏎ " + input.Split("⏎ ").Last();
|
||||
return input.Length < maxLen ? input : "<" + input.Substring(input.Length - maxLen + 2);
|
||||
}
|
||||
|
||||
public static readonly List<Tuple<string, string>> Emojis = new List<Tuple<string, string>> {
|
||||
new Tuple<string, string>("⏎ ", "\n"),
|
||||
new Tuple<string, string>(":xd:", Emoji.FaceWithTearsOfJoy.Sequence.AsString),
|
||||
new Tuple<string, string>(":check:", Emoji.WhiteHeavyCheckMark.Sequence.AsString),
|
||||
new Tuple<string, string>(":thinking:", Emoji.ThinkingFace.Sequence.AsString),
|
||||
new Tuple<string, string>(":eyes:", Emoji.Eyes.Sequence.AsString),
|
||||
new Tuple<string, string>(":heart:", Emoji.RedHeart.Sequence.AsString),
|
||||
new Tuple<string, string>(":shrug:", Emoji.PersonShrugging.Sequence.AsString),
|
||||
new Tuple<string, string>(":shrugf:", Emoji.WomanShrugging.Sequence.AsString),
|
||||
new Tuple<string, string>(":shrugm:", Emoji.ManShrugging.Sequence.AsString)
|
||||
};
|
||||
|
||||
public static readonly List<ConsoleKey> SpecialKeys = new List<ConsoleKey> {
|
||||
ConsoleKey.Backspace,
|
||||
ConsoleKey.Tab,
|
||||
ConsoleKey.Clear,
|
||||
ConsoleKey.Enter,
|
||||
ConsoleKey.Pause,
|
||||
ConsoleKey.Escape,
|
||||
ConsoleKey.PageUp,
|
||||
ConsoleKey.PageDown,
|
||||
ConsoleKey.End,
|
||||
ConsoleKey.Home,
|
||||
ConsoleKey.LeftArrow,
|
||||
ConsoleKey.UpArrow,
|
||||
ConsoleKey.RightArrow,
|
||||
ConsoleKey.DownArrow,
|
||||
ConsoleKey.Select,
|
||||
ConsoleKey.Print,
|
||||
ConsoleKey.Execute,
|
||||
ConsoleKey.PrintScreen,
|
||||
ConsoleKey.Insert,
|
||||
ConsoleKey.Delete,
|
||||
ConsoleKey.Help,
|
||||
ConsoleKey.LeftWindows,
|
||||
ConsoleKey.RightWindows,
|
||||
ConsoleKey.Applications,
|
||||
ConsoleKey.Sleep,
|
||||
ConsoleKey.F1,
|
||||
ConsoleKey.F2,
|
||||
ConsoleKey.F3,
|
||||
ConsoleKey.F4,
|
||||
ConsoleKey.F5,
|
||||
ConsoleKey.F6,
|
||||
ConsoleKey.F7,
|
||||
ConsoleKey.F8,
|
||||
ConsoleKey.F9,
|
||||
ConsoleKey.F10,
|
||||
ConsoleKey.F11,
|
||||
ConsoleKey.F12,
|
||||
ConsoleKey.F13,
|
||||
ConsoleKey.F14,
|
||||
ConsoleKey.F15,
|
||||
ConsoleKey.F16,
|
||||
ConsoleKey.F17,
|
||||
ConsoleKey.F18,
|
||||
ConsoleKey.F19,
|
||||
ConsoleKey.F20,
|
||||
ConsoleKey.F21,
|
||||
ConsoleKey.F22,
|
||||
ConsoleKey.F23,
|
||||
ConsoleKey.F24,
|
||||
ConsoleKey.BrowserBack,
|
||||
ConsoleKey.BrowserForward,
|
||||
ConsoleKey.BrowserRefresh,
|
||||
ConsoleKey.BrowserStop,
|
||||
ConsoleKey.BrowserSearch,
|
||||
ConsoleKey.BrowserFavorites,
|
||||
ConsoleKey.BrowserHome,
|
||||
ConsoleKey.VolumeMute,
|
||||
ConsoleKey.VolumeDown,
|
||||
ConsoleKey.VolumeUp,
|
||||
ConsoleKey.MediaNext,
|
||||
ConsoleKey.MediaPrevious,
|
||||
ConsoleKey.MediaStop,
|
||||
ConsoleKey.MediaPlay,
|
||||
ConsoleKey.LaunchMail,
|
||||
ConsoleKey.LaunchMediaSelect,
|
||||
ConsoleKey.LaunchApp1,
|
||||
ConsoleKey.LaunchApp2,
|
||||
ConsoleKey.Oem1,
|
||||
ConsoleKey.OemPlus,
|
||||
ConsoleKey.OemComma,
|
||||
ConsoleKey.OemMinus,
|
||||
ConsoleKey.OemPeriod,
|
||||
ConsoleKey.Oem2,
|
||||
ConsoleKey.Oem3,
|
||||
ConsoleKey.Oem4,
|
||||
ConsoleKey.Oem5,
|
||||
ConsoleKey.Oem6,
|
||||
ConsoleKey.Oem7,
|
||||
ConsoleKey.Oem8,
|
||||
ConsoleKey.Oem102,
|
||||
ConsoleKey.Process,
|
||||
ConsoleKey.Packet,
|
||||
ConsoleKey.Attention,
|
||||
ConsoleKey.CrSel,
|
||||
ConsoleKey.ExSel,
|
||||
ConsoleKey.EraseEndOfFile,
|
||||
ConsoleKey.Play,
|
||||
ConsoleKey.Zoom,
|
||||
ConsoleKey.NoName,
|
||||
ConsoleKey.Pa1,
|
||||
ConsoleKey.OemClear
|
||||
};
|
||||
}
|
||||
}
|
|
@ -10,553 +10,498 @@ using static telegram.CommandManager;
|
|||
|
||||
// ReSharper disable SwitchStatementMissingSomeEnumCasesNoDefault
|
||||
|
||||
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
|
||||
* refactor everything
|
||||
* re-evaluate ClearCurrentConsoleLine function
|
||||
* When TDLib 1.6 is released: implement contacts
|
||||
*/
|
||||
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
|
||||
* refactor everything
|
||||
* re-evaluate ClearCurrentConsoleLine function
|
||||
* When TDLib 1.6 is released: implement contacts
|
||||
*/
|
||||
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static class tgcli
|
||||
{
|
||||
public static volatile Td.TdClient client = new Td.TdClient();
|
||||
public static string dbdir = "";
|
||||
public static volatile bool authorized;
|
||||
public static volatile string connectionState = "Connecting";
|
||||
public static long currentChatId = 0;
|
||||
public static volatile int currentChatUserId = 0;
|
||||
public static volatile bool currentUserRead;
|
||||
public static volatile Td.TdApi.Message lastMessage;
|
||||
public static volatile bool quitting;
|
||||
public static volatile string currentInputLine = "";
|
||||
public static volatile List<string> messageQueue = new List<string>();
|
||||
public static volatile List<string> missedMessages = new List<string>();
|
||||
public static volatile string prefix = "[tgcli";
|
||||
public static volatile bool silent;
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static class tgcli {
|
||||
public static volatile Td.TdClient client = new Td.TdClient();
|
||||
public static string dbdir = "";
|
||||
public static volatile bool authorized;
|
||||
public static volatile string connectionState = "Connecting";
|
||||
public static long currentChatId = 0;
|
||||
public static volatile int currentChatUserId = 0;
|
||||
public static volatile bool currentUserRead;
|
||||
public static volatile Td.TdApi.Message lastMessage;
|
||||
public static volatile bool quitting;
|
||||
public static volatile string currentInputLine = "";
|
||||
public static volatile List<string> messageQueue = new List<string>();
|
||||
public static volatile List<string> missedMessages = new List<string>();
|
||||
public static volatile string prefix = "[tgcli";
|
||||
public static volatile bool silent;
|
||||
|
||||
public static volatile object @lock = new object();
|
||||
public static volatile object @lock = new object();
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
if (args.Length == 1 && args[0] == "-s")
|
||||
silent = true;
|
||||
private static void Main(string[] args) {
|
||||
if (args.Length == 1 && args[0] == "-s")
|
||||
silent = true;
|
||||
|
||||
dbdir =
|
||||
$"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}{Path.DirectorySeparatorChar}.tgcli";
|
||||
if (!Directory.Exists(dbdir))
|
||||
Directory.CreateDirectory(dbdir);
|
||||
dbdir = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}{Path.DirectorySeparatorChar}.tgcli";
|
||||
if (!Directory.Exists(dbdir))
|
||||
Directory.CreateDirectory(dbdir);
|
||||
|
||||
client.Send(new Td.TdApi.SetLogStream
|
||||
{
|
||||
LogStream = new Td.TdApi.LogStream.LogStreamFile
|
||||
{
|
||||
Path = Path.Combine(dbdir, "tdlib.log"),
|
||||
MaxFileSize = 10000000
|
||||
}
|
||||
});
|
||||
client.Send(new Td.TdApi.SetLogStream {
|
||||
LogStream = new Td.TdApi.LogStream.LogStreamFile {Path = Path.Combine(dbdir, "tdlib.log"), MaxFileSize = 10000000}
|
||||
});
|
||||
|
||||
client.Send(new Td.TdApi.SetLogVerbosityLevel
|
||||
{
|
||||
NewVerbosityLevel = 2
|
||||
});
|
||||
client.Send(new Td.TdApi.SetLogVerbosityLevel {NewVerbosityLevel = 2});
|
||||
|
||||
Console.Clear();
|
||||
ClearCurrentConsoleLine();
|
||||
Console.Clear();
|
||||
ClearCurrentConsoleLine();
|
||||
|
||||
client.UpdateReceived += HandleUpdate;
|
||||
client.UpdateReceived += HandleUpdate;
|
||||
|
||||
OnAuthUpdate(new Td.TdApi.Update.UpdateAuthorizationState()
|
||||
{
|
||||
AuthorizationState = new Td.TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters()
|
||||
});
|
||||
OnAuthUpdate(new Td.TdApi.Update.UpdateAuthorizationState() {
|
||||
AuthorizationState = new Td.TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters()
|
||||
});
|
||||
|
||||
while (!authorized)
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
while (!authorized) {
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
|
||||
ScreenUpdate();
|
||||
while (!quitting)
|
||||
MainLoop();
|
||||
ClearCurrentConsoleLine();
|
||||
Console.WriteLine($"{Ansi.Yellow}[tgcli] Shutting down...{Ansi.ResetAll}");
|
||||
}
|
||||
ScreenUpdate();
|
||||
while (!quitting)
|
||||
MainLoop();
|
||||
ClearCurrentConsoleLine();
|
||||
Console.WriteLine($"{Ansi.Yellow}[tgcli] Shutting down...{Ansi.ResetAll}");
|
||||
}
|
||||
|
||||
private static void MainLoop()
|
||||
{
|
||||
var key = Console.ReadKey(true);
|
||||
OnKeyPressed(key);
|
||||
}
|
||||
private static void MainLoop() {
|
||||
var key = Console.ReadKey(true);
|
||||
OnKeyPressed(key);
|
||||
}
|
||||
|
||||
private static void HandleUpdate(object sender, Td.TdApi.Update e)
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case Td.TdApi.Update.UpdateAuthorizationState state:
|
||||
OnAuthUpdate(state);
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateNewMessage message:
|
||||
{
|
||||
Task.Run(() => AddMessageToQueue(message.Message));
|
||||
break;
|
||||
}
|
||||
case Td.TdApi.Update.UpdateMessageContent message:
|
||||
Task.Run(() => AddMessageToQueue(message));
|
||||
Task.Run(() =>
|
||||
{
|
||||
var msg = GetMessage(message.ChatId, message.MessageId);
|
||||
if (msg.IsOutgoing && currentChatId == msg.ChatId)
|
||||
{
|
||||
lastMessage = msg;
|
||||
}
|
||||
});
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateMessageSendSucceeded sentMsg:
|
||||
lastMessage = sentMsg.Message;
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateChatReadOutbox update:
|
||||
if (lastMessage != null && lastMessage.ChatId == update.ChatId)
|
||||
{
|
||||
currentUserRead = true;
|
||||
ScreenUpdate();
|
||||
}
|
||||
private static void HandleUpdate(object sender, Td.TdApi.Update e) {
|
||||
switch (e) {
|
||||
case Td.TdApi.Update.UpdateAuthorizationState state:
|
||||
OnAuthUpdate(state);
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateNewMessage message: {
|
||||
Task.Run(() => AddMessageToQueue(message.Message));
|
||||
break;
|
||||
}
|
||||
case Td.TdApi.Update.UpdateMessageContent message:
|
||||
Task.Run(() => AddMessageToQueue(message));
|
||||
Task.Run(() => {
|
||||
var msg = GetMessage(message.ChatId, message.MessageId);
|
||||
if (msg.IsOutgoing && currentChatId == msg.ChatId) {
|
||||
lastMessage = msg;
|
||||
}
|
||||
});
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateMessageSendSucceeded sentMsg:
|
||||
lastMessage = sentMsg.Message;
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateChatReadOutbox update:
|
||||
if (lastMessage != null && lastMessage.ChatId == update.ChatId) {
|
||||
currentUserRead = true;
|
||||
ScreenUpdate();
|
||||
}
|
||||
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateConnectionState state:
|
||||
switch (state.State)
|
||||
{
|
||||
case Td.TdApi.ConnectionState.ConnectionStateConnecting _:
|
||||
connectionState = "Connecting";
|
||||
if (!authorized) return;
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connecting to Telegram servers...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.ConnectionState.ConnectionStateConnectingToProxy _:
|
||||
connectionState = "Connecting";
|
||||
if (!authorized) return;
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connecting to Proxy...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.ConnectionState.ConnectionStateReady _:
|
||||
if (!authorized) return;
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connected.");
|
||||
Task.Run(() =>
|
||||
{
|
||||
HandleCommand("u");
|
||||
connectionState = "Ready";
|
||||
ScreenUpdate();
|
||||
});
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.ConnectionState.ConnectionStateUpdating _:
|
||||
connectionState = "Updating";
|
||||
if (!authorized) return;
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Updating message cache...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.ConnectionState.ConnectionStateWaitingForNetwork _:
|
||||
connectionState = "Waiting for Network";
|
||||
if (!authorized) return;
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Lost connection. Waiting for network...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateConnectionState state:
|
||||
switch (state.State) {
|
||||
case Td.TdApi.ConnectionState.ConnectionStateConnecting _:
|
||||
connectionState = "Connecting";
|
||||
if (!authorized)
|
||||
return;
|
||||
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateSecretChat update:
|
||||
var chat = update.SecretChat;
|
||||
switch (chat.State)
|
||||
{
|
||||
case Td.TdApi.SecretChatState.SecretChatStateClosed _:
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat with {chat.Id} was closed.");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.SecretChatState.SecretChatStatePending _:
|
||||
break;
|
||||
case Td.TdApi.SecretChatState.SecretChatStateReady _:
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Green}[tgcli] Secret chat {chat.Id} connected.");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
}
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connecting to Telegram servers...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.ConnectionState.ConnectionStateConnectingToProxy _:
|
||||
connectionState = "Connecting";
|
||||
if (!authorized)
|
||||
return;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connecting to Proxy...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.ConnectionState.ConnectionStateReady _:
|
||||
if (!authorized)
|
||||
return;
|
||||
|
||||
public static void ScreenUpdate()
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
ClearCurrentConsoleLine();
|
||||
messageQueue.ForEach(p => Console.WriteLine(p + Ansi.ResetAll));
|
||||
if (messageQueue.Count > 0 && !silent)
|
||||
Console.Write("\a"); //ring terminal bell
|
||||
messageQueue.Clear();
|
||||
var status = GetFormattedStatus(currentUserRead);
|
||||
var output = prefix;
|
||||
if (connectionState != "Ready")
|
||||
output += $" | {connectionState}";
|
||||
if (currentChatUserId != 0)
|
||||
output += status;
|
||||
else
|
||||
output += "]";
|
||||
output += " > ";
|
||||
output += TruncateMessageStart(currentInputLine, Console.LargestWindowWidth - GetActualStringWidth(output));
|
||||
Console.Write(output);
|
||||
}
|
||||
}
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connected.");
|
||||
Task.Run(() => {
|
||||
HandleCommand("u");
|
||||
connectionState = "Ready";
|
||||
ScreenUpdate();
|
||||
});
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.ConnectionState.ConnectionStateUpdating _:
|
||||
connectionState = "Updating";
|
||||
if (!authorized)
|
||||
return;
|
||||
|
||||
private static void OnKeyPressed(ConsoleKeyInfo key)
|
||||
{
|
||||
switch (key.Key)
|
||||
{
|
||||
case ConsoleKey.Enter when connectionState != "Ready":
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] " +
|
||||
"Connection unstable. Check your network connection and try again.");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case ConsoleKey.Enter when currentInputLine.StartsWith("/"):
|
||||
{
|
||||
var command = currentInputLine.Substring(1);
|
||||
currentInputLine = "";
|
||||
HandleCommand(command);
|
||||
ScreenUpdate();
|
||||
return;
|
||||
}
|
||||
case ConsoleKey.Enter when currentChatId == 0:
|
||||
{
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] " +
|
||||
"No chat selected. Select a chat with /open <query>");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
}
|
||||
case ConsoleKey.Enter:
|
||||
SendMessage(currentInputLine, currentChatId);
|
||||
currentInputLine = "";
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case ConsoleKey.Backspace when currentInputLine.Length >= 1:
|
||||
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt))
|
||||
{
|
||||
var lastIndex = currentInputLine.TrimEnd().LastIndexOf(" ", StringComparison.Ordinal);
|
||||
if (lastIndex < 0)
|
||||
lastIndex = 0;
|
||||
currentInputLine = currentInputLine.Substring(0, lastIndex);
|
||||
if (lastIndex != 0)
|
||||
currentInputLine += " ";
|
||||
ScreenUpdate();
|
||||
return;
|
||||
}
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Updating message cache...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.ConnectionState.ConnectionStateWaitingForNetwork _:
|
||||
connectionState = "Waiting for Network";
|
||||
if (!authorized)
|
||||
return;
|
||||
|
||||
currentInputLine = currentInputLine.Substring(0, currentInputLine.Length - 1);
|
||||
ScreenUpdate();
|
||||
break;
|
||||
default:
|
||||
{
|
||||
switch (key.Key)
|
||||
{
|
||||
case ConsoleKey.N when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
currentInputLine += "⏎";
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.D when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("q");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.Q when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("q");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.E when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("c");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.U when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("u");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.O when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
if (string.IsNullOrWhiteSpace(currentInputLine))
|
||||
currentInputLine = "/o ";
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.L when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("cl");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
}
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Lost connection. Waiting for network...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
}
|
||||
|
||||
if (!SpecialKeys.Contains(key.Key))
|
||||
{
|
||||
currentInputLine += key.KeyChar;
|
||||
ScreenUpdate();
|
||||
}
|
||||
break;
|
||||
case Td.TdApi.Update.UpdateSecretChat update:
|
||||
var chat = update.SecretChat;
|
||||
switch (chat.State) {
|
||||
case Td.TdApi.SecretChatState.SecretChatStateClosed _:
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] Secret chat with {chat.Id} was closed.");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.SecretChatState.SecretChatStatePending _: break;
|
||||
case Td.TdApi.SecretChatState.SecretChatStateReady _:
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Green}[tgcli] Secret chat {chat.Id} connected.");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void OnAuthUpdate(Td.TdApi.Update.UpdateAuthorizationState state)
|
||||
{
|
||||
switch (state.AuthorizationState)
|
||||
{
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters _:
|
||||
client.Send(new Td.TdApi.SetTdlibParameters
|
||||
{
|
||||
Parameters = new Td.TdApi.TdlibParameters
|
||||
{
|
||||
ApiId = 600606,
|
||||
ApiHash = "c973f46778be4b35481ce45e93271e82",
|
||||
DatabaseDirectory = dbdir,
|
||||
UseMessageDatabase = true,
|
||||
SystemLanguageCode = "en_US",
|
||||
DeviceModel = Environment.MachineName,
|
||||
SystemVersion = ".NET Core CLR " + Environment.Version,
|
||||
ApplicationVersion = "0.1a",
|
||||
EnableStorageOptimizer = true,
|
||||
UseSecretChats = true
|
||||
}
|
||||
});
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitEncryptionKey _:
|
||||
client.Send(new Td.TdApi.CheckDatabaseEncryptionKey());
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitPhoneNumber _:
|
||||
{
|
||||
Console.Write("[tgcli] login> ");
|
||||
var phone = Console.ReadLine();
|
||||
client.Send(new Td.TdApi.SetAuthenticationPhoneNumber
|
||||
{
|
||||
PhoneNumber = phone
|
||||
});
|
||||
break;
|
||||
}
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitCode _:
|
||||
{
|
||||
Console.Write("[tgcli] code> ");
|
||||
var code = Console.ReadLine();
|
||||
client.Send(new Td.TdApi.CheckAuthenticationCode
|
||||
{
|
||||
Code = code
|
||||
});
|
||||
break;
|
||||
}
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitPassword _:
|
||||
{
|
||||
Console.Write("[tgcli] 2fa password> ");
|
||||
var pass = ReadConsolePassword();
|
||||
client.Send(new Td.TdApi.CheckAuthenticationPassword
|
||||
{
|
||||
Password = pass
|
||||
});
|
||||
break;
|
||||
}
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateReady _:
|
||||
Console.WriteLine("[tgcli] logged in.");
|
||||
authorized = true;
|
||||
connectionState = "Ready";
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateClosed _:
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Logged out successfully. All local data has been deleted.");
|
||||
ScreenUpdate();
|
||||
Environment.Exit(0);
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateClosing _:
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Logging out...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateLoggingOut _:
|
||||
if (authorized) return;
|
||||
Console.WriteLine(
|
||||
"[tgcli] This session has been destroyed externally, to fix this delete ~/.tgcli");
|
||||
Environment.Exit(1);
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"unknown state: {state.AuthorizationState.DataType}");
|
||||
Environment.Exit(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
public static void ScreenUpdate() {
|
||||
lock (@lock) {
|
||||
ClearCurrentConsoleLine();
|
||||
messageQueue.ForEach(p => Console.WriteLine(p + Ansi.ResetAll));
|
||||
if (messageQueue.Count > 0 && !silent)
|
||||
Console.Write("\a"); //ring terminal bell
|
||||
messageQueue.Clear();
|
||||
var status = GetFormattedStatus(currentUserRead);
|
||||
var output = prefix;
|
||||
if (connectionState != "Ready")
|
||||
output += $" | {connectionState}";
|
||||
if (currentChatUserId != 0)
|
||||
output += status;
|
||||
else
|
||||
output += "]";
|
||||
output += " > ";
|
||||
output += TruncateMessageStart(currentInputLine, Console.LargestWindowWidth - GetActualStringWidth(output));
|
||||
Console.Write(output);
|
||||
}
|
||||
}
|
||||
|
||||
public static string FormatMessage(Td.TdApi.Message msg)
|
||||
{
|
||||
string text;
|
||||
if (msg.Content is Td.TdApi.MessageContent.MessageText messageText)
|
||||
text = messageText.Text.Text;
|
||||
else if (msg.Content is Td.TdApi.MessageContent.MessagePhoto photo)
|
||||
text = !string.IsNullOrWhiteSpace(photo.Caption.Text)
|
||||
? $"[unsupported {msg.Content.DataType}] {photo.Caption.Text}"
|
||||
: $"[unsupported {msg.Content.DataType}]";
|
||||
else if (msg.Content is Td.TdApi.MessageContent.MessageDocument document)
|
||||
text = !string.IsNullOrWhiteSpace(document.Caption.Text)
|
||||
? $"[unsupported {msg.Content.DataType}] {document.Caption.Text}"
|
||||
: $"[unsupported {msg.Content.DataType}]";
|
||||
else
|
||||
text = $"[unsupported {msg.Content.DataType}]";
|
||||
var sender = GetUser(msg.SenderUserId);
|
||||
var chat = GetChat(msg.ChatId);
|
||||
var username = TruncateString(GetFormattedUsername(sender), 10);
|
||||
var time = FormatTime(msg.Date);
|
||||
var isChannel = msg.IsChannelPost;
|
||||
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate ||
|
||||
chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
|
||||
var isSecret = chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
|
||||
var isReply = msg.ReplyToMessageId != 0;
|
||||
private static void OnKeyPressed(ConsoleKeyInfo key) {
|
||||
switch (key.Key) {
|
||||
case ConsoleKey.Enter when connectionState != "Ready":
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] " + "Connection unstable. Check your network connection and try again.");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case ConsoleKey.Enter when currentInputLine.StartsWith("/"): {
|
||||
var command = currentInputLine.Substring(1);
|
||||
currentInputLine = "";
|
||||
HandleCommand(command);
|
||||
ScreenUpdate();
|
||||
return;
|
||||
}
|
||||
case ConsoleKey.Enter when currentChatId == 0: {
|
||||
lock (@lock)
|
||||
messageQueue.Add($"{Ansi.Red}[tgcli] " + "No chat selected. Select a chat with /open <query>");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
}
|
||||
case ConsoleKey.Enter:
|
||||
SendMessage(currentInputLine, currentChatId);
|
||||
currentInputLine = "";
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case ConsoleKey.Backspace when currentInputLine.Length >= 1:
|
||||
if (key.Modifiers.HasFlag(ConsoleModifiers.Alt)) {
|
||||
var lastIndex = currentInputLine.TrimEnd().LastIndexOf(" ", StringComparison.Ordinal);
|
||||
if (lastIndex < 0)
|
||||
lastIndex = 0;
|
||||
currentInputLine = currentInputLine.Substring(0, lastIndex);
|
||||
if (lastIndex != 0)
|
||||
currentInputLine += " ";
|
||||
ScreenUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
chat.Title = TruncateString(chat.Title, 20);
|
||||
currentInputLine = currentInputLine.Substring(0, currentInputLine.Length - 1);
|
||||
ScreenUpdate();
|
||||
break;
|
||||
default: {
|
||||
switch (key.Key) {
|
||||
case ConsoleKey.N when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
currentInputLine += "⏎ ";
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.D when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("q");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.Q when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("q");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.E when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("c");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.U when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("u");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.O when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
if (string.IsNullOrWhiteSpace(currentInputLine))
|
||||
currentInputLine = "/o ";
|
||||
ScreenUpdate();
|
||||
return;
|
||||
case ConsoleKey.L when key.Modifiers.HasFlag(ConsoleModifiers.Control):
|
||||
HandleCommand("cl");
|
||||
ScreenUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
Td.TdApi.Message replyMessage;
|
||||
if (!SpecialKeys.Contains(key.Key)) {
|
||||
currentInputLine += key.KeyChar;
|
||||
ScreenUpdate();
|
||||
}
|
||||
|
||||
var msgPrefix =
|
||||
$"{Ansi.Bold}{Ansi.Green}[{time}] {(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} " +
|
||||
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
|
||||
var finalOutput = msgPrefix;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var indent = new string(' ', GetActualStringWidth(msgPrefix));
|
||||
var arrows = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} ";
|
||||
private static void OnAuthUpdate(Td.TdApi.Update.UpdateAuthorizationState state) {
|
||||
switch (state.AuthorizationState) {
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters _:
|
||||
client.Send(new Td.TdApi.SetTdlibParameters {
|
||||
Parameters = new Td.TdApi.TdlibParameters {
|
||||
ApiId = 600606,
|
||||
ApiHash = "c973f46778be4b35481ce45e93271e82",
|
||||
DatabaseDirectory = dbdir,
|
||||
UseMessageDatabase = true,
|
||||
SystemLanguageCode = "en_US",
|
||||
DeviceModel = Environment.MachineName,
|
||||
SystemVersion = ".NET Core CLR " + Environment.Version,
|
||||
ApplicationVersion = "0.1a",
|
||||
EnableStorageOptimizer = true,
|
||||
UseSecretChats = true
|
||||
}
|
||||
});
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitEncryptionKey _:
|
||||
client.Send(new Td.TdApi.CheckDatabaseEncryptionKey());
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitPhoneNumber _: {
|
||||
Console.Write("[tgcli] login> ");
|
||||
var phone = Console.ReadLine();
|
||||
client.Send(new Td.TdApi.SetAuthenticationPhoneNumber {PhoneNumber = phone});
|
||||
break;
|
||||
}
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitCode _: {
|
||||
Console.Write("[tgcli] code> ");
|
||||
var code = Console.ReadLine();
|
||||
client.Send(new Td.TdApi.CheckAuthenticationCode {Code = code});
|
||||
break;
|
||||
}
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateWaitPassword _: {
|
||||
Console.Write("[tgcli] 2fa password> ");
|
||||
var pass = ReadConsolePassword();
|
||||
client.Send(new Td.TdApi.CheckAuthenticationPassword {Password = pass});
|
||||
break;
|
||||
}
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateReady _:
|
||||
Console.WriteLine("[tgcli] logged in.");
|
||||
authorized = true;
|
||||
connectionState = "Ready";
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateClosed _:
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Logged out successfully. All local data has been deleted.");
|
||||
ScreenUpdate();
|
||||
Environment.Exit(0);
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateClosing _:
|
||||
messageQueue.Add($"{Ansi.Yellow}[tgcli] Logging out...");
|
||||
ScreenUpdate();
|
||||
break;
|
||||
case Td.TdApi.AuthorizationState.AuthorizationStateLoggingOut _:
|
||||
if (authorized)
|
||||
return;
|
||||
|
||||
if (isReply)
|
||||
{
|
||||
try
|
||||
{
|
||||
replyMessage = GetMessage(chat.Id, msg.ReplyToMessageId);
|
||||
finalOutput = $"{FormatMessageReply(replyMessage, msgPrefix)}";
|
||||
}
|
||||
catch
|
||||
{
|
||||
//ignored; reply to deleted msg
|
||||
}
|
||||
}
|
||||
Console.WriteLine("[tgcli] This session has been destroyed externally, to fix this delete ~/.tgcli");
|
||||
Environment.Exit(1);
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine($"unknown state: {state.AuthorizationState.DataType}");
|
||||
Environment.Exit(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var rest = $"{text}{(msg.EditDate == 0 ? "" : $"{Ansi.Yellow}*")}";
|
||||
var lines = rest.Split("\n").ToList();
|
||||
if (!isReply)
|
||||
{
|
||||
finalOutput += arrows + lines.First();
|
||||
lines.RemoveAt(0);
|
||||
}
|
||||
public static string FormatMessage(Td.TdApi.Message msg) {
|
||||
string text;
|
||||
if (msg.Content is Td.TdApi.MessageContent.MessageText messageText)
|
||||
text = messageText.Text.Text;
|
||||
else if (msg.Content is Td.TdApi.MessageContent.MessagePhoto photo)
|
||||
text = !string.IsNullOrWhiteSpace(photo.Caption.Text)
|
||||
? $"[unsupported {msg.Content.DataType}] {photo.Caption.Text}"
|
||||
: $"[unsupported {msg.Content.DataType}]";
|
||||
else if (msg.Content is Td.TdApi.MessageContent.MessageDocument document)
|
||||
text = !string.IsNullOrWhiteSpace(document.Caption.Text)
|
||||
? $"[unsupported {msg.Content.DataType}] {document.Caption.Text}"
|
||||
: $"[unsupported {msg.Content.DataType}]";
|
||||
else
|
||||
text = $"[unsupported {msg.Content.DataType}]";
|
||||
var sender = GetUser(msg.SenderUserId);
|
||||
var chat = GetChat(msg.ChatId);
|
||||
var username = TruncateString(GetFormattedUsername(sender), 10);
|
||||
var time = FormatTime(msg.Date);
|
||||
var isChannel = msg.IsChannelPost;
|
||||
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate || chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
|
||||
var isSecret = chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
|
||||
var isReply = msg.ReplyToMessageId != 0;
|
||||
|
||||
lines.ForEach(l => finalOutput += "\n" + indent + arrows + l);
|
||||
chat.Title = TruncateString(chat.Title, 20);
|
||||
|
||||
return finalOutput;
|
||||
}
|
||||
Td.TdApi.Message replyMessage;
|
||||
|
||||
public static string FormatMessageReply(Td.TdApi.Message msg, string origPrefix)
|
||||
{
|
||||
string text;
|
||||
if (msg.Content is Td.TdApi.MessageContent.MessageText messageText)
|
||||
text = messageText.Text.Text;
|
||||
else
|
||||
text = $"[unsupported {msg.Content.DataType}]";
|
||||
var sender = GetUser(msg.SenderUserId);
|
||||
var chat = GetChat(msg.ChatId);
|
||||
var username = GetFormattedUsername(sender);
|
||||
var time = FormatTime(msg.Date);
|
||||
var isChannel = msg.IsChannelPost;
|
||||
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate ||
|
||||
chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
|
||||
var isSecret = chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
|
||||
var msgPrefix = $"{Ansi.Bold}{Ansi.Green}[{time}] {(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} "
|
||||
+ $"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
|
||||
var finalOutput = msgPrefix;
|
||||
|
||||
chat.Title = TruncateString(chat.Title, 20);
|
||||
var indent = new string(' ', GetActualStringWidth(msgPrefix));
|
||||
var arrows = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} ";
|
||||
|
||||
var finalOutput = "";
|
||||
var replyPrefix = $"{origPrefix}{Ansi.Yellow}Re: {Ansi.Bold}{Ansi.Green}[{time}] " +
|
||||
$"{(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} " +
|
||||
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
|
||||
if (isReply) {
|
||||
try {
|
||||
replyMessage = GetMessage(chat.Id, msg.ReplyToMessageId);
|
||||
finalOutput = $"{FormatMessageReply(replyMessage, msgPrefix)}";
|
||||
}
|
||||
catch {
|
||||
//ignored; reply to deleted msg
|
||||
}
|
||||
}
|
||||
|
||||
var indent = new string(' ', GetActualStringWidth(replyPrefix));
|
||||
var arrows = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} ";
|
||||
var rest = $"{text}{(msg.EditDate == 0 ? "" : $"{Ansi.Yellow}*")}";
|
||||
var lines = rest.Split("\n").ToList();
|
||||
if (!isReply) {
|
||||
finalOutput += arrows + lines.First();
|
||||
lines.RemoveAt(0);
|
||||
}
|
||||
|
||||
var rest = $"{text}{(msg.EditDate == 0 ? "" : $"{Ansi.Yellow}*")}";
|
||||
lines.ForEach(l => finalOutput += "\n" + indent + arrows + l);
|
||||
|
||||
finalOutput += replyPrefix;
|
||||
return finalOutput;
|
||||
}
|
||||
|
||||
var lines = rest.Split("\n").ToList();
|
||||
finalOutput += arrows + lines.First();
|
||||
lines.RemoveAt(0);
|
||||
lines.ForEach(l => finalOutput += "\n" + indent + arrows + l);
|
||||
public static string FormatMessageReply(Td.TdApi.Message msg, string origPrefix) {
|
||||
string text;
|
||||
if (msg.Content is Td.TdApi.MessageContent.MessageText messageText)
|
||||
text = messageText.Text.Text;
|
||||
else
|
||||
text = $"[unsupported {msg.Content.DataType}]";
|
||||
var sender = GetUser(msg.SenderUserId);
|
||||
var chat = GetChat(msg.ChatId);
|
||||
var username = GetFormattedUsername(sender);
|
||||
var time = FormatTime(msg.Date);
|
||||
var isChannel = msg.IsChannelPost;
|
||||
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate || chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
|
||||
var isSecret = chat.Type is Td.TdApi.ChatType.ChatTypeSecret;
|
||||
|
||||
return finalOutput;
|
||||
}
|
||||
chat.Title = TruncateString(chat.Title, 20);
|
||||
|
||||
private static string FormatMessage(Td.TdApi.Update.UpdateMessageContent msg)
|
||||
{
|
||||
string text;
|
||||
if (msg.NewContent is Td.TdApi.MessageContent.MessageText messageText)
|
||||
text = messageText.Text.Text;
|
||||
else
|
||||
text = $"[unsupported {msg.NewContent.DataType}]";
|
||||
var message = GetMessage(msg.ChatId, msg.MessageId);
|
||||
var sender = GetUser(message.SenderUserId);
|
||||
var chat = GetChat(msg.ChatId);
|
||||
var username = GetFormattedUsername(sender);
|
||||
var time = FormatTime(message.EditDate);
|
||||
var isChannel = message.IsChannelPost;
|
||||
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate;
|
||||
var finalOutput = "";
|
||||
var replyPrefix = $"{origPrefix}{Ansi.Yellow}Re: {Ansi.Bold}{Ansi.Green}[{time}] "
|
||||
+ $"{(isSecret ? $"{Ansi.Red}[sec] " : "")}{Ansi.Cyan}{chat.Title} "
|
||||
+ $"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
|
||||
|
||||
return $"{Ansi.Bold}{Ansi.Green}[{time}] {Ansi.Cyan}{chat.Title} " +
|
||||
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}" +
|
||||
$"{(message.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} " +
|
||||
$"{text}" +
|
||||
$"{Ansi.Yellow}*";
|
||||
}
|
||||
var indent = new string(' ', GetActualStringWidth(replyPrefix));
|
||||
var arrows = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} ";
|
||||
|
||||
public static void AddMessageToQueue(Td.TdApi.Message msg)
|
||||
{
|
||||
//handle muted
|
||||
if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId)
|
||||
return;
|
||||
var rest = $"{text}{(msg.EditDate == 0 ? "" : $"{Ansi.Yellow}*")}";
|
||||
|
||||
//we aren't interested in backlog
|
||||
if (connectionState != "Ready")
|
||||
return;
|
||||
finalOutput += replyPrefix;
|
||||
|
||||
var formattedMessage = FormatMessage(msg);
|
||||
var lines = rest.Split("\n").ToList();
|
||||
finalOutput += arrows + lines.First();
|
||||
lines.RemoveAt(0);
|
||||
lines.ForEach(l => finalOutput += "\n" + indent + arrows + l);
|
||||
|
||||
if (currentChatId != 0 && msg.ChatId != currentChatId)
|
||||
lock (@lock)
|
||||
missedMessages.Add(formattedMessage);
|
||||
else
|
||||
lock (@lock)
|
||||
messageQueue.Add(formattedMessage);
|
||||
return finalOutput;
|
||||
}
|
||||
|
||||
if (msg.ChatId == currentChatId)
|
||||
MarkRead(msg.ChatId, msg.Id);
|
||||
ScreenUpdate();
|
||||
}
|
||||
private static string FormatMessage(Td.TdApi.Update.UpdateMessageContent msg) {
|
||||
string text;
|
||||
if (msg.NewContent is Td.TdApi.MessageContent.MessageText messageText)
|
||||
text = messageText.Text.Text;
|
||||
else
|
||||
text = $"[unsupported {msg.NewContent.DataType}]";
|
||||
var message = GetMessage(msg.ChatId, msg.MessageId);
|
||||
var sender = GetUser(message.SenderUserId);
|
||||
var chat = GetChat(msg.ChatId);
|
||||
var username = GetFormattedUsername(sender);
|
||||
var time = FormatTime(message.EditDate);
|
||||
var isChannel = message.IsChannelPost;
|
||||
var isPrivate = chat.Type is Td.TdApi.ChatType.ChatTypePrivate;
|
||||
|
||||
public static void AddMessageToQueue(Td.TdApi.Update.UpdateMessageContent msg)
|
||||
{
|
||||
//handle muted
|
||||
if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId
|
||||
|| GetMessage(msg.ChatId, msg.MessageId).EditDate == 0)
|
||||
return;
|
||||
return $"{Ansi.Bold}{Ansi.Green}[{time}] {Ansi.Cyan}{chat.Title} "
|
||||
+ $"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}"
|
||||
+ $"{(message.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} "
|
||||
+ $"{text}"
|
||||
+ $"{Ansi.Yellow}*";
|
||||
}
|
||||
|
||||
var formattedMessage = FormatMessage(msg);
|
||||
public static void AddMessageToQueue(Td.TdApi.Message msg) {
|
||||
//handle muted
|
||||
if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId)
|
||||
return;
|
||||
|
||||
if (currentChatId != 0 && msg.ChatId != currentChatId)
|
||||
lock (@lock)
|
||||
missedMessages.Add(formattedMessage);
|
||||
else
|
||||
lock (@lock)
|
||||
messageQueue.Add(formattedMessage);
|
||||
ScreenUpdate();
|
||||
}
|
||||
}
|
||||
//we aren't interested in backlog
|
||||
if (connectionState != "Ready")
|
||||
return;
|
||||
|
||||
var formattedMessage = FormatMessage(msg);
|
||||
|
||||
if (currentChatId != 0 && msg.ChatId != currentChatId)
|
||||
lock (@lock)
|
||||
missedMessages.Add(formattedMessage);
|
||||
else
|
||||
lock (@lock)
|
||||
messageQueue.Add(formattedMessage);
|
||||
|
||||
if (msg.ChatId == currentChatId)
|
||||
MarkRead(msg.ChatId, msg.Id);
|
||||
ScreenUpdate();
|
||||
}
|
||||
|
||||
public static void AddMessageToQueue(Td.TdApi.Update.UpdateMessageContent msg) {
|
||||
//handle muted
|
||||
if (GetChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId || GetMessage(msg.ChatId, msg.MessageId).EditDate == 0)
|
||||
return;
|
||||
|
||||
var formattedMessage = FormatMessage(msg);
|
||||
|
||||
if (currentChatId != 0 && msg.ChatId != currentChatId)
|
||||
lock (@lock)
|
||||
missedMessages.Add(formattedMessage);
|
||||
else
|
||||
lock (@lock)
|
||||
messageQueue.Add(formattedMessage);
|
||||
ScreenUpdate();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue