mod_http_muc_log/mod_http_muc_log.lua
author Kim Alvefur <zash@zash.se>
Tue, 07 Apr 2015 18:12:40 +0200
changeset 1658 1a6d6221c5f6
parent 1629 c427de617ada
child 1675 c813b69ae279
permissions -rw-r--r--
mod_http_muc_log: Cleanup [luacheck]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
local st = require "util.stanza";
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
     2
local mt = require"util.multitable";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
local datetime = require"util.datetime";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
local jid_split = require"util.jid".split;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
local nodeprep = require"util.encodings".stringprep.nodeprep;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
local uuid = require"util.uuid".generate;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
local it = require"util.iterators";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
local gettime = require"socket".gettime;
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
     9
local url = require"socket.url";
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    10
local xml_escape = st.xml_escape;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    11
local t_concat = table.concat;
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
    12
local os_time, os_date = os.time, os.date;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
1571
eed7db9f3157 mod_mam_muc, mod_http_muc_log: Change store name from 'archive2' to 'muc_log' to distinguish it from personal MAM archives. Old data will require migration.
Kim Alvefur <zash@zash.se>
parents: 1564
diff changeset
    14
local archive = module:open_store("muc_log", "archive");
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
-- Support both old and new MUC code
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
local mod_muc = module:depends"muc";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
local rooms = rawget(mod_muc, "rooms");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
local each_room = rawget(mod_muc, "each_room") or function() return it.values(rooms); end;
1554
8059b7cdaf17 mod_http_muc_log: Make MUC local code identical to mod_mam_muc
Kim Alvefur <zash@zash.se>
parents: 1553
diff changeset
    20
local new_muc = not rooms;
8059b7cdaf17 mod_http_muc_log: Make MUC local code identical to mod_mam_muc
Kim Alvefur <zash@zash.se>
parents: 1553
diff changeset
    21
if new_muc then
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
	rooms = module:shared"muc/rooms";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
	function (jid)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
		return rooms[jid];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
	end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
local function get_room(name)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
	local jid = name .. '@' .. module.host;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
	return get_room_from_jid(jid);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
module:depends"http";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    36
local function render(template, values)
1629
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    37
	-- This function takes a string template and a table of values.
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    38
	-- Sequences like {name} in the template string are substituted
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    39
	-- with values from the table, optionally depending on a modifier
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    40
	-- symbol.
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    41
	--
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    42
	-- Variants are:
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    43
	-- {name} is substituted for values["name"] and is XML escaped
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    44
	-- {name? sub-template } renders a sub-template if values["name"] is false-ish
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    45
	-- {name& sub-template } renders a sub-template if values["name"] is true-ish
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    46
	-- {name# sub-template } renders a sub-template using an array of values
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    47
	-- {name!} is substituted *without* XML escaping
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    48
	return (template:gsub("%b{}", function (block)
1629
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    49
		local name, opt, e = block:sub(2, -2):match("^([%a_][%w_]*)(%p?)()");
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    50
		local value = values[name];
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    51
		if opt == '#' then
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    52
			if not value or not value[1] then return ""; end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    53
			local out, subtpl = {}, block:sub(e+1, -2);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    54
			for i=1, #value do
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    55
				out[i] = render(subtpl, value[i]);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    56
			end
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    57
			return t_concat(out);
1629
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    58
		elseif opt == '&' then
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    59
			if not value then return ""; end
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    60
			return render(block:sub(e+1, -2), values);
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    61
		elseif opt == '?' and not value then
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    62
			return render(block:sub(e+1, -2), values);
c427de617ada mod_http_muc_log: Alter template language to more simply allow optional bits in templates
Kim Alvefur <zash@zash.se>
parents: 1610
diff changeset
    63
		elseif value ~= nil then
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    64
			if type(value) ~= "string" then
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    65
				value = tostring(value);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    66
			end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    67
			if opt ~= '!' then
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    68
				return xml_escape(value);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    69
			end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    70
			return value;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    71
		end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    72
	end));
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    73
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    74
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
    75
local template;
1582
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    76
do
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    77
	local template_file = module:get_option_string(module.name .. "_template", module.name .. ".html");
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    78
	template_file = assert(module:load_resource(template_file));
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    79
	template = template_file:read("*a");
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    80
	template_file:close();
8e282eb0c70c mod_http_muc_log: Split out template into a configurable file.
Kim Alvefur <zash@zash.se>
parents: 1581
diff changeset
    81
end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    82
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
    83
-- local base_url = module:http_url() .. '/'; -- TODO: Generate links in a smart way
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    84
local get_link do
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    85
	local link, path = { path = '/' }, { "", "", is_directory = true };
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    86
	function get_link(room, date)
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    87
		path[1], path[2] = room, date;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    88
		path.is_directory = not date;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    89
		link.path = url.build_path(path);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    90
		return url.build(link);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    91
	end
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
    92
end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    93
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    94
local function public_room(room)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    95
	if type(room) == "string" then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    96
		room = get_room(room);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    97
	end
1575
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
    98
	return (room
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
    99
		and not (room.get_hidden or room.is_hidden)(room)
464ed6bc5a73 mod_http_muc_log: Fix use with old (until 0.10) MUC API
Kim Alvefur <zash@zash.se>
parents: 1571
diff changeset
   100
		and not (room.get_members_only or room.is_members_only)(room)
1580
63571115302f mod_http_muc_log: List rooms with logging explicitly enabled
Kim Alvefur <zash@zash.se>
parents: 1579
diff changeset
   101
		and room._data.logging == true);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   102
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   103
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   104
local function sort_Y(a,b) return a.year > b.year end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   105
local function sort_m(a,b) return a.n > b.n end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   106
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   107
local t_diff = os_time(os_date("*t")) - os_time(os_date("!*t"));
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   108
local function time(t)
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   109
	return os_time(t) + t_diff;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   110
end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   111
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   112
local function find_once(room, query, retval)
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   113
	if query then query.limit = 1; else query = { limit = 1 }; end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   114
	local iter, err = archive:find(room, query);
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   115
	if not iter then return iter, err; end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   116
	if retval then
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   117
		return select(retval, iter());
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   118
	end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   119
	return iter();
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   120
end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   121
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   122
local function years_page(event, path)
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   123
	local response = event.response;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   124
1552
81b3599c02fb mod_http_muc_log: Have URL of date list page end with a slash
Kim Alvefur <zash@zash.se>
parents: 1550
diff changeset
   125
	local room = nodeprep(path:match("^(.*)/$"));
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   126
	if not room or not public_room(room) then return end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   127
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   128
	local dates = mt.new();
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   129
	module:log("debug", "Find all dates with messages");
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   130
	local next_day;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   131
	repeat
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   132
		local when = find_once(room, { start = next_day; with = "message<groupchat"; }, 3);
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   133
		if not when then break; end
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   134
		local t = os_date("!*t", when);
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   135
		dates:set(t.year, t.month, t.day, when );
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   136
		next_day = when + (86400 - (when % 86400));
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   137
	until not next_day;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   138
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   139
	local years = {};
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   140
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   141
	for current_year, months_t in pairs(dates.data) do
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   142
		local t = { year = current_year, month = 1, day = 1 };
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   143
		local months = { };
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   144
		local year = { year = current_year, months = months };
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   145
		years[#years+1] = year;
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   146
		for current_month, days_t in pairs(months_t) do
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   147
			t.day = 1;
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   148
			t.month = current_month;
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   149
			local tmp = os_date("!*t", time(t));
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   150
			local days = {};
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   151
			local week = { days = days }
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   152
			local weeks = { week };
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   153
			local month = { year = year.year, month = os_date("!%B", time(t)), n = current_month, weeks = weeks };
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   154
			months[#months+1] = month;
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   155
			local current_day = 1;
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   156
			for _=1, (tmp.wday+5)%7 do
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   157
				days[current_day], current_day = {}, current_day+1;
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   158
			end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   159
			for i = 1, 31 do
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   160
				t.day = i;
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   161
				tmp = os_date("!*t", time(t));
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   162
				if tmp.month ~= current_month then break end
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   163
				if i > 1 and tmp.wday == 2 then
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   164
					days = {};
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   165
					weeks[#weeks+1] = { days = days };
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   166
					current_day = 1;
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   167
				end
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   168
				days[current_day], current_day = { wday = tmp.wday, day = i, href = days_t[i] and datetime.date(days_t[i]) }, current_day+1;
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   169
			end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   170
		end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   171
		table.sort(year, sort_m);
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   172
	end
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   173
	table.sort(years, sort_Y);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   174
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
   175
	response.headers.content_type = "text/html; charset=utf-8";
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   176
	return render(template, {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   177
		title = get_room(room):get_name();
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   178
		jid = get_room(room).jid;
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   179
		years = years;
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   180
		links = {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   181
			{ href = "../", rel = "up", text = "Back to room list" },
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   182
		};
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   183
	});
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   184
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   185
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   186
local function logs_page(event, path)
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   187
	local response = event.response;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   188
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   189
	local room, date = path:match("^(.-)/(%d%d%d%d%-%d%d%-%d%d)$");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   190
	room = nodeprep(room);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   191
	if not room then
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   192
		return years_page(event, path);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   193
	end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   194
	if not public_room(room) then return end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   195
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   196
	local logs, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   197
	local iter, err = archive:find(room, {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   198
		["start"] = datetime.parse(date.."T00:00:00Z");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   199
		["end"]   = datetime.parse(date.."T23:59:59Z");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   200
		-- with = "message<groupchat";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   201
	});
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   202
	if not iter then
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   203
		module:log("warn", "Could not search archive: %s", err or "no error");
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   204
		return 500;
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   205
	end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   206
1577
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
   207
	local first, last;
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   208
	local verb, subject, body;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   209
	for key, item, when in iter do
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   210
		body = item:get_child_text("body");
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   211
		subject = item:get_child_text("subject");
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   212
		verb = nil;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   213
		if subject then
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   214
			verb, body = "set the topic to", subject;
1555
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   215
		elseif body and body:sub(1,4) == "/me " then
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   216
			verb, body = body:sub(5), nil;
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   217
		elseif item.name == "presence" then
2e51f70cd7ea mod_http_muc_log: Make pretty
Kim Alvefur <zash@zash.se>
parents: 1554
diff changeset
   218
			verb = item.attr.type == "unavailable" and "has left" or "has joined";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   219
		end
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   220
		if body or verb then
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   221
			logs[i], i = {
1578
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   222
				key = key;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   223
				datetime = datetime.datetime(when);
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   224
				time = datetime.time(when);
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   225
				verb = verb;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   226
				body = body;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   227
				nick = select(3, jid_split(item.attr.from));
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   228
				st_name = item.name;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   229
				st_type = item.attr.type;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   230
			}, i + 1;
a68ec7a2dc02 mod_http_muc_log: Show only messages with presentable content
Kim Alvefur <zash@zash.se>
parents: 1577
diff changeset
   231
		end
1577
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
   232
		first = first or key;
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
   233
		last = key;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   234
	end
1577
0a6974f2cb55 mod_http_muc_log: Use archive IDs to find next and previous dates that contain messages
Kim Alvefur <zash@zash.se>
parents: 1576
diff changeset
   235
	if i == 1 then return end -- No items
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   236
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   237
	module:log("debug", "Find next date with messages");
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   238
	local next_when = find_once(room, { after = last }, 3);
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   239
	if next_when then
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   240
		next_when = datetime.date(next_when);
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   241
		module:log("debug", "Next message: %s", datetime.datetime(next_when));
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   242
	else
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   243
		next_when = "";
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
   244
	end
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   245
1550
1b2823b41f7f mod_http_muc_log: Strip some whitespace
Kim Alvefur <zash@zash.se>
parents: 1549
diff changeset
   246
	module:log("debug", "Find prev date with messages");
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   247
	local prev_when = find_once(room, { before = first, reverse = true }, 3);
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   248
	if prev_when then
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   249
		prev_when = datetime.date(prev_when);
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   250
		module:log("debug", "Previous message: %s", datetime.datetime(prev_when));
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   251
	else
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   252
		prev_when = "";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   253
	end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   254
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
   255
	response.headers.content_type = "text/html; charset=utf-8";
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   256
	return render(template, {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   257
		title = ("%s - %s"):format(get_room(room):get_name(), date);
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   258
		jid = get_room(room).jid;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   259
		lines = logs;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   260
		links = {
1610
2c8b985ebde5 mod_http_muc_log: Switch to a calendar view for selecting dates
Kim Alvefur <zash@zash.se>
parents: 1609
diff changeset
   261
			{ href = "./", rel = "up", text = "Back to calendar" },
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   262
			{ href = prev_when, rel = "prev", text = prev_when},
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   263
			{ href = next_when, rel = "next", text = next_when},
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   264
		};
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   265
	});
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   266
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   267
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   268
local function list_rooms(event)
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   269
	local response = event.response;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   270
	local room_list, i = {}, 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   271
	for room in each_room() do
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   272
		if public_room(room) then
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   273
			room_list[i], i = {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   274
				href = get_link(jid_split(room.jid), nil);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   275
				name = room:get_name();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   276
				description = room:get_description();
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   277
			}, i + 1;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   278
		end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   279
	end
1576
91b91052e0e8 mod_http_muc_log: Send a HTML mime type with responses
Kim Alvefur <zash@zash.se>
parents: 1575
diff changeset
   280
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   281
	response.headers.content_type = "text/html; charset=utf-8";
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   282
	return render(template, {
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   283
		title = module:get_option_string("name", "Prosody Chatrooms");
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   284
		jid = module.host;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   285
		rooms = room_list;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   286
	});
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   287
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   288
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   289
local cache = setmetatable({}, {__mode = 'v'});
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   290
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   291
local function with_cache(f)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   292
	return function (event, path)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   293
		local request, response = event.request, event.response;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   294
		local ckey = path or "";
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   295
		local cached = cache[ckey];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   296
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   297
		if cached then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   298
			local etag = cached.etag;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   299
			local if_none_match = request.headers.if_none_match;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   300
			if etag == if_none_match then
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   301
				module:log("debug", "Client cache hit");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   302
				return 304;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   303
			end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   304
			module:log("debug", "Server cache hit");
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   305
			response.headers.etag = etag;
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
   306
			response.headers.content_type = "text/html; charset=utf-8";
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   307
			return cached[1];
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   308
		end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   309
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   310
		local start = gettime();
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   311
		local rendered = f(event, path);
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   312
		module:log("debug", "Rendering took %dms", math.floor( (gettime() - start) * 1000 + 0.5));
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   313
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   314
		if type(rendered) == "string" then
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   315
			local etag = uuid();
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   316
			cached = { rendered, etag = etag, date = datetime.date() };
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   317
			response.headers.etag = etag;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   318
			cache[ckey] = cached;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   319
		end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   320
1579
9e784ddac236 mod_http_muc_log: Include charset in Content-Type header
Kim Alvefur <zash@zash.se>
parents: 1578
diff changeset
   321
		response.headers.content_type = "text/html; charset=utf-8";
1658
1a6d6221c5f6 mod_http_muc_log: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1629
diff changeset
   322
		return rendered;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   323
	end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   324
end
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   325
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   326
-- How is cache invalidation a hard problem? ;)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   327
module:hook("muc-broadcast-message", function (event)
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   328
	local room = event.room;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   329
	local room_name = jid_split(room.jid);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   330
	local today = datetime.date();
1581
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   331
	cache[get_link(room_name)] = nil;
9f6cd252d233 mod_http_muc_log: Revamp template system
Kim Alvefur <zash@zash.se>
parents: 1580
diff changeset
   332
	cache[get_link(room_name, today)] = nil;
1549
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   333
end);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   334
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   335
module:provides("http", {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   336
	route = {
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   337
		["GET /"] = list_rooms;
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   338
		["GET /*"] = with_cache(logs_page);
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   339
	};
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   340
});
f9f8bf82ece7 mod_http_muc_log: MUC log module using new archive API
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   341