Annotate

plugins/mod_pubsub/commands.lib.lua @ 13585:8091c1b8023e

mod_pubsub: Expand shell commands to include node/item management
author Matthew Wild <mwild1@gmail.com>
date Tue, 07 Jan 2025 14:27:18 +0000
child 13623:e226f9632a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13585
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local it = require "prosody.util.iterators";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local st = require "prosody.util.stanza";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local pubsub_lib = module:require("mod_pubsub/pubsub");
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local function add_commands(get_service)
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 name = "create_node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 desc = "Create a node with the specified name";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 { name = "node_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 handler = function (self, service_jid, node_name) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 return get_service(service_jid):create(node_name, true);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 name = "list_nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 desc = "List nodes on a pubsub service";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 handler = function (self, service_jid) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 -- luacheck: ignore 431/service
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local service = get_service(service_jid);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local nodes = select(2, assert(service:get_nodes(true)));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local count = 0;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 for node_name in pairs(nodes) do
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 count = count + 1;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 self.session.print(node_name);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return true, ("%d nodes"):format(count);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 name = "list_items";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 desc = "List items on a pubsub node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 { name = "node_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 handler = function (self, service_jid, node_name) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 -- luacheck: ignore 431/service
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local service = get_service(service_jid);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local items = select(2, assert(service:get_items(node_name, true)));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 local count = 0;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 for item_name in pairs(items) do
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 count = count + 1;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 self.session.print(item_name);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 return true, ("%d items"):format(count);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 name = "get_item";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 desc = "Show item content on a pubsub node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 { name = "node_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 { name = "item_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 handler = function (self, service_jid, node_name, item_name) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 -- luacheck: ignore 431/service
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 local service = get_service(service_jid);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local items = select(2, assert(service:get_items(node_name, true)));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if not items[item_name] then
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 return false, "Item not found";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 self.session.print(items[item_name]);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 return true;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 name = "get_node_config";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 desc = "Get the current configuration for a node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 { name = "node_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 { name = "option_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 handler = function (self, service_jid, node_name, option_name) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 -- luacheck: ignore 431/service
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 local service = get_service(service_jid);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 local config = select(2, assert(service:get_node_config(node_name, true)));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 local config_form = pubsub_lib.node_config_form:form(config, "submit");
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 local count = 0;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 if option_name then
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 count = 1;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local field = config_form:get_child_with_attr("field", nil, "var", option_name);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 if not field then
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 return false, "option not found";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 self.session.print(field:get_child_text("value"));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 else
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 local opts = {};
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 for field in config_form:childtags("field") do
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 opts[field.attr.var] = field:get_child_text("value");
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 for k, v in it.sorted_pairs(opts) do
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 count = count + 1;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 self.session.print(k, v);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 return true, ("Showing %d config options"):format(count);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 name = "set_node_config_option";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 desc = "Set a config option on a pubsub node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 { name = "node_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 { name = "option_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 { name = "option_value", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 handler = function (self, service_jid, node_name, option_name, option_value) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 -- luacheck: ignore 431/service
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 local service = get_service(service_jid);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 local config = select(2, assert(service:get_node_config(node_name, true)));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 local new_config_form = st.stanza("x", { xmlns = "jabber:x:data" })
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 :tag("field", { var = option_name })
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 :text_tag("value", option_value)
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 :up();
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 local new_config = pubsub_lib.node_config_form:data(new_config_form, config);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 assert(service:set_node_config(node_name, true, new_config));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 local applied_config = select(2, assert(service:get_node_config(node_name, true)));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 local applied_config_form = pubsub_lib.node_config_form:form(applied_config, "submit");
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 local applied_field = applied_config_form:get_child_with_attr("field", nil, "var", option_name);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 if not applied_field then
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 return false, "Unknown config field: "..option_name;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 return true, "Applied config: "..applied_field:get_child_text("value");
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 name = "delete_item";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 desc = "Delete a single item from a node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 { name = "node_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 { name = "item_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 handler = function (self, service_jid, node_name, item_name) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 -- luacheck: ignore 431/service
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 local service = get_service(service_jid);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 return assert(service:retract(node_name, true, item_name));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 name = "delete_all_items";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 desc = "Delete all items from a node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 { name = "node_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 { name = "notify_subscribers", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 handler = function (self, service_jid, node_name, notify_subscribers) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 -- luacheck: ignore 431/service
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 local service = get_service(service_jid);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 return assert(service:purge(node_name, true, notify_subscribers == "true"));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 name = "create_node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 desc = "Create a new node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 { name = "node_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 handler = function (self, service_jid, node_name) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 -- luacheck: ignore 431/service
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 local service = get_service(service_jid);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 return assert(service:create(node_name, true));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 module:add_item("shell-command", {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 section = "pubsub";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 section_desc = "Manage publish/subscribe nodes";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 name = "delete_node";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 desc = "Delete a node entirely";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 args = {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 { name = "service_jid", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 { name = "node_name", type = "string" };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 };
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 host_selector = "service_jid";
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 handler = function (self, service_jid, node_name) --luacheck: ignore 212/self
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 -- luacheck: ignore 431/service
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 local service = get_service(service_jid);
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 return assert(service:delete(node_name, true));
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 end;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 });
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 end
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 return {
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 add_commands = add_commands;
8091c1b8023e mod_pubsub: Expand shell commands to include node/item management
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 }