comparison mod_muc_log_http/muc_log_http/mod_muc_log_http.lua @ 1450:5107278268ae

mod_muc_log, mod_muc_log_http: Make compatible with both new and old MUC API
author Vadim Misbakh-Soloviov <mva@mva.name>
date Tue, 24 Jun 2014 13:14:54 +0700
parents 7dbde05b48a9
children 7ebec464914e
comparison
equal deleted inserted replaced
1449:365f6db9531a 1450:5107278268ae
64 end 64 end
65 local function urlencode(s) 65 local function urlencode(s)
66 return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return ("%%%02x"):format(c:byte()); end)); 66 return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return ("%%%02x"):format(c:byte()); end));
67 end 67 end
68 68
69 local function generate_room_list(component) 69 local function get_room_from_jid(jid)
70 local rooms = ""; 70 local node, host = split_jid(jid);
71 local component_host = hosts[component]; 71 local component = hosts[host];
72 if component_host and component_host.muc ~= nil then 72 if component then
73 for jid, room in pairs(component_host.muc.rooms) do 73 local muc = component.modules.muc
74 if muc and rawget(muc,"rooms") then
75 -- We're running 0.9.x or 0.10 (old MUC API)
76 return muc.rooms[jid];
77 elseif muc and rawget(muc,"get_room_from_jid") then
78 -- We're running >0.10 (new MUC API)
79 return muc.get_room_from_jid(jid);
80 else
81 return
82 end
83 end
84 end
85
86 local function get_room_list(host)
87 local component = hosts[host];
88 local list = {};
89 if component then
90 local muc = component.modules.muc
91 if muc and rawget(muc,"rooms") then
92 -- We're running 0.9.x or 0.10 (old MUC API)
93 for _, room in pairs(muc.rooms) do
94 list[room.jid] = room;
95 end
96 return list;
97 elseif muc and rawget(muc,"each_room") then
98 -- We're running >0.10 (new MUC API)
99 for room, _ in muc.each_room() do
100 list[room.jid] = room;
101 end
102 return list;
103 end
104 end
105 end
106
107 local function generate_room_list(host)
108 local rooms;
109
110 for jid, room in pairs(get_room_list(host)) do
74 local node = split_jid(jid); 111 local node = split_jid(jid);
75 if not room._data.hidden and room._data.logging and node then 112 if not room._data.hidden and room._data.logging and node then
76 rooms = rooms .. html.rooms.bit:gsub("###ROOM###", urlencode(node)):gsub("###COMPONENT###", component); 113 rooms = (rooms or "") .. html.rooms.bit:gsub("###ROOM###", urlencode(node)):gsub("###COMPONENT###", host);
77 end 114 end
78 end 115 end
79 return html.rooms.body:gsub("###ROOMS_STUFF###", rooms):gsub("###COMPONENT###", component), "Chatroom logs for "..component; 116
80 end 117 if rooms then
118 return html.rooms.body:gsub("###ROOMS_STUFF###", rooms):gsub("###COMPONENT###", host), "Chatroom logs for "..host;
119 end
81 end 120 end
82 121
83 -- Calendar stuff 122 -- Calendar stuff
84 local function get_days_for_month(month, year) 123 local function get_days_for_month(month, year)
85 if month == 2 then 124 if month == 2 then
222 local since = ""; 261 local since = "";
223 local to = ""; 262 local to = "";
224 local topic = ""; 263 local topic = "";
225 local component = hosts[host]; 264 local component = hosts[host];
226 265
227 if not(component and component.muc and component.muc.rooms[bare_room_jid]) then 266 if not(get_room_from_jid(bare_room_jid)) then
228 return; 267 return;
229 end 268 end
230 269
231 path = path:gsub("/[^/]*$", ""); 270 path = path:gsub("/[^/]*$", "");
232 attributes = lfs.attributes(path); 271 attributes = lfs.attributes(path);
233 do 272 do
234 local found = 0; 273 local found = 0;
235 for jid, room in pairs(component.muc.rooms) do 274 module:log("debug", generate_room_list(host));
275 for jid, room in pairs(get_room_list(host)) do
236 local node = split_jid(jid) 276 local node = split_jid(jid)
237 if not room._data.hidden and room._data.logging and node then 277 if not room._data.hidden and room._data.logging and node then
238 if found == 0 then 278 if found == 0 then
239 previous_room = node 279 previous_room = node
240 elseif found == 1 then 280 elseif found == 1 then
247 287
248 rooms = rooms .. html.days.rooms.bit:gsub("###ROOM###", urlencode(node)); 288 rooms = rooms .. html.days.rooms.bit:gsub("###ROOM###", urlencode(node));
249 end 289 end
250 end 290 end
251 291
252 room = component.muc.rooms[bare_room_jid]; 292 room = get_room_from_jid(bare_room_jid);
253 if room._data.hidden or not room._data.logging then 293 if room._data.hidden or not room._data.logging then
254 room = nil; 294 room = nil;
255 end 295 end
256 end 296 end
257 if attributes and room then 297 if attributes and room then
609 local response = event.response; 649 local response = event.response;
610 local request = event.request; 650 local request = event.request;
611 local room; 651 local room;
612 652
613 local node, day, more = request.url.path:match("^/"..url_base.."/+([^/]*)/*([^/]*)/*(.*)$"); 653 local node, day, more = request.url.path:match("^/"..url_base.."/+([^/]*)/*([^/]*)/*(.*)$");
654
614 if more ~= "" then 655 if more ~= "" then
615 response.status_code = 404; 656 response.status_code = 404;
616 return response:send(handle_error(response.status_code, "Unknown URL.")); 657 return response:send(handle_error(response.status_code, "Unknown URL."));
617 end 658 end
618 if node == "" then node = nil; end 659 if node == "" then node = nil; end
623 if not html.doc then 664 if not html.doc then
624 response.status_code = 500; 665 response.status_code = 500;
625 return response:send(handle_error(response.status_code, "Muc Theme is not loaded.")); 666 return response:send(handle_error(response.status_code, "Muc Theme is not loaded."));
626 end 667 end
627 668
628 669 if node then room = get_room_from_jid(node.."@"..my_host); end
629 if node then room = hosts[my_host].modules.muc.rooms[node.."@"..my_host]; end
630 if node and not room then 670 if node and not room then
631 response.status_code = 404; 671 response.status_code = 404;
632 return response:send(handle_error(response.status_code, "Room doesn't exist.")); 672 return response:send(handle_error(response.status_code, "Room doesn't exist."));
633 end 673 end
634 if room and (room._data.hidden or not room._data.logging) then 674 if room and (room._data.hidden or not room._data.logging) then
635 response.status_code = 404; 675 response.status_code = 404;
636 return response:send(handle_error(response.status_code, "There're no logs for this room.")); 676 return response:send(handle_error(response.status_code, "There're no logs for this room."));
637 end 677 end
638
639 678
640 if not node then -- room list for component 679 if not node then -- room list for component
641 return response:send(create_doc(generate_room_list(my_host))); 680 return response:send(create_doc(generate_room_list(my_host)));
642 elseif not day then -- room's listing 681 elseif not day then -- room's listing
643 return response:send(create_doc(generate_day_room_content(node.."@"..my_host))); 682 return response:send(create_doc(generate_day_room_content(node.."@"..my_host)));