Software /
code /
prosody-modules
Comparison
mod_muc_moderation/mod_muc_moderation.lua @ 3897:3a96070f4a14
mod_muc_moderation: Initial commit of XEP-0425: Message Moderation
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 22 Feb 2020 21:11:31 +0100 |
child | 3900:06971a04216f |
comparison
equal
deleted
inserted
replaced
3896:987b203bb091 | 3897:3a96070f4a14 |
---|---|
1 -- Imports | |
2 local dt = require "util.datetime"; | |
3 local id = require "util.id"; | |
4 local jid = require "util.jid"; | |
5 local st = require "util.stanza"; | |
6 | |
7 -- Plugin dependencies | |
8 local mod_muc = module:depends "muc"; | |
9 | |
10 local muc_util = module:require "muc/util"; | |
11 local valid_roles = muc_util.valid_roles; | |
12 | |
13 local muc_log_archive = module:open_store("muc_log", "archive"); | |
14 | |
15 if not muc_log_archive.set then | |
16 module:log("warn", "Selected archive storage module does not support message replacement, no tombstones will be saved"); | |
17 end | |
18 | |
19 -- Namespaces | |
20 local xmlns_fasten = "urn:xmpp:fasten:0"; | |
21 local xmlns_moderate = "urn:xmpp:message-moderate:0"; | |
22 local xmlns_retract = "urn:xmpp:message-retract:0"; | |
23 | |
24 -- Discovering support | |
25 module:hook("muc-disco#info", function (event) | |
26 event.reply:tag("feature", { var = xmlns_moderate }):up(); | |
27 end); | |
28 | |
29 -- Main handling | |
30 module:hook("iq-set/bare/" .. xmlns_fasten .. ":apply-to", function (event) | |
31 local stanza, origin = event.stanza, event.origin; | |
32 | |
33 -- Collect info we need | |
34 local apply_to = stanza.tags[1]; | |
35 local moderate_tag = apply_to:get_child("moderate", xmlns_moderate); | |
36 if not moderate_tag then return end -- some other kind of fastening? | |
37 | |
38 local reason = moderate_tag:get_child_text("reason"); | |
39 | |
40 local room_jid = stanza.attr.to; | |
41 local room_node = jid.split(room_jid); | |
42 local room = mod_muc.get_room_from_jid(room_jid); | |
43 | |
44 local stanza_id = apply_to.attr.id; | |
45 | |
46 -- Permissions | |
47 local actor = stanza.attr.from; | |
48 local actor_nick = room:get_occupant_jid(actor); | |
49 local affiliation = room:get_affiliation(actor); | |
50 local role = room:get_role(actor_nick) or room:get_default_role(affiliation); | |
51 if valid_roles[role or "none"] < valid_roles.moderator then | |
52 origin.send(st.error_reply(stanza, "auth", "forbidden", "Insufficient privileges")); | |
53 return true; | |
54 end | |
55 | |
56 -- Original stanza to base tombstone on | |
57 local original, err; | |
58 if muc_log_archive.get then | |
59 original, err = muc_log_archive:get(room_node, stanza_id); | |
60 else | |
61 -- COMPAT missing :get API | |
62 err = "item-not-found"; | |
63 for i, item in muc_log_archive:find(room_node, { key = stanza_id, limit = 1 }) do | |
64 if i == stanza_id then | |
65 original, err = item, nil; | |
66 end | |
67 end | |
68 end | |
69 if not original then | |
70 if err == "item-not-found" then | |
71 origin.send(st.error_reply(stanza, "modify", "item-not-found")); | |
72 else | |
73 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); | |
74 end | |
75 return true; | |
76 end | |
77 | |
78 -- Replacements | |
79 local tombstone = st.message({ from = original.attr.from, type = "groupchat", id = original.attr.id }) | |
80 :tag("moderated", { xmlns = xmlns_moderate, by = actor_nick }) | |
81 :tag("retracted", { xmlns = xmlns_retract, stamp = dt.datetime() }):up(); | |
82 | |
83 local announcement = st.message({ from = room_jid, type = "groupchat", id = id.medium(), }) | |
84 :tag("apply-to", { xmlns = xmlns_fasten, id = stanza_id }) | |
85 :tag("moderated", { xmlns = xmlns_moderate, by = actor_nick }) | |
86 :tag("retract", { xmlns = xmlns_retract }):up(); | |
87 | |
88 if reason then | |
89 tombstone:text_tag("reason", reason); | |
90 announcement:text_tag("reason", reason); | |
91 end | |
92 | |
93 if muc_log_archive.set then | |
94 -- Tombstone | |
95 local was_replaced = muc_log_archive:set(room_node, stanza_id, tombstone); | |
96 if not was_replaced then | |
97 origin.send(st.error_reply(stanza, "wait", "internal-server-error")); | |
98 return true; | |
99 end | |
100 end | |
101 | |
102 -- Done, tell people about it | |
103 module:log("info", "Message with id '%s' in room %s moderated by %s, reason: %s", stanza_id, room_jid, actor, reason); | |
104 module:log("debug", ":broadcast(%s)", announcement); | |
105 room:broadcast(announcement); | |
106 | |
107 origin.send(st.reply(stanza)); | |
108 return true; | |
109 end); | |
110 | |
111 module:hook("muc-message-is-historic", function (event) | |
112 -- Ensure moderation messages are stored | |
113 if event.stanza.attr.from == event.room.jid then | |
114 return event.stanza:get_child("apply-to", xmlns_fasten); | |
115 end | |
116 end, 1); |