comparison mod_muc_offline_delivery/mod_muc_offline_delivery.lua @ 4339:3b7847c9bd26

mod_muc_deliver_offline: New module for delivery of MUC messages to offline users
author Matthew Wild <mwild1@gmail.com>
date Fri, 15 Jan 2021 18:57:12 +0000
parents
children 8bd36bba2292
comparison
equal deleted inserted replaced
4338:0227fb4d1b40 4339:3b7847c9bd26
1 local st = require "util.stanza";
2
3 module:add_item("muc-registration-field", {
4 name = "{http://tigase.org/protocol/muc}offline";
5 type = "boolean";
6 label = "Receive messages while not connected to the room";
7 value = false;
8 });
9
10 module:hook("muc-registration-submitted", function (event)
11 local deliver_offline = event.submitted_data["{http://tigase.org/protocol/muc}offline"] or nil;
12 event.affiliation_data.offline_delivery = deliver_offline;
13 end);
14
15 module:hook("muc-add-history", function (event)
16 module:log("debug", "Broadcasting message to offline occupants...");
17 local sent = 0;
18 local room = event.room;
19 for jid, affiliation, data in room:each_affiliation() do --luacheck: ignore 213/affiliation
20 local reserved_nickname = data and data.reserved_nickname;
21 module:log("debug", "Affiliated: %s, %s: %s", jid, reserved_nickname, data and data.offline_delivery);
22 if reserved_nickname and data.offline_delivery then
23 local is_absent = not room:get_occupant_by_nick(room.jid.."/"..reserved_nickname);
24 if is_absent then
25 local msg = st.clone(event.stanza);
26 msg.attr.to = jid;
27 module:send(msg);
28 sent = sent + 1;
29 end
30 end
31 end
32 if sent > 0 then
33 module:log("debug", "Sent message to %d offline occupants", sent);
34 end
35 end);