MCPcopy Create free account
hub / github.com/ElementsProject/lightning / rpc_command_hook_callback

Function rpc_command_hook_callback

lightningd/jsonrpc.c:923–1002  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

921}
922
923static bool
924rpc_command_hook_callback(struct rpc_command_hook_payload *p,
925 const char *buffer, const jsmntok_t *resulttok)
926{
927 const struct lightningd *ld = p->cmd->ld;
928 const jsmntok_t *tok, *custom_return;
929 static char *error = "";
930 char *method;
931
932 if (!resulttok || !buffer)
933 return true;
934
935 tok = json_get_member(buffer, resulttok, "result");
936 if (tok) {
937 if (!json_tok_streq(buffer, tok, "continue")) {
938 error = "'result' should only be 'continue'.";
939 goto log_error_and_skip;
940 }
941 /* plugin tells us to do nothing. just pass. */
942 return true;
943 }
944
945 /* didn't just continue but hook was already modified by prior plugin */
946 if (p->custom_result != NULL ||
947 p->custom_error != NULL ||
948 p->custom_replace != NULL) {
949 /* get method name and log error (only the first time). */
950 tok = json_get_member(p->buffer, p->request, "method");
951 method = tal_strndup(p, p->buffer + tok->start, tok->end - tok->start);
952 log_unusual(ld->log, "rpc_command hook '%s' already modified, ignoring.", method );
953 rpc_command_hook_final(p);
954 return false;
955 }
956
957 /* If the registered plugin did not respond with continue,
958 * it wants either to replace the request... */
959 tok = json_get_member(buffer, resulttok, "replace");
960 if (tok) {
961 /* We need to make copies here, as buffer and tokens
962 * can be reused. */
963 json_dup_contents(p, buffer, tok,
964 &p->custom_buffer, &p->custom_replace);
965 return true;
966 }
967
968 /* ...or return a custom JSONRPC response. */
969 tok = json_get_member(buffer, resulttok, "return");
970 if (tok) {
971 custom_return = json_get_member(buffer, tok, "result");
972 if (custom_return) {
973 p->custom_result = json_strdup(p, buffer, custom_return);
974 return true;
975 }
976
977 custom_return = json_get_member(buffer, tok, "error");
978 if (custom_return) {
979 enum jsonrpc_errcode code;
980 const char *errmsg;

Callers

nothing calls this directly

Calls 6

json_get_memberFunction · 0.85
json_tok_streqFunction · 0.85
rpc_command_hook_finalFunction · 0.85
json_dup_contentsFunction · 0.85
json_to_jsonrpc_errcodeFunction · 0.85
json_strdupFunction · 0.50

Tested by

no test coverage detected