Software /
code /
prosody
Comparison
plugins/mod_announce.lua @ 13484:e22609460975
mod_announce: Add shell commands and APIs for sending to all/online/roles
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 24 Apr 2024 11:50:13 +0100 |
parent | 12977:74b9e05af71e |
child | 13485:3bdbaba15c00 |
comparison
equal
deleted
inserted
replaced
13483:7b070909bd15 | 13484:e22609460975 |
---|---|
4 -- | 4 -- |
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 | 8 |
9 local st, jid = require "prosody.util.stanza", require "prosody.util.jid"; | 9 local usermanager = require "prosody.core.usermanager"; |
10 local id = require "prosody.util.id"; | |
11 local jid = require "prosody.util.jid"; | |
12 local st = require "prosody.util.stanza"; | |
10 | 13 |
11 local hosts = prosody.hosts; | 14 local hosts = prosody.hosts; |
12 | 15 |
13 function send_to_online(message, host) | 16 function send_to_online(message, host) |
17 host = host or module.host; | |
14 local sessions; | 18 local sessions; |
15 if host then | 19 if host then |
16 sessions = { [host] = hosts[host] }; | 20 sessions = { [host] = hosts[host] }; |
17 else | 21 else |
18 sessions = hosts; | 22 sessions = hosts; |
28 module:send(message); | 32 module:send(message); |
29 end | 33 end |
30 end | 34 end |
31 end | 35 end |
32 | 36 |
37 return c; | |
38 end | |
39 | |
40 function send_to_all(message, host) | |
41 host = host or module.host; | |
42 local c = 0; | |
43 for username in usermanager.users(host) do | |
44 message.attr.to = username.."@"..host; | |
45 module:send(st.clone(message)); | |
46 c = c + 1; | |
47 end | |
48 return c; | |
49 end | |
50 | |
51 function send_to_role(message, role, host) | |
52 host = host or module.host; | |
53 local c = 0; | |
54 for _, recipient_jid in ipairs(usermanager.get_jids_with_role(role, host)) do | |
55 message.attr.to = recipient_jid; | |
56 module:send(st.clone(message)); | |
57 c = c + 1; | |
58 end | |
33 return c; | 59 return c; |
34 end | 60 end |
35 | 61 |
36 module:default_permission("prosody:admin", ":send-announcement"); | 62 module:default_permission("prosody:admin", ":send-announcement"); |
37 | 63 |
80 end | 106 end |
81 | 107 |
82 local fields = announce_layout:data(data.form); | 108 local fields = announce_layout:data(data.form); |
83 | 109 |
84 module:log("info", "Sending server announcement to all online users"); | 110 module:log("info", "Sending server announcement to all online users"); |
85 local message = st.message({type = "headline"}, fields.announcement):up() | 111 local message = st.message({type = "headline"}, fields.announcement):up(); |
86 :tag("subject"):text(fields.subject or "Announcement"); | 112 if fields.subject and fields.subject ~= "" then |
113 message:text_tag("subject", fields.subject); | |
114 end | |
87 | 115 |
88 local count = send_to_online(message, data.to); | 116 local count = send_to_online(message, data.to); |
89 | 117 |
90 module:log("info", "Announcement sent to %d online users", count); | 118 module:log("info", "Announcement sent to %d online users", count); |
91 return { status = "completed", info = ("Announcement sent to %d online users"):format(count) }; | 119 return { status = "completed", info = ("Announcement sent to %d online users"):format(count) }; |
97 module:depends "adhoc"; | 125 module:depends "adhoc"; |
98 local adhoc_new = module:require "adhoc".new; | 126 local adhoc_new = module:require "adhoc".new; |
99 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin"); | 127 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin"); |
100 module:provides("adhoc", announce_desc); | 128 module:provides("adhoc", announce_desc); |
101 | 129 |
130 module:add_item("shell-command", { | |
131 section = "announce"; | |
132 section_desc = "Broadcast announcements to users"; | |
133 name = "all"; | |
134 desc = "Send announcement to all users on the host"; | |
135 args = { | |
136 { name = "host", type = "string" }; | |
137 { name = "text", type = "string" }; | |
138 }; | |
139 host_selector = "host"; | |
140 handler = function(self, host, text) | |
141 local msg = st.message({ from = host, id = id.short() }) | |
142 :text_tag("body", text); | |
143 local count = send_to_all(msg, host); | |
144 return true, ("Announcement sent to %d users"):format(count); | |
145 end; | |
146 }); | |
147 | |
148 module:add_item("shell-command", { | |
149 section = "announce"; | |
150 section_desc = "Broadcast announcements to users"; | |
151 name = "online"; | |
152 desc = "Send announcement to all online users on the host"; | |
153 args = { | |
154 { name = "host", type = "string" }; | |
155 { name = "text", type = "string" }; | |
156 }; | |
157 host_selector = "host"; | |
158 handler = function(self, host, text) | |
159 local msg = st.message({ from = host, id = id.short(), type = "headline" }) | |
160 :text_tag("body", text); | |
161 local count = send_to_online(msg, host); | |
162 return true, ("Announcement sent to %d users"):format(count); | |
163 end; | |
164 }); | |
165 | |
166 module:add_item("shell-command", { | |
167 section = "announce"; | |
168 section_desc = "Broadcast announcements to users"; | |
169 name = "role"; | |
170 desc = "Send announcement to users with a specific role on the host"; | |
171 args = { | |
172 { name = "host", type = "string" }; | |
173 { name = "role", type = "string" }; | |
174 { name = "text", type = "string" }; | |
175 }; | |
176 host_selector = "host"; | |
177 handler = function(self, host, role, text) | |
178 local msg = st.message({ from = host, id = id.short() }) | |
179 :text_tag("body", text); | |
180 local count = send_to_role(msg, role, host); | |
181 return true, ("Announcement sent to %d users"):format(count); | |
182 end; | |
183 }); |