mod_mam_muc/mod_mam_muc.lua
author Kim Alvefur <zash@zash.se>
Sat, 18 Jan 2014 21:04:33 +0100
changeset 1275 50c427295767
parent 1146 9fa89dc7a86f
child 1276 01dfaf2f2782
permissions -rw-r--r--
mod_mam_muc: Make the logging option in the room config reflect logging and inherit the default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
-- XEP-0313: Message Archive Management for Prosody
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
     2
-- Copyright (C) 2011-2013 Kim Alvefur
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
--
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
-- This file is MIT/X11 licensed.
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
local xmlns_mam     = "urn:xmpp:mam:tmp";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
local xmlns_delay   = "urn:xmpp:delay";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
local xmlns_forward = "urn:xmpp:forward:0";
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
     9
local muc_form_config_option = "muc#roomconfig_enablelogging"
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
local st = require "util.stanza";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
local rsm = module:require "mod_mam/rsm";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
local jid_bare = require "util.jid".bare;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
local jid_split = require "util.jid".split;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
    16
local getmetatable = getmetatable;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
    17
local function is_stanza(x)
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
    18
	return getmetatable(x) == st.stanza_mt;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
    19
end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
local tostring = tostring;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
local time_now = os.time;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
local m_min = math.min;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    27
local log_all_rooms = module:get_option_boolean("muc_log_all_rooms", false);
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    28
local log_by_default = module:get_option_boolean("muc_log_by_default", true);
1142
fabdaa0d99e3 mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents: 1141
diff changeset
    29
local advertise_archive = module:get_option_boolean("muc_log_advertise", true);
fabdaa0d99e3 mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents: 1141
diff changeset
    30
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
    31
local archive = module:open_store("archive2", "archive");
1139
b32d65e41755 mod_mam_muc: Get room objects in a less awkward fashion
Kim Alvefur <zash@zash.se>
parents: 1138
diff changeset
    32
local rooms = hosts[module.host].modules.muc.rooms;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    34
module:hook("muc-config-form", function(event)
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    35
	local room, form = event.room, event.form;
1275
50c427295767 mod_mam_muc: Make the logging option in the room config reflect logging and inherit the default
Kim Alvefur <zash@zash.se>
parents: 1146
diff changeset
    36
	local logging_enabled = room._data.logging;
50c427295767 mod_mam_muc: Make the logging option in the room config reflect logging and inherit the default
Kim Alvefur <zash@zash.se>
parents: 1146
diff changeset
    37
	if logging_enabled == nil then
50c427295767 mod_mam_muc: Make the logging option in the room config reflect logging and inherit the default
Kim Alvefur <zash@zash.se>
parents: 1146
diff changeset
    38
		logging_enabled = log_by_default;
50c427295767 mod_mam_muc: Make the logging option in the room config reflect logging and inherit the default
Kim Alvefur <zash@zash.se>
parents: 1146
diff changeset
    39
	end
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    40
	table.insert(form,
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    41
	{
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    42
		name = muc_form_config_option,
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    43
		type = "boolean",
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    44
		label = "Enable Logging?",
1275
50c427295767 mod_mam_muc: Make the logging option in the room config reflect logging and inherit the default
Kim Alvefur <zash@zash.se>
parents: 1146
diff changeset
    45
		value = logging_enabled,
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    46
	}
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    47
	);
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    48
end);
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    49
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    50
module:hook("muc-config-submitted", function(event)
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    51
	local room, fields, changed = event.room, event.fields, event.changed;
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    52
	local new = fields[muc_form_config_option];
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    53
	if new ~= room._data.logging then
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    54
		room._data.logging = new;
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    55
		if type(changed) == "table" then
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    56
			changed[muc_form_config_option] = true;
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    57
		else
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    58
			event.changed = true;
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    59
		end
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    60
	end
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    61
end);
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    62
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
    63
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    64
-- Handle archive queries
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    65
module:hook("iq-get/bare/"..xmlns_mam..":query", function(event)
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    66
	local origin, stanza = event.origin, event.stanza;
1144
ccb0c5afe658 mod_mam_muc: Fix room lookup, should be indexed by bare jid
Kim Alvefur <zash@zash.se>
parents: 1143
diff changeset
    67
	local room = stanza.attr.to;
1146
9fa89dc7a86f mod_mam_muc: Search the rooms archive correctly (copypaste error from mod_mam)
Kim Alvefur <zash@zash.se>
parents: 1145
diff changeset
    68
	local room_node = jid_split(room);
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    69
	local query = stanza.tags[1];
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    70
1139
b32d65e41755 mod_mam_muc: Get room objects in a less awkward fashion
Kim Alvefur <zash@zash.se>
parents: 1138
diff changeset
    71
	local room_obj = rooms[room];
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    72
	if not room_obj then
1145
5a00f9bec6e7 mod_mam_muc: Send item-not-found if the requested room does not exist
Kim Alvefur <zash@zash.se>
parents: 1144
diff changeset
    73
		return origin.send(st.error_reply(stanza, "cancel", "item-not-found"))
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    74
	end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    75
	local from = jid_bare(stanza.attr.from);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    76
1140
402cb9b604eb mod_mam_muc: Send proper error reply when one is not allowed to query archive
Kim Alvefur <zash@zash.se>
parents: 1139
diff changeset
    77
	-- Banned or not a member of a members-only room?
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    78
	if room_obj._affiliations[from] == "outcast"
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    79
		or room_obj._data.members_only and not room_obj._affiliations[from] then
1140
402cb9b604eb mod_mam_muc: Send proper error reply when one is not allowed to query archive
Kim Alvefur <zash@zash.se>
parents: 1139
diff changeset
    80
		return origin.send(st.error_reply(stanza, "auth", "forbidden"))
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
	end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    82
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    83
	local qid = query.attr.queryid;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    84
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    85
	-- Search query parameters
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    86
	local qstart = query:get_child_text("start");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    87
	local qend = query:get_child_text("end");
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
    88
	module:log("debug", "Archive query, id %s from %s until %s)",
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
    89
		tostring(qid), qstart or "the dawn of time", qend or "now");
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    90
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    91
	if qstart or qend then -- Validate timestamps
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    92
		local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend))
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    93
		if (qstart and not vstart) or (qend and not vend) then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    94
			origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp"))
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    95
			return true
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    96
		end
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    97
		qstart, qend = vstart, vend;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
    98
	end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    99
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   100
	-- RSM stuff
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   101
	local qset = rsm.get(query);
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   102
	local qmax = m_min(qset and qset.max or default_max_items, max_max_items);
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   103
	local reverse = qset and qset.before or false;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   104
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   105
	local before, after = qset and qset.before, qset and qset.after;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   106
	if type(before) ~= "string" then before = nil; end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   107
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
   108
	-- Load all the data!
1146
9fa89dc7a86f mod_mam_muc: Search the rooms archive correctly (copypaste error from mod_mam)
Kim Alvefur <zash@zash.se>
parents: 1145
diff changeset
   109
	local data, err = archive:find(room_node, {
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   110
		start = qstart; ["end"] = qend; -- Time range
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   111
		limit = qmax;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   112
		before = before; after = after;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   113
		reverse = reverse;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   114
		total = true;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   115
	});
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
   116
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   117
	if not data then
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   118
		return origin.send(st.error_reply(stanza, "cancel", "internal-server-error"));
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   119
	end
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   120
	local count = err;
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
   121
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   122
	-- Wrap it in stuff and deliver
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   123
	local first, last;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   124
	for id, item, when in data do
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   125
		local fwd_st = st.message{ to = origin.full_jid }
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   126
			:tag("result", { xmlns = xmlns_mam, queryid = qid, id = id })
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
   127
				:tag("forwarded", { xmlns = xmlns_forward })
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
   128
					:tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   129
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   130
		if not is_stanza(item) then
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   131
			item = st.deserialize(item);
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
   132
		end
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   133
		item.attr.xmlns = "jabber:client";
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   134
		fwd_st:add_child(item);
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   135
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   136
		if not first then first = id; end
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   137
		last = id;
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   138
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   139
		origin.send(fwd_st);
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
   140
	end
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
   141
	-- That's all folks!
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
   142
	module:log("debug", "Archive query %s completed", tostring(qid));
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   143
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   144
	if reverse then first, last = last, first; end
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   145
	return origin.send(st.reply(stanza)
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   146
		:query(xmlns_mam):add_child(rsm.generate {
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   147
			first = first, last = last, count = count }));
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   148
end);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   149
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   150
-- Handle messages
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   151
local function message_handler(event)
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   152
	local stanza = event.stanza;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   153
	local orig_type = stanza.attr.type or "normal";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   154
	local orig_to = stanza.attr.to;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   155
	local orig_from = stanza.attr.from;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   156
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   157
	-- Only store groupchat messages
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   158
	if not (orig_type == "groupchat" and (stanza:get_child("body") or stanza:get_child("subject"))) then
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   159
		return;
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   160
		-- Chat states and other non-content messages, what TODO?
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   161
	end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   162
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   163
	local room = jid_split(orig_to);
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   164
	local room_obj = rooms[orig_to]
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   165
	if not room_obj then return end -- No such room
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   166
1143
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
   167
	if not ( log_all_rooms == true -- Logging forced on all rooms
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
   168
	or (room_obj._data.logging == nil and log_by_default == true)
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
   169
	or room_obj._data.logging ) then return end -- Don't log
8098683b6d6f mod_mam_muc: Allow archiving to be enabled trough in the room configuration
Kim Alvefur <zash@zash.se>
parents: 1142
diff changeset
   170
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   171
	local nick = room_obj._jid_nick[orig_from];
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   172
	if not nick then return end -- Message from someone not in the room?
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   173
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   174
	stanza.attr.from = nick;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   175
	-- And stash it
1142
fabdaa0d99e3 mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents: 1141
diff changeset
   176
	local ok, id = archive:append(room, time_now(), "", stanza);
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   177
	stanza.attr.from = orig_from;
1142
fabdaa0d99e3 mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents: 1141
diff changeset
   178
	if ok and advertise_archive then
fabdaa0d99e3 mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents: 1141
diff changeset
   179
		stanza:tag("archived", { xmlns = xmlns_mam, by = jid_bare(orig_to), id = id }):up();
fabdaa0d99e3 mod_mam_muc: Stap archived messages
Kim Alvefur <zash@zash.se>
parents: 1141
diff changeset
   180
	end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   181
end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   182
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   183
module:hook("message/bare", message_handler, 2);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   184
1141
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   185
-- TODO should we perhaps log presence as well?
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   186
-- And role/affiliation changes?
1091be1c3aba mod_mam_muc: Switch to new stanza storage API
Kim Alvefur <zash@zash.se>
parents: 1140
diff changeset
   187
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   188
module:add_feature(xmlns_mam);