annotate mod_admin_web/admin_web/mod_admin_web.lua @ 560:b62f5e38f865

mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 17 Jan 2012 00:57:47 +0000
parents fc5df35a3fe4
children da69b65288e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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">
396
8cb21ef24e5d mod_admin_web: Show S2S certificate validity
Florian Zeitz <florob@babelmonkeys.de>
parents: 328
diff changeset
13 -- <encrypted>
8cb21ef24e5d mod_admin_web: Show S2S certificate validity
Florian Zeitz <florob@babelmonkeys.de>
parents: 328
diff changeset
14 -- <valid/> / <invalid/>
8cb21ef24e5d mod_admin_web: Show S2S certificate validity
Florian Zeitz <florob@babelmonkeys.de>
parents: 328
diff changeset
15 -- </encrypted>
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
16 -- <compressed/>
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
17 -- <in/> / <out/>
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
18 -- </session>
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
19
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
20 local st = require "util.stanza";
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
21 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
22 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
23 local pubsub = require "util.pubsub";
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
24 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
25 local jid_bare = require "util.jid".bare;
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
26 local lfs = require "lfs";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
27 local open = io.open;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
28 local stat = lfs.attributes;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
29
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
30 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
31
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
32 local service = {};
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33
549
fc5df35a3fe4 mod_admin_web: Use proper path for www_data files (Thanks Zash)
Florian Zeitz <florob@babelmonkeys.de>
parents: 396
diff changeset
34 local http_base = module.path:gsub("/[^/]+$","") .. "/www_files";
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
35
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
36 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
37 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
38 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
39
309
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
40 local response_301 = { status = "301 Moved Permanently" };
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
41 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
42 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
43 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
44
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
45 local mime_map = {
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
46 html = "text/html";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
47 xml = "text/xml";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
48 js = "text/javascript";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
49 css = "text/css";
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
50 };
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
51
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
52 local idmap = {};
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
53
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
54 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
55 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
56 local id = idmap[name];
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
57 if not id then
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
58 id = uuid_generate();
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
59 idmap[name] = id;
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
60 end
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
61 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
62 if session.secure then
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
63 item:tag("encrypted"):up();
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
64 end
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
65 if session.compressed then
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
66 item:tag("compressed"):up();
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
67 end
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
68 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
69 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
70 end
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
71
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
72 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
73 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
74 local id = idmap[name];
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
75 if id then
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
76 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
77 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
78 end
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
79 end
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
80
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
81 function add_host(session, type, host)
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
82 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
83 local id = idmap[name.."_"..type];
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
84 if not id then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
85 id = uuid_generate();
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
86 idmap[name.."_"..type] = id;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
87 end
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
88 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
89 :tag(type):up();
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
90 if session.secure then
396
8cb21ef24e5d mod_admin_web: Show S2S certificate validity
Florian Zeitz <florob@babelmonkeys.de>
parents: 328
diff changeset
91 if session.cert_identity_status == "valid" then
8cb21ef24e5d mod_admin_web: Show S2S certificate validity
Florian Zeitz <florob@babelmonkeys.de>
parents: 328
diff changeset
92 item:tag("encrypted"):tag("valid"):up():up();
8cb21ef24e5d mod_admin_web: Show S2S certificate validity
Florian Zeitz <florob@babelmonkeys.de>
parents: 328
diff changeset
93 else
8cb21ef24e5d mod_admin_web: Show S2S certificate validity
Florian Zeitz <florob@babelmonkeys.de>
parents: 328
diff changeset
94 item:tag("encrypted"):tag("invalid"):up():up();
8cb21ef24e5d mod_admin_web: Show S2S certificate validity
Florian Zeitz <florob@babelmonkeys.de>
parents: 328
diff changeset
95 end
288
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 if session.compressed then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
98 item:tag("compressed"):up();
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
99 end
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
100 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
101 module:log("debug", "Added host " .. name .. " s2s" .. type);
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
102 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
103
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
104 function del_host(session, type, host)
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
105 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
106 local id = idmap[name.."_"..type];
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
107 if id then
317
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
108 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
109 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
110 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
111 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
112
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
113 local function preprocess_path(path)
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
114 if path:sub(1,1) ~= "/" then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
115 path = "/"..path;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
116 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
117 local level = 0;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
118 for component in path:gmatch("([^/]+)/") do
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
119 if component == ".." then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
120 level = level - 1;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
121 elseif component ~= "." then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
122 level = level + 1;
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 if level < 0 then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
125 return nil;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
126 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
127 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
128 return path;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
129 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
130
309
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
131 function serve_file(path, base)
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
132 local full_path = http_base..path;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
133 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
134 if not path:find("/$") then
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
135 local response = response_301;
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
136 response.headers = { ["Location"] = base .. "/" };
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
137 return response;
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
138 end
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
139 if stat(full_path.."/index.html", "mode") == "file" then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
140 return serve_file(path.."/index.html");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
141 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
142 return response_403;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
143 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
144 local f, err = open(full_path, "rb");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
145 if not f then return response_404; end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
146 local data = f:read("*a");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
147 f:close();
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
148 if not data then
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
149 return response_403;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
150 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
151 local ext = path:match("%.([^.]*)$");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
152 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
153 return {
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
154 headers = { ["Content-Type"] = mime; };
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
155 body = data;
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
156 };
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
157 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
158
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
159 local function handle_file_request(method, body, request)
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
160 local path = preprocess_path(request.url.path);
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
161 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
162 path_stripped = path:gsub("^/[^/]+", ""); -- Strip /admin/
5ec9125575fc mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents: 304
diff changeset
163 return serve_file(path_stripped, path);
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
164 end
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
165
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
166 function module.load()
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
167 local http_conf = config.get("*", "core", "webadmin_http_ports");
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
168
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
169 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
170 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
171
304
8f3499ae1e27 mod_admin_web: Fix initialisation code, undeclared variable and wrong event scope
Matthew Wild <mwild1@gmail.com>
parents: 303
diff changeset
172 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
173 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
174 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
175 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
176 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
177 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
178 capabilities = {
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
179 member = {
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
180 create = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
181 publish = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
182 retract = false;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
183 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
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 = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
186 unsubscribe = true;
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 = true;
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 = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
189 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
190
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
191 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
192 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
193 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
194 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
195
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
196 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
197 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
198
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
199 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
200 };
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 owner = {
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
203 create = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
204 publish = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
205 retract = 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_nodes = 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 = 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 = 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 = 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 = true;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
212 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
213
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
214 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
215 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
216 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
217 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
218
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
219 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
220 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
221
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
222 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
223 };
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
224 };
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
225 });
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
226
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
227 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
228 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
229 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
230 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
231 else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
232 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
233 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
234 end
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
235
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
236 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
237 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
238 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
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 end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
241 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
242 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
243 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
244 end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
245 end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
246
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
247 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
248 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
249 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
250 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
251 else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
252 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
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 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
257 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
258 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
259 end
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
260 end
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
261
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
262 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
263 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
264 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
265 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
266 local reply;
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
267 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
268 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
269 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
270 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
271 :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
272 else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
273 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
274 end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
275 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
276 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
277 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
278 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
279 :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
280 else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
281 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
282 end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
283 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
284 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
285 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
286 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
287 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
288 end
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
289
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
290 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
291 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
292 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
293 end
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
294 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
295 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
296 :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
297 :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
298 else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
299 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
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 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
302 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
303 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
304 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
305 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
306 end
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 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
309 :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
310 :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
311 else
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
312 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
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 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
315 end);
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
316
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
317 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
318 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
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("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
322 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
323 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
324 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
325 end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
326
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
327 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
328 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
329 end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
330
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
331 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
332 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
333 end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
334
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
335 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
336 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
337 end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
338
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
339 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
340 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
341 end);
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
342
301
b241c79a0eb7 mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 295
diff changeset
343 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
344 end);
288
9233d7ee3c09 mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
345
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
346 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
347 item = st.clone(item);
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
348 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
349 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
350 :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
351 :tag("items", { node = node })
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
352 :add_child(item);
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
353 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
354 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
355 message.attr.to = jid;
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
356 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
357 end
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
358 end
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
359
319
ba2e78661ea8 mod_admin_web: Make module global. Host to administrate is now chooseable
Florian Zeitz <florob@babelmonkeys.de>
parents: 317
diff changeset
360 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
361 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
362 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
363 return "member";
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
364 else
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
365 return "none";
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
366 end
4f78f5020aa9 mod_admin_web: Get rid of the mod_pubsub dependency
Florian Zeitz <florob@babelmonkeys.de>
parents: 309
diff changeset
367 end