view mod_csi_muc_priorities/mod_csi_muc_priorities.lua @ 5193:2bb29ece216b

mod_http_oauth2: Implement stateless dynamic client registration Replaces previous explicit registration that required either the additional module mod_adhoc_oauth2_client or manually editing the database. That method was enough to have something to test with, but would not probably not scale easily. Dynamic client registration allows creating clients on the fly, which may be even easier in theory. In order to not allow basically unauthenticated writes to the database, we implement a stateless model here. per_host_key := HMAC(config -> oauth2_registration_key, hostname) client_id := JWT { client metadata } signed with per_host_key client_secret := HMAC(per_host_key, client_id) This should ensure everything we need to know is part of the client_id, allowing redirects etc to be validated, and the client_secret can be validated with only the client_id and the per_host_key. A nonce injected into the client_id JWT should ensure nobody can submit the same client metadata and retrieve the same client_secret
author Kim Alvefur <zash@zash.se>
date Fri, 03 Mar 2023 21:14:19 +0100
parents 1b68954a743a
children
line wrap: on
line source

local jid_bare, jid_split = import("util.jid", "bare", "split");

-- luacheck: ignore 122
local user_sessions = prosody.hosts[module.host].sessions;

module:hook("csi-is-stanza-important", function (event)
	local stanza, session = event.stanza, event.session;
	if stanza.name == "message" then
		if stanza.attr.type == "groupchat" then
			local room_jid = jid_bare(stanza.attr.from);

			local username = session.username;
			local priorities = user_sessions[username].csi_muc_priorities;

			if priorities then
				local priority = priorities[room_jid];
				if priority ~= nil then
					event.reason = "muc priority";
					return priority;
				end
			end

			-- Look for mention
			local rooms = session.rooms_joined;
			if rooms then
				local body = stanza:get_child_text("body");
				if not body then return end
				local room_nick = rooms[room_jid];
				if room_nick then
					if body:find(room_nick, 1, true) then
						event.reason = "muc mention";
						return true;
					end
					-- Your own messages
					if stanza.attr.from == (room_jid .. "/" .. room_nick) then
					event.reason = "muc own message";
						return true;
					end
				end
			end

			-- Standard importance and no mention, leave to other modules to decide for now
			return nil;
		end
	end
end);

module:depends("adhoc");

local dataform = require"util.dataforms";
local adhoc_inital_data = require "util.adhoc".new_initial_data_form;
local instructions = [[
These settings affect battery optimizations performed by the server
while your client has indicated that it is inactive.
]]

local priority_settings_form = dataform.new {
	title = "Prioritize addresses of group chats";
	instructions = instructions;
	{
		type = "hidden";
		name = "FORM_TYPE";
		value = "xmpp:modules.prosody.im/mod_"..module.name;
	};
	{
		type = "jid-multi";
		name = "important";
		label = "Higher priority";
		desc = "Group chats more important to you";
	};
	{
		type = "jid-multi";
		name = "unimportant";
		label = "Lower priority";
		desc = "E.g. large noisy public channels";
	};
}

local store = module:open_store();
module:hook("resource-bind", function (event)
	local username = event.session.username;
	user_sessions[username].csi_muc_priorities = store:get(username);
end);

local adhoc_command_handler = adhoc_inital_data(priority_settings_form, function (data)
	local username = jid_split(data.from);
	local prioritized_jids = user_sessions[username].csi_muc_priorities or store:get(username);
	local important = {};
	local unimportant = {};
	if prioritized_jids then
		for jid, priority in pairs(prioritized_jids) do
			if priority then
				table.insert(important, jid);
			else
				table.insert(unimportant, jid);
			end
		end
		table.sort(important);
		table.sort(unimportant);
	end
	return {
		important = important;
		unimportant = unimportant;
	};
end, function(fields, form_err, data)
	if form_err then
		return { status = "completed", error = { message = "Problem in submitted form" } };
	end
	local prioritized_jids = {};
	if fields.unimportant then
		if fields.unimportant then
			for _, jid in ipairs(fields.unimportant) do
				prioritized_jids[jid] = false;
			end
		end
		if fields.important then
			for _, jid in ipairs(fields.important) do
				prioritized_jids[jid] = true;
			end
		end
	end

	local username = jid_split(data.from);
	local ok, err = store:set(username, prioritized_jids);
	if ok then
		user_sessions[username].csi_muc_priorities = prioritized_jids;
		return { status = "completed", info = "Priorities updated" };
	else
		return { status = "completed", error = { message = "Error saving priorities: "..err } };
	end
end);

module:add_item("adhoc", module:require "adhoc".new("Configure group chat priorities",
	"xmpp:modules.prosody.im/mod_"..module.name, adhoc_command_handler, "local_user"));