Comparison

plugins/mod_announce.lua @ 3228:65e5dfcf5a9f

mod_announce: Add ad-hoc command for sending announcements \o/
author Matthew Wild <mwild1@gmail.com>
date Fri, 11 Jun 2010 12:07:25 +0100
parent 3199:0badae62de28
child 3275:05c1d8269043
comparison
equal deleted inserted replaced
3227:c80d052569c8 3228:65e5dfcf5a9f
9 local st, jid, set = require "util.stanza", require "util.jid", require "util.set"; 9 local st, jid, set = require "util.stanza", require "util.jid", require "util.set";
10 10
11 local is_admin = require "core.usermanager".is_admin; 11 local is_admin = require "core.usermanager".is_admin;
12 local admins = set.new(config.get(module:get_host(), "core", "admins")); 12 local admins = set.new(config.get(module:get_host(), "core", "admins"));
13 13
14 function send_to_online(message, server)
15 if server then
16 sessions = { [server] = hosts[server] };
17 else
18 sessions = hosts;
19 end
20
21 local c = 0;
22 for hostname, host_session in pairs(sessions) do
23 if host_session.sessions then
24 message.attr.from = hostname;
25 for username in pairs(host_session.sessions) do
26 c = c + 1;
27 message.attr.to = username.."@"..hostname;
28 core_post_stanza(host_session, message);
29 end
30 end
31 end
32
33 return c;
34 end
35
36
37 -- Old <message>-based jabberd-style announcement sending
14 function handle_announcement(data) 38 function handle_announcement(data)
15 local origin, stanza = data.origin, data.stanza; 39 local origin, stanza = data.origin, data.stanza;
16 local host, resource = select(2, jid.split(stanza.attr.to)); 40 local host, resource = select(2, jid.split(stanza.attr.to));
17 41
18 if resource ~= "announce/online" then 42 if resource ~= "announce/online" then
29 local host_session = hosts[host]; 53 local host_session = hosts[host];
30 local message = st.clone(stanza); 54 local message = st.clone(stanza);
31 message.attr.type = "headline"; 55 message.attr.type = "headline";
32 message.attr.from = host; 56 message.attr.from = host;
33 57
34 local c = 0; 58 local c = send_to_online(message, host);
35 for user in pairs(host_session.sessions) do
36 c = c + 1;
37 message.attr.to = user.."@"..host;
38 core_post_stanza(host_session, message);
39 end
40
41 module:log("info", "Announcement sent to %d online users", c); 59 module:log("info", "Announcement sent to %d online users", c);
42 return true; 60 return true;
43 end 61 end
62 module:hook("message/host", handle_announcement);
44 63
45 module:hook("message/host", handle_announcement); 64 -- Ad-hoc command (XEP-0133)
65 local dataforms_new = require "util.dataforms".new;
66 local announce_layout = dataforms_new{
67 title = "Making an Announcement";
68 instructions = "Fill out this form to make an announcement to all\nactive users of this service.";
69
70 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
71 { name = "subject", type = "text-single", label = "Subject" };
72 { name = "announcement", type = "text-multi", required = true, label = "Announcement" };
73 };
74
75 function announce_handler(self, data, state)
76 if state then
77 if data.action == "cancel" then
78 return { status = "canceled" };
79 end
80
81 local fields = announce_layout:data(data.form);
82
83 module:log("info", "Sending server announcement to all online users");
84 local message = st.message({type = "headline"}, fields.announcement):up()
85 :tag("subject"):text(fields.subject or "Announcement");
86
87 local count = send_to_online(message, data.to);
88
89 module:log("info", "Announcement sent to %d online users", count);
90 return { status = "completed", info = ("Announcement sent to %d online users"):format(count) };
91 else
92 return { status = "executing", form = announce_layout }, "executing";
93 end
94
95 return true;
96 end
97
98 local adhoc_new = module:require "adhoc".new;
99 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
100 module:add_item("adhoc", announce_desc);
101