From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server-vie001.gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,URIBL_DBL_BLOCKED_OPENDNS, URIBL_ZEN_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=new2025; t=1765823008; bh=Rg5+9XS3wVOcjF5n4yzp8sIOSS1CVxnHUEbyFP9vXCI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Message-ID:Date:From: Reply-To:Subject:To:Cc:In-Reply-To:References:Resent-Date: Resent-From:Resent-To:Resent-Cc:User-Agent:Content-Type: Content-Transfer-Encoding; b=WiAA4Ro9vTUMpTd2C1by+hiuc7Zo57NAhg3sOfJcwCyzJdMqAHSlOiWTNVrIDYH5C V/yzCWUJF9EASqh/ek3W+xCXrPgFsjRZrYWVTMcht48e0BDebN92PoLpYkXyhevsnY ZK7v3O2qBtUOAcMRCQsS6NJKEw6tWfjym9oqfirqFEGpGBFN6VxwpbbBLcpGSe3ySj BGoLpwBaXu7ex76nQDSItM9NTXh9I7rKUN6e5Qf4qdiJjtVUzm55en2XXoHTD/gMHh H7kZjI68jdPCRIOW/TAUT2Uzii/bkgNPpWIKisDCVH7G197RTjaMCGMhZZazLCZ4/p 31W2g9RXCMUBg== Received: from kanazawa.tail3212ad.ts.net (unknown [125.165.188.218]) by server-vie001.gnuweeb.org (Postfix) with ESMTPSA id CAD1C3204D50; Mon, 15 Dec 2025 18:23:26 +0000 (UTC) From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v1 4/4] modules: create a `user` module Date: Tue, 16 Dec 2025 01:23:06 +0700 Message-ID: <20251215182308.48766-5-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251215182308.48766-1-kiizuha@gnuweeb.org> References: <20251215182308.48766-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This module provides a `/user` command that retrieves and displays information about the user who invoked the command, this uses `msg->from` field to get the user's information. Signed-off-by: Muhammad Rizki --- configure | 1 + modules/Makefile | 1 + modules/user/Makefile | 7 +++ modules/user/mod_info.h | 8 +++ modules/user/user.c | 112 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 modules/user/Makefile create mode 100644 modules/user/mod_info.h create mode 100644 modules/user/user.c diff --git a/configure b/configure index 2f93056..cd4f39e 100755 --- a/configure +++ b/configure @@ -317,6 +317,7 @@ if test "${use_cpp_thread}" = "yes"; then fi; add_config "CONFIG_MODULE_PING"; +add_config "CONFIG_MODULE_USER"; add_make_var "CC" "${cc}"; add_make_var "CXX" "${cxx}"; diff --git a/modules/Makefile b/modules/Makefile index 1cb9985..7cc2e77 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -7,6 +7,7 @@ OBJ_CC += \ GW_MODULE_LIST := include $(BASE_DIR)/modules/ping/Makefile +include $(BASE_DIR)/modules/user/Makefile $(BASE_DIR)/modules/module.o: $(BASE_DIR)/modules/module_list.h $(BASE_DIR)/modules/module.o: $(BASE_DIR)/modules/module_table.h diff --git a/modules/user/Makefile b/modules/user/Makefile new file mode 100644 index 0000000..0f0c61b --- /dev/null +++ b/modules/user/Makefile @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-only + +ifeq ($(CONFIG_MODULE_USER),y) +DEP_DIRS += $(BASE_DEP_DIR)/modules/user +GW_MODULE_LIST += user +OBJ_CC += $(BASE_DIR)/modules/user/user.o +endif diff --git a/modules/user/mod_info.h b/modules/user/mod_info.h new file mode 100644 index 0000000..e60a841 --- /dev/null +++ b/modules/user/mod_info.h @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 Muhammad Rizki + */ + +#include + +extern struct gw_bot_module gw_mod_info_user; diff --git a/modules/user/user.c b/modules/user/user.c new file mode 100644 index 0000000..4e4809e --- /dev/null +++ b/modules/user/user.c @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 Muhammad Rizki + */ + +#include "mod_info.h" +#include +#include +#include +#include +#include + +#define APPEND(fmt, ...) \ + snprintf(msg_buf + strlen(msg_buf), \ + sizeof(msg_buf) - strlen(msg_buf), \ + fmt, ##__VA_ARGS__) + +static int user_init(struct tg_bot_ctx *ctx) +{ + (void)ctx; + return 0; +} + +static int __user_handle(struct tg_bot_ctx *ctx, struct tg_message *msg) +{ + char msg_buf[1024] = "*User Info*\n```sh\n"; + + APPEND("ID : %" PRIu64 "\n", msg->from->id); + APPEND("First Name : %s\n", msg->from->first_name); + + if (msg->from->last_name) + APPEND("Last Name : %s\n", msg->from->last_name); + + if (msg->from->username) + APPEND("Username : %s\n", msg->from->username); + + if (msg->from->language_code) + APPEND("Language Code : %s\n", msg->from->language_code); + + APPEND("Is Bot : %s\n", + msg->from->is_bot ? "Yes" : "No"); + APPEND("Is Premium : %s\n", + msg->from->is_premium ? "Yes" : "No"); + APPEND("Can Join Groups : %s\n", + msg->from->can_join_groups ? "Yes" : "No"); + APPEND("Can Read All Groups : %s\n", + msg->from->can_read_all_group_messages ? "Yes" : "No"); + APPEND("Can Connect Business : %s\n", + msg->from->can_connect_to_business ? "Yes" : "No"); + APPEND("Supports Inline Query : %s\n", + msg->from->supports_inline_queries ? "Yes" : "No"); + APPEND("Added Attachment Menu : %s\n", + msg->from->added_to_attachment_menu ? "Yes" : "No"); + APPEND("Has Main Web App : %s\n", + msg->from->has_main_web_app ? "Yes" : "No"); + APPEND("```\n"); + + + const struct tga_call_send_message call = { + .chat_id = msg->chat->id, + .text = msg_buf, + .parse_mode = "MarkdownV2", + .disable_web_page_preview = true, + .disable_notification = true, + .reply_to_message_id = msg->message_id, + .reply_markup = NULL, + }; + + return tgapi_call_send_message(&ctx->tctx, &call); +} + +/* + * Commands: /user, .user, !user + * Description: Get user information who invoked the command. + */ +static int user_handle(struct tg_bot_ctx *ctx, struct tg_update *up) +{ + const char *text; + + assert(up->type & TG_UPDATE_MESSAGE); + + if (up->type != TG_UPDATE_MESSAGE) + return 0; + + if (up->message.type != TG_MSG_TEXT) + return 0; + + text = up->message.text; + if (!text) + return 0; + + if (!(text[0] == '/' || text[0] == '!' || text[0] == '.')) + return 0; + + if (!strcmp(&text[1], "user")) + return __user_handle(ctx, &up->message); + + return 0; +} + +static void user_shutdown(struct tg_bot_ctx *ctx) +{ + (void)ctx; +} + +struct gw_bot_module gw_mod_info_user = { + .name = "user", + .listen_update_types = TG_UPDATE_MESSAGE, + .init = user_init, + .handle = user_handle, + .shutdown = user_shutdown, +}; -- Muhammad Rizki