comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 67:e839b4453387

mod_adhoc_cmd_admin: Add announce command
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 30 Oct 2009 01:32:12 +0100
parents 59f490390528
children b47216512a1d
comparison
equal deleted inserted replaced
66:b86ae5e21a56 67:e839b4453387
36 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 36 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
37 { name = "max_items", type = "list-single", label = "Maximum number of users", 37 { name = "max_items", type = "list-single", label = "Maximum number of users",
38 value = { "25", "50", "75", "100", "150", "200", "all" } }; 38 value = { "25", "50", "75", "100", "150", "200", "all" } };
39 }; 39 };
40 40
41 local announce_layout = dataforms_new{
42 title = "Making an Announcement";
43 instructions = "Fill out this form to make an announcement to all\nactive users of this service.";
44
45 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
46 { name = "subject", type = "text-single", label = "Subject" };
47 { name = "announcement", type = "text-multi", required = true, label = "Announcement" };
48 };
49
41 function add_user_command_handler(item, origin, stanza) 50 function add_user_command_handler(item, origin, stanza)
42 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then 51 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then
43 if stanza.tags[1].attr.action == "cancel" then 52 if stanza.tags[1].attr.action == "cancel" then
44 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid))); 53 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)));
45 sessions[stanza.tags[1].attr.sessionid] = nil; 54 sessions[stanza.tags[1].attr.sessionid] = nil;
46 return true; 55 return true;
47 end 56 end
48 form = stanza.tags[1]:child_with_ns("jabber:x:data"); 57 local form = stanza.tags[1]:child_with_ns("jabber:x:data");
49 local fields = add_user_layout:data(form); 58 local fields = add_user_layout:data(form);
50 local username, host, resource = jid.split(fields.accountjid); 59 local username, host, resource = jid.split(fields.accountjid);
51 if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then 60 if (fields.password == fields["password-verify"]) and username and host and host == stanza.attr.to then
52 if usermanager_user_exists(username, host) then 61 if usermanager_user_exists(username, host) then
53 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up() 62 origin.send(st.error_reply(stanza, "cancel", "conflict", "Account already exists"):up()
94 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid))); 103 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)));
95 sessions[stanza.tags[1].attr.sessionid] = nil; 104 sessions[stanza.tags[1].attr.sessionid] = nil;
96 return true; 105 return true;
97 end 106 end
98 107
99 form = stanza.tags[1]:child_with_ns("jabber:x:data"); 108 local form = stanza.tags[1]:child_with_ns("jabber:x:data");
100 local fields = add_user_layout:data(form); 109 local fields = add_user_layout:data(form);
101 110
102 local max_items = nil 111 local max_items = nil
103 if fields.max_items ~= "all" then 112 if fields.max_items ~= "all" then
104 max_items = tonumber(fields.max_items); 113 max_items = tonumber(fields.max_items);
126 end 135 end
127 136
128 return true; 137 return true;
129 end 138 end
130 139
140 function announce_handler(item, origin, stanza)
141 if stanza.tags[1].attr.sessionid and sessions[stanza.tags[1].attr.sessionid] then
142 if stanza.tags[1].attr.action == "cancel" then
143 origin.send(st.reply(stanza):add_child(item:cmdtag("canceled", stanza.tags[1].attr.sessionid)));
144 sessions[stanza.tags[1].attr.sessionid] = nil;
145 return true;
146 end
147
148 local form = stanza.tags[1]:child_with_ns("jabber:x:data");
149 local fields = add_user_layout:data(form);
150
151 module:log("info", "Sending server announcement to all online users");
152 local host_session = hosts[stanza.attr.to];
153 local message = st.message({type = "headline", from = stanza.attr.to}, fields.announcement):up()
154 :tag("subject"):text(fields.subject or "Announcement");
155
156 local c = 0;
157 for user in pairs(host_session.sessions) do
158 c = c + 1;
159 message.attr.to = user.."@"..stanza.attr.to;
160 core_post_stanza(host_session, message);
161 end
162
163 module:log("info", "Announcement sent to %d online users", c);
164
165 origin.send(st.reply(stanza):add_child(item:cmdtag("completed", stanza.tags[1].attr.sessionid)
166 :tag("note"):text("Announcement sent.")));
167 sessions[stanza.tags[1].attr.sessionid] = nil;
168 return true;
169 else
170 local sessionid=uuid.generate();
171 sessions[sessionid] = "executing";
172 origin.send(st.reply(stanza):add_child(item:cmdtag("executing", sessionid):add_child(announce_layout:form())));
173 end
174
175 return true;
176 end
177
131 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin"); 178 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
132 local get_online_users_desc = adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users", get_online_users_command_handler, "admin"); 179 local get_online_users_desc = adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users", get_online_users_command_handler, "admin");
180 local announce_desc = adhoc_new("Send Announcement to Online Users", "http://jabber.org/protocol/admin#announce", announce_handler, "admin");
133 181
134 function module.unload() 182 function module.unload()
135 module:remove_item("adhoc", add_user_desc); 183 module:remove_item("adhoc", add_user_desc);
136 module:remove_item("adhoc", get_online_users_desc); 184 module:remove_item("adhoc", get_online_users_desc);
185 module:remove_item("adhoc", announce_desc);
137 end 186 end
138 187
139 module:add_item("adhoc", add_user_desc); 188 module:add_item("adhoc", add_user_desc);
140 module:add_item("adhoc", get_online_users_desc); 189 module:add_item("adhoc", get_online_users_desc);
190 module:add_item("adhoc", announce_desc);