Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: Allow plugins to publish and subscribe to custom notifications #4496

Merged
merged 17 commits into from
May 3, 2021
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion lightningd/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,44 @@ static void plugin_manifest_timeout(struct plugin *plugin)
fatal("Can't recover from plugin failure, terminating.");
}

static const char *plugin_notifications_add(const char *buffer,
const jsmntok_t *result,
struct plugin *plugin)
{
char *name;
const jsmntok_t *method, *obj;
const jsmntok_t *notifications =
json_get_member(buffer, result, "notifications");

if (!notifications)
return NULL;

if (notifications->type != JSMN_ARRAY)
return tal_fmt(plugin,
"\"result.notifications\" is not an array");

for (size_t i = 0; i < notifications->size; i++) {
obj = json_get_arr(notifications, i);
Comment on lines +1319 to +1320
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json_for_each_arr(i, obj, notifications) does these two lines, BTW (and slightly more efficiently).

if (obj->type != JSMN_OBJECT)
return tal_fmt(
plugin,
"\"result.notifications[%zu]\" is not an object",
i);

method = json_get_member(buffer, obj, "method");
if (method == NULL || method->type != JSMN_STRING)
return tal_fmt(plugin,
"\"result.notifications[%zu].name\" "
"missing or not a string.",
i);

name = json_strdup(plugin->plugins, buffer, method);
tal_arr_expand(&plugin->plugins->notification_topics, name);
}

return NULL;
}

static const char *plugin_parse_getmanifest_response(const char *buffer,
const jsmntok_t *toks,
const jsmntok_t *idtok,
Expand Down Expand Up @@ -1353,7 +1391,9 @@ static const char *plugin_parse_getmanifest_response(const char *buffer,
}
}

err = plugin_opts_add(plugin, buffer, resulttok);
err = plugin_notifications_add(buffer, resulttok, plugin);
if (!err)
err = plugin_opts_add(plugin, buffer, resulttok);
if (!err)
err = plugin_rpcmethods_add(plugin, buffer, resulttok);
if (!err)
Expand Down