| 921 | } |
| 922 | |
| 923 | static bool |
| 924 | rpc_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; |
nothing calls this directly
no test coverage detected