Skip to content

Commit

Permalink
updated forwarding logics, pmcenter now prefers enabling message link…
Browse files Browse the repository at this point in the history
…s by default
  • Loading branch information
Elepover committed Apr 27, 2020
1 parent 2e1e2cb commit 6d4952f
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 32 deletions.
16 changes: 12 additions & 4 deletions FILE_STRUCTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ File structure of pmcenter's source code:
|- pmcenter - Source code main directory
|- BotCommands - Every bot command's processing logic
| |- ...
|- BotProcess - Bot's message routing logics
| |- ...
|- CallbackActions - Every inline keyboard command's processing logic
| |- ...
|- CommandLines - Every commandline's processing logic
| |- ...
|- Configurations - Configurations' processing logics
| |- ...
|- Enums - pmcenter's self-defined enums
|- EventHandlers - Global error handler and Ctrl-C handler
| |- ...
|- Interfaces - pmcenter's self-defined interfaces
| |- ...
Expand All @@ -18,15 +22,19 @@ File structure of pmcenter's source code:
| | | |- ...
| | |- Writing - For writing things to database
| | | |- ...
| |- H2Helper - http/2 helper
| | |- ...
| |- Logging - Logging module
| | |- ...
| |- NetworkTest - Testing network quality, used by some commands
| | |- ...
| |- Threads - Threads' logics
| | |- ...
| |- UpdateHelper - pmcenter updates helper
| | |- ...
| |- ...
|- BotProcess.cs - Bot's master logic
|- CommandLineProcess.cs - Loading commandlines to memory
|- CommandLineRouter.cs - Commandlines' router
|- CommandLines.cs - Loading commandlines to memory
|- CommandManager.cs - Bot commands' router
|- Program.cs - Main entry of pmcenter
|- Setup.cs - Setup wizard's processing logic
|- Template.cs - As its name
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ Tip: After upgrades, you can send `/saveconf` command to the bot to fix missing
- **Emojis** are supported, and were used by default.
- Currently the response of the `/info` command is unchangeable.
- Familiar with another language? Pull requests are welcome!
- Please think twice before turning on `EnableMsgLink`, it makes it possible for you to reply to messages that are forwarded anonymously or from channels, however, it will cost more and more of your storage space and memory as the storage grows and makes it slower for pmcenter to process configuration files.
- ~~Please think twice before turning on `EnableMsgLink`, it makes it possible for you to reply to messages that are forwarded anonymously or from channels, however, it will cost more and more of your storage space and memory as the storage grows and makes it slower for pmcenter to process configuration files.~~
- Now Message Links play an important role in pmcenter's basic functions. Turning it off is NOT recommended.

#### Changing File Location

Expand Down
3 changes: 2 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ docker run -d -v $(pwd)/pmcenter.json:/opt/pmcenter/pmcenter.json --restart alwa
- 目前 `/info` 命令的回复尚且无法更改。
- 欢迎 Pull Requests.
- 切换中文语言包,只需发送 `/switchlang https://raw.githubusercontent.com/Elepover/pmcenter/master/locales/pmcenter_locale_zh.json`
- 在启用 `EnableMsgLink` 前请三思:虽然此功能允许您回复匿名转发消息及频道消息,但 pmcenter 的存储和内存占用将随消息量增长而增加,并将拖慢 pmcenter 操作配置文件时的速度。
- ~~在启用 `EnableMsgLink` 前请三思:虽然此功能允许您回复匿名转发消息及频道消息,但 pmcenter 的存储和内存占用将随消息量增长而增加,并将拖慢 pmcenter 操作配置文件时的速度。~~
- 现在消息链接在 pmcenter 正常功能中起着重要作用,我们不推荐将其禁用。

#### 改变文件位置

Expand Down
2 changes: 1 addition & 1 deletion pmcenter/BotCommands/InfoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public async Task<bool> ExecuteAsync(TelegramBotClient botClient, Update update)
}
else if (targetMessage.Dice != null)
{
sb.Append("\n🎲 Dice: `");
sb.Append("\n🎲 Dice/Dart: `");
sb.Append(targetMessage.Dice.Value);
sb.Append("`");
}
Expand Down
6 changes: 3 additions & 3 deletions pmcenter/BotProcess/BotProcess.OwnerReplying.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static async Task OwnerReplying(Update update)
Vars.CurrentConf.DisableNotifications,
update.Message.MessageId).ConfigureAwait(false);
// The message is forwarded anonymously
if ((update.Message.ReplyToMessage.ForwardFromChat?.Id == Vars.AnonymousChannelId) && !Vars.CurrentConf.DisableMessageLinkTip)
if (!string.IsNullOrEmpty(update.Message.ReplyToMessage.ForwardSenderName) && !Vars.CurrentConf.DisableMessageLinkTip)
{
_ = await Vars.Bot.SendTextMessageAsync(
update.Message.From.Id,
Expand Down Expand Up @@ -67,10 +67,10 @@ private static async Task OwnerReplying(Update update)
if (Vars.CurrentConf.EnableRepliedConfirmation)
{
var replyToMessage = Vars.CurrentLang.Message_ReplySuccessful;
replyToMessage = replyToMessage.Replace("$1", $"[{update.Message.ReplyToMessage.ForwardFrom.FirstName} (@{update.Message.ReplyToMessage.ForwardFrom.Username})](tg://user?id={update.Message.ReplyToMessage.ForwardFrom.Id})");
replyToMessage = replyToMessage.Replace("$1", $"[{Methods.GetComposedUsername(update.Message.ReplyToMessage.ForwardFrom)}](tg://user?id={update.Message.ReplyToMessage.ForwardFrom.Id})");
_ = await Vars.Bot.SendTextMessageAsync(update.Message.From.Id, replyToMessage, ParseMode.Markdown, false, false, update.Message.MessageId).ConfigureAwait(false);
}
Log($"Successfully passed owner's reply to {update.Message.ReplyToMessage.ForwardFrom.FirstName} (@{update.Message.ReplyToMessage.ForwardFrom.Username} / {update.Message.ReplyToMessage.ForwardFrom.Id})", "BOT");
Log($"Successfully passed owner's reply to {Methods.GetComposedUsername(update.Message.ReplyToMessage.ForwardFrom, true, true)}", "BOT");
}
}
}
32 changes: 14 additions & 18 deletions pmcenter/BotProcess/BotProcess.UserLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,29 @@ private static async Task UserLogic(Update update)
}
// check for real message sender
// check if forwarded from channels
bool forwarderNotReal = false;
if (update.Message.ForwardFrom == null && update.Message.ForwardFromChat != null)
{
// is forwarded from channel
forwarderNotReal = true;

if (update.Message.ForwardFrom != null && update.Message.ForwardFromChat == null)
// is forwarded from chats, but the forwarder is not the message sender
if (update.Message.ForwardFrom.Id != update.Message.From.Id)
forwarderNotReal = true;

if (!string.IsNullOrEmpty(update.Message.ForwardSenderName) || !string.IsNullOrEmpty(forwardedMessage.ForwardSenderName))
// is anonymously forwarded
forwarderNotReal = true;

if (forwarderNotReal)
_ = await Vars.Bot.SendTextMessageAsync(Vars.CurrentConf.OwnerUID,
Vars.CurrentLang.Message_ForwarderNotReal
.Replace("$2", update.Message.From.Id.ToString())
.Replace("$1", "[" + update.Message.From.FirstName + " " + update.Message.From.LastName + "](tg://user?id=" + update.Message.From.Id + ")"),
.Replace("$1", $"[{GetComposedUsername(update.Message.From)}](tg://user?id={update.Message.From.Id})"),
ParseMode.Markdown,
false,
Vars.CurrentConf.DisableNotifications,
forwardedMessage.MessageId).ConfigureAwait(false);
}
if (update.Message.ForwardFrom != null && update.Message.ForwardFromChat == null)
{
// is forwarded from chats
if (update.Message.ForwardFrom.Id != update.Message.From.Id)
{
_ = await Vars.Bot.SendTextMessageAsync(Vars.CurrentConf.OwnerUID,
Vars.CurrentLang.Message_ForwarderNotReal
.Replace("$2", update.Message.From.Id.ToString())
.Replace("$1", "[" + update.Message.From.FirstName + " " + update.Message.From.LastName + "](tg://user?id=" + update.Message.From.Id + ")"),
ParseMode.Markdown,
false,
Vars.CurrentConf.DisableNotifications,
forwardedMessage.MessageId).ConfigureAwait(false);
}
}

// process cc
if (Vars.CurrentConf.EnableCc)
Expand Down
2 changes: 1 addition & 1 deletion pmcenter/Configurations/Conf.ConfObj.New.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ConfObj()
CatchAllExceptions = false;
NoStartupMessage = false;
ContChatTarget = -1;
EnableMsgLink = false;
EnableMsgLink = true;
AllowUserRetraction = false;
ConfSyncInterval = 30000;
AdvancedLogging = false;
Expand Down
4 changes: 2 additions & 2 deletions pmcenter/EventHandlers/CtrlCHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace pmcenter
{
public static partial class EventHandlers
{
public static void CtrlCHandler(object sender, ConsoleCancelEventArgs e)
public static async void CtrlCHandler(object sender, ConsoleCancelEventArgs e)
{
Vars.CtrlCCounter++;
if (Vars.CtrlCCounter > 3)
Expand All @@ -24,7 +24,7 @@ public static void CtrlCHandler(object sender, ConsoleCancelEventArgs e)
Vars.IsCtrlCHandled = true;
Log("Interrupt! pmcenter is exiting...", LogLevel.Warning);
Log("If pmcenter was unresponsive, press Ctrl-C 3 more times.");
ExitApp(130);
await ExitApp(130);
}
}
}
5 changes: 4 additions & 1 deletion pmcenter/Methods/Methods.ExitApp.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using static pmcenter.Methods.Logging;

namespace pmcenter
{
public static partial class Methods
{
public static void ExitApp(int code)
public static async Task ExitApp(int code)
{
Log("Attempting to exit gracefully...");
Log("Stopping bot message receiving...");
Expand All @@ -17,6 +18,8 @@ public static void ExitApp(int code)
sw.Start();

Vars.IsShuttingDown = true;
Log("Saving configurations...");
await Conf.SaveConf();
if (Vars.IsPerformanceTestExecuting)
{
while (!Vars.IsPerformanceTestExecuting)
Expand Down

0 comments on commit 6d4952f

Please sign in to comment.