comparison mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua @ 124:843cadf36306

mod_adhoc*: Move state handling to mod_adhoc itself
author Florian Zeitz <florob@babelmonkeys.de>
date Sat, 23 Jan 2010 04:43:28 +0100
parents c04443ea114c
children bdd1641c159d
comparison
equal deleted inserted replaced
123:c04443ea114c 124:843cadf36306
19 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid"; 19 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
20 local dataforms_new = require "util.dataforms".new; 20 local dataforms_new = require "util.dataforms".new;
21 module:log("debug", module:get_name()); 21 module:log("debug", module:get_name());
22 local adhoc_new = module:require "adhoc".new; 22 local adhoc_new = module:require "adhoc".new;
23 23
24 local sessions = {};
25
26 local add_user_layout = dataforms_new{ 24 local add_user_layout = dataforms_new{
27 title = "Adding a User"; 25 title = "Adding a User";
28 instructions = "Fill out this form to add a user."; 26 instructions = "Fill out this form to add a user.";
29 27
30 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 28 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
85 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" }; 83 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
86 { name = "subject", type = "text-single", label = "Subject" }; 84 { name = "subject", type = "text-single", label = "Subject" };
87 { name = "announcement", type = "text-multi", required = true, label = "Announcement" }; 85 { name = "announcement", type = "text-multi", required = true, label = "Announcement" };
88 }; 86 };
89 87
90 function add_user_command_handler(self, data, sessid) 88 function add_user_command_handler(self, data, state)
91 if sessid and sessions[sessid] then 89 if state then
92 if data.action == "cancel" then 90 if data.action == "cancel" then
93 sessions[sessid] = nil; 91 return { status = "canceled" };
94 return { status = "canceled" }, sessid;
95 end 92 end
96 local fields = add_user_layout:data(data.form); 93 local fields = add_user_layout:data(data.form);
97 local username, host, resource = jid.split(fields.accountjid); 94 local username, host, resource = jid.split(fields.accountjid);
98 if (fields["password"] == fields["password-verify"]) and username and host and host == data.to then 95 if (fields["password"] == fields["password-verify"]) and username and host and host == data.to then
99 if usermanager_user_exists(username, host) then 96 if usermanager_user_exists(username, host) then
100 sessions[sessid] = nil; 97 return { status = "error", error = { type = "cancel", condition = "conflict", message = "Account already exists" } };
101 return { status = "error", error = { type = "cancel", condition = "conflict", message = "Account already exists" } }, sessid;
102 else 98 else
103 if usermanager_create_user(username, fields.password, host) then 99 if usermanager_create_user(username, fields.password, host) then
104 sessions[sessid] = nil;
105 module:log("info", "Created new account " .. username.."@"..host); 100 module:log("info", "Created new account " .. username.."@"..host);
106 return { status = "completed", info = "Account successfully created" }, sessid; 101 return { status = "completed", info = "Account successfully created" };
107 else 102 else
108 sessions[sessid] = nil;
109 return { status = "error", error = { type = "wait", condition = "internal-server-error", 103 return { status = "error", error = { type = "wait", condition = "internal-server-error",
110 message = "Failed to write data to disk" } }, sessid; 104 message = "Failed to write data to disk" } };
111 end 105 end
112 end 106 end
113 else 107 else
114 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]); 108 module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]);
115 sessions[sessid] = nil;
116 return { status = "error", error = { type = "cancel", condition = "conflict", 109 return { status = "error", error = { type = "cancel", condition = "conflict",
117 message = "Invalid data.\nPassword mismatch, or empty username" } }, sessid; 110 message = "Invalid data.\nPassword mismatch, or empty username" } };
118 end 111 end
119 else 112 else
120 local sessionid=uuid.generate(); 113 return { status = "executing", form = add_user_layout }, "executing";
121 sessions[sessionid] = "executing"; 114 end
122 return { status = "executing", form = add_user_layout }, sessionid; 115 end
123 end 116
124 end 117 function change_user_password_command_handler(self, data, state)
125 118 if state then
126 function change_user_password_command_handler(self, data, sessid) 119 if data.action == "cancel" then
127 if sessid and sessions[sessid] then 120 return { status = "canceled" };
128 if data.action == "cancel" then
129 sessions[sessid] = nil;
130 return { status = "canceled" }, sessid;
131 end 121 end
132 local fields = change_user_password_layout:data(data.form); 122 local fields = change_user_password_layout:data(data.form);
133 local username, host, resource = jid.split(fields.accountjid); 123 local username, host, resource = jid.split(fields.accountjid);
134 if usermanager_user_exists(username, host) and usermanager_create_user(username, fields.password, host) then 124 if usermanager_user_exists(username, host) and usermanager_create_user(username, fields.password, host) then
135 return { status = "completed", info = "Password successfully changed" }, sessid; 125 return { status = "completed", info = "Password successfully changed" };
136 else 126 else
137 sessions[sessid] = nil; 127 return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } };
138 return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } }, sessid; 128 end
139 end 129 else
140 sessions[sessid] = nil; 130 return { status = "executing", form = change_user_password_layout }, "executing";
141 return { status = "canceled" }, sessid; 131 end
142 else 132 end
143 local sessionid=uuid.generate(); 133
144 sessions[sessionid] = "executing"; 134 function delete_user_command_handler(self, data, state)
145 return { status = "executing", form = change_user_password_layout }, sessionid; 135 if state then
146 end 136 if data.action == "cancel" then
147 end 137 return { status = "canceled" };
148
149 function delete_user_command_handler(self, data, sessid)
150 if sessid and sessions[sessid] then
151 if data.action == "cancel" then
152 sessions[sessid] = nil;
153 return { status = "canceled" }, sessid;
154 end 138 end
155 local fields = delete_user_layout:data(data.form); 139 local fields = delete_user_layout:data(data.form);
156 local failed = {}; 140 local failed = {};
157 local succeeded = {}; 141 local succeeded = {};
158 for _, aJID in ipairs(fields.accountjids) do 142 for _, aJID in ipairs(fields.accountjids) do
163 else 147 else
164 module:log("debug", "Tried to delete non-existant user "..aJID); 148 module:log("debug", "Tried to delete non-existant user "..aJID);
165 failed[#failed+1] = aJID; 149 failed[#failed+1] = aJID;
166 end 150 end
167 end 151 end
168 sessions[sessid] = nil;
169 return {status = "completed", info = (#succeeded ~= 0 and 152 return {status = "completed", info = (#succeeded ~= 0 and
170 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "").. 153 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
171 (#failed ~= 0 and 154 (#failed ~= 0 and
172 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") }, sessid; 155 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
173 else 156 else
174 local sessionid=uuid.generate(); 157 return { status = "executing", form = delete_user_layout }, "executing";
175 sessions[sessionid] = "executing"; 158 end
176 return { status = "executing", form = delete_user_layout }, sessionid; 159 end
177 end 160
178 end 161 function get_user_password_handler(self, data, state)
179 162 if state then
180 function get_user_password_handler(self, data, sessid) 163 if data.action == "cancel" then
181 if sessid and sessions[sessid] then 164 return { status = "canceled" };
182 if data.action == "cancel" then
183 sessions[sessid] = nil;
184 return { status = "canceled" }, sessid;
185 end 165 end
186 local fields = get_user_password_layout:data(data.form); 166 local fields = get_user_password_layout:data(data.form);
187 local user, host, resource = jid.split(fields.accountjid); 167 local user, host, resource = jid.split(fields.accountjid);
188 local accountjid = ""; 168 local accountjid = "";
189 local password = ""; 169 local password = "";
190 if usermanager_user_exists(user, host) then 170 if usermanager_user_exists(user, host) then
191 accountjid = fields.accountjid; 171 accountjid = fields.accountjid;
192 password = usermanager_get_password(user, host); 172 password = usermanager_get_password(user, host);
193 else 173 else
194 sessions[sessid] = nil; 174 return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } };
195 return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } }, sessid; 175 end
196 end 176 return { status = "completed", result = { layout = get_user_password_result_layout, data = {accountjid = accountjid, password = password} } };
197 sessions[sessid] = nil; 177 else
198 return { status = "completed", result = { layout = get_user_password_result_layout, data = {accountjid = accountjid, password = password} } }, sessid; 178 return { status = "executing", form = get_user_password_layout }, "executing";
199 else 179 end
200 local sessionid=uuid.generate(); 180 end
201 sessions[sessionid] = "executing"; 181
202 return { status = "executing", form = get_user_password_layout }, sessionid; 182 function get_online_users_command_handler(self, data, state)
203 end 183 if state then
204 end 184 if data.action == "cancel" then
205 185 return { status = "canceled" };
206 function get_online_users_command_handler(self, data, sessid)
207 if sessid and sessions[sessid] then
208 if data.action == "cancel" then
209 sessions[sessid] = nil;
210 return { status = "canceled" }, sessid;
211 end 186 end
212 187
213 local fields = add_user_layout:data(data.form); 188 local fields = add_user_layout:data(data.form);
214 189
215 local max_items = nil 190 local max_items = nil
223 break; 198 break;
224 end 199 end
225 users = ((users and users.."\n") or "")..(username.."@"..data.to); 200 users = ((users and users.."\n") or "")..(username.."@"..data.to);
226 count = count + 1; 201 count = count + 1;
227 end 202 end
228 sessions[sessid] = nil; 203 return { status = "completed", result = {layout = get_online_users_result_layout, data = {onlineuserjids=users}} };
229 return { status = "completed", result = {layout = get_online_users_result_layout, data = {onlineuserjids=users}} }, sessid; 204 else
230 else 205 return { status = "executing", form = get_online_users_layout }, "executing";
231 local sessionid=uuid.generate(); 206 end
232 sessions[sessionid] = "executing"; 207 end
233 return { status = "executing", form = get_online_users_layout }, sessionid; 208
234 end 209 function announce_handler(self, data, state)
235 end 210 if state then
236 211 if data.action == "cancel" then
237 function announce_handler(self, data, sessid) 212 return { status = "canceled" };
238 if sessid and sessions[sessid] then
239 if data.action == "cancel" then
240 sessions[sessid] = nil;
241 return { status = "canceled" }, sessid;
242 end 213 end
243 214
244 local fields = announce_layout:data(data.form); 215 local fields = announce_layout:data(data.form);
245 216
246 module:log("info", "Sending server announcement to all online users"); 217 module:log("info", "Sending server announcement to all online users");
254 message.attr.to = user.."@"..data.to; 225 message.attr.to = user.."@"..data.to;
255 core_post_stanza(host_session, message); 226 core_post_stanza(host_session, message);
256 end 227 end
257 228
258 module:log("info", "Announcement sent to %d online users", c); 229 module:log("info", "Announcement sent to %d online users", c);
259 sessions[sessid] = nil; 230 return { status = "completed", info = "Announcement sent." };
260 return { status = "completed", info = "Announcement sent." }, sessid; 231 else
261 else 232 return { status = "executing", form = announce_layout }, "executing";
262 local sessionid=uuid.generate();
263 sessions[sessionid] = "executing";
264 return { status = "executing", form = announce_layout }, sessionid;
265 end 233 end
266 234
267 return true; 235 return true;
268 end 236 end
269 237