334 lines
11 KiB
C#
334 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Td = TdLib;
|
|
using static tgcli.core.Util;
|
|
|
|
namespace tgcli.core
|
|
{
|
|
public class Program
|
|
{
|
|
public static Td.TdClient client = new Td.TdClient();
|
|
public static string dbdir = "";
|
|
public static bool authorized;
|
|
public static long currentChatId = 0;
|
|
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 string prefix = "[tgcli]";
|
|
public static object _lock = new object();
|
|
|
|
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
|
|
};
|
|
private static void Main()
|
|
{
|
|
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.SetLogVerbosityLevel
|
|
{
|
|
NewVerbosityLevel = 2
|
|
});
|
|
|
|
Console.Clear();
|
|
ClearCurrentConsoleLine();
|
|
|
|
client.UpdateReceived += HandleUpdate;
|
|
|
|
OnAuthUpdate("authorizationStateWaitTdlibParameters");
|
|
while (!authorized)
|
|
{
|
|
Thread.Sleep(1);
|
|
}
|
|
|
|
ScreenUpdate();
|
|
while (true)
|
|
MainLoop();
|
|
}
|
|
|
|
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.AuthorizationState.DataType);
|
|
break;
|
|
case Td.TdApi.Update.UpdateNewMessage message:
|
|
{
|
|
var msg = message.Message;
|
|
Task.Run(() => AddMessageToQueue(msg));
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void ScreenUpdate()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
ClearCurrentConsoleLine();
|
|
messageQueue.ForEach(p => Console.WriteLine(p + Ansi.ResetAll));
|
|
messageQueue.Clear();
|
|
Console.Write(prefix + " > " + currentInputLine);
|
|
}
|
|
}
|
|
|
|
private static void OnKeyPressed(ConsoleKeyInfo key)
|
|
{
|
|
if (key.Key == ConsoleKey.Enter)
|
|
{
|
|
if (currentInputLine.StartsWith("/"))
|
|
{
|
|
var command = currentInputLine.Substring(1);
|
|
currentInputLine = "";
|
|
Command.ParseCommand(command);
|
|
ScreenUpdate();
|
|
return;
|
|
}
|
|
|
|
if (currentChatId == 0)
|
|
{
|
|
lock(_lock)
|
|
messageQueue.Add($"{Ansi.Bold}{Ansi.Red}[tgcli] " +
|
|
$"No chat selected. Select a chat with /open <query>");
|
|
ScreenUpdate();
|
|
return;
|
|
}
|
|
|
|
sendMessage(currentInputLine, currentChatId);
|
|
currentInputLine = "";
|
|
ScreenUpdate();
|
|
}
|
|
else if (key.Key == ConsoleKey.Backspace && currentInputLine.Length >= 1)
|
|
{
|
|
currentInputLine = currentInputLine.Substring(0, currentInputLine.Length - 1);
|
|
ScreenUpdate();
|
|
}
|
|
else if (!specialKeys.Contains(key.Key))
|
|
{
|
|
currentInputLine += key.KeyChar;
|
|
ScreenUpdate();
|
|
}
|
|
}
|
|
|
|
private static void OnAuthUpdate(string state)
|
|
{
|
|
if (state == "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
|
|
}
|
|
});
|
|
}
|
|
|
|
else if (state == "authorizationStateWaitEncryptionKey")
|
|
{
|
|
client.Send(new Td.TdApi.CheckDatabaseEncryptionKey());
|
|
}
|
|
|
|
else if (state == "authorizationStateWaitPhoneNumber")
|
|
{
|
|
Console.Write("[tgcli] login> ");
|
|
var phone = Console.ReadLine();
|
|
client.Send(new Td.TdApi.SetAuthenticationPhoneNumber
|
|
{
|
|
PhoneNumber = phone
|
|
});
|
|
}
|
|
|
|
else if (state == "authorizationStateWaitCode")
|
|
{
|
|
Console.Write("[tgcli] code> ");
|
|
var code = Console.ReadLine();
|
|
client.Send(new Td.TdApi.CheckAuthenticationCode
|
|
{
|
|
Code = code
|
|
});
|
|
}
|
|
|
|
else if (state == "authorizationStateWaitPassword")
|
|
{
|
|
Console.Write("[tgcli] 2fa password> ");
|
|
var pass = ReadConsolePassword();
|
|
client.Send(new Td.TdApi.CheckAuthenticationPassword
|
|
{
|
|
Password = pass
|
|
});
|
|
}
|
|
|
|
else if (state == "authorizationStateReady")
|
|
{
|
|
Console.WriteLine("[tgcli] logged in.");
|
|
authorized = true;
|
|
}
|
|
|
|
else
|
|
{
|
|
Console.WriteLine($"unknown state: {state}");
|
|
Environment.Exit(1);
|
|
}
|
|
}
|
|
|
|
public static string FormatMessage(Td.TdApi.Message msg)
|
|
{
|
|
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;
|
|
|
|
return $"{Ansi.Bold}{Ansi.Green}[{time}] {Ansi.Cyan}{chat.Title} " +
|
|
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}" +
|
|
$"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} " +
|
|
$"{text}";
|
|
}
|
|
public static void AddMessageToQueue(Td.TdApi.Message msg)
|
|
{
|
|
//handle muted
|
|
if (getChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId)
|
|
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();
|
|
}
|
|
}
|
|
} |