Merge 0.7->trunk
[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                 return;
26         end
27         
28         module:log("info", "Sending server announcement to all online users");
29         local host_session = hosts[host];
30         local message = st.clone(stanza);
31         message.attr.type = "headline";
32         message.attr.from = host;
33         
34         local c = 0;
35         for user in pairs(host_session.sessions) do
36                 c = c + 1;
37                 message.attr.to = user.."@"..host;
38                 core_post_stanza(host_session, message);
39         end
40         
41         module:log("info", "Announcement sent to %d online users", c);
42         return true;
43 end
44
45 module:hook("message/host", handle_announcement);