Software /
code /
prosody-modules
Comparison
mod_admin_web/admin_web/mod_admin_web.lua @ 753:9d5731af2c27
Merge with Oliver Gerlich
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 27 Jul 2012 14:29:59 +0100 |
parent | 696:da69b65288e4 |
parent | 747:e54b92a26d40 |
child | 761:48f8b312a509 |
comparison
equal
deleted
inserted
replaced
752:9bbd99f2057a | 753:9d5731af2c27 |
---|---|
19 | 19 |
20 local st = require "util.stanza"; | 20 local st = require "util.stanza"; |
21 local uuid_generate = require "util.uuid".generate; | 21 local uuid_generate = require "util.uuid".generate; |
22 local is_admin = require "core.usermanager".is_admin; | 22 local is_admin = require "core.usermanager".is_admin; |
23 local pubsub = require "util.pubsub"; | 23 local pubsub = require "util.pubsub"; |
24 local httpserver = require "net.httpserver"; | |
25 local jid_bare = require "util.jid".bare; | 24 local jid_bare = require "util.jid".bare; |
26 local lfs = require "lfs"; | 25 local lfs = require "lfs"; |
27 local open = io.open; | 26 local open = io.open; |
28 local stat = lfs.attributes; | 27 local stat = lfs.attributes; |
29 | 28 |
30 module:set_global(); | 29 module:set_global(); |
31 | 30 |
32 local service = {}; | 31 local service = {}; |
33 | 32 |
34 local http_base = module.path:gsub("/[^/]+$","") .. "/www_files"; | 33 local http_base = module.path:gsub("/[^/]+$","") .. "/www_files/"; |
35 | 34 |
36 local xmlns_adminsub = "http://prosody.im/adminsub"; | 35 local xmlns_adminsub = "http://prosody.im/adminsub"; |
37 local xmlns_c2s_session = "http://prosody.im/streams/c2s"; | 36 local xmlns_c2s_session = "http://prosody.im/streams/c2s"; |
38 local xmlns_s2s_session = "http://prosody.im/streams/s2s"; | 37 local xmlns_s2s_session = "http://prosody.im/streams/s2s"; |
39 | |
40 local response_301 = { status = "301 Moved Permanently" }; | |
41 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" }; | |
42 local response_403 = { status = "403 Forbidden", body = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :(" }; | |
43 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" }; | |
44 | 38 |
45 local mime_map = { | 39 local mime_map = { |
46 html = "text/html"; | 40 html = "text/html"; |
47 xml = "text/xml"; | 41 xml = "text/xml"; |
48 js = "text/javascript"; | 42 js = "text/javascript"; |
108 local notifier = st.stanza("retract", { id = id }); | 102 local notifier = st.stanza("retract", { id = id }); |
109 service[host]:retract(xmlns_s2s_session, host, id, notifier); | 103 service[host]:retract(xmlns_s2s_session, host, id, notifier); |
110 end | 104 end |
111 end | 105 end |
112 | 106 |
113 local function preprocess_path(path) | 107 function serve_file(event, path) |
114 if path:sub(1,1) ~= "/" then | 108 local full_path = http_base .. path; |
115 path = "/"..path; | 109 |
116 end | |
117 local level = 0; | |
118 for component in path:gmatch("([^/]+)/") do | |
119 if component == ".." then | |
120 level = level - 1; | |
121 elseif component ~= "." then | |
122 level = level + 1; | |
123 end | |
124 if level < 0 then | |
125 return nil; | |
126 end | |
127 end | |
128 return path; | |
129 end | |
130 | |
131 function serve_file(path, base) | |
132 local full_path = http_base..path; | |
133 if stat(full_path, "mode") == "directory" then | 110 if stat(full_path, "mode") == "directory" then |
134 if not path:find("/$") then | |
135 local response = response_301; | |
136 response.headers = { ["Location"] = base .. "/" }; | |
137 return response; | |
138 end | |
139 if stat(full_path.."/index.html", "mode") == "file" then | 111 if stat(full_path.."/index.html", "mode") == "file" then |
140 return serve_file(path.."/index.html"); | 112 return serve_file(event, path.."/index.html"); |
141 end | 113 end |
142 return response_403; | 114 return 403; |
143 end | 115 end |
116 | |
144 local f, err = open(full_path, "rb"); | 117 local f, err = open(full_path, "rb"); |
145 if not f then return response_404; end | 118 if not f then |
119 return 404; | |
120 end | |
121 | |
146 local data = f:read("*a"); | 122 local data = f:read("*a"); |
147 f:close(); | 123 f:close(); |
148 if not data then | 124 if not data then |
149 return response_403; | 125 return 403; |
150 end | 126 end |
127 | |
151 local ext = path:match("%.([^.]*)$"); | 128 local ext = path:match("%.([^.]*)$"); |
152 local mime = mime_map[ext]; -- Content-Type should be nil when not known | 129 event.response.headers.content_type = mime_map[ext]; -- Content-Type should be nil when not known |
153 return { | 130 return data; |
154 headers = { ["Content-Type"] = mime; }; | 131 end |
155 body = data; | 132 |
156 }; | 133 function module.add_host(module) |
157 end | 134 -- Setup HTTP server |
158 | 135 module:depends("http"); |
159 local function handle_file_request(method, body, request) | 136 module:provides("http", { |
160 local path = preprocess_path(request.url.path); | 137 name = "admin"; |
161 if not path then return response_400; end | 138 route = { |
162 path_stripped = path:gsub("^/[^/]+", ""); -- Strip /admin/ | 139 ["GET"] = function(event) |
163 return serve_file(path_stripped, path); | 140 event.response.headers.location = event.request.path .. "/"; |
164 end | 141 return 301; |
165 | 142 end; |
166 function module.load() | 143 ["GET /*"] = serve_file; |
167 local http_conf = config.get("*", "core", "webadmin_http_ports"); | 144 } |
168 | 145 }); |
169 httpserver.new_from_config(http_conf, handle_file_request, { base = "admin" }); | 146 |
170 end | 147 -- Setup adminsub service |
171 | 148 local ok, err; |
172 prosody.events.add_handler("server-started", function () | 149 service[module.host] = pubsub.new({ |
173 for host_name, host_table in pairs(hosts) do | 150 broadcaster = function(node, jids, item) return simple_broadcast(node, jids, item, module.host) end; |
174 service[host_name] = pubsub.new({ | 151 normalize_jid = jid_bare; |
175 broadcaster = function(node, jids, item) return simple_broadcast(node, jids, item, host_name) end; | 152 get_affiliation = function(jid) return get_affiliation(jid, module.host) end; |
176 normalize_jid = jid_bare; | 153 capabilities = { |
177 get_affiliation = function(jid) return get_affiliation(jid, host_name) end; | 154 member = { |
178 capabilities = { | 155 create = false; |
179 member = { | 156 publish = false; |
180 create = false; | 157 retract = false; |
181 publish = false; | 158 get_nodes = true; |
182 retract = false; | 159 |
183 get_nodes = true; | 160 subscribe = true; |
184 | 161 unsubscribe = true; |
185 subscribe = true; | 162 get_subscription = true; |
186 unsubscribe = true; | 163 get_subscriptions = true; |
187 get_subscription = true; | 164 get_items = true; |
188 get_subscriptions = true; | 165 |
189 get_items = true; | 166 subscribe_other = false; |
190 | 167 unsubscribe_other = false; |
191 subscribe_other = false; | 168 get_subscription_other = false; |
192 unsubscribe_other = false; | 169 get_subscriptions_other = false; |
193 get_subscription_other = false; | 170 |
194 get_subscriptions_other = false; | 171 be_subscribed = true; |
195 | 172 be_unsubscribed = true; |
196 be_subscribed = true; | 173 |
197 be_unsubscribed = true; | 174 set_affiliation = false; |
198 | |
199 set_affiliation = false; | |
200 }; | |
201 | |
202 owner = { | |
203 create = true; | |
204 publish = true; | |
205 retract = true; | |
206 get_nodes = true; | |
207 | |
208 subscribe = true; | |
209 unsubscribe = true; | |
210 get_subscription = true; | |
211 get_subscriptions = true; | |
212 get_items = true; | |
213 | |
214 subscribe_other = true; | |
215 unsubscribe_other = true; | |
216 get_subscription_other = true; | |
217 get_subscriptions_other = true; | |
218 | |
219 be_subscribed = true; | |
220 be_unsubscribed = true; | |
221 | |
222 set_affiliation = true; | |
223 }; | |
224 }; | 175 }; |
225 }); | 176 |
226 | 177 owner = { |
227 if not select(2, service[host_name]:get_nodes(true))[xmlns_s2s_session] then | 178 create = true; |
228 local ok, errmsg = service[host_name]:create(xmlns_s2s_session, true); | 179 publish = true; |
180 retract = true; | |
181 get_nodes = true; | |
182 | |
183 subscribe = true; | |
184 unsubscribe = true; | |
185 get_subscription = true; | |
186 get_subscriptions = true; | |
187 get_items = true; | |
188 | |
189 subscribe_other = true; | |
190 unsubscribe_other = true; | |
191 get_subscription_other = true; | |
192 get_subscriptions_other = true; | |
193 | |
194 be_subscribed = true; | |
195 be_unsubscribed = true; | |
196 | |
197 set_affiliation = true; | |
198 }; | |
199 }; | |
200 }); | |
201 | |
202 -- Create node for s2s sessions | |
203 ok, err = service[module.host]:create(xmlns_s2s_session, true); | |
204 if not ok then | |
205 module:log("warn", "Could not create node " .. xmlns_s2s_session .. ": " .. tostring(err)); | |
206 else | |
207 service[module.host]:set_affiliation(xmlns_s2s_session, true, module.host, "owner") | |
208 end | |
209 | |
210 -- Add outgoing s2s sessions | |
211 for remotehost, session in pairs(hosts[module.host].s2sout) do | |
212 if session.type ~= "s2sout_unauthed" then | |
213 add_host(session, "out", module.host); | |
214 end | |
215 end | |
216 | |
217 -- Add incomming s2s sessions | |
218 for session in pairs(incoming_s2s) do | |
219 if session.to_host == module.host then | |
220 add_host(session, "in", module.host); | |
221 end | |
222 end | |
223 | |
224 -- Create node for c2s sessions | |
225 ok, err = service[module.host]:create(xmlns_c2s_session, true); | |
226 if not ok then | |
227 module:log("warn", "Could not create node " .. xmlns_c2s_session .. ": " .. tostring(err)); | |
228 else | |
229 service[module.host]:set_affiliation(xmlns_c2s_session, true, module.host, "owner") | |
230 end | |
231 | |
232 -- Add c2s sessions | |
233 for username, user in pairs(hosts[module.host].sessions or {}) do | |
234 for resource, session in pairs(user.sessions or {}) do | |
235 add_client(session, module.host); | |
236 end | |
237 end | |
238 | |
239 -- Register adminsub handler | |
240 module:hook("iq/host/http://prosody.im/adminsub:adminsub", function(event) | |
241 local origin, stanza = event.origin, event.stanza; | |
242 local adminsub = stanza.tags[1]; | |
243 local action = adminsub.tags[1]; | |
244 local reply; | |
245 if action.name == "subscribe" then | |
246 local ok, ret = service[module.host]:add_subscription(action.attr.node, stanza.attr.from, stanza.attr.from); | |
247 if ok then | |
248 reply = st.reply(stanza) | |
249 :tag("adminsub", { xmlns = xmlns_adminsub }); | |
250 else | |
251 reply = st.error_reply(stanza, "cancel", ret); | |
252 end | |
253 elseif action.name == "unsubscribe" then | |
254 local ok, ret = service[module.host]:remove_subscription(action.attr.node, stanza.attr.from, stanza.attr.from); | |
255 if ok then | |
256 reply = st.reply(stanza) | |
257 :tag("adminsub", { xmlns = xmlns_adminsub }); | |
258 else | |
259 reply = st.error_reply(stanza, "cancel", ret); | |
260 end | |
261 elseif action.name == "items" then | |
262 local node = action.attr.node; | |
263 local ok, ret = service[module.host]:get_items(node, stanza.attr.from); | |
229 if not ok then | 264 if not ok then |
230 module:log("warn", "Could not create node " .. xmlns_s2s_session .. ": " .. tostring(errmsg)); | 265 return origin.send(st.error_reply(stanza, "cancel", ret)); |
231 else | 266 end |
232 service[host_name]:set_affiliation(xmlns_s2s_session, true, host_name, "owner") | 267 |
233 end | 268 local data = st.stanza("items", { node = node }); |
234 end | 269 for _, entry in pairs(ret) do |
235 | 270 data:add_child(entry); |
236 for remotehost, session in pairs(host_table.s2sout) do | 271 end |
237 if session.type ~= "s2sout_unauthed" then | 272 if data then |
238 add_host(session, "out", host_name); | |
239 end | |
240 end | |
241 for session in pairs(incoming_s2s) do | |
242 if session.to_host == host_name then | |
243 add_host(session, "in", host_name); | |
244 end | |
245 end | |
246 | |
247 if not select(2, service[host_name]:get_nodes(true))[xmlns_c2s_session] then | |
248 local ok, errmsg = service[host_name]:create(xmlns_c2s_session, true); | |
249 if not ok then | |
250 module:log("warn", "Could not create node " .. xmlns_c2s_session .. ": " .. tostring(errmsg)); | |
251 else | |
252 service[host_name]:set_affiliation(xmlns_c2s_session, true, host_name, "owner") | |
253 end | |
254 end | |
255 | |
256 for username, user in pairs(host_table.sessions or {}) do | |
257 for resource, session in pairs(user.sessions or {}) do | |
258 add_client(session, host_name); | |
259 end | |
260 end | |
261 | |
262 host_table.events.add_handler("iq/host/http://prosody.im/adminsub:adminsub", function(event) | |
263 local origin, stanza = event.origin, event.stanza; | |
264 local adminsub = stanza.tags[1]; | |
265 local action = adminsub.tags[1]; | |
266 local reply; | |
267 if action.name == "subscribe" then | |
268 local ok, ret = service[host_name]:add_subscription(action.attr.node, stanza.attr.from, stanza.attr.from); | |
269 if ok then | |
270 reply = st.reply(stanza) | |
271 :tag("adminsub", { xmlns = xmlns_adminsub }); | |
272 else | |
273 reply = st.error_reply(stanza, "cancel", ret); | |
274 end | |
275 elseif action.name == "unsubscribe" then | |
276 local ok, ret = service[host_name]:remove_subscription(action.attr.node, stanza.attr.from, stanza.attr.from); | |
277 if ok then | |
278 reply = st.reply(stanza) | |
279 :tag("adminsub", { xmlns = xmlns_adminsub }); | |
280 else | |
281 reply = st.error_reply(stanza, "cancel", ret); | |
282 end | |
283 elseif action.name == "items" then | |
284 local node = action.attr.node; | |
285 local ok, ret = service[host_name]:get_items(node, stanza.attr.from); | |
286 if not ok then | |
287 return origin.send(st.error_reply(stanza, "cancel", ret)); | |
288 end | |
289 | |
290 local data = st.stanza("items", { node = node }); | |
291 for _, entry in pairs(ret) do | |
292 data:add_child(entry); | |
293 end | |
294 if data then | |
295 reply = st.reply(stanza) | |
296 :tag("adminsub", { xmlns = xmlns_adminsub }) | |
297 :add_child(data); | |
298 else | |
299 reply = st.error_reply(stanza, "cancel", "item-not-found"); | |
300 end | |
301 elseif action.name == "adminfor" then | |
302 local data = st.stanza("adminfor"); | |
303 for host_name in pairs(hosts) do | |
304 if is_admin(stanza.attr.from, host_name) then | |
305 data:tag("item"):text(host_name):up(); | |
306 end | |
307 end | |
308 reply = st.reply(stanza) | 273 reply = st.reply(stanza) |
309 :tag("adminsub", { xmlns = xmlns_adminsub }) | 274 :tag("adminsub", { xmlns = xmlns_adminsub }) |
310 :add_child(data); | 275 :add_child(data); |
311 else | 276 else |
312 reply = st.error_reply(stanza, "feature-not-implemented"); | 277 reply = st.error_reply(stanza, "cancel", "item-not-found"); |
313 end | 278 end |
314 return origin.send(reply); | 279 elseif action.name == "adminfor" then |
315 end); | 280 local data = st.stanza("adminfor"); |
316 | 281 for host_name in pairs(hosts) do |
317 host_table.events.add_handler("resource-bind", function(event) | 282 if is_admin(stanza.attr.from, host_name) then |
318 add_client(event.session, host_name); | 283 data:tag("item"):text(host_name):up(); |
319 end); | 284 end |
320 | 285 end |
321 host_table.events.add_handler("resource-unbind", function(event) | 286 reply = st.reply(stanza) |
322 del_client(event.session, host_name); | 287 :tag("adminsub", { xmlns = xmlns_adminsub }) |
323 service[host_name]:remove_subscription(xmlns_c2s_session, host_name, event.session.full_jid); | 288 :add_child(data); |
324 service[host_name]:remove_subscription(xmlns_s2s_session, host_name, event.session.full_jid); | 289 else |
325 end); | 290 reply = st.error_reply(stanza, "feature-not-implemented"); |
326 | 291 end |
327 host_table.events.add_handler("s2sout-established", function(event) | 292 return origin.send(reply); |
328 add_host(event.session, "out", host_name); | 293 end); |
329 end); | 294 |
330 | 295 -- Add/remove c2s sessions |
331 host_table.events.add_handler("s2sin-established", function(event) | 296 module:hook("resource-bind", function(event) |
332 add_host(event.session, "in", host_name); | 297 add_client(event.session, module.host); |
333 end); | 298 end); |
334 | 299 |
335 host_table.events.add_handler("s2sout-destroyed", function(event) | 300 module:hook("resource-unbind", function(event) |
336 del_host(event.session, "out", host_name); | 301 del_client(event.session, module.host); |
337 end); | 302 service[module.host]:remove_subscription(xmlns_c2s_session, module.host, event.session.full_jid); |
338 | 303 service[module.host]:remove_subscription(xmlns_s2s_session, module.host, event.session.full_jid); |
339 host_table.events.add_handler("s2sin-destroyed", function(event) | 304 end); |
340 del_host(event.session, "in", host_name); | 305 |
341 end); | 306 -- Add/remove s2s sessions |
342 | 307 module:hook("s2sout-established", function(event) |
343 end | 308 add_host(event.session, "out", module.host); |
344 end); | 309 end); |
310 | |
311 module:hook("s2sin-established", function(event) | |
312 add_host(event.session, "in", module.host); | |
313 end); | |
314 | |
315 module:hook("s2sout-destroyed", function(event) | |
316 del_host(event.session, "out", module.host); | |
317 end); | |
318 | |
319 module:hook("s2sin-destroyed", function(event) | |
320 del_host(event.session, "in", module.host); | |
321 end); | |
322 end | |
345 | 323 |
346 function simple_broadcast(node, jids, item, host) | 324 function simple_broadcast(node, jids, item, host) |
347 item = st.clone(item); | 325 item = st.clone(item); |
348 item.attr.xmlns = nil; -- Clear the pubsub namespace | 326 item.attr.xmlns = nil; -- Clear the pubsub namespace |
349 local message = st.message({ from = host, type = "headline" }) | 327 local message = st.message({ from = host, type = "headline" }) |