|
1 local st, jid, set = require "util.stanza", require "util.jid", require "util.set"; |
|
2 |
|
3 local admins = set.new(config.get(module:get_host(), "core", "admins")); |
|
4 |
|
5 function handle_announcement(data) |
|
6 local origin, stanza = data.origin, data.stanza; |
|
7 local host, resource = select(2, jid.split(stanza.attr.to)); |
|
8 |
|
9 if resource ~= "announce/online" then |
|
10 return; -- Not an announcement |
|
11 end |
|
12 |
|
13 if not admins:contains(jid.bare(origin.full_jid)) then |
|
14 -- Not an admin? Not allowed! |
|
15 module:log("warn", "Non-admin %s tried to send server announcement", tostring(jid.bare(origin.full_jid))); |
|
16 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
|
17 return; |
|
18 end |
|
19 |
|
20 module:log("info", "Sending server announcement to all online users"); |
|
21 local host_session = hosts[host]; |
|
22 local message = st.clone(stanza); |
|
23 message.attr.type = "headline"; |
|
24 message.attr.from = host; |
|
25 |
|
26 local c = 0; |
|
27 for user in pairs(host_session.sessions) do |
|
28 c = c + 1; |
|
29 message.attr.to = user.."@"..host; |
|
30 core_post_stanza(host_session, message); |
|
31 end |
|
32 |
|
33 module:log("info", "Announcement sent to %d online users", c); |
|
34 return true; |
|
35 end |
|
36 |
|
37 module:hook("message/host", handle_announcement); |