2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local st, jid = require "util.stanza", require "util.jid";
11 local is_admin = require "core.usermanager".is_admin;
13 function send_to_online(message, host)
16 sessions = { [host] = hosts[host] };
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
27 message.attr.to = username.."@"..hostname;
37 -- Old <message>-based jabberd-style announcement sending
38 function handle_announcement(event)
39 local origin, stanza = event.origin, event.stanza;
40 local node, host, resource = jid.split(stanza.attr.to);
42 if resource ~= "announce/online" then
43 return; -- Not an announcement
46 if not is_admin(stanza.attr.from) then
47 -- Not an admin? Not allowed!
48 module:log("warn", "Non-admin '%s' tried to send server announcement", stanza.attr.from);
52 module:log("info", "Sending server announcement to all online users");
53 local message = st.clone(stanza);
54 message.attr.type = "headline";
55 message.attr.from = host;
57 local c = send_to_online(message, host);
58 module:log("info", "Announcement sent to %d online users", c);
61 module:hook("message/host", handle_announcement);
63 -- Ad-hoc command (XEP-0133)
64 local dataforms_new = require "util.dataforms".new;
65 local announce_layout = dataforms_new{
66 title = "Making an Announcement";
67 instructions = "Fill out this form to make an announcement to all\nactive users of this service.";
69 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
70 { name = "subject", type = "text-single", label = "Subject" };
71 { name = "announcement", type = "text-multi", required = true, label = "Announcement" };
74 function announce_handler(self, data, state)
76 if data.action == "cancel" then
77 return { status = "canceled" };
80 local fields = announce_layout:data(data.form);
82 module:log("info", "Sending server announcement to all online users");
83 local message = st.message({type = "headline"}, fields.announcement):up()
84 :tag("subject"):text(fields.subject or "Announcement");
86 local count = send_to_online(message, data.to);
88 module:log("info", "Announcement sent to %d online users", count);
89 return { status = "completed", info = ("Announcement sent to %d online users"):format(count) };
91 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = announce_layout }, "executing";
97 local adhoc_new = module:require "adhoc".new;
98 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
99 module:provides("adhoc", announce_desc);