mod_muc_hats_adhoc/mod_muc_hats_adhoc.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 23 Sep 2022 22:41:15 +0100
changeset 5058 62480053c87b
parent 3952 11825788a452
permissions -rw-r--r--
mod_cloud_notify_encrypted: Additional debug logging when enabling/skipping
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3952
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
module:depends("adhoc");
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local adhoc_new = module:require("adhoc").new;
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local hats_api = module:depends("muc_hats_api");
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local dataforms_new = require "util.dataforms".new;
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
local adhoc_simple = require "util.adhoc".new_simple_form;
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
local function generate_error_message(errors)
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
	local errmsg = {};
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
	for name, err in pairs(errors) do
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
		errmsg[#errmsg + 1] = name .. ": " .. err;
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
	end
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
	return { status = "completed", error = { message = table.concat(errmsg, "\n") } };
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
end
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
local add_hat_form_layout = dataforms_new {
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
	title = "Add a hat";
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
	instructions = "Assign a hat to a room member";
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
	{ name = "user",  type = "jid-single",  label = "User JID", required = true };
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
	{ name = "room",  type = "jid-single",  label = "Room JID", required = true };
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
	{ name = "title", type = "text-single", label = "Hat title" };
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	{ name = "uri",   type = "text-single", label = "Hat URI",  required = true };
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
};
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
local add_hat_handler = adhoc_simple(add_hat_form_layout, function (fields, errs)
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
	if errs then
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
		return generate_error_message(errs);
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
	end
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
	local ok, err_cond, err_text = hats_api.add_user_hat(fields.user, fields.room, fields.uri, {
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
		active = true;
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
		required = true;
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
		title = fields.title;
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
	});
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
	return {
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
		status = "completed";
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
		info = ok and "The hat has been added successfully" or ("There was an error adding the hat: "..(err_text or err_cond));
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
	};
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
end);
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
local remove_hat_form_layout = dataforms_new {
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
	title = "Remove a hat";
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
	instructions = "Remove a hat from a room member";
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
	{ name = "user",  type = "jid-single",  label = "User JID", required = true };
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
	{ name = "room",  type = "jid-single",  label = "Room JID", required = true };
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
	{ name = "uri",   type = "text-single", label = "Hat URI",  required = true };
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
};
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
local remove_hat_handler = adhoc_simple(remove_hat_form_layout, function (fields, errs)
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
	if errs then
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
		return generate_error_message(errs);
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
	end
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
	local ok, err_cond, err_text = hats_api.remove_user_hat(fields.user, fields.room, fields.uri);
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	return {
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
		status = "completed";
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
		info = ok and "The hat has been removed successfully" or ("There was an error removing the hat: "..(err_text or err_cond));
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
	};
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
end);
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
local add_hat_desc = adhoc_new("Add hat to a user", "http://prosody.im/protocol/hats#add", add_hat_handler, "admin");
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
local remove_hat_desc = adhoc_new("Remove hat from a user", "http://prosody.im/protocol/hats#remove", remove_hat_handler, "admin");
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
module:provides("adhoc", add_hat_desc);
11825788a452 mod_muc_hats_adhoc: Ad-hoc commands for adding/removing MUC hats
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
module:provides("adhoc", remove_hat_desc);