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