diff options
| author | 2025-08-03 03:23:26 +0300 | |
|---|---|---|
| committer | 2025-08-03 03:23:26 +0300 | |
| commit | 0a0cff51b27ce35c4fa5d8fd56f60d2b7a89c90a (patch) | |
| tree | 6724a6204584153044301489a6dae364886c7867 /src | |
| parent | Add @crabravebot to the list of allowed inline bots (diff) | |
| download | ukkobot-0a0cff51b27ce35c4fa5d8fd56f60d2b7a89c90a.tar.gz ukkobot-0a0cff51b27ce35c4fa5d8fd56f60d2b7a89c90a.tar.xz ukkobot-0a0cff51b27ce35c4fa5d8fd56f60d2b7a89c90a.zip | |
Moved inline bot handling to a new file
Diffstat (limited to 'src')
| -rw-r--r-- | src/inline_bots.zig | 71 | ||||
| -rw-r--r-- | src/main.zig | 60 | ||||
| -rw-r--r-- | src/utils.zig | 14 |
3 files changed, 88 insertions, 57 deletions
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 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const types = @import("types.zig"); | ||
| 3 | const utils = @import("utils.zig"); | ||
| 4 | |||
| 5 | const Bot = @import("Bot.zig"); | ||
| 6 | |||
| 7 | const whitelist = [_]i64{ | ||
| 8 | 90832338, // @vid | ||
| 9 | 109158646, // @bing | ||
| 10 | 114528005, // @pic | ||
| 11 | 136269978, // @ImageFetcherBot | ||
| 12 | 140267078, // @gif | ||
| 13 | 154595593, // @wiki | ||
| 14 | 184730458, // @UnitConversionBot | ||
| 15 | 223493268, // @minroobot | ||
| 16 | 296635833, // @lastfmrobot | ||
| 17 | 473587803, // @LyBot | ||
| 18 | 595898211, // @DeezerMusicBot | ||
| 19 | 733460033, // @crabravebot | ||
| 20 | 870410041, // @HowGayBot | ||
| 21 | 7904498194, // @tanstiktokbot | ||
| 22 | }; | ||
| 23 | |||
| 24 | const blacklist = [_]i64{ | ||
| 25 | 6465471545, // @DickGrowerBot | ||
| 26 | 7759097490, // @CookieGrowerBot | ||
| 27 | }; | ||
| 28 | |||
| 29 | comptime { | ||
| 30 | std.testing.expect(utils.isSorted(i64, &whitelist)) catch unreachable; | ||
| 31 | std.testing.expect(utils.isSorted(i64, &blacklist)) catch unreachable; | ||
| 32 | } | ||
| 33 | |||
| 34 | inline fn isWhitelisted(bot: types.User) bool { | ||
| 35 | return utils.isIn(i64, bot.id, &whitelist); | ||
| 36 | } | ||
| 37 | |||
| 38 | inline fn isBlacklisted(bot: types.User) bool { | ||
| 39 | return utils.isIn(i64, bot.id, &blacklist); | ||
| 40 | } | ||
| 41 | |||
| 42 | // Returns true if processing of message should continue | ||
| 43 | pub fn onInlineBot(bot: *Bot, msg: types.Message, via: types.User) !bool { | ||
| 44 | if (isWhitelisted(via)) { | ||
| 45 | return true; | ||
| 46 | } | ||
| 47 | |||
| 48 | std.log.info("Deleting an unallowed inline bot message from {?s} {}", .{ via.username, via.id }); | ||
| 49 | try bot.deleteMessage(.{ | ||
| 50 | .chat_id = msg.chat.id, | ||
| 51 | .message_id = msg.message_id, | ||
| 52 | }); | ||
| 53 | |||
| 54 | if (!isBlacklisted(via)) { | ||
| 55 | // Not explicitly blacklisted, notify dev group | ||
| 56 | const text = try std.fmt.allocPrint( | ||
| 57 | bot.allocator, | ||
| 58 | "Deleted a message sent via inline bot @{?s} <code>{}</code>", | ||
| 59 | .{ via.username, via.id }, | ||
| 60 | ); | ||
| 61 | defer bot.allocator.free(text); | ||
| 62 | |||
| 63 | try bot.sendMessage_(.{ | ||
| 64 | .chat_id = bot.config.dev_group, | ||
| 65 | .text = text, | ||
| 66 | .parse_mode = .html, | ||
| 67 | }); | ||
| 68 | } | ||
| 69 | |||
| 70 | return false; | ||
| 71 | } | ||
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"); | |||
| 8 | const Config = @import("Config.zig"); | 8 | const Config = @import("Config.zig"); |
| 9 | const GPA = std.heap.GeneralPurposeAllocator(.{}); | 9 | const GPA = std.heap.GeneralPurposeAllocator(.{}); |
| 10 | 10 | ||
| 11 | const onInlineBot = @import("inline_bots.zig").onInlineBot; | ||
| 12 | |||
| 11 | pub fn main() !void { | 13 | pub fn main() !void { |
| 12 | defer std.log.info("We're done", .{}); | 14 | defer std.log.info("We're done", .{}); |
| 13 | 15 | ||
| @@ -102,65 +104,9 @@ fn wrappedMain(bot: *Bot) !void { | |||
| 102 | }); | 104 | }); |
| 103 | } | 105 | } |
| 104 | 106 | ||
| 105 | const allowed_inline_bots = [_]i64{ | ||
| 106 | 90832338, // @vid | ||
| 107 | 109158646, // @bing | ||
| 108 | 114528005, // @pic | ||
| 109 | 136269978, // @ImageFetcherBot | ||
| 110 | 140267078, // @gif | ||
| 111 | 154595593, // @wiki | ||
| 112 | 184730458, // @UnitConversionBot | ||
| 113 | 223493268, // @minroobot | ||
| 114 | 296635833, // @lastfmrobot | ||
| 115 | 473587803, // @LyBot | ||
| 116 | 595898211, // @DeezerMusicBot | ||
| 117 | 733460033, // @crabravebot | ||
| 118 | 870410041, // @HowGayBot | ||
| 119 | 7904498194, // @tanstiktokbot | ||
| 120 | }; | ||
| 121 | |||
| 122 | const disallowed_inline_bots = [_]i64{ | ||
| 123 | 6465471545, // @DickGrowerBot | ||
| 124 | 7759097490, // @CookieGrowerBot | ||
| 125 | }; | ||
| 126 | |||
| 127 | inline fn isSorted(arr: []const i64) bool { | ||
| 128 | return std.sort.isSorted(i64, arr, {}, std.sort.asc(i64)); | ||
| 129 | } | ||
| 130 | |||
| 131 | comptime { | ||
| 132 | std.testing.expect(isSorted(&allowed_inline_bots)) catch unreachable; | ||
| 133 | std.testing.expect(isSorted(&disallowed_inline_bots)) catch unreachable; | ||
| 134 | } | ||
| 135 | |||
| 136 | fn orderI64(a: i64, b: i64) std.math.Order { | ||
| 137 | return std.math.order(a, b); | ||
| 138 | } | ||
| 139 | |||
| 140 | inline fn isIn(val: i64, arr: []const i64) bool { | ||
| 141 | return std.sort.binarySearch(i64, arr, val, orderI64) != null; | ||
| 142 | } | ||
| 143 | |||
| 144 | fn onMessage(bot: *Bot, msg: types.Message) !void { | 107 | fn onMessage(bot: *Bot, msg: types.Message) !void { |
| 145 | if (msg.via_bot) |via| { | 108 | if (msg.via_bot) |via| { |
| 146 | if (!isIn(via.id, &allowed_inline_bots)) { | 109 | if (!try onInlineBot(bot, msg, via)) { |
| 147 | std.log.info("Deleting an unallowed inline bot message from {?s} {}", .{ via.username, via.id }); | ||
| 148 | try bot.deleteMessage(.{ | ||
| 149 | .chat_id = msg.chat.id, | ||
| 150 | .message_id = msg.message_id, | ||
| 151 | }); | ||
| 152 | |||
| 153 | if (!isIn(via.id, &disallowed_inline_bots)) { | ||
| 154 | // Notify dev group, perhaps this bot should be allowed? | ||
| 155 | const text = try std.fmt.allocPrint(bot.allocator, "Deleted a message sent by inline bot @{?s} <code>{}</code>", .{ via.username, via.id }); | ||
| 156 | defer bot.allocator.free(text); | ||
| 157 | |||
| 158 | try bot.sendMessage_(.{ | ||
| 159 | .chat_id = bot.config.dev_group, | ||
| 160 | .text = text, | ||
| 161 | .parse_mode = .html, | ||
| 162 | }); | ||
| 163 | } | ||
| 164 | return; | 110 | return; |
| 165 | } | 111 | } |
| 166 | } | 112 | } |
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 { | |||
| 38 | return lc_global.?; | 38 | return lc_global.?; |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | // arr should be sorted as according to isSorted. | ||
| 42 | pub inline fn isIn(comptime T: type, val: T, arr: []const T) bool { | ||
| 43 | const order = struct { | ||
| 44 | pub fn f(a: T, b: T) std.math.Order { | ||
| 45 | return std.math.order(a, b); | ||
| 46 | } | ||
| 47 | }.f; | ||
| 48 | return std.sort.binarySearch(T, arr, val, order) != null; | ||
| 49 | } | ||
| 50 | |||
| 41 | pub inline fn isNull(value: anytype) bool { | 51 | pub inline fn isNull(value: anytype) bool { |
| 42 | return switch (@typeInfo(@TypeOf(value))) { | 52 | return switch (@typeInfo(@TypeOf(value))) { |
| 43 | .null => true, | 53 | .null => true, |
| @@ -46,6 +56,10 @@ pub inline fn isNull(value: anytype) bool { | |||
| 46 | }; | 56 | }; |
| 47 | } | 57 | } |
| 48 | 58 | ||
| 59 | pub inline fn isSorted(comptime T: type, arr: []const T) bool { | ||
| 60 | return std.sort.isSorted(T, arr, {}, std.sort.asc(T)); | ||
| 61 | } | ||
| 62 | |||
| 49 | pub fn isTgWhitespaceStr(str: []const u8) !bool { | 63 | pub fn isTgWhitespaceStr(str: []const u8) !bool { |
| 50 | const view = try Utf8View.init(str); | 64 | const view = try Utf8View.init(str); |
| 51 | const gc = try getGC(); | 65 | const gc = try getGC(); |