Comparison

mod_admin_web/admin_web/mod_admin_web.lua @ 301:b241c79a0eb7

mod_admin_web: Add a live view for C2S connections
author Florian Zeitz <florob@babelmonkeys.de>
date Mon, 27 Dec 2010 04:19:41 +0100
parent 295:e373de5907aa
child 303:0f53c88bab9a
comparison
equal deleted inserted replaced
300:b81e4f86a231 301:b241c79a0eb7
1 -- Copyright (C) 2010 Florian Zeitz 1 -- Copyright (C) 2010 Florian Zeitz
2 -- 2 --
3 -- This file is MIT/X11 licensed. Please see the 3 -- This file is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information. 4 -- COPYING file in the source package for more information.
5 -- 5 --
6
7 -- <session xmlns="http://prosody.im/streams/c2s" jid="alice@example.com/brussels">
8 -- <encrypted/>
9 -- <compressed/>
10 -- </session>
6 11
7 -- <session xmlns="http://prosody.im/streams/s2s" jid="example.com"> 12 -- <session xmlns="http://prosody.im/streams/s2s" jid="example.com">
8 -- <encrypted/> 13 -- <encrypted/>
9 -- <compressed/> 14 -- <compressed/>
10 -- <in/> / <out/> 15 -- <in/> / <out/>
20 local host = module:get_host(); 25 local host = module:get_host();
21 local service = config.get("*", "core", "webadmin_pubsub_host") or ("pubsub." .. host); 26 local service = config.get("*", "core", "webadmin_pubsub_host") or ("pubsub." .. host);
22 27
23 local http_base = (prosody.paths.plugins or "./plugins/") .. "admin_web/www_files"; 28 local http_base = (prosody.paths.plugins or "./plugins/") .. "admin_web/www_files";
24 29
25 local xmlns_sessions = "http://prosody.im/streams/s2s"; 30 local xmlns_c2s_session = "http://prosody.im/streams/c2s";
31 local xmlns_s2s_session = "http://prosody.im/streams/s2s";
26 32
27 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" }; 33 local response_400 = { status = "400 Bad Request", body = "<h1>Bad Request</h1>Sorry, we didn't understand your request :(" };
28 local response_403 = { status = "403 Forbidden", body = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :(" }; 34 local response_403 = { status = "403 Forbidden", body = "<h1>Forbidden</h1>You don't have permission to view the contents of this directory :(" };
29 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" }; 35 local response_404 = { status = "404 Not Found", body = "<h1>Page Not Found</h1>Sorry, we couldn't find what you were looking for :(" };
30 36
35 css = "text/css"; 41 css = "text/css";
36 }; 42 };
37 43
38 local idmap = {}; 44 local idmap = {};
39 45
46 function add_client(session)
47 local name = session.full_jid;
48 local id = idmap[name];
49 if not id then
50 id = uuid_generate();
51 idmap[name] = id;
52 end
53 local item = stanza.stanza("item", { id = id }):tag("session", {xmlns = xmlns_c2s_session, jid = name}):up();
54 if session.secure then
55 item:tag("encrypted"):up();
56 end
57 if session.compressed then
58 item:tag("compressed"):up();
59 end
60 hosts[service].modules.pubsub.service:publish(xmlns_c2s_session, service, id, item);
61 module:log("debug", "Added client " .. name);
62 end
63
64 function del_client(session)
65 local name = session.full_jid;
66 local id = idmap[name];
67 if id then
68 local notifier = stanza.stanza("retract", { id = id });
69 hosts[service].modules.pubsub.service:retract(xmlns_c2s_session, service, id, notifier);
70 end
71 end
72
40 function add_host(session, type) 73 function add_host(session, type)
41 local name = (type == "out" and session.to_host) or (type == "in" and session.from_host); 74 local name = (type == "out" and session.to_host) or (type == "in" and session.from_host);
42 local id = idmap[name.."_"..type]; 75 local id = idmap[name.."_"..type];
43 if not id then 76 if not id then
44 id = uuid_generate(); 77 id = uuid_generate();
45 idmap[name.."_"..type] = id; 78 idmap[name.."_"..type] = id;
46 end 79 end
47 local item = stanza.stanza("item", { id = id }):tag("session", {xmlns = xmlns_sessions, jid = name}) 80 local item = stanza.stanza("item", { id = id }):tag("session", {xmlns = xmlns_s2s_session, jid = name})
48 :tag(type):up(); 81 :tag(type):up();
49 if session.secure then 82 if session.secure then
50 item:tag("encrypted"):up(); 83 item:tag("encrypted"):up();
51 end 84 end
52 if session.compressed then 85 if session.compressed then
53 item:tag("compressed"):up(); 86 item:tag("compressed"):up();
54 end 87 end
55 hosts[service].modules.pubsub.service:publish(xmlns_sessions, service, id, item); 88 hosts[service].modules.pubsub.service:publish(xmlns_s2s_session, service, id, item);
56 module:log("debug", "Added host " .. name .. " s2s" .. type); 89 module:log("debug", "Added host " .. name .. " s2s" .. type);
57 end 90 end
58 91
59 function del_host(session, type) 92 function del_host(session, type)
60 local name = (type == "out" and session.to_host) or (type == "in" and session.from_host); 93 local name = (type == "out" and session.to_host) or (type == "in" and session.from_host);
61 local id = idmap[name.."_"..type]; 94 local id = idmap[name.."_"..type];
62 if id then 95 if id then
63 local notifier = stanza.stanza("retract", { id = id }); 96 local notifier = stanza.stanza("retract", { id = id });
64 hosts[service].modules.pubsub.service:retract(xmlns_sessions, service, id, notifier); 97 hosts[service].modules.pubsub.service:retract(xmlns_s2s_session, service, id, notifier);
65 end 98 end
66 end 99 end
67 100
68 local function preprocess_path(path) 101 local function preprocess_path(path)
69 if path:sub(1,1) ~= "/" then 102 if path:sub(1,1) ~= "/" then
116 149
117 function module.load() 150 function module.load()
118 local host_session = prosody.hosts[host]; 151 local host_session = prosody.hosts[host];
119 local http_conf = config.get("*", "core", "webadmin_http_ports"); 152 local http_conf = config.get("*", "core", "webadmin_http_ports");
120 153
121 if not select(2, hosts[service].modules.pubsub.service:get_nodes(service))[xmlns_sessions] then 154 if not select(2, hosts[service].modules.pubsub.service:get_nodes(service))[xmlns_s2s_session] then
122 local ok, errmsg = hosts[service].modules.pubsub.service:create(xmlns_sessions, service); 155 local ok, errmsg = hosts[service].modules.pubsub.service:create(xmlns_s2s_session, service);
123 if not ok then 156 if not ok then
124 error("Could not create node: " .. tostring(errmsg)); 157 module:log("warn", "Could not create node " .. xmlns_s2s_session .. ": " .. tostring(errmsg));
125 end 158 end
126 end 159 end
127 160
128 for remotehost, session in pairs(host_session.s2sout) do 161 for remotehost, session in pairs(host_session.s2sout) do
129 if session.type ~= "s2sout_unauthed" then 162 if session.type ~= "s2sout_unauthed" then
134 if session.to_host == host then 167 if session.to_host == host then
135 add_host(session, "in"); 168 add_host(session, "in");
136 end 169 end
137 end 170 end
138 171
172 if not select(2, hosts[service].modules.pubsub.service:get_nodes(service))[xmlns_c2s_session] then
173 local ok, errmsg = hosts[service].modules.pubsub.service:create(xmlns_c2s_session, service);
174 if not ok then
175 module:log("warn", "Could not create node " .. xmlns_c2s_session .. ": " .. tostring(errmsg));
176 end
177 end
178
179 for username, user in pairs(host_session.sessions or {}) do
180 for resource, session in pairs(user.sessions or {}) do
181 add_client(session);
182 end
183 end
184
139 httpserver.new_from_config(http_conf, handle_file_request, { base = "admin" }); 185 httpserver.new_from_config(http_conf, handle_file_request, { base = "admin" });
140 end 186 end
187
188 module:hook("resource-bind", function(event)
189 add_client(event.session);
190 end);
191
192 module:hook("resource-unbind", function(event)
193 del_client(event.session);
194 end);
141 195
142 module:hook("s2sout-established", function(event) 196 module:hook("s2sout-established", function(event)
143 add_host(event.session, "out"); 197 add_host(event.session, "out");
144 end); 198 end);
145 199