# HG changeset patch # User Matthew Wild # Date 1637062868 0 # Node ID 0dc2c3530d64d87b964774924ced425921637d82 # Parent ba3344926e180e7236e23c09352d78cf50630242 MUC: Add 'muc-pre-set-affiliation' event, allowing to block change or modify data diff -r ba3344926e18 -r 0dc2c3530d64 plugins/muc/muc.lib.lua --- a/plugins/muc/muc.lib.lua Mon Nov 15 16:11:03 2021 +0000 +++ b/plugins/muc/muc.lib.lua Tue Nov 16 11:41:08 2021 +0000 @@ -1423,6 +1423,24 @@ end end + local event_data = { + room = self; + actor = actor; + jid = jid; + affiliation = affiliation or "none"; + reason = reason; + previous_affiliation = target_affiliation; + data = data and data or nil; -- coerce false to nil + }; + if not module:fire_event("muc-pre-set-affiliation", event_data) then + local err = event_data.error or { type = "cancel", condition = "not-allowed" }; + return nil, err.type, err.condition; + end + if affiliation and not data and event_data.data then + -- Allow handlers to add data when none was going to be set + data = event_data.data; + end + -- Set in 'database' self._affiliations[jid] = affiliation; if not affiliation or data == false or (data ~= nil and next(data) == nil) then @@ -1497,16 +1515,8 @@ self:save(true); - module:fire_event("muc-set-affiliation", { - room = self; - actor = actor; - jid = jid; - affiliation = affiliation or "none"; - reason = reason; - previous_affiliation = target_affiliation; - data = data and data or nil; -- coerce false to nil - in_room = next(occupants_updated) ~= nil; - }); + event_data.in_room = next(occupants_updated) ~= nil; + module:fire_event("muc-set-affiliation", event_data); return true; end