mod_muc_ping/mod_muc_ping.lua
author tmolitor <thilo@eightysoft.de>
Tue, 07 Apr 2020 22:11:52 +0200
changeset 3974 e0f3e29ab18a
parent 3651 a0ca5d0a49ba
permissions -rw-r--r--
mod_nooffline_noerror: Fix for missing log
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3353
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
local st = require "util.stanza";
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
local jid_bare = import("util.jid", "bare");
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
local mod_muc = module:depends"muc";
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
local rooms = rawget(mod_muc, "rooms");
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
if not rooms then
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
	module:log("warn", "mod_%s is compatible with Prosody up to 0.10.x", module.name);
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
	return;
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
end
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
module:hook("iq/full", function (event)
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
	local origin, stanza = event.origin, event.stanza;
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
	if stanza.attr.type ~= "get" or not stanza:get_child("ping", "urn:xmpp:ping") then
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
		return;
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
	end
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
	local from = stanza.attr.from;
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
	local room_nick = stanza.attr.to;
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
	local room_jid = jid_bare(room_nick);
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
	local room = rooms[room_jid];
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
	if not room then return; end
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
	if room._jid_nick[from] == room_nick then
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
		origin.send(st.reply(stanza));
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
		return true;
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
	end
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
end);
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
module:hook("muc-disco#info", function(event)
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
	event.reply:tag("feature", {var="urn:xmpp:ping"}):up();
3651
a0ca5d0a49ba mod_muc_ping: Advertise XEP-0410 feature
Kim Alvefur <zash@zash.se>
parents: 3353
diff changeset
    32
	event.reply:tag("feature", {var = "http://jabber.org/protocol/muc#self-ping-optimization"}):up();
3353
35dc7c38e362 mod_muc_ping: Implements the Server Optimization part of XEP-0410: MUC Self-Ping
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
end);