tgcli/telegram/tgcli.cs
2019-12-12 13:00:35 +01:00

596 lines
24 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Td = TdLib;
using static telegram.Util;
namespace telegram
{
/*
* TODO:
* reply to x messages ago
* cap length & truncate extremely long chat names!
* replace emoji on send & un-replace on edit, two-way dictionary!!
* replace more emojis on send (is there a lib for that)
* make typing newlines actually good (inputline as list?)
* fix history not fully displaying on channel open (query returning less results than expected?)
* waaay more error messages instead of just doing nothing
* make Util.getActualStringWidth better
* make the command system not shit (classes & manager & /help etc)
* refactor everything
* publish AUR package
* make login less frustrating
* add option to disable terminal bell
* command /s /search -> list matching chats, option 1-n, archived indicator
* secret chats!
* split with newline if received message enters next line
* fix issues when current_input message is longer than term width (only show as much as fits?)
* maintain a list of statuses per user (online, read, typing) ? -> make otr indicators more reliable
* photo/document/etc captions
* photo download & show externally
* maybe cursor input nav (cmd+del, left/right, up for last inputs, etc)
*/
public 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 currentUserOnline;
public static volatile bool currentUserTyping;
public static volatile bool currentUserRead;
public static volatile CancellationTokenSource ctsTyping;
public static volatile Task typingTask;
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 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 (!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 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:
{
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.UpdateUserStatus uStatus when uStatus.UserId == currentChatUserId:
currentUserOnline = uStatus.Status is Td.TdApi.UserStatus.UserStatusOnline;
ScreenUpdate();
break;
case Td.TdApi.Update.UpdateChatReadOutbox update:
if (lastMessage != null && lastMessage.ChatId == update.ChatId)
{
currentUserRead = true;
ScreenUpdate();
}
break;
case Td.TdApi.Update.UpdateUserChatAction action when action.Action is
Td.TdApi.ChatAction.ChatActionTyping && action.UserId == currentChatUserId
&& action.ChatId == currentChatUserId:
currentUserTyping = true;
ScreenUpdate();
ctsTyping?.Cancel();
ctsTyping = new CancellationTokenSource();
typingTask = new Task(() =>
{
Task.Delay(5000);
if (!ctsTyping.IsCancellationRequested)
currentUserTyping = false;
}, ctsTyping.Token);
typingTask.Start();
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 _:
connectionState = "Ready";
if (!authorized) return;
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connected.");
Task.Run(() =>
{
Command.ParseCommand("u");
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;
}
}
public static void ScreenUpdate()
{
lock (_lock)
{
ClearCurrentConsoleLine();
messageQueue.ForEach(p => Console.WriteLine(p + Ansi.ResetAll));
if (messageQueue.Count > 0)
Console.Write("\a"); //ring terminal bell
messageQueue.Clear();
var status = getFormattedStatus(currentUserOnline, currentUserRead, currentUserTyping);
Console.Write(prefix
+ (connectionState == "Ready" ? "" : $" ({connectionState})")
+ (currentChatUserId != 0 ? status : "]") + " > " + currentInputLine);
}
}
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 = "";
Command.ParseCommand(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;
}
currentInputLine = currentInputLine.Substring(0, currentInputLine.Length - 1);
ScreenUpdate();
break;
default:
{
if (key.Key == ConsoleKey.N && key.Modifiers.HasFlag(ConsoleModifiers.Control))
{
currentInputLine += "⏎";
ScreenUpdate();
return;
}
if (key.Key == ConsoleKey.D && key.Modifiers.HasFlag(ConsoleModifiers.Control))
{
Command.ParseCommand("q");
ScreenUpdate();
return;
}
if (key.Key == ConsoleKey.L && key.Modifiers.HasFlag(ConsoleModifiers.Control))
{
Command.ParseCommand("cl");
ScreenUpdate();
return;
}
if (!specialKeys.Contains(key.Key))
{
currentInputLine += key.KeyChar;
ScreenUpdate();
}
break;
}
}
}
private static void OnAuthUpdate(string state)
{
switch (state)
{
case "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
}
});
break;
case "authorizationStateWaitEncryptionKey":
client.Send(new Td.TdApi.CheckDatabaseEncryptionKey());
break;
case "authorizationStateWaitPhoneNumber":
{
Console.Write("[tgcli] login> ");
var phone = Console.ReadLine();
client.Send(new Td.TdApi.SetAuthenticationPhoneNumber
{
PhoneNumber = phone
});
break;
}
case "authorizationStateWaitCode":
{
Console.Write("[tgcli] code> ");
var code = Console.ReadLine();
client.Send(new Td.TdApi.CheckAuthenticationCode
{
Code = code
});
break;
}
case "authorizationStateWaitPassword":
{
Console.Write("[tgcli] 2fa password> ");
var pass = ReadConsolePassword();
client.Send(new Td.TdApi.CheckAuthenticationPassword
{
Password = pass
});
break;
}
case "authorizationStateReady":
Console.WriteLine("[tgcli] logged in.");
authorized = true;
break;
default:
Console.WriteLine($"unknown state: {state}");
Environment.Exit(1);
break;
}
}
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;
var isReply = msg.ReplyToMessageId != 0;
Td.TdApi.Message replyMessage;
var replyText = "";
var prefix = $"{Ansi.Bold}{Ansi.Green}[{time}] {Ansi.Cyan}{chat.Title} " +
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
var finalOutput = prefix;
var indent = new string(' ', getActualStringWidth(prefix));
var arrows = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} ";
if (isReply)
{
try
{
replyMessage = getMessage(chat.Id, msg.ReplyToMessageId);
finalOutput = $"{FormatMessageReply(replyMessage, prefix)}";
}
catch
{
//ignored; reply to deleted msg
}
}
var rest = $"{text}{(msg.EditDate == 0 ? "" : $"{Ansi.Yellow}*")}";
var lines = rest.Split("\n").ToList();
if (!isReply)
{
finalOutput += arrows + lines.First();
lines.RemoveAt(0);
}
lines.ForEach(l => finalOutput += "\n" + indent + arrows + l);
return finalOutput;
}
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;
var finalOutput = "";
var prefix = $"{origPrefix}{Ansi.Yellow}Re: {Ansi.Bold}{Ansi.Green}[{time}] {Ansi.Cyan}{chat.Title} " +
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}";
var indent = new string(' ', getActualStringWidth(prefix));
var arrows = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} ";
var rest = $"{text}{(msg.EditDate == 0 ? "" : $"{Ansi.Yellow}*")}";
finalOutput += prefix;
var lines = rest.Split("\n").ToList();
finalOutput += arrows + lines.First();
lines.RemoveAt(0);
lines.ForEach(l => finalOutput += "\n" + indent + arrows + l);
return finalOutput;
}
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;
return $"{Ansi.Bold}{Ansi.Green}[{time}] {Ansi.Cyan}{chat.Title} " +
$"{(isPrivate || isChannel ? "" : $"{Ansi.Yellow}{username} ")}" +
$"{(message.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} " +
$"{text}" +
$"{Ansi.Yellow}*";
}
public static void AddMessageToQueue(Td.TdApi.Message msg)
{
//handle muted
if (getChat(msg.ChatId).NotificationSettings.MuteFor > 0 && currentChatId != msg.ChatId)
return;
//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();
}
}
}