mod_admin_web/admin_web/mod_admin_web.lua
author Florian Zeitz <florob@babelmonkeys.de>
Fri, 04 Feb 2011 17:08:51 +0100
changeset 334 54d4445cc5c6
parent 328 73001ddff453
child 396 8cb21ef24e5d
permissions -rw-r--r--
mod_admin_web: Add some margin so that elements don't overlap.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
     1
-- Copyright (C) 2010 Florian Zeitz
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
     2
--
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
     3
-- This file is MIT/X11 licensed. Please see the
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
     4
-- COPYING file in the source package for more information.
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
     5
--
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
     6
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
     7
-- <session xmlns="http://prosody.im/streams/c2s" jid="alice@example.com/brussels">
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
     8
--   <encrypted/>
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
     9
--   <compressed/>
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    10
-- </session>
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    11
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    12
-- <session xmlns="http://prosody.im/streams/s2s" jid="example.com">
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    13
--   <encrypted/>
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    14
--   <compressed/>
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    15
--   <in/> / <out/>
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    16
-- </session>
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    17
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
    18
local st = require "util.stanza";
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    19
local uuid_generate = require "util.uuid".generate;
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
    20
local is_admin = require "usermanager".is_admin;
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
    21
local pubsub = require "util.pubsub";
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    22
local httpserver = require "net.httpserver";
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
    23
local jid_bare = require "util.jid".bare;
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    24
local lfs = require "lfs";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    25
local open = io.open;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    26
local stat = lfs.attributes;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    27
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    28
module:set_global();
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    29
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    30
local service = {};
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    31
328
73001ddff453 mod_admin_web: Fix path building
Florian Zeitz <florob@babelmonkeys.de>
parents: 319
diff changeset
    32
local http_base = (prosody.paths.plugins or "./plugins") .. "/admin_web/www_files";
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    33
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
    34
local xmlns_adminsub = "http://prosody.im/adminsub";
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    35
local xmlns_c2s_session = "http://prosody.im/streams/c2s";
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    36
local xmlns_s2s_session = "http://prosody.im/streams/s2s";
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    37
309
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
    38
local response_301 = { status = "301 Moved Permanently" };
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    39
local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" };
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    40
local response_403 = { status = "403 Forbidden", body = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :(" };
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    41
local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" };
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    42
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    43
local mime_map = {
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    44
	html = "text/html";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    45
	xml = "text/xml";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    46
	js = "text/javascript";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    47
	css = "text/css";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    48
};
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    49
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    50
local idmap = {};
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    51
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    52
function add_client(session, host)
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    53
	local name = session.full_jid;
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    54
	local id = idmap[name];
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    55
	if not id then
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    56
		id = uuid_generate();
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    57
		idmap[name] = id;
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    58
	end
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
    59
	local item = st.stanza("item", { id = id }):tag("session", {xmlns = xmlns_c2s_session, jid = name}):up();
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    60
	if session.secure then
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    61
		item:tag("encrypted"):up();
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    62
	end
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    63
	if session.compressed then
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    64
		item:tag("compressed"):up();
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    65
	end
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    66
	service[host]:publish(xmlns_c2s_session, host, id, item);
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    67
	module:log("debug", "Added client " .. name);
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    68
end
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    69
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    70
function del_client(session, host)
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    71
	local name = session.full_jid;
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    72
	local id = idmap[name];
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    73
	if id then
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
    74
		local notifier = st.stanza("retract", { id = id });
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    75
		service[host]:retract(xmlns_c2s_session, host, id, notifier);
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    76
	end
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    77
end
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
    78
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    79
function add_host(session, type, host)
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    80
	local name = (type == "out" and session.to_host) or (type == "in" and session.from_host);
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    81
	local id = idmap[name.."_"..type];
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    82
	if not id then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    83
		id = uuid_generate();
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    84
		idmap[name.."_"..type] = id;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    85
	end
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
    86
	local item = st.stanza("item", { id = id }):tag("session", {xmlns = xmlns_s2s_session, jid = name})
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    87
		:tag(type):up();
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    88
	if session.secure then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    89
		item:tag("encrypted"):up();
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    90
	end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    91
	if session.compressed then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    92
		item:tag("compressed"):up();
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    93
	end
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    94
	service[host]:publish(xmlns_s2s_session, host, id, item);
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    95
	module:log("debug", "Added host " .. name .. " s2s" .. type);
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    96
end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    97
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
    98
function del_host(session, type, host)
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
    99
	local name = (type == "out" and session.to_host) or (type == "in" and session.from_host);
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   100
	local id = idmap[name.."_"..type];
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   101
	if id then
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   102
		local notifier = st.stanza("retract", { id = id });
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   103
		service[host]:retract(xmlns_s2s_session, host, id, notifier);
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   104
	end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   105
end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   106
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   107
local function preprocess_path(path)
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   108
	if path:sub(1,1) ~= "/" then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   109
		path = "/"..path;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   110
	end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   111
	local level = 0;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   112
	for component in path:gmatch("([^/]+)/") do
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   113
		if component == ".." then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   114
			level = level - 1;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   115
		elseif component ~= "." then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   116
			level = level + 1;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   117
		end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   118
		if level < 0 then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   119
			return nil;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   120
		end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   121
	end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   122
	return path;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   123
end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   124
309
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
   125
function serve_file(path, base)
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   126
	local full_path = http_base..path;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   127
	if stat(full_path, "mode") == "directory" then
309
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
   128
		if not path:find("/$") then
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
   129
			local response = response_301;
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
   130
			response.headers = { ["Location"] = base .. "/" };
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
   131
			return response;
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
   132
		end
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   133
		if stat(full_path.."/index.html", "mode") == "file" then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   134
			return serve_file(path.."/index.html");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   135
		end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   136
		return response_403;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   137
	end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   138
	local f, err = open(full_path, "rb");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   139
	if not f then return response_404; end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   140
	local data = f:read("*a");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   141
	f:close();
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   142
	if not data then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   143
		return response_403;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   144
	end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   145
	local ext = path:match("%.([^.]*)$");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   146
	local mime = mime_map[ext]; -- Content-Type should be nil when not known
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   147
	return {
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   148
		headers = { ["Content-Type"] = mime; };
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   149
		body = data;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   150
	};
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   151
end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   152
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   153
local function handle_file_request(method, body, request)
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   154
	local path = preprocess_path(request.url.path);
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   155
	if not path then return response_400; end
309
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
   156
	path_stripped = path:gsub("^/[^/]+", ""); -- Strip /admin/
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
   157
	return serve_file(path_stripped, path);
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   158
end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   159
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   160
function module.load()
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   161
	local http_conf = config.get("*", "core", "webadmin_http_ports");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   162
303
0f53c88bab9a mod_admin_web: Move pubsub service initialisation into a server-started handler, in case we get loaded before the service
Matthew Wild <mwild1@gmail.com>
parents: 301
diff changeset
   163
	httpserver.new_from_config(http_conf, handle_file_request, { base = "admin" });
0f53c88bab9a mod_admin_web: Move pubsub service initialisation into a server-started handler, in case we get loaded before the service
Matthew Wild <mwild1@gmail.com>
parents: 301
diff changeset
   164
end
0f53c88bab9a mod_admin_web: Move pubsub service initialisation into a server-started handler, in case we get loaded before the service
Matthew Wild <mwild1@gmail.com>
parents: 301
diff changeset
   165
304
8f3499ae1e27 mod_admin_web: Fix initialisation code, undeclared variable and wrong event scope
Matthew Wild <mwild1@gmail.com>
parents: 303
diff changeset
   166
prosody.events.add_handler("server-started", function ()
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   167
	for host_name, host_table in pairs(hosts) do
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   168
		service[host_name] = pubsub.new({
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   169
			broadcaster = function(node, jids, item) return simple_broadcast(node, jids, item, host_name) end;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   170
			normalize_jid = jid_bare;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   171
			get_affiliation = function(jid) return get_affiliation(jid, host_name) end;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   172
			capabilities = {
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   173
				member = {
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   174
					create = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   175
					publish = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   176
					retract = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   177
					get_nodes = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   178
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   179
					subscribe = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   180
					unsubscribe = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   181
					get_subscription = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   182
					get_subscriptions = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   183
					get_items = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   184
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   185
					subscribe_other = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   186
					unsubscribe_other = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   187
					get_subscription_other = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   188
					get_subscriptions_other = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   189
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   190
					be_subscribed = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   191
					be_unsubscribed = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   192
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   193
					set_affiliation = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   194
				};
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   195
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   196
				owner = {
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   197
					create = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   198
					publish = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   199
					retract = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   200
					get_nodes = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   201
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   202
					subscribe = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   203
					unsubscribe = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   204
					get_subscription = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   205
					get_subscriptions = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   206
					get_items = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   207
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   208
					subscribe_other = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   209
					unsubscribe_other = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   210
					get_subscription_other = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   211
					get_subscriptions_other = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   212
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   213
					be_subscribed = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   214
					be_unsubscribed = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   215
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   216
					set_affiliation = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   217
				};
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   218
			};
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   219
		});
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   220
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   221
		if not select(2, service[host_name]:get_nodes(true))[xmlns_s2s_session] then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   222
			local ok, errmsg = service[host_name]:create(xmlns_s2s_session, true);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   223
			if not ok then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   224
				module:log("warn", "Could not create node " .. xmlns_s2s_session .. ": " .. tostring(errmsg));
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   225
			else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   226
				service[host_name]:set_affiliation(xmlns_s2s_session, true, host_name, "owner")
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   227
			end
295
e373de5907aa mod_admin_web: Only create the node once when loading onto multiple hosts
Florian Zeitz <florob@babelmonkeys.de>
parents: 292
diff changeset
   228
		end
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   229
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   230
		for remotehost, session in pairs(host_table.s2sout) do
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   231
			if session.type ~= "s2sout_unauthed" then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   232
				add_host(session, "out", host_name);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   233
			end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   234
		end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   235
		for session in pairs(incoming_s2s) do
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   236
			if session.to_host == host_name then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   237
				add_host(session, "in", host_name);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   238
			end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   239
		end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   240
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   241
		if not select(2, service[host_name]:get_nodes(true))[xmlns_c2s_session] then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   242
			local ok, errmsg = service[host_name]:create(xmlns_c2s_session, true);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   243
			if not ok then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   244
				module:log("warn", "Could not create node " .. xmlns_c2s_session .. ": " .. tostring(errmsg));
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   245
			else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   246
				service[host_name]:set_affiliation(xmlns_c2s_session, true, host_name, "owner")
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   247
			end
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   248
		end
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   249
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   250
		for username, user in pairs(host_table.sessions or {}) do
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   251
			for resource, session in pairs(user.sessions or {}) do
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   252
				add_client(session, host_name);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   253
			end
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   254
		end
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   255
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   256
		host_table.events.add_handler("iq/host/http://prosody.im/adminsub:adminsub", function(event)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   257
			local origin, stanza = event.origin, event.stanza;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   258
			local adminsub = stanza.tags[1];
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   259
			local action = adminsub.tags[1];
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   260
			local reply;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   261
			if action.name == "subscribe" then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   262
				local ok, ret = service[host_name]:add_subscription(action.attr.node, stanza.attr.from, stanza.attr.from);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   263
				if ok then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   264
					reply = st.reply(stanza)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   265
						:tag("adminsub", { xmlns = xmlns_adminsub });
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   266
				else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   267
					reply = st.error_reply(stanza, "cancel", ret);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   268
				end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   269
			elseif action.name == "unsubscribe" then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   270
				local ok, ret = service[host_name]:remove_subscription(action.attr.node, stanza.attr.from, stanza.attr.from);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   271
				if ok then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   272
					reply = st.reply(stanza)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   273
						:tag("adminsub", { xmlns = xmlns_adminsub });
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   274
				else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   275
					reply = st.error_reply(stanza, "cancel", ret);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   276
				end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   277
			elseif action.name == "items" then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   278
				local node = action.attr.node;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   279
				local ok, ret = service[host_name]:get_items(node, stanza.attr.from);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   280
				if not ok then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   281
					return origin.send(st.error_reply(stanza, "cancel", ret));
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   282
				end
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   283
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   284
				local data = st.stanza("items", { node = node });
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   285
				for _, entry in pairs(ret) do
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   286
					data:add_child(entry);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   287
				end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   288
				if data then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   289
					reply = st.reply(stanza)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   290
						:tag("adminsub", { xmlns = xmlns_adminsub })
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   291
							:add_child(data);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   292
				else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   293
					reply = st.error_reply(stanza, "cancel", "item-not-found");
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   294
				end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   295
			elseif action.name == "adminfor" then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   296
				local data = st.stanza("adminfor");
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   297
				for host_name in pairs(hosts) do
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   298
					if is_admin(stanza.attr.from, host_name) then
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   299
						data:tag("item"):text(host_name):up();
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   300
					end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   301
				end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   302
				reply = st.reply(stanza)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   303
					:tag("adminsub", { xmlns = xmlns_adminsub })
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   304
						:add_child(data);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   305
			else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   306
				reply = st.error_reply(stanza, "feature-not-implemented");
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   307
			end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   308
			return origin.send(reply);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   309
		end);
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
   310
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   311
		host_table.events.add_handler("resource-bind", function(event)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   312
			add_client(event.session, host_name);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   313
		end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   314
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   315
		host_table.events.add_handler("resource-unbind", function(event)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   316
			del_client(event.session, host_name);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   317
			service[host_name]:remove_subscription(xmlns_c2s_session, host_name, event.session.full_jid);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   318
			service[host_name]:remove_subscription(xmlns_s2s_session, host_name, event.session.full_jid);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   319
		end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   320
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   321
		host_table.events.add_handler("s2sout-established", function(event)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   322
			add_host(event.session, "out", host_name);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   323
		end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   324
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   325
		host_table.events.add_handler("s2sin-established", function(event)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   326
			add_host(event.session, "in", host_name);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   327
		end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   328
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   329
		host_table.events.add_handler("s2sout-destroyed", function(event)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   330
			del_host(event.session, "out", host_name);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   331
		end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   332
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   333
		host_table.events.add_handler("s2sin-destroyed", function(event)
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   334
			del_host(event.session, "in", host_name);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   335
		end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   336
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
   337
	end
303
0f53c88bab9a mod_admin_web: Move pubsub service initialisation into a server-started handler, in case we get loaded before the service
Matthew Wild <mwild1@gmail.com>
parents: 301
diff changeset
   338
end);
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
   339
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   340
function simple_broadcast(node, jids, item, host)
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   341
	item = st.clone(item);
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   342
	item.attr.xmlns = nil; -- Clear the pubsub namespace
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   343
	local message = st.message({ from = host, type = "headline" })
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   344
		:tag("event", { xmlns = xmlns_adminsub .. "#event" })
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   345
			:tag("items", { node = node })
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   346
				:add_child(item);
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   347
	for jid in pairs(jids) do
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   348
		module:log("debug", "Sending notification to %s", jid);
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   349
		message.attr.to = jid;
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   350
		core_post_stanza(hosts[host], message);
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   351
	end
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   352
end
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   353
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
   354
function get_affiliation(jid, host)
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   355
	local bare_jid = jid_bare(jid);
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   356
	if is_admin(bare_jid, host) then
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   357
		return "member";
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   358
	else
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   359
		return "none";
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   360
	end
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
   361
end