summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2025-07-22 08:23:22 +0300
committerGravatar Uko Kokņevičs2025-07-22 08:23:22 +0300
commit1d1df7b540721d58db174764a0f720e3c638a67b (patch)
tree44bbfc972dac04e32729a3065c90dea0334149d8 /src/main.zig
parentreplace trimming with just checking if the string is whitespace (diff)
downloadukkobot-1d1df7b540721d58db174764a0f720e3c638a67b.tar.gz
ukkobot-1d1df7b540721d58db174764a0f720e3c638a67b.tar.xz
ukkobot-1d1df7b540721d58db174764a0f720e3c638a67b.zip
Whitelist allowed inline bots
Also update to Zig 0.14.1 Also don't send the animations rn
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig120
1 files changed, 106 insertions, 14 deletions
diff --git a/src/main.zig b/src/main.zig
index b164284..9e1de47 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -46,6 +46,27 @@ fn loadConfig(allocator: Allocator, filename: []const u8) !std.json.Parsed(Confi
46 ); 46 );
47} 47}
48 48
49fn reportError(bot: *Bot, msg: types.Message, err: anyerror) !void {
50 std.log.err("While handling {}: {}", .{ msg, err });
51 const msgStr = try std.json.stringifyAlloc(bot.allocator, msg, .{
52 .whitespace = .indent_2,
53 .emit_null_optional_fields = false,
54 });
55 defer bot.allocator.free(msgStr);
56
57 const devMsg = try std.fmt.allocPrint(bot.allocator, "<code>{}</code> while handling\n<pre>{s}</pre>", .{ err, msgStr });
58 defer bot.allocator.free(devMsg);
59
60 bot.sendMessage_(.{
61 .chat_id = bot.config.dev_group,
62 .text = devMsg,
63 .parse_mode = .html,
64 }) catch |err2| {
65 std.log.err("While trying to report the error: {}", .{err2});
66 return err2;
67 };
68}
69
49fn wrappedMain(bot: *Bot) !void { 70fn wrappedMain(bot: *Bot) !void {
50 try bot.sendMessage_(.{ 71 try bot.sendMessage_(.{
51 .chat_id = bot.config.dev_group, 72 .chat_id = bot.config.dev_group,
@@ -63,8 +84,9 @@ fn wrappedMain(bot: *Bot) !void {
63 defer gup.offset = update.update_id + 1; 84 defer gup.offset = update.update_id + 1;
64 85
65 if (update.message) |message| { 86 if (update.message) |message| {
66 // TODO: Catch minor errors, report them 87 onMessage(bot, message) catch |err| {
67 try onMessage(bot, message); 88 try reportError(bot, message, err);
89 };
68 } 90 }
69 } 91 }
70 } 92 }
@@ -80,7 +102,63 @@ fn wrappedMain(bot: *Bot) !void {
80 }); 102 });
81} 103}
82 104
105const allowed_inline_bots = [_]i64{
106 90832338, // @vid
107 109158646, // @bing
108 114528005, // @pic
109 140267078, // @gif
110 154595593, // @wiki
111 184730458, // @UnitConversionBot
112 296635833, // @lastfmrobot
113 595898211, // @DeezerMusicBot
114 870410041, // @HowGayBot
115 7904498194, // @tanstiktokbot
116};
117
118comptime {
119 std.testing.expect(std.sort.isSorted(
120 i64,
121 &allowed_inline_bots,
122 {},
123 std.sort.asc(i64),
124 )) catch unreachable;
125}
126
127fn orderI64(a: i64, b: i64) std.math.Order {
128 return std.math.order(a, b);
129}
130
131fn isAllowedInlineBot(id: i64) bool {
132 return std.sort.binarySearch(
133 i64,
134 &allowed_inline_bots,
135 id,
136 orderI64,
137 ) != null;
138}
139
83fn onMessage(bot: *Bot, msg: types.Message) !void { 140fn onMessage(bot: *Bot, msg: types.Message) !void {
141 if (msg.via_bot) |via| {
142 if (!isAllowedInlineBot(via.id)) {
143 std.log.info("Deleting an unallowed inline bot message from {?s} {}", .{ via.username, via.id });
144 try bot.deleteMessage(.{
145 .chat_id = msg.chat.id,
146 .message_id = msg.message_id,
147 });
148
149 const text = try std.fmt.allocPrint(bot.allocator, "Deleted a message sent by inline bot @{?s} <code>{}</code>", .{ via.username, via.id });
150 defer bot.allocator.free(text);
151
152 try bot.sendMessage_(.{
153 .chat_id = bot.config.dev_group,
154 .text = text,
155 .parse_mode = .html,
156 });
157
158 return;
159 }
160 }
161
84 if (msg.text) |text| { 162 if (msg.text) |text| {
85 try onTextMessage(bot, msg, text); 163 try onTextMessage(bot, msg, text);
86 } 164 }
@@ -94,12 +172,14 @@ fn onMessage(bot: *Bot, msg: types.Message) !void {
94 172
95fn onNewMember(bot: *Bot, msg: types.Message, new_member: types.User) !void { 173fn onNewMember(bot: *Bot, msg: types.Message, new_member: types.User) !void {
96 if (new_member.id == try bot.getId()) { 174 if (new_member.id == try bot.getId()) {
175 return;
176 // TODO:
97 // Bot is added to a new group 177 // Bot is added to a new group
98 return bot.sendAnimation_(.{ 178 // return bot.sendAnimation_(.{
99 .chat_id = msg.chat.id, 179 // .chat_id = msg.chat.id,
100 // TODO: lol 180 // // TODO: lol
101 .animation = "CgACAgQAAx0CVcPEEgACDC5mo7YHMgOE2n3qo3e9UOyd4N-uxQACNAMAAlbuDFMRWj9LxNLBkDUE", 181 // .animation = "CgACAgQAAx0CVcPEEgACDC5mo7YHMgOE2n3qo3e9UOyd4N-uxQACNAMAAlbuDFMRWj9LxNLBkDUE",
102 }); 182 // });
103 } 183 }
104 184
105 var sb = ArrayList(u8).init(bot.allocator); 185 var sb = ArrayList(u8).init(bot.allocator);
@@ -111,13 +191,25 @@ fn onNewMember(bot: *Bot, msg: types.Message, new_member: types.User) !void {
111 try new_member.writeFormattedName(w); 191 try new_member.writeFormattedName(w);
112 try w.writeAll("! Be on your bestest behaviour now!!"); 192 try w.writeAll("! Be on your bestest behaviour now!!");
113 193
114 try bot.sendAnimation_(.{ 194 // TODO:
195 // try bot.sendAnimation_(.{
196 // .chat_id = msg.chat.id,
197 // // TODO: lol
198 // .animation = "CgACAgQAAx0CVcPEEgACC9Vmo6_zCxMp3ZNXSMM1nI6nMkIhgwACNwMAAtDmDFMop6BHmV7lUTUE",
199 // .caption = sb.items,
200 // .parse_mode = .html,
201 // .show_caption_above_media = true,
202 // .reply_parameters = .{
203 // .allow_sending_without_reply = true,
204 // .message_id = msg.message_id,
205 // .chat_id = msg.chat.id,
206 // },
207 // });
208
209 try bot.sendMessage_(.{
115 .chat_id = msg.chat.id, 210 .chat_id = msg.chat.id,
116 // TODO: lol 211 .text = sb.items,
117 .animation = "CgACAgQAAx0CVcPEEgACC9Vmo6_zCxMp3ZNXSMM1nI6nMkIhgwACNwMAAtDmDFMop6BHmV7lUTUE",
118 .caption = sb.items,
119 .parse_mode = .html, 212 .parse_mode = .html,
120 .show_caption_above_media = true,
121 .reply_parameters = .{ 213 .reply_parameters = .{
122 .allow_sending_without_reply = true, 214 .allow_sending_without_reply = true,
123 .message_id = msg.message_id, 215 .message_id = msg.message_id,
@@ -201,8 +293,8 @@ fn onTextMessage(bot: *Bot, msg: types.Message, text: []const u8) !void {
201 } else if (std.ascii.startsWithIgnoreCase(text, "big ")) { 293 } else if (std.ascii.startsWithIgnoreCase(text, "big ")) {
202 const the_text = text[4..]; 294 const the_text = text[4..];
203 if (!try utils.isTgWhitespaceStr(the_text)) { 295 if (!try utils.isTgWhitespaceStr(the_text)) {
204 const cd = try utils.getCD(); 296 const lc = try utils.getLetterCasing();
205 const uppercased = try cd.toUpperStr(bot.allocator, the_text); 297 const uppercased = try lc.toUpperStr(bot.allocator, the_text);
206 defer bot.allocator.free(uppercased); 298 defer bot.allocator.free(uppercased);
207 299
208 var output = ArrayList(u8).init(bot.allocator); 300 var output = ArrayList(u8).init(bot.allocator);