Annotate

plugins/mod_announce.lua @ 13652:a08065207ef0

net.server_epoll: Call :shutdown() on TLS sockets when supported Comment from Matthew: This fixes a potential issue where the Prosody process gets blocked on sockets waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends layer 7 data, and this can cause problems for sockets which are in the process of being cleaned up. This depends on LuaSec changes which are not yet upstream. From Martijn's original email: So first my analysis of luasec. in ssl.c the socket is put into blocking mode right before calling SSL_shutdown() inside meth_destroy(). My best guess to why this is is because meth_destroy is linked to the __close and __gc methods, which can't exactly be called multiple times and luasec does want to make sure that a tls session is shutdown as clean as possible. I can't say I disagree with this reasoning and don't want to change this behaviour. My solution to this without changing the current behaviour is to introduce a shutdown() method. I am aware that this overlaps in a conflicting way with tcp's shutdown method, but it stays close to the OpenSSL name. This method calls SSL_shutdown() in the current (non)blocking mode of the underlying socket and returns a boolean whether or not the shutdown is completed (matching SSL_shutdown()'s 0 or 1 return values), and returns the familiar ssl_ioerror() strings on error with a false for completion. This error can then be used to determine if we have wantread/wantwrite to finalize things. Once meth_shutdown() has been called once a shutdown flag will be set, which indicates to meth_destroy() that the SSL_shutdown() has been handled by the application and it shouldn't be needed to set the socket to blocking mode. I've left the SSL_shutdown() call in the LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a timeout for the shutdown code, which might allow SSL_shutdown() to clean up anyway at the last possible moment. Another thing I've changed to luasec is the call to socket_setblocking() right before calling close(2) in socket_destroy() in usocket.c. According to the latest POSIX[0]: Note that the requirement for close() on a socket to block for up to the current linger interval is not conditional on the O_NONBLOCK setting. Which I read to mean that removing O_NONBLOCK on the socket before close doesn't impact the behaviour and only causes noise in system call tracers. I didn't touch the windows bits of this, since I don't do windows. For the prosody side of things I've made the TLS shutdown bits resemble interface:onwritable(), and put it under a combined guard of self._tls and self.conn.shutdown. The self._tls bit is there to prevent getting stuck on this condition, and self.conn.shutdown is there to prevent the code being called by instances where the patched luasec isn't deployed. The destroy() method can be called from various places and is read by me as the "we give up" error path. To accommodate for these unexpected entrypoints I've added a single call to self.conn:shutdown() to prevent the socket being put into blocking mode. I have no expectations that there is any other use here. Same as previous, the self.conn.shutdown check is there to make sure it's not called on unpatched luasec deployments and self._tls is there to make sure we don't call shutdown() on tcp sockets. I wouldn't recommend logging of the conn:shutdown() error inside close(), since a lot of clients simply close the connection before SSL_shutdown() is done.
author Martijn van Duren <martijn@openbsd.org>
date Thu, 06 Feb 2025 15:04:38 +0000
parent 13485:3bdbaba15c00
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
13484
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
9 local usermanager = require "prosody.core.usermanager";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
10 local id = require "prosody.util.id";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
11 local jid = require "prosody.util.jid";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
12 local st = require "prosody.util.stanza";
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
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
14 local hosts = prosody.hosts;
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
16 function send_to_online(message, host)
13484
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
17 host = host or module.host;
3275
05c1d8269043 mod_announce: Changed a global variable to local.
Waqas Hussain <waqas20@gmail.com>
parents: 3228
diff changeset
18 local sessions;
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
19 if host then
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
20 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
21 else
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
22 sessions = hosts;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
23 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
24
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
25 local c = 0;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
26 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
27 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
28 message.attr.from = hostname;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
29 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
30 c = c + 1;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
31 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
32 module:send(message);
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
33 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
34 end
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 return c;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
38 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
39
13484
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
40 function send_to_all(message, host)
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
41 host = host or module.host;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
42 local c = 0;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
43 for username in usermanager.users(host) do
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
44 message.attr.to = username.."@"..host;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
45 module:send(st.clone(message));
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
46 c = c + 1;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
47 end
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
48 return c;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
49 end
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
50
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
51 function send_to_role(message, role, host)
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
52 host = host or module.host;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
53 local c = 0;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
54 for _, recipient_jid in ipairs(usermanager.get_jids_with_role(role, host)) do
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
55 message.attr.to = recipient_jid;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
56 module:send(st.clone(message));
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
57 c = c + 1;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
58 end
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
59 return c;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
60 end
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
61
12642
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 10545
diff changeset
62 module:default_permission("prosody:admin", ":send-announcement");
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
63
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
64 -- Old <message>-based jabberd-style announcement sending
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
65 function handle_announcement(event)
8974
71500c68fed4 mod_announce: Fix luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 8960
diff changeset
66 local stanza = event.stanza;
10545
2fbcdf6da331 mod_announce: Silence luacheck warning about unused variable
Kim Alvefur <zash@zash.se>
parents: 10026
diff changeset
67 -- luacheck: ignore 211/node
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
68 local node, host, resource = jid.split(stanza.attr.to);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
69
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 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
71 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
72 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
73
12642
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 10545
diff changeset
74 if not module:may(":send-announcement", event) then
9061f9621330 Switch to a new role-based authorization framework, removing is_admin()
Matthew Wild <mwild1@gmail.com>
parents: 10545
diff changeset
75 -- Not allowed!
3278
5ca2ed58788f mod_announce: A little cleanup.
Waqas Hussain <waqas20@gmail.com>
parents: 3276
diff changeset
76 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
77 return;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
79
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 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
81 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
82 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
83 message.attr.from = host;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
84
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
85 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
86 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
87 return true;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
89 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
90
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
91 -- Ad-hoc command (XEP-0133)
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12642
diff changeset
92 local dataforms_new = require "prosody.util.dataforms".new;
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
93 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
94 title = "Making an Announcement";
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
95 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
96
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
97 { 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
98 { 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
99 { 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
100 };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
101
8974
71500c68fed4 mod_announce: Fix luacheck warnings
Kim Alvefur <zash@zash.se>
parents: 8960
diff changeset
102 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
103 if state then
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
104 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
105 return { status = "canceled" };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
106 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
107
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
108 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
109
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
110 module:log("info", "Sending server announcement to all online users");
13484
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
111 local message = st.message({type = "headline"}, fields.announcement):up();
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
112 if fields.subject and fields.subject ~= "" then
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
113 message:text_tag("subject", fields.subject);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
114 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
115
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
116 local count = send_to_online(message, data.to);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5370
diff changeset
117
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
118 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
119 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
120 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
121 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
122 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
123 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
124
8960
9d0d1e427b82 mod_announce: Depend on mod_adhoc for consistent behaviour (thanks meaz, Link Mauve)
Kim Alvefur <zash@zash.se>
parents: 8680
diff changeset
125 module:depends "adhoc";
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3199
diff changeset
126 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
127 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
128 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
129
13484
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
130 module:add_item("shell-command", {
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
131 section = "announce";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
132 section_desc = "Broadcast announcements to users";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
133 name = "all";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
134 desc = "Send announcement to all users on the host";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
135 args = {
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
136 { name = "host", type = "string" };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
137 { name = "text", type = "string" };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
138 };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
139 host_selector = "host";
13485
3bdbaba15c00 mod_announce: Suppress luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents: 13484
diff changeset
140 handler = function(self, host, text) --luacheck: ignore 212/self
13484
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
141 local msg = st.message({ from = host, id = id.short() })
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
142 :text_tag("body", text);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
143 local count = send_to_all(msg, host);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
144 return true, ("Announcement sent to %d users"):format(count);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
145 end;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
146 });
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
147
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
148 module:add_item("shell-command", {
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
149 section = "announce";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
150 section_desc = "Broadcast announcements to users";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
151 name = "online";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
152 desc = "Send announcement to all online users on the host";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
153 args = {
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
154 { name = "host", type = "string" };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
155 { name = "text", type = "string" };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
156 };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
157 host_selector = "host";
13485
3bdbaba15c00 mod_announce: Suppress luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents: 13484
diff changeset
158 handler = function(self, host, text) --luacheck: ignore 212/self
13484
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
159 local msg = st.message({ from = host, id = id.short(), type = "headline" })
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
160 :text_tag("body", text);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
161 local count = send_to_online(msg, host);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
162 return true, ("Announcement sent to %d users"):format(count);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
163 end;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
164 });
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
165
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
166 module:add_item("shell-command", {
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
167 section = "announce";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
168 section_desc = "Broadcast announcements to users";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
169 name = "role";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
170 desc = "Send announcement to users with a specific role on the host";
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
171 args = {
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
172 { name = "host", type = "string" };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
173 { name = "role", type = "string" };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
174 { name = "text", type = "string" };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
175 };
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
176 host_selector = "host";
13485
3bdbaba15c00 mod_announce: Suppress luacheck warnings
Matthew Wild <mwild1@gmail.com>
parents: 13484
diff changeset
177 handler = function(self, host, role, text) --luacheck: ignore 212/self
13484
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
178 local msg = st.message({ from = host, id = id.short() })
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
179 :text_tag("body", text);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
180 local count = send_to_role(msg, role, host);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
181 return true, ("Announcement sent to %d users"):format(count);
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
182 end;
e22609460975 mod_announce: Add shell commands and APIs for sending to all/online/roles
Matthew Wild <mwild1@gmail.com>
parents: 12977
diff changeset
183 });