plugins/mod_announce.lua

Fri, 11 Jun 2010 21:30:24 +0500

author
Waqas Hussain <waqas20@gmail.com>
date
Fri, 11 Jun 2010 21:30:24 +0500
changeset 3239
5ea90ee96022
parent 3198
0badae62de28
child 3228
65e5dfcf5a9f
permissions
-rw-r--r--

sessionmanager: Fixed a traceback on invalid usernames (typo in previous commit).

1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1397
diff changeset
8
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 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
10
1396
ce3eb5f71899 mod_announce: Use usermanager.is_admin to verify admin status
Waqas Hussain <waqas20@gmail.com>
parents: 1385
diff changeset
11 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
12 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
13
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 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
15 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
16 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
17
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 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
19 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
20 end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
1397
4c7b8b8ab569 mod_announce: Work with non-local admins
Waqas Hussain <waqas20@gmail.com>
parents: 1396
diff changeset
22 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
23 -- Not an admin? Not allowed!
1397
4c7b8b8ab569 mod_announce: Work with non-local admins
Waqas Hussain <waqas20@gmail.com>
parents: 1396
diff changeset
24 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
25 return;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 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
29 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
30 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
31 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
32 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
33
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local c = 0;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 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
36 c = c + 1;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 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
38 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
39 end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 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
42 return true;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 module:hook("message/host", handle_announcement);

mercurial