Changeset

1004:290c21a5e0ee

mod_muc_log, mod_muc_log_http: cleanup syntax (off with the "~= nil"), and cut down wild table indexing.
author Marco Cirillo <maranda@lightwitch.org>
date Thu, 09 May 2013 02:13:09 +0200
parents 1003:767999c39f0a
children 1005:591590de34ef
files mod_muc_log/mod_muc_log.lua mod_muc_log_http/muc_log_http/mod_muc_log_http.lua
diffstat 2 files changed, 32 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_log/mod_muc_log.lua	Wed May 08 23:30:50 2013 +0100
+++ b/mod_muc_log/mod_muc_log.lua	Thu May 09 02:13:09 2013 +0200
@@ -1,4 +1,5 @@
 local prosody = prosody;
+local hosts = prosody.hosts;
 local tostring = tostring;
 local splitJid = require "util.jid".split;
 local cm = require "core.configmanager";
@@ -36,11 +37,12 @@
 		(stanza.name == "iq") or
 	   	(stanza.name == "message" and tostring(stanza.attr.type) == "groupchat")
 	then
-		local node, host, resource = splitJid(stanza.attr.to);
-		if node ~= nil and host ~= nil then
+		local node, host = splitJid(stanza.attr.to);
+		local muc = hosts[host].muc;
+		if node and host then
 			local bare = node .. "@" .. host;
-			if host == mod_host and prosody.hosts[host] ~= nil and prosody.hosts[host].muc ~= nil and prosody.hosts[host].muc.rooms[bare] ~= nil then
-				local room = prosody.hosts[host].muc.rooms[bare]
+			if muc and muc.rooms[bare] then
+				local room = muc.rooms[bare]
 				local today = os.date("%y%m%d");
 				local now = os.date("%X")
 				local mucTo = nil
@@ -53,14 +55,14 @@
 				
 				if stanza.name == "presence" and stanza.attr.type == nil then
 					mucFrom = stanza.attr.to;
-					if room._occupants ~= nil and room._occupants[stanza.attr.to] ~= nil then -- if true, the user has already joined the room
+					if room._occupants and room._occupants[stanza.attr.to] then -- if true, the user has already joined the room
 						alreadyJoined = true;
 						stanza:tag("alreadyJoined"):text("true"); -- we need to log the information that the user has already joined, so add this and remove after logging
 					end
 				elseif stanza.name == "iq" and stanza.attr.type == "set" then -- kick, to is the room, from is the admin, nick who is kicked is attr of iq->query->item
-					if stanza.tags[1] ~= nil and stanza.tags[1].name == "query" then
+					if stanza.tags[1] and stanza.tags[1].name == "query" then
 						local tmp = stanza.tags[1];
-						if tmp.tags[1] ~= nil and tmp.tags[1].name == "item" and tmp.tags[1].attr.nick ~= nil then
+						if tmp.tags[1] ~= nil and tmp.tags[1].name == "item" and tmp.tags[1].attr.nick then
 							tmp = tmp.tags[1];
 							for jid, nick in pairs(room._jid_nick) do
 								if nick == stanza.attr.to .. "/" .. tmp.attr.nick then
@@ -79,7 +81,7 @@
 					end
 				end
 
-				if (mucFrom ~= nil or mucTo ~= nil) then
+				if (mucFrom or mucTo) then
 					local data = data_load(node, host, datastore .. "/" .. today);
 					local realFrom = stanza.attr.from;
 					local realTo = stanza.attr.to;
--- a/mod_muc_log_http/muc_log_http/mod_muc_log_http.lua	Wed May 08 23:30:50 2013 +0100
+++ b/mod_muc_log_http/muc_log_http/mod_muc_log_http.lua	Thu May 09 02:13:09 2013 +0200
@@ -1,6 +1,7 @@
 module:depends("http");
 
 local prosody = prosody;
+local hosts = prosody.hosts;
 local my_host = module:get_host();
 local tabSort = table.sort;
 local strchar = string.char;
@@ -65,8 +66,9 @@
 
 local function generateRoomListSiteContent(component)
 	local rooms = "";
-	if prosody.hosts[component] and prosody.hosts[component].muc ~= nil then
-		for jid, room in pairs(prosody.hosts[component].muc.rooms) do
+	local component_host = hosts[component];
+	if component_host and component_host.muc ~= nil then
+		for jid, room in pairs(component_host.muc.rooms) do
 			local node = splitJid(jid);
 			if not room._data.hidden and node then
 				rooms = rooms .. html.rooms.bit:gsub("###ROOM###", node):gsub("###COMPONENT###", component);
@@ -194,7 +196,7 @@
 	end
 	local bareDay = str_format("20%.02d-%.02d-%.02d", year, month, day);
 	room = p_encode(room);
-	local attributes, err = lfs.attributes(path.."/"..str_format("%.02d%.02d%.02d", year, month, day).."/"..room..".dat")
+	local attributes, err = lfs.attributes(path.."/"..str_format("%.02d%.02d%.02d", year, month, day).."/"..room..".dat");
 	if attributes ~= nil and attributes.mode == "file" then
 		local s = html.days.bit;
 		s = s:gsub("###BARE_DAY###", webPath .. bareDay);
@@ -208,7 +210,7 @@
 	local days = "";
 	local arrDays = {};
 	local tmp;
-	local node, host, resource = splitJid(bareRoomJid);
+	local node, host = splitJid(bareRoomJid);
 	local path = data_getpath(node, host, datastore);
 	local room = nil;
 	local nextRoom = "";
@@ -218,8 +220,9 @@
 	local since = "";
 	local to = "";
 	local topic = "";
+	local component = hosts[host];
 
-	if not(prosody.hosts[host] and prosody.hosts[host].muc and prosody.hosts[host].muc.rooms[bareRoomJid]) then
+	if not(component and component.muc and component.muc.rooms[bareRoomJid]) then
 		return;
 	end
 
@@ -227,7 +230,7 @@
 	attributes = lfs.attributes(path);
 	do
 		local found = 0;
-		for jid, room in pairs(prosody.hosts[host].muc.rooms) do
+		for jid, room in pairs(component.muc.rooms) do
 			local node = splitJid(jid)
 			if not room._data.hidden and node then
 				if found == 0 then
@@ -244,7 +247,7 @@
 			end
 		end
 
-		room = prosody.hosts[host].muc.rooms[bareRoomJid];
+		room = component.muc.rooms[bareRoomJid];
 		if room._data.hidden then
 			room = nil;
 		end
@@ -438,7 +441,7 @@
 end
 
 local function findNextDay(bareRoomJid, bare_day)
-	local node, host, resource = splitJid(bareRoomJid);
+	local node, host = splitJid(bareRoomJid);
 	local day = incrementDay(bare_day);
 	local max_trys = 7;
 
@@ -496,7 +499,7 @@
 end
 
 local function findPreviousDay(bareRoomJid, bare_day)
-	local node, host, resource = splitJid(bareRoomJid);
+	local node, host = splitJid(bareRoomJid);
 	local day = decrementDay(bare_day);
 	local max_trys = 7;
 	module:log("debug", day);
@@ -520,7 +523,7 @@
 	local month;
 	local day;
 	local tmp;
-	local node, host, resource = splitJid(bareRoomJid);
+	local node, host = splitJid(bareRoomJid);
 	local year, month, day = bare_day:match("^20(%d%d)-(%d%d)-(%d%d)$");
 	local previousDay = findPreviousDay(bareRoomJid, bare_day);
 	local nextDay = findNextDay(bareRoomJid, bare_day);
@@ -538,25 +541,25 @@
 	temptime.year = tonumber(year)
 	calendar = createMonth(temptime.month, temptime.year, {callback=perDayCallback, path=path, room=node, webPath="../"}) or ""
 
-	if bare_day ~= nil then
+	if bare_day then
 		local data = data_load(node, host, datastore .. "/" .. bare_day:match("^20(.*)"):gsub("-", ""));
-		if data ~= nil then
+		if data then
 			for i=1, #data, 1 do
 				local stanza = lom.parse(data[i]);
-				if stanza ~= nil and stanza.attr ~= nil and stanza.attr.time ~= nil then
+				if stanza and stanza.attr and stanza.attr.time then
 					local timeStuff = html.day.time:gsub("###TIME###", stanza.attr.time):gsub("###UTC###", stanza.attr.utc or stanza.attr.time);
 					if stanza[1] ~= nil then
 						local nick;
 						local tmp;
 
 						-- grep nick from "from" resource
-						if stanza[1].attr.from ~= nil then -- presence and messages
+						if stanza[1].attr.from then -- presence and messages
 							nick = htmlEscape(stanza[1].attr.from:match("/(.+)$"));
-						elseif stanza[1].attr.to ~= nil then -- iq
+						elseif stanza[1].attr.to then -- iq
 							nick = htmlEscape(stanza[1].attr.to:match("/(.+)$"));
 						end
 
-						if stanza[1].tag == "presence" and nick ~= nil then
+						if stanza[1].tag == "presence" and nick then
 							tmp = parsePresenceStanza(stanza[1], timeStuff, nick);
 						elseif stanza[1].tag == "message" then
 							tmp = parseMessageStanza(stanza[1], timeStuff, nick);
@@ -565,7 +568,7 @@
 						else
 							module:log("info", "unknown stanza subtag in log found. room: %s; day: %s", bareRoomJid, year .. "/" .. month .. "/" .. day);
 						end
-						if tmp ~= nil then
+						if tmp then
 							ret = ret .. tmp
 							tmp = nil;
 						end
@@ -611,7 +614,7 @@
 
 	node = urldecode(node);
 
-	if not html.doc then
+	if not html.doc then 
 		response.status_code = 500;
 		return response:send(handle_error(response.status_code, "Muc Theme is not loaded."));
 	end
@@ -684,7 +687,7 @@
 	config = module:get_option_table("muc_log_http_config", {});
 	if config.showStatus == nil then config.showStatus = true; end
 	if config.showJoin == nil then config.showJoin = true; end
-	if config.urlBase ~= nil and type(config.urlBase) then urlBase = config.urlBase; end
+	if config.urlBase and type(config.urlBase) == "string" then urlBase = config.urlBase; end
 
 	theme = config.theme or "prosody";
 	local themePath = themesParent .. "/" .. tostring(theme);