Annotate

mod_broadcast/mod_broadcast.lua @ 1015:0fc9e1f086c1

mod_broadcast: Allow admins to broadcast
author Matthew Wild <mwild1@gmail.com>
date Sun, 19 May 2013 22:09:54 +0100
parent 786:e318a341d332
child 1016:9f7c97e55593
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1015
0fc9e1f086c1 mod_broadcast: Allow admins to broadcast
Matthew Wild <mwild1@gmail.com>
parents: 786
diff changeset
1 local is_admin = require "core.usermanager".is_admin;
786
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local allowed_senders = module:get_option_set("broadcast_senders", {});
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local jid_bare = require "util.jid".bare;
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 function send_to_online(message)
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local c = 0;
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 for hostname, host_session in pairs(hosts) do
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if host_session.sessions then
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 for username in pairs(host_session.sessions) do
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 c = c + 1;
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 message.attr.to = username.."@"..hostname;
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 module:send(message);
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return c;
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 function send_message(event)
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local stanza = event.stanza;
1015
0fc9e1f086c1 mod_broadcast: Allow admins to broadcast
Matthew Wild <mwild1@gmail.com>
parents: 786
diff changeset
22 local from = stanza.attr.from;
0fc9e1f086c1 mod_broadcast: Allow admins to broadcast
Matthew Wild <mwild1@gmail.com>
parents: 786
diff changeset
23 if is_admin(from) or allowed_senders:contains(jid_bare(from)) then
786
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local c = send_to_online(stanza);
1015
0fc9e1f086c1 mod_broadcast: Allow admins to broadcast
Matthew Wild <mwild1@gmail.com>
parents: 786
diff changeset
25 module:log("debug", "Broadcast stanza from %s to %d online users", from, c);
786
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return true;
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 else
1015
0fc9e1f086c1 mod_broadcast: Allow admins to broadcast
Matthew Wild <mwild1@gmail.com>
parents: 786
diff changeset
28 module:log("warn", "Broadcasting is not allowed for %s", from);
786
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
e318a341d332 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)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 module:hook("message/bare", send_message);