mod_muc_log_http/muc_log_http/mod_muc_log_http.lua
changeset 390 8090d246c268
parent 389 60248c463ec3
child 391 99e7ef4fc7d2
equal deleted inserted replaced
389:60248c463ec3 390:8090d246c268
    19 local urldecode = require "net.http".urldecode;
    19 local urldecode = require "net.http".urldecode;
    20 local datamanager = require "util.datamanager";
    20 local datamanager = require "util.datamanager";
    21 local data_load, data_getpath = datamanager.load, datamanager.getpath;
    21 local data_load, data_getpath = datamanager.load, datamanager.getpath;
    22 local datastore = "muc_log";
    22 local datastore = "muc_log";
    23 local urlBase = "muc_log";
    23 local urlBase = "muc_log";
    24 local muc_hosts = {};
       
    25 local config = nil;
    24 local config = nil;
    26 local tostring = _G.tostring;
    25 local tostring = _G.tostring;
    27 local tonumber = _G.tonumber;
    26 local tonumber = _G.tonumber;
    28 local os_date, os_time = os.date, os.time;
    27 local os_date, os_time = os.date, os.time;
    29 local str_format = string.format;
    28 local str_format = string.format;
    99 	return t;
    98 	return t;
   100 end
    99 end
   101 
   100 
   102 local function generateComponentListSiteContent()
   101 local function generateComponentListSiteContent()
   103 	local components = "";
   102 	local components = "";
   104 	for component,muc_host in pairs(muc_hosts or {}) do
   103 	for component,host in pairs(hosts) do
   105 		components = components .. html.components.bit:gsub("###COMPONENT###", component);
   104 		if host.modules.muc and host.modules.muc_log then
       
   105 			components = components .. html.components.bit:gsub("###COMPONENT###", component);
       
   106 		end
   106 	end
   107 	end
   107 	return html.components.body:gsub("###COMPONENTS_STUFF###", components);
   108 	return html.components.body:gsub("###COMPONENTS_STUFF###", components);
   108 end
   109 end
   109 
   110 
   110 local function generateRoomListSiteContent(component)
   111 local function generateRoomListSiteContent(component)
   263 	local to = "";
   264 	local to = "";
   264 	local topic = "";
   265 	local topic = "";
   265 
   266 
   266 	path = path:gsub("/[^/]*$", "");
   267 	path = path:gsub("/[^/]*$", "");
   267 	attributes = lfs.attributes(path);
   268 	attributes = lfs.attributes(path);
   268 	if muc_hosts ~= nil and muc_hosts[host] and prosody.hosts[host] ~= nil and prosody.hosts[host].muc ~= nil and prosody.hosts[host].muc.rooms[bareRoomJid] ~= nil then
   269 	do
   269 		local found = 0;
   270 		local found = 0;
   270 		for jid, room in pairs(prosody.hosts[host].muc.rooms) do
   271 		for jid, room in pairs(prosody.hosts[host].muc.rooms) do
   271 			local node = splitJid(jid)
   272 			local node = splitJid(jid)
   272 			if not room._data.hidden and node then
   273 			if not room._data.hidden and node then
   273 				if found == 0 then
   274 				if found == 0 then
   646 	if node == "" then node = nil; end
   647 	if node == "" then node = nil; end
   647 	if day  == "" then day  = nil; end
   648 	if day  == "" then day  = nil; end
   648 
   649 
   649 	node = urldecode(node);
   650 	node = urldecode(node);
   650 
   651 
   651 	assert(muc_hosts and html.doc, "MUC hosts or theme not loaded");
   652 	assert(html.doc, "MUC hosts or theme not loaded");
   652 
   653 
   653 	if host and not(hosts[host] and hosts[host].modules.muc) then return { status = "404 Not found", body = "No such MUC component" }; end
   654 	if host and not(hosts[host] and hosts[host].modules.muc and hosts[host].modules.muc_log) then return { status = "404 Not found", body = "No such MUC component" }; end
   654 	if host and node and not(hosts[host].modules.muc.rooms[node.."@"..host]) then return { status = "404 Not found", body = "No such MUC room" }; end
   655 	if host and node and not(hosts[host].modules.muc.rooms[node.."@"..host]) then return { status = "404 Not found", body = "No such MUC room" }; end
   655 
   656 
   656 	if not host then -- main component list
   657 	if not host then -- main component list
   657 		return createDoc(generateComponentListSiteContent());
   658 		return createDoc(generateComponentListSiteContent());
   658 	elseif not node then -- room list for component
   659 	elseif not node then -- room list for component
   742 		return false;
   743 		return false;
   743 	end
   744 	end
   744 	-- module:log("debug", (require "util.serialization").serialize(html));
   745 	-- module:log("debug", (require "util.serialization").serialize(html));
   745 
   746 
   746 	httpserver.new_from_config({ config.http_port or true }, handle_request, { base = urlBase, ssl = false, port = 5290 });
   747 	httpserver.new_from_config({ config.http_port or true }, handle_request, { base = urlBase, ssl = false, port = 5290 });
   747 
   748 end
   748 	for jid, host in pairs(prosody.hosts) do
       
   749 		if host.muc then
       
   750 			local enabledModules = config_get(jid, "core", "modules_enabled");
       
   751 			if enabledModules then
       
   752 				for _,mod in ipairs(enabledModules) do
       
   753 					if(mod == "muc_log") then
       
   754 						module:log("debug", "component: %s", tostring(jid));
       
   755 						muc_hosts[jid] = true;
       
   756 						break;
       
   757 					end
       
   758 				end
       
   759 			end
       
   760 		end
       
   761 	end
       
   762 	module:log("debug", "loaded mod_muc_log_http");
       
   763 end
       
   764 
       
   765 function module.unload()
       
   766 	muc_hosts = nil;
       
   767 	module:log("debug", "unloaded mod_muc_log_http");
       
   768 end
       
   769 
       
   770 module:hook("component-activated", function(component, config)
       
   771 	if config.core and config.core.modules_enabled then
       
   772 		for _,mod in ipairs(config.core.modules_enabled) do
       
   773 			if(mod == "muc_log") then
       
   774 				module:log("debug", "component: %s", tostring(component));
       
   775 				muc_hosts[component] = true;
       
   776 				break;
       
   777 			end
       
   778 		end
       
   779 	end
       
   780 end);