plugins/mod_announce.lua
author Waqas Hussain <waqas20@gmail.com>
Tue, 23 Jun 2009 23:55:56 +0500
changeset 1396 ce3eb5f71899
parent 1385 8999dd4253f9
child 1397 4c7b8b8ab569
permissions -rw-r--r--
mod_announce: Use usermanager.is_admin to verify admin status
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
local st, jid, set = require "util.stanza", require "util.jid", require "util.set";
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
1396
ce3eb5f71899 mod_announce: Use usermanager.is_admin to verify admin status
Waqas Hussain <waqas20@gmail.com>
parents: 1385
diff changeset
     3
local is_admin = require "core.usermanager".is_admin;
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local admins = set.new(config.get(module:get_host(), "core", "admins"));
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
function handle_announcement(data)
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
	local origin, stanza = data.origin, data.stanza;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
	local host, resource = select(2, jid.split(stanza.attr.to));
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
	
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
	if resource ~= "announce/online" then
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
		return; -- Not an announcement
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
	end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
	
1396
ce3eb5f71899 mod_announce: Use usermanager.is_admin to verify admin status
Waqas Hussain <waqas20@gmail.com>
parents: 1385
diff changeset
    14
	if not is_admin(origin.full_jid) then
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
		-- Not an admin? Not allowed!
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
		module:log("warn", "Non-admin %s tried to send server announcement", tostring(jid.bare(origin.full_jid)));
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
		origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
		return;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
	end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
	
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
	module:log("info", "Sending server announcement to all online users");
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
	local host_session = hosts[host];
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	local message = st.clone(stanza);
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	message.attr.type = "headline";
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
	message.attr.from = host;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
	
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
	local c = 0;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
	for user in pairs(host_session.sessions) do
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
		c = c + 1;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
		message.attr.to = user.."@"..host;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
		core_post_stanza(host_session, message);
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
	end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
	
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
	module:log("info", "Announcement sent to %d online users", c);
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
	return true;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
module:hook("message/host", handle_announcement);