fix replies, better emoji handling, add connection state checks

This commit is contained in:
Laura Hausmann 2019-12-11 13:17:35 +01:00
parent 1f2e39f1a0
commit 80d8f8ebb1
Signed by: zotan
GPG key ID: 5EC1D38FFC321311
4 changed files with 104 additions and 23 deletions

View file

@ -2,9 +2,9 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="36e5cfaf-0aa9-4137-8110-a5678a9c5443" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/.idea.tgcli/.idea/contentModel.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.tgcli/.idea/contentModel.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.tgcli/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.tgcli/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/telegram/Util.cs" beforeDir="false" afterPath="$PROJECT_DIR$/telegram/Util.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/telegram/telegram.csproj" beforeDir="false" afterPath="$PROJECT_DIR$/telegram/telegram.csproj" afterDir="false" />
<change beforePath="$PROJECT_DIR$/telegram/tgcli.cs" beforeDir="false" afterPath="$PROJECT_DIR$/telegram/tgcli.cs" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -122,7 +122,7 @@
<workItem from="1575536428992" duration="12000" />
<workItem from="1575536474314" duration="39215000" />
<workItem from="1575724030612" duration="6335000" />
<workItem from="1575806456895" duration="30336000" />
<workItem from="1575806456895" duration="35215000" />
</task>
<servers />
</component>

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NeoSmart.Unicode;
using static TdLib.TdApi;
using static telegram.tgcli;
@ -143,10 +144,11 @@ namespace telegram
if (string.IsNullOrWhiteSpace(message))
return;
message = message.Replace("⏎", "\n")
.Replace(":xd:", "😂")
.Replace(":shrug:", "🤷")
.Replace(":shrugf:", "🤷‍♀️")
.Replace(":shrugm:", "🤷‍♂️");
.Replace(":xd:", Emoji.FaceWithTearsOfJoy.Sequence.AsString)
.Replace(":heart:", Emoji.RedHeart.Sequence.AsString)
.Replace(":shrug:", Emoji.PersonShrugging.Sequence.AsString)
.Replace(":shrugf:", Emoji.WomanShrugging.Sequence.AsString)
.Replace(":shrugm:", Emoji.ManShrugging.Sequence.AsString);
client.ExecuteAsync(new SendMessage
{
ChatId = chatId,

View file

@ -18,4 +18,8 @@
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Unicode.net" Version="0.1.2" />
</ItemGroup>
</Project>

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
@ -31,15 +32,17 @@ namespace telegram
* 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
* error message if network/connection state changed to offline
* edited msgs -> cache all messages by id (oof) and print old message? // NO DONT :(
* maybe cursor input nav (cmd+del, left/right, up for last inputs, etc)
* NOPE //edited msgs -> cache all messages by id (oof) and print old message?
*/
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;
@ -247,6 +250,41 @@ namespace telegram
}, ctsTyping.Token);
typingTask.Start();
break;
case Td.TdApi.Update.UpdateConnectionState state :
switch (state.State)
{
case Td.TdApi.ConnectionState.ConnectionStateConnecting _:
connectionState = "Connecting";
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connecting to Telegram servers...");
ScreenUpdate();
break;
case Td.TdApi.ConnectionState.ConnectionStateConnectingToProxy _:
connectionState = "Connecting";
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connecting to Proxy...");
ScreenUpdate();
break;
case Td.TdApi.ConnectionState.ConnectionStateReady _:
connectionState = "Ready";
messageQueue.Add($"{Ansi.Yellow}[tgcli] Connected.");
Task.Run(() =>
{
Command.ParseCommand("u");
ScreenUpdate();
});
ScreenUpdate();
break;
case Td.TdApi.ConnectionState.ConnectionStateUpdating _:
connectionState = "Updating";
messageQueue.Add($"{Ansi.Yellow}[tgcli] Updating message cache...");
ScreenUpdate();
break;
case Td.TdApi.ConnectionState.ConnectionStateWaitingForNetwork _:
connectionState = "Waiting for Network";
messageQueue.Add($"{Ansi.Yellow}[tgcli] Lost connection. Waiting for network...");
ScreenUpdate();
break;
}
break;
}
}
@ -260,7 +298,9 @@ namespace telegram
Console.Write("\a"); //ring terminal bell
messageQueue.Clear();
var status = getFormattedStatus(currentUserOnline, currentUserRead, currentUserTyping);
Console.Write(prefix + (currentChatUserId != 0 ? status : "]") + " > " + currentInputLine);
Console.Write(prefix
+ (connectionState == "Ready" ? "" : $" ({connectionState})")
+ (currentChatUserId != 0 ? status : "]") + " > " + currentInputLine);
}
}
@ -318,6 +358,12 @@ namespace telegram
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;
@ -394,7 +440,7 @@ namespace telegram
}
}
public static string FormatMessage(Td.TdApi.Message msg, bool cascade = true)
public static string FormatMessage(Td.TdApi.Message msg)
{
string text;
if (msg.Content is Td.TdApi.MessageContent.MessageText messageText)
@ -411,20 +457,19 @@ namespace telegram
Td.TdApi.Message replyMessage;
var replyText = "";
var finalOutput = "";
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 furtherIndent = new string(' ', getActualStringWidth("»»» "));
var arrows = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} ";
if (isReply && cascade)
if (isReply)
{
try
{
replyMessage = getMessage(chat.Id, msg.ReplyToMessageId);
replyText = $"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} " +
$"{Ansi.Yellow}Re: {FormatMessage(replyMessage, false)}\n";
finalOutput = $"{FormatMessageReply(replyMessage, prefix)}";
}
catch
{
@ -432,17 +477,11 @@ namespace telegram
}
}
var rest = $"{replyText}" +
$"{(msg.IsOutgoing ? $"{Ansi.Blue}»»»" : $"{Ansi.Magenta}«««")} " +
$"{text}{(msg.EditDate == 0 ? "" : $"{Ansi.Yellow}*")}";
finalOutput += prefix;
var rest = $"{text}{(msg.EditDate == 0 ? "" : $"{Ansi.Yellow}*")}";
var lines = rest.Split("\n").ToList();
finalOutput += lines.First();
lines.RemoveAt(0);
if (isReply && lines.Count > 0)
if (!isReply)
{
finalOutput += "\n" + indent + lines.First();
finalOutput += arrows + lines.First();
lines.RemoveAt(0);
}
lines.ForEach(l => finalOutput += "\n" + indent + arrows + l);
@ -450,6 +489,38 @@ namespace telegram
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;
@ -478,6 +549,10 @@ namespace telegram
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)