# HG changeset patch # User Matthew Wild # Date 1344351075 -3600 # Node ID e318a341d332b04b90cafe6f519da0d0415494ad # Parent e781e63a49f41a9c8a5a4645b3033f41fc2df575 mod_broadcast: New module to set up a component that forwards received messages to all online users (similar to Openfire's broadcast module) (thanks Yann Verry) diff -r e781e63a49f4 -r e318a341d332 mod_broadcast/mod_broadcast.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_broadcast/mod_broadcast.lua Tue Aug 07 15:51:15 2012 +0100 @@ -0,0 +1,30 @@ +local allowed_senders = module:get_option_set("broadcast_senders", {}); + +local jid_bare = require "util.jid".bare; + +function send_to_online(message) + local c = 0; + for hostname, host_session in pairs(hosts) do + if host_session.sessions then + for username in pairs(host_session.sessions) do + c = c + 1; + message.attr.to = username.."@"..hostname; + module:send(message); + end + end + end + return c; +end + +function send_message(event) + local stanza = event.stanza; + if allowed_senders:contains(jid_bare(stanza.attr.from)) then + local c = send_to_online(stanza); + module:log("debug", "Broadcast stanza from %s to %d online users", stanza.attr.from, c); + return true; + else + module:log("warn", "Broadcasting is not allowed for %s", stanza.attr.from); + end +end + +module:hook("message/bare", send_message);