Software /
code /
prosody
Annotate
plugins/mod_announce.lua @ 1385:8999dd4253f9
mod_announce: New module to send a message to all online users
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 22 Jun 2009 18:05:36 +0100 |
child | 1396:ce3eb5f71899 |
rev | line source |
---|---|
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 |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 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
|
4 |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 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
|
6 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
|
7 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
|
8 |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 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
|
10 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
|
11 end |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 if not admins:contains(jid.bare(origin.full_jid)) then |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 -- 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
|
15 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
|
16 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
|
17 return; |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local c = 0; |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 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
|
28 c = c + 1; |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 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
|
30 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
|
31 end |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 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
|
34 return true; |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
8999dd4253f9
mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 module:hook("message/host", handle_announcement); |