plugins/mod_announce.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 20 Feb 2023 18:10:15 +0000
branch0.12
changeset 12898 0598d822614f
parent 10549 2fbcdf6da331
child 12646 9061f9621330
permissions -rw-r--r--
mod_websocket: Fire pre-session-close event (fixes #1800) This event was added in a7c183bb4e64 and is required to make mod_smacks know that a session was intentionally closed and shouldn't be hibernated (see fcea4d9e7502). Because this was missing from mod_websocket's session.close(), mod_smacks would always attempt to hibernate websocket sessions even if they closed cleanly. That mod_websocket has its own copy of session.close() is something to fix another day (probably not in the stable branch). So for now this commit makes the minimal change to get things working again. Thanks to Damian and the Jitsi team for reporting.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
     4
--
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
     6
-- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
     7
--
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
     8
3276
4e44469b0583 mod_announce: Removed unused variables.
Waqas Hussain <waqas20@gmail.com>
parents: 3275
diff changeset
     9
local st, jid = require "util.stanza", require "util.jid";
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
5370
7838acadb0fa mod_announce, mod_auth_anonymous, mod_c2s, mod_c2s, mod_component, mod_iq, mod_message, mod_presence, mod_tls: Access prosody.{hosts,bare_sessions,full_sessions} instead of the old globals
Kim Alvefur <zash@zash.se>
parents: 5076
diff changeset
    11
local hosts = prosody.hosts;
1396
ce3eb5f71899 mod_announce: Use usermanager.is_admin to verify admin status
Waqas Hussain <waqas20@gmail.com>
parents: 1385
diff changeset
    12
local is_admin = require "core.usermanager".is_admin;
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
    14
function send_to_online(message, host)
3275
05c1d8269043 mod_announce: Changed a global variable to local.
Waqas Hussain <waqas20@gmail.com>
parents: 3228
diff changeset
    15
	local sessions;
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
    16
	if host then
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
    17
		sessions = { [host] = hosts[host] };
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    18
	else
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    19
		sessions = hosts;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    20
	end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    21
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    22
	local c = 0;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    23
	for hostname, host_session in pairs(sessions) do
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    24
		if host_session.sessions then
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    25
			message.attr.from = hostname;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    26
			for username in pairs(host_session.sessions) do
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    27
				c = c + 1;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    28
				message.attr.to = username.."@"..hostname;
5014
b2006c1cfa85 mod_announce, mod_motd, mod_pubsub, mod_register, mod_watchregistrations, mod_welcome: Use module:send() instead of core_*_stanza()
Kim Alvefur <zash@zash.se>
parents: 4926
diff changeset
    29
				module:send(message);
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    30
			end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    31
		end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    32
	end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    33
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    34
	return c;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    35
end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    36
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    37
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    38
-- Old <message>-based jabberd-style announcement sending
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
    39
function handle_announcement(event)
8977
71500c68fed4 mod_announce: Fix luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 8963
diff changeset
    40
	local stanza = event.stanza;
10549
2fbcdf6da331 mod_announce: Silence luacheck warning about unused variable
Kim Alvefur <zash@zash.se>
parents: 10030
diff changeset
    41
	-- luacheck: ignore 211/node
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
    42
	local node, host, resource = jid.split(stanza.attr.to);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
    43
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
	if resource ~= "announce/online" then
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
		return; -- Not an announcement
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
    47
10030
1117138fa372 mod_announce: Check for admin on current virtualhost instead of global (fixes #1365) (thanks yc)
Kim Alvefur <zash@zash.se>
parents: 8977
diff changeset
    48
	if not is_admin(stanza.attr.from, host) then
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
		-- Not an admin? Not allowed!
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
    50
		module:log("warn", "Non-admin '%s' tried to send server announcement", stanza.attr.from);
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
		return;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
    53
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
	module:log("info", "Sending server announcement to all online users");
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
	local message = st.clone(stanza);
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
	message.attr.type = "headline";
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
	message.attr.from = host;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
    58
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    59
	local c = send_to_online(message, host);
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	module:log("info", "Announcement sent to %d online users", c);
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
	return true;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
end
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    63
module:hook("message/host", handle_announcement);
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    65
-- Ad-hoc command (XEP-0133)
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    66
local dataforms_new = require "util.dataforms".new;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    67
local announce_layout = dataforms_new{
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    68
	title = "Making an Announcement";
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    69
	instructions = "Fill out this form to make an announcement to all\nactive users of this service.";
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    70
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    71
	{ name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    72
	{ name = "subject", type = "text-single", label = "Subject" };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    73
	{ name = "announcement", type = "text-multi", required = true, label = "Announcement" };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    74
};
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    75
8977
71500c68fed4 mod_announce: Fix luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 8963
diff changeset
    76
function announce_handler(_, data, state)
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    77
	if state then
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    78
		if data.action == "cancel" then
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    79
			return { status = "canceled" };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    80
		end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    81
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    82
		local fields = announce_layout:data(data.form);
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    83
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    84
		module:log("info", "Sending server announcement to all online users");
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    85
		local message = st.message({type = "headline"}, fields.announcement):up()
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    86
			:tag("subject"):text(fields.subject or "Announcement");
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
    87
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    88
		local count = send_to_online(message, data.to);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
    89
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    90
		module:log("info", "Announcement sent to %d online users", count);
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    91
		return { status = "completed", info = ("Announcement sent to %d online users"):format(count) };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    92
	else
5076
88fb94df9b18 mod_admin_adhoc, mod_announce: Explicitly specify possible actions for ad-hoc commands
Florian Zeitz <florob@babelmonkeys.de>
parents: 5014
diff changeset
    93
		return { status = "executing", actions = {"next", "complete", default = "complete"}, form = announce_layout }, "executing";
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    94
	end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    95
end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    96
8963
9d0d1e427b82 mod_announce: Depend on mod_adhoc for consistent behaviour (thanks meaz, Link Mauve)
Kim Alvefur <zash@zash.se>
parents: 8683
diff changeset
    97
module:depends "adhoc";
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    98
local adhoc_new = module:require "adhoc".new;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
    99
local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
4926
58714123f600 mod_adhoc, mod_admin_adhoc, mod_announce: Use module:provides() to manage Ad-Hoc commands
Florian Zeitz <florob@babelmonkeys.de>
parents: 3278
diff changeset
   100
module:provides("adhoc", announce_desc);
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
   101