| 772 | } |
| 773 | |
| 774 | static bool |
| 775 | rpc_command_hook_callback(struct rpc_command_hook_payload *p, |
| 776 | const char *buffer, const jsmntok_t *resulttok) |
| 777 | { |
| 778 | const struct lightningd *ld = p->cmd->ld; |
| 779 | const jsmntok_t *tok, *custom_return; |
| 780 | static char *error = ""; |
| 781 | char *method; |
| 782 | |
| 783 | if (!resulttok || !buffer) |
| 784 | return true; |
| 785 | |
| 786 | tok = json_get_member(buffer, resulttok, "result"); |
| 787 | if (tok) { |
| 788 | if (!json_tok_streq(buffer, tok, "continue")) { |
| 789 | error = "'result' should only be 'continue'."; |
| 790 | goto log_error_and_skip; |
| 791 | } |
| 792 | /* plugin tells us to do nothing. just pass. */ |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | /* didn't just continue but hook was already modified by prior plugin */ |
| 797 | if (p->custom_result != NULL || |
| 798 | p->custom_error != NULL || |
| 799 | p->custom_replace != NULL) { |
| 800 | /* get method name and log error (only the first time). */ |
| 801 | tok = json_get_member(p->buffer, p->request, "method"); |
| 802 | method = tal_strndup(p, p->buffer + tok->start, tok->end - tok->start); |
| 803 | log_unusual(ld->log, "rpc_command hook '%s' already modified, ignoring.", method ); |
| 804 | rpc_command_hook_final(p); |
| 805 | return false; |
| 806 | } |
| 807 | |
| 808 | /* If the registered plugin did not respond with continue, |
| 809 | * it wants either to replace the request... */ |
| 810 | tok = json_get_member(buffer, resulttok, "replace"); |
| 811 | if (tok) { |
| 812 | /* We need to make copies here, as buffer and tokens |
| 813 | * can be reused. */ |
| 814 | p->custom_replace = json_tok_copy(p, tok); |
| 815 | p->custom_buffer = tal_dup_talarr(p, char, buffer); |
| 816 | return true; |
| 817 | } |
| 818 | |
| 819 | /* ...or return a custom JSONRPC response. */ |
| 820 | tok = json_get_member(buffer, resulttok, "return"); |
| 821 | if (tok) { |
| 822 | custom_return = json_get_member(buffer, tok, "result"); |
| 823 | if (custom_return) { |
| 824 | p->custom_result = json_strdup(p, buffer, custom_return); |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | custom_return = json_get_member(buffer, tok, "error"); |
| 829 | if (custom_return) { |
| 830 | enum jsonrpc_errcode code; |
| 831 | const char *errmsg; |
nothing calls this directly
no test coverage detected