Software /
code /
prosody
Annotate
plugins/mod_announce.lua @ 1497:4c2c403ca04b
util.xmlrpc: Add create_request
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 08 Jul 2009 15:11:11 +0100 |
parent | 1397:4c7b8b8ab569 |
child | 1522:569d58d21612 |
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 |
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 |
1397
4c7b8b8ab569
mod_announce: Work with non-local admins
Waqas Hussain <waqas20@gmail.com>
parents:
1396
diff
changeset
|
14 if not is_admin(stanza.attr.from) 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! |
1397
4c7b8b8ab569
mod_announce: Work with non-local admins
Waqas Hussain <waqas20@gmail.com>
parents:
1396
diff
changeset
|
16 module:log("warn", "Non-admin %s tried to send server announcement", tostring(jid.bare(stanza.attr.from))); |
1385
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); |