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 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/inline_bots.zig (limited to 'src/inline_bots.zig') 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; +} -- cgit v1.2.3