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