comparison mod_muc_log_http/muc_log_http/mod_muc_log_http.lua @ 380:5ef71af77ae2

mod_muc_log_http: Spaces -> Tabs.
author Waqas Hussain <waqas20@gmail.com>
date Mon, 25 Jul 2011 02:21:40 +0500
parents 03e1dc036a28
children f932dc371ce7
comparison
equal deleted inserted replaced
379:eebc19c224fb 380:5ef71af77ae2
44 ]]-- 44 ]]--
45 local html = {}; 45 local html = {};
46 local theme; 46 local theme;
47 47
48 local function checkDatastorePathExists(node, host, today, create) 48 local function checkDatastorePathExists(node, host, today, create)
49 create = create or false; 49 create = create or false;
50 local path = data_getpath(node, host, datastore, "dat", true); 50 local path = data_getpath(node, host, datastore, "dat", true);
51 path = path:gsub("/[^/]*$", ""); 51 path = path:gsub("/[^/]*$", "");
52 52
53 -- check existance 53 -- check existance
54 local attributes, err = lfs.attributes(path); 54 local attributes, err = lfs.attributes(path);
55 if attributes == nil or attributes.mode ~= "directory" then 55 if attributes == nil or attributes.mode ~= "directory" then
56 module:log("warn", "muc_log folder isn't a folder: %s", path); 56 module:log("warn", "muc_log folder isn't a folder: %s", path);
57 return false; 57 return false;
58 end 58 end
59 59
60 attributes, err = lfs.attributes(path .. "/" .. today); 60 attributes, err = lfs.attributes(path .. "/" .. today);
61 if attributes == nil then 61 if attributes == nil then
62 if create then 62 if create then
63 return lfs.mkdir(path .. "/" .. today); 63 return lfs.mkdir(path .. "/" .. today);
64 else 64 else
65 return false; 65 return false;
66 end 66 end
67 elseif attributes.mode == "directory" then 67 elseif attributes.mode == "directory" then
68 return true; 68 return true;
69 end 69 end
70 return false; 70 return false;
71 end 71 end
72 72
73 function createDoc(body) 73 function createDoc(body)
74 if body then 74 if body then
75 body = body:gsub("%%", "%%%%"); 75 body = body:gsub("%%", "%%%%");
76 return html.doc:gsub("###BODY_STUFF###", body); 76 return html.doc:gsub("###BODY_STUFF###", body);
77 end 77 end
78 end 78 end
79 79
80 function urlunescape (escapedUrl) 80 function urlunescape (escapedUrl)
81 escapedUrl = escapedUrl:gsub("+", " ") 81 escapedUrl = escapedUrl:gsub("+", " ")
82 escapedUrl = escapedUrl:gsub("%%(%x%x)", function(h) return strchar(tonumber(h,16)) end) 82 escapedUrl = escapedUrl:gsub("%%(%x%x)", function(h) return strchar(tonumber(h,16)) end)
83 escapedUrl = escapedUrl:gsub("\r\n", "\n") 83 escapedUrl = escapedUrl:gsub("\r\n", "\n")
84 return escapedUrl 84 return escapedUrl
85 end 85 end
86 86
87 local function htmlEscape(t) 87 local function htmlEscape(t)
88 if t then 88 if t then
89 t = t:gsub("<", "&lt;"); 89 t = t:gsub("<", "&lt;");
90 t = t:gsub(">", "&gt;"); 90 t = t:gsub(">", "&gt;");
91 t = t:gsub("(http://[%a%d@%.:/&%?=%-_#%%~]+)", function(h) 91 t = t:gsub("(http://[%a%d@%.:/&%?=%-_#%%~]+)", function(h)
92 h = urlunescape(h) 92 h = urlunescape(h)
93 return "<a href='" .. h .. "'>" .. h .. "</a>"; 93 return "<a href='" .. h .. "'>" .. h .. "</a>";
94 end); 94 end);
95 t = t:gsub("\n", "<br />"); 95 t = t:gsub("\n", "<br />");
96 t = t:gsub("%%", "%%%%"); 96 t = t:gsub("%%", "%%%%");
97 else 97 else
98 t = ""; 98 t = "";
99 end 99 end
100 return t; 100 return t;
101 end 101 end
102 102
103 function splitUrl(url) 103 function splitUrl(url)
104 local tmp = url:sub(string.len("/muc_log/") + 1); 104 local tmp = url:sub(string.len("/muc_log/") + 1);
105 local day = nil; 105 local day = nil;
106 local room = nil; 106 local room = nil;
107 local component = nil; 107 local component = nil;
108 local at = nil; 108 local at = nil;
109 local slash = nil; 109 local slash = nil;
110 local slash2 = nil; 110 local slash2 = nil;
111 111
112 slash = tmp:find("/"); 112 slash = tmp:find("/");
113 if slash then 113 if slash then
114 component = tmp:sub(1, slash - 1); 114 component = tmp:sub(1, slash - 1);
115 if tmp:len() > slash then 115 if tmp:len() > slash then
116 room = tmp:sub(slash + 1); 116 room = tmp:sub(slash + 1);
117 slash = room:find("/"); 117 slash = room:find("/");
118 if slash then 118 if slash then
119 tmp = room; 119 tmp = room;
120 room = tmp:sub(1, slash - 1); 120 room = tmp:sub(1, slash - 1);
121 if tmp:len() > slash then 121 if tmp:len() > slash then
122 day = tmp:sub(slash + 1); 122 day = tmp:sub(slash + 1);
123 slash = day:find("/"); 123 slash = day:find("/");
124 if slash then 124 if slash then
125 day = day:sub(1, slash - 1); 125 day = day:sub(1, slash - 1);
126 end 126 end
127 end 127 end
128 end 128 end
129 end 129 end
130 end 130 end
131 131
132 return room, component, day; 132 return room, component, day;
133 end 133 end
134 134
135 local function generateComponentListSiteContent() 135 local function generateComponentListSiteContent()
136 local components = ""; 136 local components = "";
137 for component,muc_host in pairs(muc_hosts or {}) do 137 for component,muc_host in pairs(muc_hosts or {}) do
138 components = components .. html.components.bit:gsub("###COMPONENT###", component); 138 components = components .. html.components.bit:gsub("###COMPONENT###", component);
139 end 139 end
140 if components ~= "" then 140 if components ~= "" then
141 return html.components.body:gsub("###COMPONENTS_STUFF###", components); 141 return html.components.body:gsub("###COMPONENTS_STUFF###", components);
142 end 142 end
143 end 143 end
144 144
145 local function generateRoomListSiteContent(component) 145 local function generateRoomListSiteContent(component)
146 local rooms = ""; 146 local rooms = "";
147 if prosody.hosts[component] and prosody.hosts[component].muc ~= nil then 147 if prosody.hosts[component] and prosody.hosts[component].muc ~= nil then
148 for jid, room in pairs(prosody.hosts[component].muc.rooms) do 148 for jid, room in pairs(prosody.hosts[component].muc.rooms) do
149 local node = splitJid(jid); 149 local node = splitJid(jid);
150 if not room._data.hidden and node then 150 if not room._data.hidden and node then
151 rooms = rooms .. html.rooms.bit:gsub("###ROOM###", node):gsub("###COMPONENT###", component); 151 rooms = rooms .. html.rooms.bit:gsub("###ROOM###", node):gsub("###COMPONENT###", component);
152 end 152 end
153 end 153 end
154 if rooms ~= "" then 154 if rooms ~= "" then
155 return html.rooms.body:gsub("###ROOMS_STUFF###", rooms):gsub("###COMPONENT###", component); 155 return html.rooms.body:gsub("###ROOMS_STUFF###", rooms):gsub("###COMPONENT###", component);
156 end 156 end
157 end 157 end
158 end 158 end
159 159
160 -- Calendar stuff 160 -- Calendar stuff
161 local function getDaysForMonth(month, year) 161 local function getDaysForMonth(month, year)
162 local daysCount = 30; 162 local daysCount = 30;
163 local leapyear = false; 163 local leapyear = false;
164 164
165 if year%4 == 0 and year%100 == 0 then 165 if year%4 == 0 and year%100 == 0 then
166 if year%400 == 0 then 166 if year%400 == 0 then
167 leapyear = true; 167 leapyear = true;
168 else 168 else
169 leapyear = false; -- turn of the century but not a leapyear 169 leapyear = false; -- turn of the century but not a leapyear
170 end 170 end
171 elseif year%4 == 0 then 171 elseif year%4 == 0 then
172 leapyear = true; 172 leapyear = true;
173 end 173 end
174 174
175 if month == 2 and leapyear then 175 if month == 2 and leapyear then
176 daysCount = 29; 176 daysCount = 29;
177 elseif month == 2 and not leapyear then 177 elseif month == 2 and not leapyear then
178 daysCount = 28; 178 daysCount = 28;
179 elseif month < 8 and month%2 == 1 or 179 elseif month < 8 and month%2 == 1 or
180 month >= 8 and month%2 == 0 180 month >= 8 and month%2 == 0
181 then 181 then
182 daysCount = 31; 182 daysCount = 31;
183 end 183 end
184 return daysCount; 184 return daysCount;
185 end 185 end
186 186
187 local function createMonth(month, year, dayCallback) 187 local function createMonth(month, year, dayCallback)
188 local htmlStr = html.month.header; 188 local htmlStr = html.month.header;
189 local days = getDaysForMonth(month, year); 189 local days = getDaysForMonth(month, year);
190 local time = os_time{year=year, month=month, day=1}; 190 local time = os_time{year=year, month=month, day=1};
191 local dow = tostring(os_date("%a", time)) 191 local dow = tostring(os_date("%a", time))
192 local title = tostring(os_date("%B", time)); 192 local title = tostring(os_date("%B", time));
193 local weekDays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; 193 local weekDays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
194 local weekDay = 0; 194 local weekDay = 0;
195 local weeks = 1; 195 local weeks = 1;
196 local logAvailableForMinimumOneDay = false; 196 local logAvailableForMinimumOneDay = false;
197 197
198 local weekDaysHtml = ""; 198 local weekDaysHtml = "";
199 for _, tmp in ipairs(weekDays) do 199 for _, tmp in ipairs(weekDays) do
200 weekDaysHtml = weekDaysHtml .. html.month.weekDay:gsub("###DAY###", tmp) .. "\n"; 200 weekDaysHtml = weekDaysHtml .. html.month.weekDay:gsub("###DAY###", tmp) .. "\n";
201 end 201 end
202 202
203 htmlStr = htmlStr:gsub("###TITLE###", title):gsub("###WEEKDAYS###", weekDaysHtml); 203 htmlStr = htmlStr:gsub("###TITLE###", title):gsub("###WEEKDAYS###", weekDaysHtml);
204 204
205 for i = 1, 31 do 205 for i = 1, 31 do
206 weekDay = weekDay + 1; 206 weekDay = weekDay + 1;
207 if weekDay == 1 then htmlStr = htmlStr .. "<tr>\n"; end 207 if weekDay == 1 then htmlStr = htmlStr .. "<tr>\n"; end
208 if i == 1 then 208 if i == 1 then
209 for _, tmp in ipairs(weekDays) do 209 for _, tmp in ipairs(weekDays) do
210 if dow ~= tmp then 210 if dow ~= tmp then
211 htmlStr = htmlStr .. html.month.emptyDay .. "\n"; 211 htmlStr = htmlStr .. html.month.emptyDay .. "\n";
212 weekDay = weekDay + 1; 212 weekDay = weekDay + 1;
213 else 213 else
214 break; 214 break;
215 end 215 end
216 end 216 end
217 end 217 end
218 if i < days + 1 then 218 if i < days + 1 then
219 local tmp = tostring(i); 219 local tmp = tostring(i);
220 if dayCallback ~= nil and dayCallback.callback ~= nil then 220 if dayCallback ~= nil and dayCallback.callback ~= nil then
221 tmp = dayCallback.callback(dayCallback.path, i, month, year, dayCallback.room, dayCallback.webPath); 221 tmp = dayCallback.callback(dayCallback.path, i, month, year, dayCallback.room, dayCallback.webPath);
222 end 222 end
223 if tmp == nil then 223 if tmp == nil then
224 tmp = tostring(i); 224 tmp = tostring(i);
225 else 225 else
226 logAvailableForMinimumOneDay = true; 226 logAvailableForMinimumOneDay = true;
227 end 227 end
228 htmlStr = htmlStr .. html.month.day:gsub("###DAY###", tmp) .. "\n"; 228 htmlStr = htmlStr .. html.month.day:gsub("###DAY###", tmp) .. "\n";
229 end 229 end
230 230
231 if i >= days then 231 if i >= days then
232 break; 232 break;
233 end 233 end
234 234
235 if weekDay == 7 then 235 if weekDay == 7 then
236 weekDay = 0; 236 weekDay = 0;
237 weeks = weeks + 1; 237 weeks = weeks + 1;
238 htmlStr = htmlStr .. "</tr>\n"; 238 htmlStr = htmlStr .. "</tr>\n";
239 end 239 end
240 end 240 end
241 241
242 if weekDay + 1 < 8 or weeks < 6 then 242 if weekDay + 1 < 8 or weeks < 6 then
243 weekDay = weekDay + 1; 243 weekDay = weekDay + 1;
244 if weekDay > 7 then 244 if weekDay > 7 then
245 weekDay = 1; 245 weekDay = 1;
246 end 246 end
247 if weekDay == 1 then 247 if weekDay == 1 then
248 weeks = weeks + 1; 248 weeks = weeks + 1;
249 end 249 end
250 for y = weeks, 6 do 250 for y = weeks, 6 do
251 if weekDay == 1 then 251 if weekDay == 1 then
252 htmlStr = htmlStr .. "<tr>\n"; 252 htmlStr = htmlStr .. "<tr>\n";
253 end 253 end
254 for i = weekDay, 7 do 254 for i = weekDay, 7 do
255 htmlStr = htmlStr .. html.month.emptyDay .. "\n"; 255 htmlStr = htmlStr .. html.month.emptyDay .. "\n";
256 end 256 end
257 weekDay = 1 257 weekDay = 1
258 htmlStr = htmlStr .. "</tr>\n"; 258 htmlStr = htmlStr .. "</tr>\n";
259 end 259 end
260 end 260 end
261 htmlStr = htmlStr .. html.month.footer; 261 htmlStr = htmlStr .. html.month.footer;
262 if logAvailableForMinimumOneDay then 262 if logAvailableForMinimumOneDay then
263 return htmlStr; 263 return htmlStr;
264 end 264 end
265 end 265 end
266 266
267 local function createYear(year, dayCallback) 267 local function createYear(year, dayCallback)
268 local year = year; 268 local year = year;
269 local tmp; 269 local tmp;
270 if tonumber(year) <= 99 then 270 if tonumber(year) <= 99 then
271 year = year + 2000; 271 year = year + 2000;
272 end 272 end
273 local htmlStr = ""; 273 local htmlStr = "";
274 for i=1, 12 do 274 for i=1, 12 do
275 tmp = createMonth(i, year, dayCallback); 275 tmp = createMonth(i, year, dayCallback);
276 if tmp then 276 if tmp then
277 htmlStr = htmlStr .. "<div style='float: left; padding: 5px;'>\n" .. tmp .. "</div>\n"; 277 htmlStr = htmlStr .. "<div style='float: left; padding: 5px;'>\n" .. tmp .. "</div>\n";
278 end 278 end
279 end 279 end
280 if htmlStr ~= "" then 280 if htmlStr ~= "" then
281 return "<div name='yearDiv' style='padding: 40px; text-align: center;'>" .. html.year.title:gsub("###YEAR###", tostring(year)) .. htmlStr .. "</div><br style='clear:both;'/> \n"; 281 return "<div name='yearDiv' style='padding: 40px; text-align: center;'>" .. html.year.title:gsub("###YEAR###", tostring(year)) .. htmlStr .. "</div><br style='clear:both;'/> \n";
282 end 282 end
283 return ""; 283 return "";
284 end 284 end
285 285
286 local function perDayCallback(path, day, month, year, room, webPath) 286 local function perDayCallback(path, day, month, year, room, webPath)
287 local webPath = webPath or "" 287 local webPath = webPath or ""
288 local year = year; 288 local year = year;
289 if year > 2000 then 289 if year > 2000 then
290 year = year - 2000; 290 year = year - 2000;
291 end 291 end
292 local bareDay = str_format("%.02d%.02d%.02d", year, month, day); 292 local bareDay = str_format("%.02d%.02d%.02d", year, month, day);
293 room = urlencode(room); 293 room = urlencode(room);
294 local attributes, err = lfs.attributes(path.."/"..bareDay.."/"..room..".dat") 294 local attributes, err = lfs.attributes(path.."/"..bareDay.."/"..room..".dat")
295 if attributes ~= nil and attributes.mode == "file" then 295 if attributes ~= nil and attributes.mode == "file" then
296 local s = html.days.bit; 296 local s = html.days.bit;
297 s = s:gsub("###BARE_DAY###", webPath .. bareDay); 297 s = s:gsub("###BARE_DAY###", webPath .. bareDay);
298 s = s:gsub("###DAY###", day); 298 s = s:gsub("###DAY###", day);
299 return s; 299 return s;
300 end 300 end
301 return; 301 return;
302 end 302 end
303 303
304 local function generateDayListSiteContentByRoom(bareRoomJid) 304 local function generateDayListSiteContentByRoom(bareRoomJid)
305 local days = ""; 305 local days = "";
306 local arrDays = {}; 306 local arrDays = {};
307 local tmp; 307 local tmp;
308 local node, host, resource = splitJid(bareRoomJid); 308 local node, host, resource = splitJid(bareRoomJid);
309 local path = data_getpath(node, host, datastore); 309 local path = data_getpath(node, host, datastore);
310 local room = nil; 310 local room = nil;
311 local nextRoom = ""; 311 local nextRoom = "";
312 local previousRoom = ""; 312 local previousRoom = "";
313 local rooms = ""; 313 local rooms = "";
314 local attributes = nil; 314 local attributes = nil;
315 local since = ""; 315 local since = "";
316 local to = ""; 316 local to = "";
317 local topic = ""; 317 local topic = "";
318 318
319 path = path:gsub("/[^/]*$", ""); 319 path = path:gsub("/[^/]*$", "");
320 attributes = lfs.attributes(path); 320 attributes = lfs.attributes(path);
321 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 321 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
322 local found = 0; 322 local found = 0;
323 for jid, room in pairs(prosody.hosts[host].muc.rooms) do 323 for jid, room in pairs(prosody.hosts[host].muc.rooms) do
324 local node = splitJid(jid) 324 local node = splitJid(jid)
325 if not room._data.hidden and node then 325 if not room._data.hidden and node then
326 if found == 0 then 326 if found == 0 then
327 previousRoom = node 327 previousRoom = node
328 elseif found == 1 then 328 elseif found == 1 then
329 nextRoom = node 329 nextRoom = node
330 found = -1 330 found = -1
331 end 331 end
332 if jid == bareRoomJid then 332 if jid == bareRoomJid then
333 found = 1 333 found = 1
334 end 334 end
335 335
336 rooms = rooms .. html.days.rooms.bit:gsub("###ROOM###", node); 336 rooms = rooms .. html.days.rooms.bit:gsub("###ROOM###", node);
337 end 337 end
338 end 338 end
339 339
340 room = prosody.hosts[host].muc.rooms[bareRoomJid]; 340 room = prosody.hosts[host].muc.rooms[bareRoomJid];
341 if room._data.hidden then 341 if room._data.hidden then
342 room = nil 342 room = nil
343 end 343 end
344 end 344 end
345 if attributes ~= nil and room ~= nil then 345 if attributes ~= nil and room ~= nil then
346 local first = 1; 346 local first = 1;
347 local alreadyDoneYears = {}; 347 local alreadyDoneYears = {};
348 local temptime = {day=0, month=0, year=0}; 348 local temptime = {day=0, month=0, year=0};
349 topic = room._data.subject or "(no subject)" 349 topic = room._data.subject or "(no subject)"
350 if topic:len() > 135 then 350 if topic:len() > 135 then
351 topic = topic:sub(1, topic:find(" ", 120)) .. " ..." 351 topic = topic:sub(1, topic:find(" ", 120)) .. " ..."
352 end 352 end
353 for folder in lfs.dir(path) do 353 for folder in lfs.dir(path) do
354 local year, month, day = folder:match("^(%d%d)(%d%d)(%d%d)"); 354 local year, month, day = folder:match("^(%d%d)(%d%d)(%d%d)");
355 if year ~= nil and alreadyDoneYears[year] == nil then 355 if year ~= nil and alreadyDoneYears[year] == nil then
356 temptime.day = tonumber(day) 356 temptime.day = tonumber(day)
357 temptime.month = tonumber(month) 357 temptime.month = tonumber(month)
358 temptime.year = 2000 + tonumber(year) 358 temptime.year = 2000 + tonumber(year)
359 if first == 1 then 359 if first == 1 then
360 to = tostring(os_date("%B %Y", os_time(temptime))) 360 to = tostring(os_date("%B %Y", os_time(temptime)))
361 first = 0 361 first = 0
362 end 362 end
363 363
364 since = tostring(os_date("%B %Y", os_time(temptime))) 364 since = tostring(os_date("%B %Y", os_time(temptime)))
365 module:log("debug", "creating overview for: " .. tostring(since)) 365 module:log("debug", "creating overview for: " .. tostring(since))
366 days = createYear(year, {callback=perDayCallback, path=path, room=node}) .. days; 366 days = createYear(year, {callback=perDayCallback, path=path, room=node}) .. days;
367 alreadyDoneYears[year] = true; 367 alreadyDoneYears[year] = true;
368 end 368 end
369 end 369 end
370 end 370 end
371 371
372 if days ~= "" then 372 if days ~= "" then
373 tmp = html.days.body:gsub("###DAYS_STUFF###", days); 373 tmp = html.days.body:gsub("###DAYS_STUFF###", days);
374 tmp = tmp:gsub("###PREVIOUS_ROOM###", previousRoom == "" and node or previousRoom); 374 tmp = tmp:gsub("###PREVIOUS_ROOM###", previousRoom == "" and node or previousRoom);
375 tmp = tmp:gsub("###NEXT_ROOM###", nextRoom == "" and node or nextRoom); 375 tmp = tmp:gsub("###NEXT_ROOM###", nextRoom == "" and node or nextRoom);
376 tmp = tmp:gsub("###ROOMS###", rooms); 376 tmp = tmp:gsub("###ROOMS###", rooms);
377 tmp = tmp:gsub("###ROOMTOPIC###", topic); 377 tmp = tmp:gsub("###ROOMTOPIC###", topic);
378 tmp = tmp:gsub("###SINCE###", since); 378 tmp = tmp:gsub("###SINCE###", since);
379 tmp = tmp:gsub("###TO###", to); 379 tmp = tmp:gsub("###TO###", to);
380 return tmp:gsub("###JID###", bareRoomJid); 380 return tmp:gsub("###JID###", bareRoomJid);
381 end 381 end
382 end 382 end
383 383
384 local function parseIqStanza(stanza, timeStuff, nick) 384 local function parseIqStanza(stanza, timeStuff, nick)
385 local text = nil; 385 local text = nil;
386 local victim = nil; 386 local victim = nil;
387 if(stanza.attr.type == "set") then 387 if(stanza.attr.type == "set") then
388 for _,tag in ipairs(stanza) do 388 for _,tag in ipairs(stanza) do
389 if tag.tag == "query" then 389 if tag.tag == "query" then
390 for _,item in ipairs(tag) do 390 for _,item in ipairs(tag) do
391 if item.tag == "item" and item.attr.nick ~= nil and item.attr.role == 'none' then 391 if item.tag == "item" and item.attr.nick ~= nil and item.attr.role == 'none' then
392 victim = item.attr.nick; 392 victim = item.attr.nick;
393 for _,reason in ipairs(item) do 393 for _,reason in ipairs(item) do
394 if reason.tag == "reason" then 394 if reason.tag == "reason" then
395 text = reason[1]; 395 text = reason[1];
396 break; 396 break;
397 end 397 end
398 end 398 end
399 break; 399 break;
400 end 400 end
401 end 401 end
402 break; 402 break;
403 end 403 end
404 end 404 end
405 if victim ~= nil then 405 if victim ~= nil then
406 if text ~= nil then 406 if text ~= nil then
407 text = html.day.reason:gsub("###REASON###", htmlEscape(text)); 407 text = html.day.reason:gsub("###REASON###", htmlEscape(text));
408 else 408 else
409 text = ""; 409 text = "";
410 end 410 end
411 return html.day.kick:gsub("###TIME_STUFF###", timeStuff):gsub("###VICTIM###", victim):gsub("###REASON_STUFF###", text); 411 return html.day.kick:gsub("###TIME_STUFF###", timeStuff):gsub("###VICTIM###", victim):gsub("###REASON_STUFF###", text);
412 end 412 end
413 end 413 end
414 return; 414 return;
415 end 415 end
416 416
417 local function parsePresenceStanza(stanza, timeStuff, nick) 417 local function parsePresenceStanza(stanza, timeStuff, nick)
418 local ret = ""; 418 local ret = "";
419 local showJoin = "block" 419 local showJoin = "block"
420 420
421 if config and not config.showJoin then 421 if config and not config.showJoin then
422 showJoin = "none"; 422 showJoin = "none";
423 end 423 end
424 424
425 if stanza.attr.type == nil then 425 if stanza.attr.type == nil then
426 local showStatus = "block" 426 local showStatus = "block"
427 if config and not config.showStatus then 427 if config and not config.showStatus then
428 showStatus = "none"; 428 showStatus = "none";
429 end 429 end
430 local show, status = nil, ""; 430 local show, status = nil, "";
431 local alreadyJoined = false; 431 local alreadyJoined = false;
432 for _, tag in ipairs(stanza) do 432 for _, tag in ipairs(stanza) do
433 if tag.tag == "alreadyJoined" then 433 if tag.tag == "alreadyJoined" then
434 alreadyJoined = true; 434 alreadyJoined = true;
435 elseif tag.tag == "show" then 435 elseif tag.tag == "show" then
436 show = tag[1]; 436 show = tag[1];
437 elseif tag.tag == "status" and tag[1] ~= nil then 437 elseif tag.tag == "status" and tag[1] ~= nil then
438 status = tag[1]; 438 status = tag[1];
439 end 439 end
440 end 440 end
441 if alreadyJoined == true then 441 if alreadyJoined == true then
442 if show == nil then 442 if show == nil then
443 show = "online"; 443 show = "online";
444 end 444 end
445 ret = html.day.presence.statusChange:gsub("###TIME_STUFF###", timeStuff); 445 ret = html.day.presence.statusChange:gsub("###TIME_STUFF###", timeStuff);
446 if status ~= "" then 446 if status ~= "" then
447 status = html.day.presence.statusText:gsub("###STATUS###", htmlEscape(status)); 447 status = html.day.presence.statusText:gsub("###STATUS###", htmlEscape(status));
448 end 448 end
449 ret = ret:gsub("###SHOW###", show):gsub("###NICK###", nick):gsub("###SHOWHIDE###", showStatus):gsub("###STATUS_STUFF###", status); 449 ret = ret:gsub("###SHOW###", show):gsub("###NICK###", nick):gsub("###SHOWHIDE###", showStatus):gsub("###STATUS_STUFF###", status);
450 else 450 else
451 ret = html.day.presence.join:gsub("###TIME_STUFF###", timeStuff):gsub("###SHOWHIDE###", showJoin):gsub("###NICK###", nick); 451 ret = html.day.presence.join:gsub("###TIME_STUFF###", timeStuff):gsub("###SHOWHIDE###", showJoin):gsub("###NICK###", nick);
452 end 452 end
453 elseif stanza.attr.type ~= nil and stanza.attr.type == "unavailable" then 453 elseif stanza.attr.type ~= nil and stanza.attr.type == "unavailable" then
454 454
455 ret = html.day.presence.leave:gsub("###TIME_STUFF###", timeStuff):gsub("###SHOWHIDE###", showJoin):gsub("###NICK###", nick); 455 ret = html.day.presence.leave:gsub("###TIME_STUFF###", timeStuff):gsub("###SHOWHIDE###", showJoin):gsub("###NICK###", nick);
456 end 456 end
457 return ret; 457 return ret;
458 end 458 end
459 459
460 local function parseMessageStanza(stanza, timeStuff, nick) 460 local function parseMessageStanza(stanza, timeStuff, nick)
461 local body, title, ret = nil, nil, ""; 461 local body, title, ret = nil, nil, "";
462 462
463 for _,tag in ipairs(stanza) do 463 for _,tag in ipairs(stanza) do
464 if tag.tag == "body" then 464 if tag.tag == "body" then
465 body = tag[1]; 465 body = tag[1];
466 if nick ~= nil then 466 if nick ~= nil then
467 break; 467 break;
468 end 468 end
469 elseif tag.tag == "nick" and nick == nil then 469 elseif tag.tag == "nick" and nick == nil then
470 nick = htmlEscape(tag[1]); 470 nick = htmlEscape(tag[1]);
471 if body ~= nil or title ~= nil then 471 if body ~= nil or title ~= nil then
472 break; 472 break;
473 end 473 end
474 elseif tag.tag == "subject" then 474 elseif tag.tag == "subject" then
475 title = tag[1]; 475 title = tag[1];
476 if nick ~= nil then 476 if nick ~= nil then
477 break; 477 break;
478 end 478 end
479 end 479 end
480 end 480 end
481 if nick ~= nil and body ~= nil then 481 if nick ~= nil and body ~= nil then
482 body = htmlEscape(body); 482 body = htmlEscape(body);
483 local me = body:find("^/me"); 483 local me = body:find("^/me");
484 local template = ""; 484 local template = "";
485 if not me then 485 if not me then
486 template = html.day.message; 486 template = html.day.message;
487 else 487 else
488 template = html.day.messageMe; 488 template = html.day.messageMe;
489 body = body:gsub("^/me ", ""); 489 body = body:gsub("^/me ", "");
490 end 490 end
491 ret = template:gsub("###TIME_STUFF###", timeStuff):gsub("###NICK###", nick):gsub("###MSG###", body); 491 ret = template:gsub("###TIME_STUFF###", timeStuff):gsub("###NICK###", nick):gsub("###MSG###", body);
492 elseif nick ~= nil and title ~= nil then 492 elseif nick ~= nil and title ~= nil then
493 title = htmlEscape(title); 493 title = htmlEscape(title);
494 ret = html.day.titleChange:gsub("###TIME_STUFF###", timeStuff):gsub("###NICK###", nick):gsub("###TITLE###", title); 494 ret = html.day.titleChange:gsub("###TIME_STUFF###", timeStuff):gsub("###NICK###", nick):gsub("###TITLE###", title);
495 end 495 end
496 return ret; 496 return ret;
497 end 497 end
498 498
499 local function incrementDay(bare_day) 499 local function incrementDay(bare_day)
500 local year, month, day = bare_day:match("^(%d%d)(%d%d)(%d%d)"); 500 local year, month, day = bare_day:match("^(%d%d)(%d%d)(%d%d)");
501 local leapyear = false; 501 local leapyear = false;
502 module:log("debug", tostring(day).."/"..tostring(month).."/"..tostring(year)) 502 module:log("debug", tostring(day).."/"..tostring(month).."/"..tostring(year))
503 503
504 day = tonumber(day); 504 day = tonumber(day);
505 month = tonumber(month); 505 month = tonumber(month);
506 year = tonumber(year); 506 year = tonumber(year);
507 507
508 if year%4 == 0 and year%100 == 0 then 508 if year%4 == 0 and year%100 == 0 then
509 if year%400 == 0 then 509 if year%400 == 0 then
510 leapyear = true; 510 leapyear = true;
511 else 511 else
512 leapyear = false; -- turn of the century but not a leapyear 512 leapyear = false; -- turn of the century but not a leapyear
513 end 513 end
514 elseif year%4 == 0 then 514 elseif year%4 == 0 then
515 leapyear = true; 515 leapyear = true;
516 end 516 end
517 517
518 if (month == 2 and leapyear and day + 1 > 29) or 518 if (month == 2 and leapyear and day + 1 > 29) or
519 (month == 2 and not leapyear and day + 1 > 28) or 519 (month == 2 and not leapyear and day + 1 > 28) or
520 (month < 8 and month%2 == 1 and day + 1 > 31) or 520 (month < 8 and month%2 == 1 and day + 1 > 31) or
521 (month < 8 and month%2 == 0 and day + 1 > 30) or 521 (month < 8 and month%2 == 0 and day + 1 > 30) or
522 (month >= 8 and month%2 == 0 and day + 1 > 31) or 522 (month >= 8 and month%2 == 0 and day + 1 > 31) or
523 (month >= 8 and month%2 == 1 and day + 1 > 30) 523 (month >= 8 and month%2 == 1 and day + 1 > 30)
524 then 524 then
525 if month + 1 > 12 then 525 if month + 1 > 12 then
526 year = year + 1; 526 year = year + 1;
527 month = 1; 527 month = 1;
528 day = 1; 528 day = 1;
529 else 529 else
530 month = month + 1; 530 month = month + 1;
531 day = 1; 531 day = 1;
532 end 532 end
533 else 533 else
534 day = day + 1; 534 day = day + 1;
535 end 535 end
536 return strformat("%.02d%.02d%.02d", year, month, day); 536 return strformat("%.02d%.02d%.02d", year, month, day);
537 end 537 end
538 538
539 local function findNextDay(bareRoomJid, bare_day) 539 local function findNextDay(bareRoomJid, bare_day)
540 local node, host, resource = splitJid(bareRoomJid); 540 local node, host, resource = splitJid(bareRoomJid);
541 local day = incrementDay(bare_day); 541 local day = incrementDay(bare_day);
542 local max_trys = 7; 542 local max_trys = 7;
543 543
544 module:log("debug", day); 544 module:log("debug", day);
545 while(not checkDatastorePathExists(node, host, day, false)) do 545 while(not checkDatastorePathExists(node, host, day, false)) do
546 max_trys = max_trys - 1; 546 max_trys = max_trys - 1;
547 if max_trys == 0 then 547 if max_trys == 0 then
548 break; 548 break;
549 end 549 end
550 day = incrementDay(day); 550 day = incrementDay(day);
551 end 551 end
552 if max_trys == 0 then 552 if max_trys == 0 then
553 return nil; 553 return nil;
554 else 554 else
555 return day; 555 return day;
556 end 556 end
557 end 557 end
558 558
559 local function decrementDay(bare_day) 559 local function decrementDay(bare_day)
560 local year, month, day = bare_day:match("^(%d%d)(%d%d)(%d%d)"); 560 local year, month, day = bare_day:match("^(%d%d)(%d%d)(%d%d)");
561 local leapyear = false; 561 local leapyear = false;
562 module:log("debug", tostring(day).."/"..tostring(month).."/"..tostring(year)) 562 module:log("debug", tostring(day).."/"..tostring(month).."/"..tostring(year))
563 563
564 day = tonumber(day); 564 day = tonumber(day);
565 month = tonumber(month); 565 month = tonumber(month);
566 year = tonumber(year); 566 year = tonumber(year);
567 567
568 if year%4 == 0 and year%100 == 0 then 568 if year%4 == 0 and year%100 == 0 then
569 if year%400 == 0 then 569 if year%400 == 0 then
570 leapyear = true; 570 leapyear = true;
571 else 571 else
572 leapyear = false; -- turn of the century but not a leapyear 572 leapyear = false; -- turn of the century but not a leapyear
573 end 573 end
574 elseif year%4 == 0 then 574 elseif year%4 == 0 then
575 leapyear = true; 575 leapyear = true;
576 end 576 end
577 577
578 if day - 1 == 0 then 578 if day - 1 == 0 then
579 if month - 1 == 0 then 579 if month - 1 == 0 then
580 year = year - 1; 580 year = year - 1;
581 month = 12; 581 month = 12;
582 day = 31; 582 day = 31;
583 else 583 else
584 month = month - 1; 584 month = month - 1;
585 if (month == 2 and leapyear) then day = 29 585 if (month == 2 and leapyear) then day = 29
586 elseif (month == 2 and not leapyear) then day = 28 586 elseif (month == 2 and not leapyear) then day = 28
587 elseif (month < 8 and month%2 == 1) or (month >= 8 and month%2 == 0) then day = 31 587 elseif (month < 8 and month%2 == 1) or (month >= 8 and month%2 == 0) then day = 31
588 else day = 30 588 else day = 30
589 end 589 end
590 end 590 end
591 else 591 else
592 day = day - 1; 592 day = day - 1;
593 end 593 end
594 return strformat("%.02d%.02d%.02d", year, month, day); 594 return strformat("%.02d%.02d%.02d", year, month, day);
595 end 595 end
596 596
597 local function findPreviousDay(bareRoomJid, bare_day) 597 local function findPreviousDay(bareRoomJid, bare_day)
598 local node, host, resource = splitJid(bareRoomJid); 598 local node, host, resource = splitJid(bareRoomJid);
599 local day = decrementDay(bare_day); 599 local day = decrementDay(bare_day);
600 local max_trys = 7; 600 local max_trys = 7;
601 module:log("debug", day); 601 module:log("debug", day);
602 while(not checkDatastorePathExists(node, host, day, false)) do 602 while(not checkDatastorePathExists(node, host, day, false)) do
603 max_trys = max_trys - 1; 603 max_trys = max_trys - 1;
604 if max_trys == 0 then 604 if max_trys == 0 then
605 break; 605 break;
606 end 606 end
607 day = decrementDay(day); 607 day = decrementDay(day);
608 end 608 end
609 if max_trys == 0 then 609 if max_trys == 0 then
610 return nil; 610 return nil;
611 else 611 else
612 return day; 612 return day;
613 end 613 end
614 end 614 end
615 615
616 local function parseDay(bareRoomJid, roomSubject, bare_day) 616 local function parseDay(bareRoomJid, roomSubject, bare_day)
617 local ret = ""; 617 local ret = "";
618 local year; 618 local year;
619 local month; 619 local month;
620 local day; 620 local day;
621 local tmp; 621 local tmp;
622 local node, host, resource = splitJid(bareRoomJid); 622 local node, host, resource = splitJid(bareRoomJid);
623 local year, month, day = bare_day:match("^(%d%d)(%d%d)(%d%d)"); 623 local year, month, day = bare_day:match("^(%d%d)(%d%d)(%d%d)");
624 local previousDay = findPreviousDay(bareRoomJid, bare_day); 624 local previousDay = findPreviousDay(bareRoomJid, bare_day);
625 local nextDay = findNextDay(bareRoomJid, bare_day); 625 local nextDay = findNextDay(bareRoomJid, bare_day);
626 local temptime = {day=0, month=0, year=0}; 626 local temptime = {day=0, month=0, year=0};
627 local path = data_getpath(node, host, datastore); 627 local path = data_getpath(node, host, datastore);
628 path = path:gsub("/[^/]*$", ""); 628 path = path:gsub("/[^/]*$", "");
629 local calendar = "" 629 local calendar = ""
630 630
631 if tonumber(year) <= 99 then 631 if tonumber(year) <= 99 then
632 year = year + 2000; 632 year = year + 2000;
633 end 633 end
634 634
635 temptime.day = tonumber(day) 635 temptime.day = tonumber(day)
636 temptime.month = tonumber(month) 636 temptime.month = tonumber(month)
637 temptime.year = tonumber(year) 637 temptime.year = tonumber(year)
638 calendar = createMonth(temptime.month, temptime.year, {callback=perDayCallback, path=path, room=node, webPath="../"}) or "" 638 calendar = createMonth(temptime.month, temptime.year, {callback=perDayCallback, path=path, room=node, webPath="../"}) or ""
639 639
640 if bare_day ~= nil then 640 if bare_day ~= nil then
641 local data = data_load(node, host, datastore .. "/" .. bare_day); 641 local data = data_load(node, host, datastore .. "/" .. bare_day);
642 if data ~= nil then 642 if data ~= nil then
643 for i=1, #data, 1 do 643 for i=1, #data, 1 do
644 local stanza = lom.parse(data[i]); 644 local stanza = lom.parse(data[i]);
645 if stanza ~= nil and stanza.attr ~= nil and stanza.attr.time ~= nil then 645 if stanza ~= nil and stanza.attr ~= nil and stanza.attr.time ~= nil then
646 local timeStuff = html.day.time:gsub("###TIME###", stanza.attr.time):gsub("###UTC###", stanza.attr.utc or stanza.attr.time); 646 local timeStuff = html.day.time:gsub("###TIME###", stanza.attr.time):gsub("###UTC###", stanza.attr.utc or stanza.attr.time);
647 if stanza[1] ~= nil then 647 if stanza[1] ~= nil then
648 local nick; 648 local nick;
649 local tmp; 649 local tmp;
650 650
651 -- grep nick from "from" resource 651 -- grep nick from "from" resource
652 if stanza[1].attr.from ~= nil then -- presence and messages 652 if stanza[1].attr.from ~= nil then -- presence and messages
653 nick = htmlEscape(stanza[1].attr.from:match("/(.+)$")); 653 nick = htmlEscape(stanza[1].attr.from:match("/(.+)$"));
654 elseif stanza[1].attr.to ~= nil then -- iq 654 elseif stanza[1].attr.to ~= nil then -- iq
655 nick = htmlEscape(stanza[1].attr.to:match("/(.+)$")); 655 nick = htmlEscape(stanza[1].attr.to:match("/(.+)$"));
656 end 656 end
657 657
658 if stanza[1].tag == "presence" and nick ~= nil then 658 if stanza[1].tag == "presence" and nick ~= nil then
659 tmp = parsePresenceStanza(stanza[1], timeStuff, nick); 659 tmp = parsePresenceStanza(stanza[1], timeStuff, nick);
660 elseif stanza[1].tag == "message" then 660 elseif stanza[1].tag == "message" then
661 tmp = parseMessageStanza(stanza[1], timeStuff, nick); 661 tmp = parseMessageStanza(stanza[1], timeStuff, nick);
662 elseif stanza[1].tag == "iq" then 662 elseif stanza[1].tag == "iq" then
663 tmp = parseIqStanza(stanza[1], timeStuff, nick); 663 tmp = parseIqStanza(stanza[1], timeStuff, nick);
664 else 664 else
665 module:log("info", "unknown stanza subtag in log found. room: %s; day: %s", bareRoomJid, year .. "/" .. month .. "/" .. day); 665 module:log("info", "unknown stanza subtag in log found. room: %s; day: %s", bareRoomJid, year .. "/" .. month .. "/" .. day);
666 end 666 end
667 if tmp ~= nil then 667 if tmp ~= nil then
668 ret = ret .. tmp 668 ret = ret .. tmp
669 tmp = nil; 669 tmp = nil;
670 end 670 end
671 end 671 end
672 end 672 end
673 end 673 end
674 end 674 end
675 if ret ~= "" then 675 if ret ~= "" then
676 if nextDay then 676 if nextDay then
677 nextDay = html.day.dayLink:gsub("###DAY###", nextDay):gsub("###TEXT###", "&gt;") 677 nextDay = html.day.dayLink:gsub("###DAY###", nextDay):gsub("###TEXT###", "&gt;")
678 end 678 end
679 if previousDay then 679 if previousDay then
680 previousDay = html.day.dayLink:gsub("###DAY###", previousDay):gsub("###TEXT###", "&lt;"); 680 previousDay = html.day.dayLink:gsub("###DAY###", previousDay):gsub("###TEXT###", "&lt;");
681 end 681 end
682 ret = ret:gsub("%%", "%%%%"); 682 ret = ret:gsub("%%", "%%%%");
683 tmp = html.day.body:gsub("###DAY_STUFF###", ret):gsub("###JID###", bareRoomJid); 683 tmp = html.day.body:gsub("###DAY_STUFF###", ret):gsub("###JID###", bareRoomJid);
684 tmp = tmp:gsub("###CALENDAR###", calendar); 684 tmp = tmp:gsub("###CALENDAR###", calendar);
685 tmp = tmp:gsub("###DATE###", tostring(os_date("%A, %B %d, %Y", os_time(temptime)))); 685 tmp = tmp:gsub("###DATE###", tostring(os_date("%A, %B %d, %Y", os_time(temptime))));
686 tmp = tmp:gsub("###TITLE_STUFF###", html.day.title:gsub("###TITLE###", roomSubject)); 686 tmp = tmp:gsub("###TITLE_STUFF###", html.day.title:gsub("###TITLE###", roomSubject));
687 tmp = tmp:gsub("###STATUS_CHECKED###", config.showStatus and "checked='checked'" or ""); 687 tmp = tmp:gsub("###STATUS_CHECKED###", config.showStatus and "checked='checked'" or "");
688 tmp = tmp:gsub("###JOIN_CHECKED###", config.showJoin and "checked='checked'" or ""); 688 tmp = tmp:gsub("###JOIN_CHECKED###", config.showJoin and "checked='checked'" or "");
689 tmp = tmp:gsub("###NEXT_LINK###", nextDay or ""); 689 tmp = tmp:gsub("###NEXT_LINK###", nextDay or "");
690 tmp = tmp:gsub("###PREVIOUS_LINK###", previousDay or ""); 690 tmp = tmp:gsub("###PREVIOUS_LINK###", previousDay or "");
691 691
692 return tmp; 692 return tmp;
693 end 693 end
694 end 694 end
695 end 695 end
696 696
697 function handle_request(method, body, request) 697 function handle_request(method, body, request)
698 local node, host, day = splitUrl(request.url.path); 698 local node, host, day = splitUrl(request.url.path);
699 699
700 node = urldecode(node); 700 node = urldecode(node);
701 701
702 if muc_hosts ~= nil and html.doc ~= nil then 702 if muc_hosts ~= nil and html.doc ~= nil then
703 if node ~= nil and host ~= nil then 703 if node ~= nil and host ~= nil then
704 local bare = node .. "@" .. host; 704 local bare = node .. "@" .. host;
705 if prosody.hosts[host] ~= nil and prosody.hosts[host].muc ~= nil then 705 if prosody.hosts[host] ~= nil and prosody.hosts[host].muc ~= nil then
706 if prosody.hosts[host].muc.rooms[bare] ~= nil then 706 if prosody.hosts[host].muc.rooms[bare] ~= nil then
707 local room = prosody.hosts[host].muc.rooms[bare]; 707 local room = prosody.hosts[host].muc.rooms[bare];
708 if day == nil then 708 if day == nil then
709 return createDoc(generateDayListSiteContentByRoom(bare)); 709 return createDoc(generateDayListSiteContentByRoom(bare));
710 else 710 else
711 local subject = "" 711 local subject = ""
712 if room._data ~= nil and room._data.subject ~= nil then 712 if room._data ~= nil and room._data.subject ~= nil then
713 subject = room._data.subject; 713 subject = room._data.subject;
714 end 714 end
715 return createDoc(parseDay(bare, subject, day)); 715 return createDoc(parseDay(bare, subject, day));
716 end 716 end
717 else 717 else
718 return createDoc(generateRoomListSiteContent(host)); 718 return createDoc(generateRoomListSiteContent(host));
719 end 719 end
720 else 720 else
721 return createDoc(generateComponentListSiteContent()); 721 return createDoc(generateComponentListSiteContent());
722 end 722 end
723 elseif host ~= nil then 723 elseif host ~= nil then
724 return createDoc(generateRoomListSiteContent(host)); 724 return createDoc(generateRoomListSiteContent(host));
725 else 725 else
726 return createDoc(generateComponentListSiteContent()); 726 return createDoc(generateComponentListSiteContent());
727 end 727 end
728 end 728 end
729 return; 729 return;
730 end 730 end
731 731
732 -- Compatibility: Lua-5.1 732 -- Compatibility: Lua-5.1
733 function split(str, pat) 733 function split(str, pat)
734 local t = {} -- NOTE: use {n = 0} in Lua-5.0 734 local t = {} -- NOTE: use {n = 0} in Lua-5.0
735 local fpat = "(.-)" .. pat 735 local fpat = "(.-)" .. pat
736 local last_end = 1 736 local last_end = 1
737 local s, e, cap = str:find(fpat, 1) 737 local s, e, cap = str:find(fpat, 1)
738 while s do 738 while s do
739 if s ~= 1 or cap ~= "" then 739 if s ~= 1 or cap ~= "" then
740 table.insert(t,cap) 740 table.insert(t,cap)
741 end 741 end
742 last_end = e+1 742 last_end = e+1
743 s, e, cap = str:find(fpat, last_end) 743 s, e, cap = str:find(fpat, last_end)
744 end 744 end
745 if last_end <= #str then 745 if last_end <= #str then
746 cap = str:sub(last_end) 746 cap = str:sub(last_end)
747 table.insert(t, cap) 747 table.insert(t, cap)
748 end 748 end
749 return t 749 return t
750 end 750 end
751 751
752 local function assign(arr, content) 752 local function assign(arr, content)
753 local tmp = html; 753 local tmp = html;
754 local idx = nil; 754 local idx = nil;
755 for _,i in ipairs(arr) do 755 for _,i in ipairs(arr) do
756 if idx ~= nil then 756 if idx ~= nil then
757 if tmp[idx] == nil then 757 if tmp[idx] == nil then
758 tmp[idx] = {}; 758 tmp[idx] = {};
759 end 759 end
760 tmp = tmp[idx]; 760 tmp = tmp[idx];
761 end 761 end
762 idx = i; 762 idx = i;
763 end 763 end
764 tmp[idx] = content; 764 tmp[idx] = content;
765 end 765 end
766 766
767 local function readFile(filepath) 767 local function readFile(filepath)
768 local f = assert(io_open(filepath, "r")); 768 local f = assert(io_open(filepath, "r"));
769 local t = f:read("*all"); 769 local t = f:read("*all");
770 f:close() 770 f:close()
771 return t; 771 return t;
772 end 772 end
773 773
774 local function loadTheme(path) 774 local function loadTheme(path)
775 for file in lfs.dir(path) do 775 for file in lfs.dir(path) do
776 if file ~= "." and file ~= ".." then 776 if file ~= "." and file ~= ".." then
777 module:log("debug", "opening theme file: " .. file); 777 module:log("debug", "opening theme file: " .. file);
778 local tmp = split(file:gsub("\.html$", ""), "_"); 778 local tmp = split(file:gsub("\.html$", ""), "_");
779 local content = readFile(path .. "/" .. file); 779 local content = readFile(path .. "/" .. file);
780 assign(tmp, content); 780 assign(tmp, content);
781 end 781 end
782 end 782 end
783 return true; 783 return true;
784 end 784 end
785 785
786 function module.load() 786 function module.load()
787 config = config_get("*", "core", "muc_log_http") or {}; 787 config = config_get("*", "core", "muc_log_http") or {};
788 if config.showStatus == nil then 788 if config.showStatus == nil then
789 config.showStatus = true; 789 config.showStatus = true;
790 end 790 end
791 if config.showJoin == nil then 791 if config.showJoin == nil then
792 config.showJoin = true; 792 config.showJoin = true;
793 end 793 end
794 794
795 theme = config.theme or "prosody"; 795 theme = config.theme or "prosody";
796 local themePath = themesParent .. "/" .. tostring(theme); 796 local themePath = themesParent .. "/" .. tostring(theme);
797 local attributes, err = lfs.attributes(themePath); 797 local attributes, err = lfs.attributes(themePath);
798 if attributes == nil or attributes.mode ~= "directory" then 798 if attributes == nil or attributes.mode ~= "directory" then
799 module:log("error", "Theme folder of theme \"".. tostring(theme) .. "\" isn't existing. expected Path: " .. themePath); 799 module:log("error", "Theme folder of theme \"".. tostring(theme) .. "\" isn't existing. expected Path: " .. themePath);
800 return false; 800 return false;
801 end 801 end
802 802
803 -- module:log("debug", (require "util.serialization").serialize(html)); 803 -- module:log("debug", (require "util.serialization").serialize(html));
804 if(not loadTheme(themePath)) then 804 if(not loadTheme(themePath)) then
805 module:log("error", "Theme \"".. tostring(theme) .. "\" is missing something."); 805 module:log("error", "Theme \"".. tostring(theme) .. "\" is missing something.");
806 return false; 806 return false;
807 end 807 end
808 -- module:log("debug", (require "util.serialization").serialize(html)); 808 -- module:log("debug", (require "util.serialization").serialize(html));
809 809
810 httpserver.new_from_config({ config.http_port or true }, handle_request, { base = urlBase, ssl = false, port = 5290 }); 810 httpserver.new_from_config({ config.http_port or true }, handle_request, { base = urlBase, ssl = false, port = 5290 });
811 811
812 for jid, host in pairs(prosody.hosts) do 812 for jid, host in pairs(prosody.hosts) do
813 if host.muc then 813 if host.muc then
814 local enabledModules = config_get(jid, "core", "modules_enabled"); 814 local enabledModules = config_get(jid, "core", "modules_enabled");
815 if enabledModules then 815 if enabledModules then
816 for _,mod in ipairs(enabledModules) do 816 for _,mod in ipairs(enabledModules) do
817 if(mod == "muc_log") then 817 if(mod == "muc_log") then
818 module:log("debug", "component: %s", tostring(jid)); 818 module:log("debug", "component: %s", tostring(jid));
819 muc_hosts[jid] = true; 819 muc_hosts[jid] = true;
820 break; 820 break;
821 end 821 end
822 end 822 end
823 end 823 end
824 end 824 end
825 end 825 end
826 module:log("debug", "loaded mod_muc_log_http"); 826 module:log("debug", "loaded mod_muc_log_http");
827 end 827 end
828 828
829 function module.unload() 829 function module.unload()
830 muc_hosts = nil; 830 muc_hosts = nil;
831 module:log("debug", "unloaded mod_muc_log_http"); 831 module:log("debug", "unloaded mod_muc_log_http");
832 end 832 end
833 833
834 module:hook("component-activated", function(component, config) 834 module:hook("component-activated", function(component, config)
835 if config.core and config.core.modules_enabled then 835 if config.core and config.core.modules_enabled then
836 for _,mod in ipairs(config.core.modules_enabled) do 836 for _,mod in ipairs(config.core.modules_enabled) do
837 if(mod == "muc_log") then 837 if(mod == "muc_log") then
838 module:log("debug", "component: %s", tostring(component)); 838 module:log("debug", "component: %s", tostring(component));
839 muc_hosts[component] = true; 839 muc_hosts[component] = true;
840 break; 840 break;
841 end 841 end
842 end 842 end
843 end 843 end
844 end); 844 end);