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 hosts
= prosody
.hosts
;
12 local is_admin
= require
"core.usermanager".is_admin
;
14 function send_to_online(message
, host
)
17 sessions
= { [host
] = hosts
[host
] };
23 for hostname
, host_session
in pairs(sessions
) do
24 if host_session
.sessions
then
25 message
.attr
.from
= hostname
;
26 for username
in pairs(host_session
.sessions
) do
28 message
.attr
.to
= username
.."@"..hostname
;
38 -- Old <message>-based jabberd-style announcement sending
39 function handle_announcement(event
)
40 local stanza
= event
.stanza
;
41 local node
, host
, resource
= jid
.split(stanza
.attr
.to
);
43 if resource
~= "announce/online" then
44 return; -- Not an announcement
47 if not is_admin(stanza
.attr
.from
) then
48 -- Not an admin? Not allowed!
49 module
:log("warn", "Non-admin '%s' tried to send server announcement", stanza
.attr
.from
);
53 module
:log("info", "Sending server announcement to all online users");
54 local message
= st
.clone(stanza
);
55 message
.attr
.type = "headline";
56 message
.attr
.from
= host
;
58 local c
= send_to_online(message
, host
);
59 module
:log("info", "Announcement sent to %d online users", c
);
62 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.";
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" };
75 function announce_handler(_
, data
, state
)
77 if data
.action
== "cancel" then
78 return { status
= "canceled" };
81 local fields
= announce_layout
:data(data
.form
);
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");
87 local count
= send_to_online(message
, data
.to
);
89 module
:log("info", "Announcement sent to %d online users", count
);
90 return { status
= "completed", info
= ("Announcement sent to %d online users"):format(count
) };
92 return { status
= "executing", actions
= {"next", "complete", default
= "complete"}, form
= announce_layout
}, "executing";
96 module
:depends
"adhoc";
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
);