comparison mod_muc_log_http/muc_log_http/mod_muc_log_http.lua @ 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 f178318c9aeb
children 043189eb41bc
comparison
equal deleted inserted replaced
1003:767999c39f0a 1004:290c21a5e0ee
1 module:depends("http"); 1 module:depends("http");
2 2
3 local prosody = prosody; 3 local prosody = prosody;
4 local hosts = prosody.hosts;
4 local my_host = module:get_host(); 5 local my_host = module:get_host();
5 local tabSort = table.sort; 6 local tabSort = table.sort;
6 local strchar = string.char; 7 local strchar = string.char;
7 local strformat = string.format; 8 local strformat = string.format;
8 local splitJid = require "util.jid".split; 9 local splitJid = require "util.jid".split;
63 return escapedUrl 64 return escapedUrl
64 end 65 end
65 66
66 local function generateRoomListSiteContent(component) 67 local function generateRoomListSiteContent(component)
67 local rooms = ""; 68 local rooms = "";
68 if prosody.hosts[component] and prosody.hosts[component].muc ~= nil then 69 local component_host = hosts[component];
69 for jid, room in pairs(prosody.hosts[component].muc.rooms) do 70 if component_host and component_host.muc ~= nil then
71 for jid, room in pairs(component_host.muc.rooms) do
70 local node = splitJid(jid); 72 local node = splitJid(jid);
71 if not room._data.hidden and node then 73 if not room._data.hidden and node then
72 rooms = rooms .. html.rooms.bit:gsub("###ROOM###", node):gsub("###COMPONENT###", component); 74 rooms = rooms .. html.rooms.bit:gsub("###ROOM###", node):gsub("###COMPONENT###", component);
73 end 75 end
74 end 76 end
192 if year > 2000 then 194 if year > 2000 then
193 year = year - 2000; 195 year = year - 2000;
194 end 196 end
195 local bareDay = str_format("20%.02d-%.02d-%.02d", year, month, day); 197 local bareDay = str_format("20%.02d-%.02d-%.02d", year, month, day);
196 room = p_encode(room); 198 room = p_encode(room);
197 local attributes, err = lfs.attributes(path.."/"..str_format("%.02d%.02d%.02d", year, month, day).."/"..room..".dat") 199 local attributes, err = lfs.attributes(path.."/"..str_format("%.02d%.02d%.02d", year, month, day).."/"..room..".dat");
198 if attributes ~= nil and attributes.mode == "file" then 200 if attributes ~= nil and attributes.mode == "file" then
199 local s = html.days.bit; 201 local s = html.days.bit;
200 s = s:gsub("###BARE_DAY###", webPath .. bareDay); 202 s = s:gsub("###BARE_DAY###", webPath .. bareDay);
201 s = s:gsub("###DAY###", day); 203 s = s:gsub("###DAY###", day);
202 return s; 204 return s;
206 208
207 local function generateDayListSiteContentByRoom(bareRoomJid) 209 local function generateDayListSiteContentByRoom(bareRoomJid)
208 local days = ""; 210 local days = "";
209 local arrDays = {}; 211 local arrDays = {};
210 local tmp; 212 local tmp;
211 local node, host, resource = splitJid(bareRoomJid); 213 local node, host = splitJid(bareRoomJid);
212 local path = data_getpath(node, host, datastore); 214 local path = data_getpath(node, host, datastore);
213 local room = nil; 215 local room = nil;
214 local nextRoom = ""; 216 local nextRoom = "";
215 local previousRoom = ""; 217 local previousRoom = "";
216 local rooms = ""; 218 local rooms = "";
217 local attributes = nil; 219 local attributes = nil;
218 local since = ""; 220 local since = "";
219 local to = ""; 221 local to = "";
220 local topic = ""; 222 local topic = "";
221 223 local component = hosts[host];
222 if not(prosody.hosts[host] and prosody.hosts[host].muc and prosody.hosts[host].muc.rooms[bareRoomJid]) then 224
225 if not(component and component.muc and component.muc.rooms[bareRoomJid]) then
223 return; 226 return;
224 end 227 end
225 228
226 path = path:gsub("/[^/]*$", ""); 229 path = path:gsub("/[^/]*$", "");
227 attributes = lfs.attributes(path); 230 attributes = lfs.attributes(path);
228 do 231 do
229 local found = 0; 232 local found = 0;
230 for jid, room in pairs(prosody.hosts[host].muc.rooms) do 233 for jid, room in pairs(component.muc.rooms) do
231 local node = splitJid(jid) 234 local node = splitJid(jid)
232 if not room._data.hidden and node then 235 if not room._data.hidden and node then
233 if found == 0 then 236 if found == 0 then
234 previousRoom = node 237 previousRoom = node
235 elseif found == 1 then 238 elseif found == 1 then
242 245
243 rooms = rooms .. html.days.rooms.bit:gsub("###ROOM###", node); 246 rooms = rooms .. html.days.rooms.bit:gsub("###ROOM###", node);
244 end 247 end
245 end 248 end
246 249
247 room = prosody.hosts[host].muc.rooms[bareRoomJid]; 250 room = component.muc.rooms[bareRoomJid];
248 if room._data.hidden then 251 if room._data.hidden then
249 room = nil; 252 room = nil;
250 end 253 end
251 end 254 end
252 if attributes ~= nil and room ~= nil then 255 if attributes ~= nil and room ~= nil then
436 end 439 end
437 return strformat("20%.02d-%.02d-%.02d", year, month, day); 440 return strformat("20%.02d-%.02d-%.02d", year, month, day);
438 end 441 end
439 442
440 local function findNextDay(bareRoomJid, bare_day) 443 local function findNextDay(bareRoomJid, bare_day)
441 local node, host, resource = splitJid(bareRoomJid); 444 local node, host = splitJid(bareRoomJid);
442 local day = incrementDay(bare_day); 445 local day = incrementDay(bare_day);
443 local max_trys = 7; 446 local max_trys = 7;
444 447
445 module:log("debug", day); 448 module:log("debug", day);
446 while(not store_exists(node, host, day)) do 449 while(not store_exists(node, host, day)) do
494 end 497 end
495 return strformat("20%.02d-%.02d-%.02d", year, month, day); 498 return strformat("20%.02d-%.02d-%.02d", year, month, day);
496 end 499 end
497 500
498 local function findPreviousDay(bareRoomJid, bare_day) 501 local function findPreviousDay(bareRoomJid, bare_day)
499 local node, host, resource = splitJid(bareRoomJid); 502 local node, host = splitJid(bareRoomJid);
500 local day = decrementDay(bare_day); 503 local day = decrementDay(bare_day);
501 local max_trys = 7; 504 local max_trys = 7;
502 module:log("debug", day); 505 module:log("debug", day);
503 while(not store_exists(node, host, day)) do 506 while(not store_exists(node, host, day)) do
504 max_trys = max_trys - 1; 507 max_trys = max_trys - 1;
518 local ret = ""; 521 local ret = "";
519 local year; 522 local year;
520 local month; 523 local month;
521 local day; 524 local day;
522 local tmp; 525 local tmp;
523 local node, host, resource = splitJid(bareRoomJid); 526 local node, host = splitJid(bareRoomJid);
524 local year, month, day = bare_day:match("^20(%d%d)-(%d%d)-(%d%d)$"); 527 local year, month, day = bare_day:match("^20(%d%d)-(%d%d)-(%d%d)$");
525 local previousDay = findPreviousDay(bareRoomJid, bare_day); 528 local previousDay = findPreviousDay(bareRoomJid, bare_day);
526 local nextDay = findNextDay(bareRoomJid, bare_day); 529 local nextDay = findNextDay(bareRoomJid, bare_day);
527 local temptime = {day=0, month=0, year=0}; 530 local temptime = {day=0, month=0, year=0};
528 local path = data_getpath(node, host, datastore); 531 local path = data_getpath(node, host, datastore);
536 temptime.day = tonumber(day) 539 temptime.day = tonumber(day)
537 temptime.month = tonumber(month) 540 temptime.month = tonumber(month)
538 temptime.year = tonumber(year) 541 temptime.year = tonumber(year)
539 calendar = createMonth(temptime.month, temptime.year, {callback=perDayCallback, path=path, room=node, webPath="../"}) or "" 542 calendar = createMonth(temptime.month, temptime.year, {callback=perDayCallback, path=path, room=node, webPath="../"}) or ""
540 543
541 if bare_day ~= nil then 544 if bare_day then
542 local data = data_load(node, host, datastore .. "/" .. bare_day:match("^20(.*)"):gsub("-", "")); 545 local data = data_load(node, host, datastore .. "/" .. bare_day:match("^20(.*)"):gsub("-", ""));
543 if data ~= nil then 546 if data then
544 for i=1, #data, 1 do 547 for i=1, #data, 1 do
545 local stanza = lom.parse(data[i]); 548 local stanza = lom.parse(data[i]);
546 if stanza ~= nil and stanza.attr ~= nil and stanza.attr.time ~= nil then 549 if stanza and stanza.attr and stanza.attr.time then
547 local timeStuff = html.day.time:gsub("###TIME###", stanza.attr.time):gsub("###UTC###", stanza.attr.utc or stanza.attr.time); 550 local timeStuff = html.day.time:gsub("###TIME###", stanza.attr.time):gsub("###UTC###", stanza.attr.utc or stanza.attr.time);
548 if stanza[1] ~= nil then 551 if stanza[1] ~= nil then
549 local nick; 552 local nick;
550 local tmp; 553 local tmp;
551 554
552 -- grep nick from "from" resource 555 -- grep nick from "from" resource
553 if stanza[1].attr.from ~= nil then -- presence and messages 556 if stanza[1].attr.from then -- presence and messages
554 nick = htmlEscape(stanza[1].attr.from:match("/(.+)$")); 557 nick = htmlEscape(stanza[1].attr.from:match("/(.+)$"));
555 elseif stanza[1].attr.to ~= nil then -- iq 558 elseif stanza[1].attr.to then -- iq
556 nick = htmlEscape(stanza[1].attr.to:match("/(.+)$")); 559 nick = htmlEscape(stanza[1].attr.to:match("/(.+)$"));
557 end 560 end
558 561
559 if stanza[1].tag == "presence" and nick ~= nil then 562 if stanza[1].tag == "presence" and nick then
560 tmp = parsePresenceStanza(stanza[1], timeStuff, nick); 563 tmp = parsePresenceStanza(stanza[1], timeStuff, nick);
561 elseif stanza[1].tag == "message" then 564 elseif stanza[1].tag == "message" then
562 tmp = parseMessageStanza(stanza[1], timeStuff, nick); 565 tmp = parseMessageStanza(stanza[1], timeStuff, nick);
563 elseif stanza[1].tag == "iq" then 566 elseif stanza[1].tag == "iq" then
564 tmp = parseIqStanza(stanza[1], timeStuff, nick); 567 tmp = parseIqStanza(stanza[1], timeStuff, nick);
565 else 568 else
566 module:log("info", "unknown stanza subtag in log found. room: %s; day: %s", bareRoomJid, year .. "/" .. month .. "/" .. day); 569 module:log("info", "unknown stanza subtag in log found. room: %s; day: %s", bareRoomJid, year .. "/" .. month .. "/" .. day);
567 end 570 end
568 if tmp ~= nil then 571 if tmp then
569 ret = ret .. tmp 572 ret = ret .. tmp
570 tmp = nil; 573 tmp = nil;
571 end 574 end
572 end 575 end
573 end 576 end
609 if node == "" then node = nil; end 612 if node == "" then node = nil; end
610 if day == "" then day = nil; end 613 if day == "" then day = nil; end
611 614
612 node = urldecode(node); 615 node = urldecode(node);
613 616
614 if not html.doc then 617 if not html.doc then
615 response.status_code = 500; 618 response.status_code = 500;
616 return response:send(handle_error(response.status_code, "Muc Theme is not loaded.")); 619 return response:send(handle_error(response.status_code, "Muc Theme is not loaded."));
617 end 620 end
618 621
619 622
682 685
683 function module.load() 686 function module.load()
684 config = module:get_option_table("muc_log_http_config", {}); 687 config = module:get_option_table("muc_log_http_config", {});
685 if config.showStatus == nil then config.showStatus = true; end 688 if config.showStatus == nil then config.showStatus = true; end
686 if config.showJoin == nil then config.showJoin = true; end 689 if config.showJoin == nil then config.showJoin = true; end
687 if config.urlBase ~= nil and type(config.urlBase) then urlBase = config.urlBase; end 690 if config.urlBase and type(config.urlBase) == "string" then urlBase = config.urlBase; end
688 691
689 theme = config.theme or "prosody"; 692 theme = config.theme or "prosody";
690 local themePath = themesParent .. "/" .. tostring(theme); 693 local themePath = themesParent .. "/" .. tostring(theme);
691 local attributes, err = lfs.attributes(themePath); 694 local attributes, err = lfs.attributes(themePath);
692 if attributes == nil or attributes.mode ~= "directory" then 695 if attributes == nil or attributes.mode ~= "directory" then