plugins/mod_announce.lua

Fri, 11 Jun 2010 12:07:25 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 11 Jun 2010 12:07:25 +0100
changeset 3228
65e5dfcf5a9f
parent 3198
0badae62de28
child 3275
05c1d8269043
permissions
-rw-r--r--

mod_announce: Add ad-hoc command for sending announcements \o/

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
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
14 function send_to_online(message, server)
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
15 if server then
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
16 sessions = { [server] = hosts[server] };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
17 else
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
18 sessions = hosts;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
19 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
20
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
21 local c = 0;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
22 for hostname, host_session in pairs(sessions) do
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
23 if host_session.sessions then
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
24 message.attr.from = hostname;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
25 for username in pairs(host_session.sessions) do
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
26 c = c + 1;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
27 message.attr.to = username.."@"..hostname;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
28 core_post_stanza(host_session, message);
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
29 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
30 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
31 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
32
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
33 return c;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
34 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
35
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
36
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
37 -- Old <message>-based jabberd-style announcement sending
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 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
39 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
40 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
41
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 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
43 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
44 end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45
1397
4c7b8b8ab569 mod_announce: Work with non-local admins
Waqas Hussain <waqas20@gmail.com>
parents: 1396
diff changeset
46 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
47 -- Not an admin? Not allowed!
1397
4c7b8b8ab569 mod_announce: Work with non-local admins
Waqas Hussain <waqas20@gmail.com>
parents: 1396
diff changeset
48 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
49 return;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 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
53 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
54 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
55 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
56 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
57
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
58 local c = send_to_online(message, host);
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 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
60 return true;
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
62 module:hook("message/host", handle_announcement);
1385
8999dd4253f9 mod_announce: New module to send a message to all online users
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
3228
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
64 -- Ad-hoc command (XEP-0133)
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
65 local dataforms_new = require "util.dataforms".new;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
66 local announce_layout = dataforms_new{
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
67 title = "Making an Announcement";
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
68 instructions = "Fill out this form to make an announcement to all\nactive users of this service.";
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
69
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
70 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
71 { name = "subject", type = "text-single", label = "Subject" };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
72 { name = "announcement", type = "text-multi", required = true, label = "Announcement" };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
73 };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
74
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
75 function announce_handler(self, data, state)
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
76 if state then
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
77 if data.action == "cancel" then
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
78 return { status = "canceled" };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
79 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
80
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
81 local fields = announce_layout:data(data.form);
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
82
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
83 module:log("info", "Sending server announcement to all online users");
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
84 local message = st.message({type = "headline"}, fields.announcement):up()
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
85 :tag("subject"):text(fields.subject or "Announcement");
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
86
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
87 local count = send_to_online(message, data.to);
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
88
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
89 module:log("info", "Announcement sent to %d online users", count);
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
90 return { status = "completed", info = ("Announcement sent to %d online users"):format(count) };
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
91 else
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
92 return { status = "executing", form = announce_layout }, "executing";
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
93 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
94
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
95 return true;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
96 end
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
97
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
98 local adhoc_new = module:require "adhoc".new;
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
99 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
100 module:add_item("adhoc", announce_desc);
65e5dfcf5a9f mod_announce: Add ad-hoc command for sending announcements \o/
Matthew Wild <mwild1@gmail.com>
parents: 3198
diff changeset
101

mercurial