File

clix/adhoc.lua @ 148:27a9f28724d3

clix.archive: Add 'irc' output format, useful for MUC export
author Kim Alvefur <zash@zash.se>
date Mon, 08 Feb 2021 22:59:49 +0100
parent 126:c9d03a70a936
child 168:75e8ca131178
line wrap: on
line source

local dataforms = require "util.dataforms";

-- TODO Cleanup, commit
return function (opts, arg)
	if opts.short_help then
		print("Execute an Ad-Hoc Command");
		return;
	end
	local function on_connect(conn)
		if opts.node then
			conn:execute_command(opts.to or conn.host, opts.node, function(cmd)
				conn:info("status: %s", cmd.status);
				local note = cmd.note;
				if note then
					conn[note.attr.type or "info"](conn, note:get_text());
				end
				if cmd.status == "executing" then
					local data = {};
					for i=1,#arg do
						local k,v = arg[i]:match"^([^=]+)=(.*)";
						if k and v then
							data[k] = v; --FIXME multiple
						end
					end
					local command_form_layout = dataforms.from_stanza(cmd.form)
					if opts.interactive then
						for i=1,#command_form_layout do
							local item = command_form_layout[i];
							if item.type ~= "hidden" and not data[item.name] then
								-- FIXME Current value isn't shown
								io.stderr:write(item.label..": ");
								if item.type:match"%-multi" then
									local t = { };
										repeat
										local line = io.read("*l");
										if line and line ~= "" then
											if item.type:match"list%-" then
												t[#t+1] = { label = line, value = line, default = true };
											else
												t[#t+1] = line;
											end
										end
									until not line or line == "";
									if item.type == "text-multi" then
										t = table.concat(t, "\n");
									end
									data[item.name] = t;
								--elseif item.type == "list-single" then
									--data[item.name] = { (io.read("*l")) };
								else
									data[item.name] = io.read("*l");
								end
							end
						end
					end
					cmd:next(command_form_layout:form(data, "submit"));
				elseif cmd.status == "completed" then
					if cmd.form then
						local command_form_layout = dataforms.from_stanza(cmd.form)
						local data = command_form_layout:data(cmd.form);
						if data.title then
							print("= " .. data.title .. " =");
							print()
						end
						if data.instructions then
							print(data.instructions);
							print()
						end
						for i, item in ipairs(command_form_layout) do
							if item.type ~= "hidden" then
								print("== " .. (item.label or item.name) .. " ==")
								print(data[item.name]);
							end
						end

					end
					conn:close();
				else
					conn:warn("unhandled command status: %s", tostring(cmd.status));
				end
			end);
		else
			conn:disco_items(opts.to or conn.host, "http://jabber.org/protocol/commands", function(items)
				-- TODO It would be neat to be able to choose from this list
				if items then
					for i=1,#items do
						print(items[i].name, items[i].node);
					end
				end
				conn:close();
			end);
		end
	end
	clix_connect(opts, on_connect, {"adhoc"});
end