Mercurial > prosody-modules
annotate mod_admin_web/admin_web/mod_admin_web.lua @ 314:df212e1fa576
mod_remote_roster: Initial commit. Implements 2.2, 2.3 of <http://jkaluza.fedorapeople.org/remote-roster.html>.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Mon, 10 Jan 2011 16:35:43 +0500 |
parents | 5ec9125575fc |
children | 4f78f5020aa9 |
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"> |
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 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
18 local stanza = require "util.stanza"; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
19 local uuid_generate = require "util.uuid".generate; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
20 local httpserver = require "net.httpserver"; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
21 local lfs = require "lfs"; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
22 local open = io.open; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
23 local stat = lfs.attributes; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
24 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
25 local host = module:get_host(); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
26 local service = config.get("*", "core", "webadmin_pubsub_host") or ("pubsub." .. host); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
27 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
28 local http_base = (prosody.paths.plugins or "./plugins/") .. "admin_web/www_files"; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
29 |
301
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
30 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
|
31 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
|
32 |
309
5ec9125575fc
mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents:
304
diff
changeset
|
33 local response_301 = { status = "301 Moved Permanently" }; |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
38 local mime_map = { |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
39 html = "text/html"; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
40 xml = "text/xml"; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
41 js = "text/javascript"; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
42 css = "text/css"; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
43 }; |
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 idmap = {}; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
46 |
301
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
47 function add_client(session) |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
48 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
|
49 local id = idmap[name]; |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
50 if not id then |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
51 id = uuid_generate(); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
52 idmap[name] = id; |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
53 end |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
54 local item = stanza.stanza("item", { id = id }):tag("session", {xmlns = xmlns_c2s_session, jid = name}):up(); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
55 if session.secure then |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
56 item:tag("encrypted"):up(); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
57 end |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
58 if session.compressed then |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
59 item:tag("compressed"):up(); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
60 end |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
61 hosts[service].modules.pubsub.service:publish(xmlns_c2s_session, service, id, item); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
62 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
|
63 end |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
64 |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
65 function del_client(session) |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
66 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
|
67 local id = idmap[name]; |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
68 if id then |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
69 local notifier = stanza.stanza("retract", { id = id }); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
70 hosts[service].modules.pubsub.service:retract(xmlns_c2s_session, service, id, notifier); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
71 end |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
72 end |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
73 |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
74 function add_host(session, type) |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
75 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
|
76 local id = idmap[name.."_"..type]; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
77 if not id then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
78 id = uuid_generate(); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
79 idmap[name.."_"..type] = id; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
80 end |
301
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
81 local item = stanza.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
|
82 :tag(type):up(); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
83 if session.secure then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
84 item:tag("encrypted"):up(); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
85 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
86 if session.compressed then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
87 item:tag("compressed"):up(); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
88 end |
301
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
89 hosts[service].modules.pubsub.service:publish(xmlns_s2s_session, service, id, item); |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
90 module:log("debug", "Added host " .. name .. " s2s" .. type); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
91 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
92 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
93 function del_host(session, type) |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
94 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
|
95 local id = idmap[name.."_"..type]; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
96 if id then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
97 local notifier = stanza.stanza("retract", { id = id }); |
301
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
98 hosts[service].modules.pubsub.service:retract(xmlns_s2s_session, service, id, notifier); |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
99 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
100 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
101 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
102 local function preprocess_path(path) |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
103 if path:sub(1,1) ~= "/" then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
104 path = "/"..path; |
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 local level = 0; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
107 for component in path:gmatch("([^/]+)/") do |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
108 if component == ".." then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
109 level = level - 1; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
110 elseif component ~= "." then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
111 level = level + 1; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
112 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
113 if level < 0 then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
114 return nil; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
115 end |
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 return path; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
118 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
119 |
309
5ec9125575fc
mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents:
304
diff
changeset
|
120 function serve_file(path, base) |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
121 local full_path = http_base..path; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
122 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
|
123 if not path:find("/$") then |
5ec9125575fc
mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents:
304
diff
changeset
|
124 local response = response_301; |
5ec9125575fc
mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents:
304
diff
changeset
|
125 response.headers = { ["Location"] = base .. "/" }; |
5ec9125575fc
mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents:
304
diff
changeset
|
126 return response; |
5ec9125575fc
mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents:
304
diff
changeset
|
127 end |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
128 if stat(full_path.."/index.html", "mode") == "file" then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
129 return serve_file(path.."/index.html"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
130 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
131 return response_403; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
132 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
133 local f, err = open(full_path, "rb"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
134 if not f then return response_404; end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
135 local data = f:read("*a"); |
292
a9e69088e678
mod_adhoc_web: Put pubsubHost into js from lua. Compat with util.pubsub changes
Florian Zeitz <florob@babelmonkeys.de>
parents:
288
diff
changeset
|
136 data = data:gsub("%%PUBSUBHOST%%", service); |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
137 f:close(); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
138 if not data then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
139 return response_403; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
140 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
141 local ext = path:match("%.([^.]*)$"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
142 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
|
143 return { |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
144 headers = { ["Content-Type"] = mime; }; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
145 body = data; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
146 }; |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
147 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
148 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
149 local function handle_file_request(method, body, request) |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
150 local path = preprocess_path(request.url.path); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
151 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
|
152 path_stripped = path:gsub("^/[^/]+", ""); -- Strip /admin/ |
5ec9125575fc
mod_admin_web: Handle paths without trailing slash
Florian Zeitz <florob@babelmonkeys.de>
parents:
304
diff
changeset
|
153 return serve_file(path_stripped, path); |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
154 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
155 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
156 function module.load() |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
157 local http_conf = config.get("*", "core", "webadmin_http_ports"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
158 |
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
|
159 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
|
160 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
|
161 |
304
8f3499ae1e27
mod_admin_web: Fix initialisation code, undeclared variable and wrong event scope
Matthew Wild <mwild1@gmail.com>
parents:
303
diff
changeset
|
162 prosody.events.add_handler("server-started", function () |
8f3499ae1e27
mod_admin_web: Fix initialisation code, undeclared variable and wrong event scope
Matthew Wild <mwild1@gmail.com>
parents:
303
diff
changeset
|
163 local host_session = prosody.hosts[host]; |
301
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
164 if not select(2, hosts[service].modules.pubsub.service:get_nodes(service))[xmlns_s2s_session] then |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
165 local ok, errmsg = hosts[service].modules.pubsub.service:create(xmlns_s2s_session, service); |
295
e373de5907aa
mod_admin_web: Only create the node once when loading onto multiple hosts
Florian Zeitz <florob@babelmonkeys.de>
parents:
292
diff
changeset
|
166 if not ok then |
301
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
167 module:log("warn", "Could not create node " .. xmlns_s2s_session .. ": " .. tostring(errmsg)); |
295
e373de5907aa
mod_admin_web: Only create the node once when loading onto multiple hosts
Florian Zeitz <florob@babelmonkeys.de>
parents:
292
diff
changeset
|
168 end |
292
a9e69088e678
mod_adhoc_web: Put pubsubHost into js from lua. Compat with util.pubsub changes
Florian Zeitz <florob@babelmonkeys.de>
parents:
288
diff
changeset
|
169 end |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
170 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
171 for remotehost, session in pairs(host_session.s2sout) do |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
172 if session.type ~= "s2sout_unauthed" then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
173 add_host(session, "out"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
174 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
175 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
176 for session in pairs(incoming_s2s) do |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
177 if session.to_host == host then |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
178 add_host(session, "in"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
179 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
180 end |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
181 |
301
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
182 if not select(2, hosts[service].modules.pubsub.service:get_nodes(service))[xmlns_c2s_session] then |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
183 local ok, errmsg = hosts[service].modules.pubsub.service:create(xmlns_c2s_session, service); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
184 if not ok then |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
185 module:log("warn", "Could not create node " .. xmlns_c2s_session .. ": " .. tostring(errmsg)); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
186 end |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
187 end |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
188 |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
189 for username, user in pairs(host_session.sessions or {}) do |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
190 for resource, session in pairs(user.sessions or {}) do |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
191 add_client(session); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
192 end |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
193 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
|
194 end); |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
195 |
301
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
196 module:hook("resource-bind", function(event) |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
197 add_client(event.session); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
198 end); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
199 |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
200 module:hook("resource-unbind", function(event) |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
201 del_client(event.session); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
202 end); |
b241c79a0eb7
mod_admin_web: Add a live view for C2S connections
Florian Zeitz <florob@babelmonkeys.de>
parents:
295
diff
changeset
|
203 |
288
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
204 module:hook("s2sout-established", function(event) |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
205 add_host(event.session, "out"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
206 end); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
207 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
208 module:hook("s2sin-established", function(event) |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
209 add_host(event.session, "in"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
210 end); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
211 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
212 module:hook("s2sout-destroyed", function(event) |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
213 del_host(event.session, "out"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
214 end); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
215 |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
216 module:hook("s2sin-destroyed", function(event) |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
217 del_host(event.session, "in"); |
9233d7ee3c09
mod_admin_web: Initial PoC commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff
changeset
|
218 end); |