From f99085fc03246eceb8a4bb66950270b88d65a504 Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Sun, 19 Jun 2022 10:27:16 +0200 Subject: [PATCH] RPC: add user.get() --- src/modules/rpc/user.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/modules/rpc/user.c b/src/modules/rpc/user.c index 8548f6100..4aa4e62fb 100644 --- a/src/modules/rpc/user.c +++ b/src/modules/rpc/user.c @@ -16,6 +16,7 @@ ModuleHeader MOD_HEADER /* Forward declarations */ void rpc_user_list(Client *client, json_t *request, json_t *params); +void rpc_user_get(Client *client, json_t *request, json_t *params); MOD_INIT() { @@ -31,6 +32,14 @@ MOD_INIT() config_error("[rpc/user] Could not register RPC handler"); return MOD_FAILED; } + memset(&r, 0, sizeof(r)); + r.method = "user.get"; + r.call = rpc_user_get; + if (!RPCHandlerAdd(modinfo->handle, &r)) + { + config_error("[rpc/user] Could not register RPC handler"); + return MOD_FAILED; + } return MOD_SUCCESS; } @@ -71,3 +80,30 @@ void rpc_user_list(Client *client, json_t *request, json_t *params) rpc_response(client, request, result); json_decref(result); } + +void rpc_user_get(Client *client, json_t *request, json_t *params) +{ + json_t *result, *list, *item; + const char *nick; + Client *acptr; + + nick = json_object_get_string(params, "nick"); + if (!nick) + { + rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_PARAMS, "Missing parameter: 'nick'"); + return; + } + + if (!(acptr = find_user(nick, NULL))) + { + // FIXME: wrong error! + // consider re-using IRC numerics? the positive ones, eg ERR_NOSUCHNICK + rpc_error(client, NULL, JSON_RPC_ERROR_INVALID_REQUEST, "Nickname not found"); + return; + } + + result = json_object(); + json_expand_client(result, "client", acptr, 1); + rpc_response(client, request, result); + json_decref(result); +}