From 0a0cff51b27ce35c4fa5d8fd56f60d2b7a89c90a Mon Sep 17 00:00:00 2001
From: Uko Kokņevičs
Date: Sun, 3 Aug 2025 03:23:26 +0300
Subject: Moved inline bot handling to a new file
---
src/inline_bots.zig | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++
src/main.zig | 60 +++-----------------------------------------
src/utils.zig | 14 +++++++++++
3 files changed, 88 insertions(+), 57 deletions(-)
create mode 100644 src/inline_bots.zig
(limited to 'src')
diff --git a/src/inline_bots.zig b/src/inline_bots.zig
new file mode 100644
index 0000000..c6fa2b7
--- /dev/null
+++ b/src/inline_bots.zig
@@ -0,0 +1,71 @@
+const std = @import("std");
+const types = @import("types.zig");
+const utils = @import("utils.zig");
+
+const Bot = @import("Bot.zig");
+
+const whitelist = [_]i64{
+ 90832338, // @vid
+ 109158646, // @bing
+ 114528005, // @pic
+ 136269978, // @ImageFetcherBot
+ 140267078, // @gif
+ 154595593, // @wiki
+ 184730458, // @UnitConversionBot
+ 223493268, // @minroobot
+ 296635833, // @lastfmrobot
+ 473587803, // @LyBot
+ 595898211, // @DeezerMusicBot
+ 733460033, // @crabravebot
+ 870410041, // @HowGayBot
+ 7904498194, // @tanstiktokbot
+};
+
+const blacklist = [_]i64{
+ 6465471545, // @DickGrowerBot
+ 7759097490, // @CookieGrowerBot
+};
+
+comptime {
+ std.testing.expect(utils.isSorted(i64, &whitelist)) catch unreachable;
+ std.testing.expect(utils.isSorted(i64, &blacklist)) catch unreachable;
+}
+
+inline fn isWhitelisted(bot: types.User) bool {
+ return utils.isIn(i64, bot.id, &whitelist);
+}
+
+inline fn isBlacklisted(bot: types.User) bool {
+ return utils.isIn(i64, bot.id, &blacklist);
+}
+
+// Returns true if processing of message should continue
+pub fn onInlineBot(bot: *Bot, msg: types.Message, via: types.User) !bool {
+ if (isWhitelisted(via)) {
+ return true;
+ }
+
+ std.log.info("Deleting an unallowed inline bot message from {?s} {}", .{ via.username, via.id });
+ try bot.deleteMessage(.{
+ .chat_id = msg.chat.id,
+ .message_id = msg.message_id,
+ });
+
+ if (!isBlacklisted(via)) {
+ // Not explicitly blacklisted, notify dev group
+ const text = try std.fmt.allocPrint(
+ bot.allocator,
+ "Deleted a message sent via inline bot @{?s} {}",
+ .{ via.username, via.id },
+ );
+ defer bot.allocator.free(text);
+
+ try bot.sendMessage_(.{
+ .chat_id = bot.config.dev_group,
+ .text = text,
+ .parse_mode = .html,
+ });
+ }
+
+ return false;
+}
diff --git a/src/main.zig b/src/main.zig
index d534296..942fd90 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -8,6 +8,8 @@ const Bot = @import("Bot.zig");
const Config = @import("Config.zig");
const GPA = std.heap.GeneralPurposeAllocator(.{});
+const onInlineBot = @import("inline_bots.zig").onInlineBot;
+
pub fn main() !void {
defer std.log.info("We're done", .{});
@@ -102,65 +104,9 @@ fn wrappedMain(bot: *Bot) !void {
});
}
-const allowed_inline_bots = [_]i64{
- 90832338, // @vid
- 109158646, // @bing
- 114528005, // @pic
- 136269978, // @ImageFetcherBot
- 140267078, // @gif
- 154595593, // @wiki
- 184730458, // @UnitConversionBot
- 223493268, // @minroobot
- 296635833, // @lastfmrobot
- 473587803, // @LyBot
- 595898211, // @DeezerMusicBot
- 733460033, // @crabravebot
- 870410041, // @HowGayBot
- 7904498194, // @tanstiktokbot
-};
-
-const disallowed_inline_bots = [_]i64{
- 6465471545, // @DickGrowerBot
- 7759097490, // @CookieGrowerBot
-};
-
-inline fn isSorted(arr: []const i64) bool {
- return std.sort.isSorted(i64, arr, {}, std.sort.asc(i64));
-}
-
-comptime {
- std.testing.expect(isSorted(&allowed_inline_bots)) catch unreachable;
- std.testing.expect(isSorted(&disallowed_inline_bots)) catch unreachable;
-}
-
-fn orderI64(a: i64, b: i64) std.math.Order {
- return std.math.order(a, b);
-}
-
-inline fn isIn(val: i64, arr: []const i64) bool {
- return std.sort.binarySearch(i64, arr, val, orderI64) != null;
-}
-
fn onMessage(bot: *Bot, msg: types.Message) !void {
if (msg.via_bot) |via| {
- if (!isIn(via.id, &allowed_inline_bots)) {
- std.log.info("Deleting an unallowed inline bot message from {?s} {}", .{ via.username, via.id });
- try bot.deleteMessage(.{
- .chat_id = msg.chat.id,
- .message_id = msg.message_id,
- });
-
- if (!isIn(via.id, &disallowed_inline_bots)) {
- // Notify dev group, perhaps this bot should be allowed?
- const text = try std.fmt.allocPrint(bot.allocator, "Deleted a message sent by inline bot @{?s} {}", .{ via.username, via.id });
- defer bot.allocator.free(text);
-
- try bot.sendMessage_(.{
- .chat_id = bot.config.dev_group,
- .text = text,
- .parse_mode = .html,
- });
- }
+ if (!try onInlineBot(bot, msg, via)) {
return;
}
}
diff --git a/src/utils.zig b/src/utils.zig
index b5d9f19..f771337 100644
--- a/src/utils.zig
+++ b/src/utils.zig
@@ -38,6 +38,16 @@ pub fn getLetterCasing() !LetterCasing {
return lc_global.?;
}
+// arr should be sorted as according to isSorted.
+pub inline fn isIn(comptime T: type, val: T, arr: []const T) bool {
+ const order = struct {
+ pub fn f(a: T, b: T) std.math.Order {
+ return std.math.order(a, b);
+ }
+ }.f;
+ return std.sort.binarySearch(T, arr, val, order) != null;
+}
+
pub inline fn isNull(value: anytype) bool {
return switch (@typeInfo(@TypeOf(value))) {
.null => true,
@@ -46,6 +56,10 @@ pub inline fn isNull(value: anytype) bool {
};
}
+pub inline fn isSorted(comptime T: type, arr: []const T) bool {
+ return std.sort.isSorted(T, arr, {}, std.sort.asc(T));
+}
+
pub fn isTgWhitespaceStr(str: []const u8) !bool {
const view = try Utf8View.init(str);
const gc = try getGC();
--
cgit v1.2.3