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