| 1346 | } |
| 1347 | |
| 1348 | static const char *plugin_rpcmethod_add(struct plugin *plugin, |
| 1349 | const char *buffer, |
| 1350 | const jsmntok_t *meth) |
| 1351 | { |
| 1352 | const jsmntok_t *nametok, *usagetok, *deprtok; |
| 1353 | struct json_command *cmd; |
| 1354 | const char *usage, *err; |
| 1355 | |
| 1356 | nametok = json_get_member(buffer, meth, "name"); |
| 1357 | usagetok = json_get_member(buffer, meth, "usage"); |
| 1358 | deprtok = json_get_member(buffer, meth, "deprecated"); |
| 1359 | |
| 1360 | if (!nametok || nametok->type != JSMN_STRING) { |
| 1361 | return tal_fmt(plugin, |
| 1362 | "rpcmethod does not have a string \"name\": %.*s", |
| 1363 | meth->end - meth->start, buffer + meth->start); |
| 1364 | } |
| 1365 | |
| 1366 | if (usagetok && usagetok->type != JSMN_STRING) { |
| 1367 | return tal_fmt(plugin, |
| 1368 | "\"usage\" is not a string: %.*s", |
| 1369 | meth->end - meth->start, buffer + meth->start); |
| 1370 | } |
| 1371 | |
| 1372 | cmd = notleak(tal(plugin, struct json_command)); |
| 1373 | cmd->name = json_strdup(cmd, buffer, nametok); |
| 1374 | if (usagetok) |
| 1375 | usage = json_strdup(tmpctx, buffer, usagetok); |
| 1376 | else |
| 1377 | return tal_fmt(plugin, |
| 1378 | "\"usage\" not provided by plugin"); |
| 1379 | |
| 1380 | err = json_parse_deprecated(cmd, buffer, deprtok, &cmd->depr_start, &cmd->depr_end); |
| 1381 | if (err) |
| 1382 | return tal_steal(plugin, err); |
| 1383 | |
| 1384 | cmd->dev_only = false; |
| 1385 | cmd->dispatch = plugin_rpcmethod_dispatch; |
| 1386 | cmd->check = plugin_rpcmethod_check; |
| 1387 | if (!jsonrpc_command_add(plugin->plugins->ld->jsonrpc, cmd, usage)) { |
| 1388 | struct plugin *p = |
| 1389 | find_plugin_for_command(plugin->plugins->ld, cmd->name); |
| 1390 | return tal_fmt( |
| 1391 | plugin, |
| 1392 | "Could not register method \"%s\", a method with " |
| 1393 | "that name is already registered by plugin %s", |
| 1394 | cmd->name, p->cmd); |
| 1395 | } |
| 1396 | tal_arr_expand(&plugin->methods, cmd->name); |
| 1397 | return NULL; |
| 1398 | } |
| 1399 | |
| 1400 | static const char *plugin_rpcmethods_add(struct plugin *plugin, |
| 1401 | const char *buffer, |
no test coverage detected