Mercurial > prosody-hg
annotate plugins/muc/gc3.lib.lua @ 13955:01f95f3de6fc
usermanager: More consistent method behaviour, errors, and (pre-)events
This commit refactors most of the exported usermanager methods substantially
to ensure that all of them correctly check for the presence of the host and
method they are calling and return consistent errors if either is incorrect or
missing.
It also unifies the event and logging code, so as a result there are more
events being fired than before this commit (it also adds pre- events so that
modules can block an action from going ahead).
We don't have good test coverage of this API, but some manual testing suggests
it seems to be working okay...
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 25 Sep 2025 16:53:35 +0100 |
| parents | 356c8ec22e9a |
| children |
| rev | line source |
|---|---|
|
13908
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local jid_bare = require "prosody.util.jid".bare; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local st = require "prosody.util.stanza"; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local muc_util = module:require "muc/util"; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local valid_affiliations = muc_util.valid_affiliations; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local xmlns_gc3 = "urn:xmpp:gc3:tmp" |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 --luacheck: ignore 113/get_room_from_jid |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 function fetch_participant_list(room, stanza, origin) |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local from_jid = stanza.attr.from; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local from_aff = room:get_affiliation(from_jid); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local from_rank = valid_affiliations[from_aff or "none"]; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local required_aff_rank = valid_affiliations[room:get_members_only() and "member" or "none"]; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 if from_rank < required_aff_rank then |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 origin.send(st.error_reply(stanza, "auth", "forbidden")); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 return true; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 local can_see_real_jids = not room:is_anonymous_for(from_jid); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local reply = st.reply(stanza) |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 :tag("participants", { xmlns = xmlns_gc3 }); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 for jid, aff in room:each_affiliation() do |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local nick = room:get_registered_nick(jid); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local visible_jid = can_see_real_jids and jid or nil; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 reply:text_tag("item", nil, { |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 affiliation = aff; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 jid = visible_jid; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 nick = nick; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 id = room:get_occupant_id_from_jid(jid); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 }); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 origin.send(reply); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 return true; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 local function room_event_handler(handler, allow_nonexistent) |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return function (event) |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local origin, stanza = event.origin, event.stanza; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 local room_jid = jid_bare(stanza.attr.to); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local room = get_room_from_jid(room_jid); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 if not room and allow_nonexistent ~= true then |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 origin.send(st.error_reply(stanza, "cancel", "item-not-found")); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 return true; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 end |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 return handler(room, stanza, origin); |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end; |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 end |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 |
|
356c8ec22e9a
MUC: Add basic "GC3" participant (affiliation) list fetch
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 module:hook("iq-get/bare/"..xmlns_gc3..":participants", room_event_handler(fetch_participant_list)); |
