mod_auth_anonymous: Fixed a syntax error.
[prosody.git] / plugins / mod_announce.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local st, jid, set = require "util.stanza", require "util.jid", require "util.set";
10
11 local is_admin = require "core.usermanager".is_admin;
12 local admins = set.new(config.get(module:get_host(), "core", "admins"));
13
14 function handle_announcement(data)
15         local origin, stanza = data.origin, data.stanza;
16         local host, resource = select(2, jid.split(stanza.attr.to));
17         
18         if resource ~= "announce/online" then
19                 return; -- Not an announcement
20         end
21         
22         if not is_admin(stanza.attr.from) then
23                 -- Not an admin? Not allowed!
24                 module:log("warn", "Non-admin %s tried to send server announcement", tostring(jid.bare(stanza.attr.from)));
25                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
26                 return;
27         end
28         
29         module:log("info", "Sending server announcement to all online users");
30         local host_session = hosts[host];
31         local message = st.clone(stanza);
32         message.attr.type = "headline";
33         message.attr.from = host;
34         
35         local c = 0;
36         for user in pairs(host_session.sessions) do
37                 c = c + 1;
38                 message.attr.to = user.."@"..host;
39                 core_post_stanza(host_session, message);
40         end
41         
42         module:log("info", "Announcement sent to %d online users", c);
43         return true;
44 end
45
46 module:hook("message/host", handle_announcement);