plugins/mod_storage_xep0227.lua
author Matthew Wild <mwild1@gmail.com>
Mon, 10 Jan 2022 15:50:55 +0000
changeset 12179 39921b979edb
parent 12178 a38b7cb5fd6a
child 12180 e7639625a848
permissions -rw-r--r--
mod_storage_xep0227: Ignore luacheck warning
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     1
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     2
local ipairs, pairs = ipairs, pairs;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     3
local setmetatable = setmetatable;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     4
local tostring = tostring;
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
     5
local next, unpack = next, table.unpack or unpack; --luacheck: ignore 113/unpack
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     6
local os_remove = os.remove;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     7
local io_open = io.open;
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
     8
local jid_bare = require "util.jid".bare;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
     9
local jid_prep = require "util.jid".prep;
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    10
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    11
local array = require "util.array";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    12
local base64 = require "util.encodings".base64;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    13
local dt = require "util.datetime";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    14
local hex = require "util.hex";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    15
local it = require "util.iterators";
6700
3d27f5855f4b mod_storage_xep0227: Use configured storage path
Kim Alvefur <zash@zash.se>
parents: 6699
diff changeset
    16
local paths = require"util.paths";
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    17
local set = require "util.set";
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
local st = require "util.stanza";
5219
060d7fc6caf4 plugins/storage/mod_xep0227: Use util.xml.
Waqas Hussain <waqas20@gmail.com>
parents: 5121
diff changeset
    19
local parse_xml_real = require "util.xml".parse;
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    20
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    21
local lfs = require "lfs";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    22
12179
39921b979edb mod_storage_xep0227: Ignore luacheck warning
Matthew Wild <mwild1@gmail.com>
parents: 12178
diff changeset
    23
local function default_get_user_xml(self, user, host) --luacheck: ignore 212/self
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    24
	local jid = user.."@"..host;
6700
3d27f5855f4b mod_storage_xep0227: Use configured storage path
Kim Alvefur <zash@zash.se>
parents: 6699
diff changeset
    25
	local path = paths.join(prosody.paths.data, jid..".xml");
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    26
	local f, err = io_open(path);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    27
	if not f then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    28
		module:log("debug", "Unable to load XML file for <%s>: %s", jid, err);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    29
		return;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    30
	end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    31
	module:log("debug", "Loaded %s", path);
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    32
	local s = f:read("*a");
6701
95a8aeca1fc9 mod_storage_xep0227: Close file handle after reading
Kim Alvefur <zash@zash.se>
parents: 6700
diff changeset
    33
	f:close();
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    34
	return parse_xml_real(s);
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    35
end
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
    36
local function default_set_user_xml(user, host, xml)
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    37
	local jid = user.."@"..host;
6700
3d27f5855f4b mod_storage_xep0227: Use configured storage path
Kim Alvefur <zash@zash.se>
parents: 6699
diff changeset
    38
	local path = paths.join(prosody.paths.data, jid..".xml");
6703
0103dc8fa179 mod_storage_xep0227: Return error from io.open if unable to open file for writing
Kim Alvefur <zash@zash.se>
parents: 6702
diff changeset
    39
	local f, err = io_open(path, "w");
0103dc8fa179 mod_storage_xep0227: Return error from io.open if unable to open file for writing
Kim Alvefur <zash@zash.se>
parents: 6702
diff changeset
    40
	if not f then return f, err; end
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    41
	if xml then
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    42
		local s = tostring(xml);
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    43
		f:write(s);
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    44
		f:close();
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    45
		return true;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    46
	else
6702
ccdd0b615106 mod_storage_xep0227: Open file for writing even if removing so os.remove has a file to delete
Kim Alvefur <zash@zash.se>
parents: 6701
diff changeset
    47
		f:close();
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    48
		return os_remove(path);
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    49
	end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    50
end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    51
local function getUserElement(xml)
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    52
	if xml and xml.name == "server-data" then
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    53
		local host = xml.tags[1];
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    54
		if host and host.name == "host" then
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    55
			local user = host.tags[1];
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    56
			if user and user.name == "user" then
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    57
				return user;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    58
			end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    59
		end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    60
	end
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    61
	module:log("warn", "Unable to find user element");
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    62
end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    63
local function createOuterXml(user, host)
6705
6a5c6c95cf78 mod_storage_xep0227: Use the registered namespace
Kim Alvefur <zash@zash.se>
parents: 6704
diff changeset
    64
	return st.stanza("server-data", {xmlns='urn:xmpp:pie:0'})
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    65
		:tag("host", {jid=host})
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    66
			:tag("user", {name = user});
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    67
end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    68
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    69
local function hex_to_base64(s)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    70
	return base64.encode(hex.from(s));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    71
end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    72
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    73
local function base64_to_hex(s)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    74
	return base64.encode(hex.from(s));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    75
end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    76
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    77
local handlers = {};
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    78
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    79
-- In order to support custom account properties
6706
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
    80
local extended = "http://prosody.im/protocol/extended-xep0227\1";
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
    81
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    82
local scram_hash_name = module:get_option_string("password_hash", "SHA-1");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    83
local scram_properties = set.new({ "server_key", "stored_key", "iteration_count", "salt" });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    84
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    85
handlers.accounts = {
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    86
	get = function(self, user)
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
    87
		user = getUserElement(self:_get_user_xml(user, self.host));
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    88
		local scram_credentials = user and user:get_child_with_attr(
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    89
			"scram-credentials", "urn:xmpp:pie:0#scram",
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    90
			"mechanism", "SCRAM-"..scram_hash_name
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    91
		);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    92
		if scram_credentials then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    93
			return {
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    94
				iteration_count = tonumber(scram_credentials:get_child_text("iter-count"));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    95
				server_key = base64_to_hex(scram_credentials:get_child_text("server-key"));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    96
				stored_key = base64_to_hex(scram_credentials:get_child_text("stored-key"));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    97
				salt = base64.decode(scram_credentials:get_child_text("salt"));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    98
			};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
    99
		elseif user and user.attr.password then
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   100
			return { password = user.attr.password };
6706
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
   101
		elseif user then
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
   102
			local data = {};
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
   103
			for k, v in pairs(user.attr) do
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
   104
				if k:sub(1, #extended) == extended then
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
   105
					data[k:sub(#extended+1)] = v;
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
   106
				end
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
   107
			end
353a7d4dfdc2 mod_storage_xep0227: Store data from mod_auth_internal_hashed in a private namespace
Kim Alvefur <zash@zash.se>
parents: 6705
diff changeset
   108
			return data;
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   109
		end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   110
	end;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   111
	set = function(self, user, data)
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   112
		if not data then
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   113
			return self:_set_user_xml(user, self.host, nil);
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   114
		end
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   115
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   116
		local xml = self:_get_user_xml(user, self.host);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   117
		if not xml then xml = createOuterXml(user, self.host); end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   118
		local usere = getUserElement(xml);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   119
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   120
		local account_properties = set.new(it.to_array(it.keys(data)));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   121
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   122
		-- Include SCRAM credentials if known
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   123
		if account_properties:contains_set(scram_properties) then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   124
			local scram_el = st.stanza("scram-credentials", { xmlns = "urn:xmpp:pie:0#scram", mechanism = "SCRAM-"..scram_hash_name })
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   125
				:text_tag("server-key", hex_to_base64(data.server_key))
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   126
				:text_tag("stored-key", hex_to_base64(data.stored_key))
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   127
				:text_tag("iter-count", ("%d"):format(data.iteration_count))
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   128
				:text_tag("salt", base64.encode(data.salt));
11844
5e9e75c277a2 mod_storage_xep0227: Add scram-credentials to user element rather than server
Matthew Wild <mwild1@gmail.com>
parents: 11793
diff changeset
   129
			usere:add_child(scram_el);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   130
			account_properties:exclude(scram_properties);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   131
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   132
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   133
		-- Include the password if present
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   134
		if account_properties:contains("password") then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   135
			usere.attr.password = data.password;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   136
			account_properties:remove("password");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   137
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   138
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   139
		-- Preserve remaining properties as namespaced attributes
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   140
		for property in account_properties do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   141
			usere.attr[extended..property] = data[property];
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   142
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   143
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   144
		return self:_set_user_xml(user, self.host, xml);
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   145
	end;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   146
};
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   147
handlers.vcard = {
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   148
	get = function(self, user)
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   149
		user = getUserElement(self:_get_user_xml(user, self.host));
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   150
		if user then
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   151
			local vcard = user:get_child("vCard", 'vcard-temp');
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   152
			if vcard then
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   153
				return st.preserialize(vcard);
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   154
			end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   155
		end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   156
	end;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   157
	set = function(self, user, data)
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   158
		local xml = self:_get_user_xml(user, self.host);
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   159
		local usere = xml and getUserElement(xml);
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   160
		if usere then
12086
e87563fefd85 mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
Matthew Wild <mwild1@gmail.com>
parents: 11844
diff changeset
   161
			usere:remove_children("vCard", "vcard-temp");
e87563fefd85 mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
Matthew Wild <mwild1@gmail.com>
parents: 11844
diff changeset
   162
			if not data then
e87563fefd85 mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
Matthew Wild <mwild1@gmail.com>
parents: 11844
diff changeset
   163
				-- No data to set, old one deleted, success
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   164
				return true;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   165
			end
12086
e87563fefd85 mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
Matthew Wild <mwild1@gmail.com>
parents: 11844
diff changeset
   166
			local vcard = st.deserialize(data);
e87563fefd85 mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
Matthew Wild <mwild1@gmail.com>
parents: 11844
diff changeset
   167
			usere:add_child(vcard);
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   168
			return self:_set_user_xml(user, self.host, xml);
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   169
		end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   170
		return true;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   171
	end;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   172
};
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   173
handlers.private = {
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   174
	get = function(self, user)
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   175
		user = getUserElement(self:_get_user_xml(user, self.host));
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   176
		if user then
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   177
			local private = user:get_child("query", "jabber:iq:private");
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   178
			if private then
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   179
				local r = {};
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   180
				for _, tag in ipairs(private.tags) do
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   181
					r[tag.name..":"..tag.attr.xmlns] = st.preserialize(tag);
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   182
				end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   183
				return r;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   184
			end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   185
		end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   186
	end;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   187
	set = function(self, user, data)
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   188
		local xml = self:_get_user_xml(user, self.host);
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   189
		local usere = xml and getUserElement(xml);
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   190
		if usere then
12086
e87563fefd85 mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
Matthew Wild <mwild1@gmail.com>
parents: 11844
diff changeset
   191
			usere:remove_children("query", "jabber:iq:private");
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   192
			if data and next(data) ~= nil then
12086
e87563fefd85 mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
Matthew Wild <mwild1@gmail.com>
parents: 11844
diff changeset
   193
				local private = st.stanza("query", {xmlns='jabber:iq:private'});
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   194
				for _,tag in pairs(data) do
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   195
					private:add_child(st.deserialize(tag));
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   196
				end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   197
				usere:add_child(private);
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   198
			end
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   199
			return self:_set_user_xml(user, self.host, xml);
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   200
		end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   201
		return true;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   202
	end;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   203
};
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   204
8354
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   205
handlers.roster = {
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   206
	get = function(self, user)
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   207
		user = getUserElement(self:_get_user_xml(user, self.host));
8354
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   208
		if user then
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   209
			local roster = user:get_child("query", "jabber:iq:roster");
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   210
			if roster then
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   211
				local r = {
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   212
					[false] = {
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   213
						version = roster.attr.version;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   214
						pending = {};
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   215
					}
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   216
				};
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   217
				for item in roster:childtags("item") do
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   218
					r[item.attr.jid] = {
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   219
						jid = item.attr.jid,
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   220
						subscription = item.attr.subscription,
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   221
						ask = item.attr.ask,
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   222
						name = item.attr.name,
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   223
						groups = {};
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   224
					};
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   225
					for group in item:childtags("group") do
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   226
						r[item.attr.jid].groups[group:get_text()] = true;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   227
					end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   228
					for pending in user:childtags("presence", "jabber:client") do
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   229
						r[false].pending[pending.attr.from] = true;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   230
					end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   231
				end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   232
				return r;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   233
			end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   234
		end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   235
	end;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   236
	set = function(self, user, data)
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   237
		local xml = self:_get_user_xml(user, self.host);
8354
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   238
		local usere = xml and getUserElement(xml);
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   239
		if usere then
12086
e87563fefd85 mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
Matthew Wild <mwild1@gmail.com>
parents: 11844
diff changeset
   240
			usere:remove_children("query", "jabber:iq:roster");
8354
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   241
			usere:maptags(function (tag)
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   242
				if tag.attr.xmlns == "jabber:client" and tag.name == "presence" and tag.attr.type == "subscribe" then
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   243
					return nil;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   244
				end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   245
				return tag;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   246
			end);
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   247
			if data and next(data) ~= nil then
12086
e87563fefd85 mod_storage_xep0227: Replace custom tag-removal helpers with :remove_children()
Matthew Wild <mwild1@gmail.com>
parents: 11844
diff changeset
   248
				local roster = st.stanza("query", {xmlns='jabber:iq:roster'});
8354
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   249
				usere:add_child(roster);
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   250
				for jid, item in pairs(data) do
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   251
					if jid then
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   252
						roster:tag("item", {
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   253
							jid = jid,
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   254
							subscription = item.subscription,
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   255
							ask = item.ask,
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   256
							name = item.name,
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   257
						});
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   258
						for group in pairs(item.groups) do
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   259
							roster:tag("group"):text(group):up();
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   260
						end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   261
						roster:up(); -- move out from item
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   262
					else
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   263
						roster.attr.version = item.version;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   264
						for pending_jid in pairs(item.pending) do
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   265
							usere:add_child(st.presence({ from = pending_jid, type = "subscribe" }));
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   266
						end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   267
					end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   268
				end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   269
			end
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   270
			return self:_set_user_xml(user, self.host, xml);
8354
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   271
		end
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   272
		return true;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   273
	end;
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   274
};
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   275
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   276
-- PEP node configuration/etc. (not items)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   277
local xmlns_pubsub_owner = "http://jabber.org/protocol/pubsub#owner";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   278
local lib_pubsub = module:require "pubsub";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   279
handlers.pep = {
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   280
	get = function (self, user)
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   281
		local xml = self:_get_user_xml(user, self.host);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   282
		local user_el = xml and getUserElement(xml);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   283
		if not user_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   284
			return nil;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   285
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   286
		local nodes = {
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   287
			--[[
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   288
			[node_name] = {
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   289
				name = node_name;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   290
				config = {};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   291
				affiliations = {};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   292
				subscribers = {};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   293
			};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   294
			]]
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   295
		};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   296
		local owner_el = user_el:get_child("pubsub", xmlns_pubsub_owner);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   297
		for node_el in owner_el:childtags() do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   298
			local node_name = node_el.attr.node;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   299
			local node = nodes[node_name];
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   300
			if not node then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   301
				node = {
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   302
					name = node_name;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   303
					config = {};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   304
					affiliations = {};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   305
					subscribers = {};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   306
				};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   307
				nodes[node_name] = node;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   308
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   309
			if node_el.name == "configure" then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   310
				local form = node_el:get_child("x", "jabber:x:data");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   311
				if form then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   312
					node.config = lib_pubsub.node_config_form:data(form);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   313
				end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   314
			elseif node_el.name == "affiliations" then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   315
				for affiliation_el in node_el:childtags("affiliation") do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   316
					local aff_jid = jid_prep(affiliation_el.attr.jid);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   317
					local aff_value = affiliation_el.attr.affiliation;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   318
					if aff_jid and aff_value then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   319
						node.affiliations[aff_jid] = aff_value;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   320
					end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   321
				end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   322
			elseif node_el.name == "subscriptions" then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   323
				for subscription_el in node_el:childtags("subscription") do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   324
					local sub_jid = jid_prep(subscription_el.attr.jid);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   325
					local sub_state = subscription_el.attr.subscription;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   326
					if sub_jid and sub_state == "subscribed" then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   327
						local options;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   328
						local subscription_options_el = subscription_el:get_child("options");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   329
						if subscription_options_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   330
							local options_form = subscription_options_el:get_child("x", "jabber:x:data");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   331
							if options_form then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   332
								options = lib_pubsub.subscription_options_form:data(options_form);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   333
							end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   334
						end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   335
						node.subscribers[sub_jid] = options or true;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   336
					end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   337
				end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   338
			else
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   339
				module:log("warn", "Ignoring unknown pubsub element: %s", node_el.name);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   340
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   341
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   342
		return nodes;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   343
	end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   344
	set = function(self, user, data)
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   345
		local xml = self:_get_user_xml(user, self.host);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   346
		local user_el = xml and getUserElement(xml);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   347
		if not user_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   348
			return true;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   349
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   350
		-- Remove existing data, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   351
		user_el:remove_children("pubsub", xmlns_pubsub_owner);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   352
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   353
		-- Generate new data
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   354
		local owner_el = st.stanza("pubsub", { xmlns = xmlns_pubsub_owner });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   355
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   356
		for node_name, node_data in pairs(data) do
12178
a38b7cb5fd6a mod_storage_xep0227: Support for exporting nodes with no stored configuration
Matthew Wild <mwild1@gmail.com>
parents: 12177
diff changeset
   357
			if node_data == true then
a38b7cb5fd6a mod_storage_xep0227: Support for exporting nodes with no stored configuration
Matthew Wild <mwild1@gmail.com>
parents: 12177
diff changeset
   358
				node_data = { config = {} };
a38b7cb5fd6a mod_storage_xep0227: Support for exporting nodes with no stored configuration
Matthew Wild <mwild1@gmail.com>
parents: 12177
diff changeset
   359
			end
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   360
			local configure_el = st.stanza("configure", { node = node_name })
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   361
				:add_child(lib_pubsub.node_config_form:form(node_data.config, "submit"));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   362
			owner_el:add_child(configure_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   363
			if node_data.affiliations and next(node_data.affiliations) ~= nil then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   364
				local affiliations_el = st.stanza("affiliations", { node = node_name });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   365
				for aff_jid, aff_value in pairs(node_data.affiliations) do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   366
					affiliations_el:tag("affiliation", { jid = aff_jid, affiliation = aff_value }):up();
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   367
				end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   368
				owner_el:add_child(affiliations_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   369
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   370
			if node_data.subscribers and next(node_data.subscribers) ~= nil then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   371
				local subscriptions_el = st.stanza("subscriptions", { node = node_name });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   372
				for sub_jid, sub_data in pairs(node_data.subscribers) do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   373
					local sub_el = st.stanza("subscription", { jid = sub_jid, subscribed = "subscribed" });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   374
					if sub_data ~= true then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   375
						local options_form = lib_pubsub.subscription_options_form:form(sub_data, "submit");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   376
						sub_el:tag("options"):add_child(options_form):up();
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   377
					end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   378
					subscriptions_el:add_child(sub_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   379
				end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   380
				owner_el:add_child(subscriptions_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   381
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   382
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   383
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   384
		user_el:add_child(owner_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   385
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   386
		return self:_set_user_xml(user, self.host, xml);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   387
	end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   388
};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   389
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   390
-- PEP items
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   391
local xmlns_pubsub = "http://jabber.org/protocol/pubsub";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   392
handlers.pep_ = {
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   393
	_stores = function (self, xml) --luacheck: ignore 212/self
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   394
		local store_names = set.new();
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   395
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   396
		local user_el = xml and getUserElement(xml);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   397
		if not user_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   398
			return store_names;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   399
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   400
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   401
		-- Locate existing pubsub element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   402
		local pubsub_el = user_el:get_child("pubsub", xmlns_pubsub);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   403
		if not pubsub_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   404
			return store_names;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   405
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   406
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   407
		-- Find node items element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   408
		for items_el in pubsub_el:childtags("items") do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   409
			store_names:add("pep_"..items_el.attr.node);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   410
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   411
		return store_names;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   412
	end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   413
	find = function (self, user, query)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   414
		-- query keys: limit, reverse, key (id)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   415
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   416
		local xml = self:_get_user_xml(user, self.host);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   417
		local user_el = xml and getUserElement(xml);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   418
		if not user_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   419
			return nil, "no 227 user element found";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   420
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   421
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   422
		local node_name = self.datastore:match("^pep_(.+)$");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   423
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   424
		-- Locate existing pubsub element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   425
		local pubsub_el = user_el:get_child("pubsub", xmlns_pubsub);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   426
		if not pubsub_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   427
			return nil;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   428
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   429
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   430
		-- Find node items element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   431
		local node_items_el;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   432
		for items_el in pubsub_el:childtags("items") do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   433
			if items_el.attr.node == node_name then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   434
				node_items_el = items_el;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   435
				break;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   436
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   437
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   438
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   439
		if not node_items_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   440
			return nil;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   441
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   442
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   443
		local user_jid = user.."@"..self.host;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   444
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   445
		local results = {};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   446
		for item_el in node_items_el:childtags("item") do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   447
			if query and query.key then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   448
				if item_el.attr.id == query.key then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   449
					table.insert(results, { item_el.attr.id, item_el.tags[1], 0, user_jid });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   450
					break;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   451
				end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   452
			else
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   453
				table.insert(results, { item_el.attr.id, item_el.tags[1], 0, user_jid });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   454
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   455
			if query and query.limit and #results >= query.limit then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   456
				break;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   457
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   458
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   459
		if query and query.reverse then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   460
			return array.reverse(results);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   461
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   462
		local i = 0;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   463
		return function ()
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   464
			i = i + 1;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   465
			local v = results[i];
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   466
			if v == nil then return nil; end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   467
			return unpack(v, 1, 4);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   468
		end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   469
	end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   470
	append = function (self, user, key, payload, when, with) --luacheck: ignore 212/when 212/with 212/key
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   471
		local xml = self:_get_user_xml(user, self.host);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   472
		local user_el = xml and getUserElement(xml);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   473
		if not user_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   474
			return true;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   475
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   476
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   477
		local node_name = self.datastore:match("^pep_(.+)$");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   478
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   479
		-- Locate existing pubsub element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   480
		local pubsub_el = user_el:get_child("pubsub", xmlns_pubsub);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   481
		if not pubsub_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   482
			pubsub_el = st.stanza("pubsub", { xmlns = xmlns_pubsub });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   483
			user_el:add_child(pubsub_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   484
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   485
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   486
		-- Find node items element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   487
		local node_items_el;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   488
		for items_el in pubsub_el:childtags("items") do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   489
			if items_el.attr.node == node_name then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   490
				node_items_el = items_el;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   491
				break;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   492
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   493
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   494
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   495
		if not node_items_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   496
			-- Doesn't exist yet, create one
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   497
			node_items_el = st.stanza("items", { node = node_name });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   498
			pubsub_el:add_child(node_items_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   499
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   500
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   501
		-- Append item to pubsub_el
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   502
		local item_el = st.stanza("item", { id = key })
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   503
			:add_child(payload);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   504
		node_items_el:add_child(item_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   505
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   506
		return self:_set_user_xml(user, self.host, xml);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   507
	end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   508
	delete = function (self, user, query)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   509
		-- query keys: limit, reverse, key (id)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   510
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   511
		local xml = self:_get_user_xml(user, self.host);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   512
		local user_el = xml and getUserElement(xml);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   513
		if not user_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   514
			return nil, "no 227 user element found";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   515
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   516
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   517
		local node_name = self.datastore:match("^pep_(.+)$");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   518
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   519
		-- Locate existing pubsub element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   520
		local pubsub_el = user_el:get_child("pubsub", xmlns_pubsub);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   521
		if not pubsub_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   522
			return nil;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   523
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   524
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   525
		-- Find node items element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   526
		local node_items_el;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   527
		for items_el in pubsub_el:childtags("items") do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   528
			if items_el.attr.node == node_name then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   529
				node_items_el = items_el;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   530
				break;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   531
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   532
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   533
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   534
		if not node_items_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   535
			return nil;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   536
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   537
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   538
		local results = array();
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   539
		for item_el in pubsub_el:childtags("item") do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   540
			if query and query.key then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   541
				if item_el.attr.id == query.key then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   542
					table.insert(results, item_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   543
					break;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   544
				end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   545
			else
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   546
				table.insert(results, item_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   547
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   548
			if query and query.limit and #results >= query.limit then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   549
				break;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   550
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   551
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   552
		if query and query.truncate then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   553
			results:sub(-query.truncate);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   554
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   555
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   556
		-- Actually remove the matching items
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   557
		local delete_keys = set.new(results:map(function (item) return item.attr.id; end));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   558
		pubsub_el:maptags(function (item_el)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   559
			if delete_keys:contains(item_el.attr.id) then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   560
				return nil;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   561
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   562
			return item_el;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   563
		end);
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   564
		return self:_set_user_xml(user, self.host, xml);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   565
	end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   566
};
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   567
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   568
-- MAM archives
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   569
local xmlns_pie_mam = "urn:xmpp:pie:0#mam";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   570
handlers.archive = {
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   571
	find = function (self, user, query)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   572
		assert(query == nil, "XEP-0313 queries are not supported on XEP-0227 files");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   573
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   574
		local xml = self:_get_user_xml(user, self.host);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   575
		local user_el = xml and getUserElement(xml);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   576
		if not user_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   577
			return nil, "no 227 user element found";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   578
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   579
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   580
		-- Locate existing archive element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   581
		local archive_el = user_el:get_child("archive", xmlns_pie_mam);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   582
		if not archive_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   583
			return nil;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   584
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   585
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   586
		local user_jid = user.."@"..self.host;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   587
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   588
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   589
		local f, s, result_el = archive_el:childtags("result", "urn:xmpp:mam:2");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   590
		return function ()
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   591
			result_el = f(s, result_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   592
			if not result_el then return nil; end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   593
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   594
			local id = result_el.attr.id;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   595
			local item = result_el:find("{urn:xmpp:forward:0}forwarded/{jabber:client}message");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   596
			assert(item, "Invalid stanza in XEP-0227 archive");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   597
			local when = dt.parse(result_el:find("{urn:xmpp:forward:0}forwarded/{urn:xmpp:delay}delay@stamp"));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   598
			local to_bare, from_bare = jid_bare(item.attr.to), jid_bare(item.attr.from);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   599
			local with = to_bare == user_jid and from_bare or to_bare;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   600
			-- id, item, when, with
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   601
			return id, item, when, with;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   602
		end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   603
	end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   604
	append = function (self, user, key, payload, when, with) --luacheck: ignore 212/when 212/with 212/key
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   605
		local xml = self:_get_user_xml(user, self.host);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   606
		local user_el = xml and getUserElement(xml);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   607
		if not user_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   608
			return true;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   609
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   610
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   611
		-- Locate existing archive element, if any
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   612
		local archive_el = user_el:get_child("archive", xmlns_pie_mam);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   613
		if not archive_el then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   614
			archive_el = st.stanza("archive", { xmlns = xmlns_pie_mam });
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   615
			user_el:add_child(archive_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   616
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   617
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   618
		local item = st.clone(payload);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   619
		item.attr.xmlns = "jabber:client";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   620
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   621
		local result_el = st.stanza("result", { xmlns = "urn:xmpp:mam:2", id = key })
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   622
			:tag("forwarded", { xmlns = "urn:xmpp:forward:0" })
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   623
				:tag("delay", { xmlns = "urn:xmpp:delay", stamp = dt.datetime(when) }):up()
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   624
				:add_child(item)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   625
			:up();
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   626
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   627
		-- Append item to archive_el
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   628
		archive_el:add_child(result_el);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   629
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   630
		return self:_set_user_xml(user, self.host, xml);
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   631
	end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   632
};
8354
cc05b6366576 mod_storage_xep0227: Add roster storage (fixes #1023)
Kim Alvefur <zash@zash.se>
parents: 8353
diff changeset
   633
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   634
-----------------------------
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   635
local driver = {};
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   636
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   637
local function users(self)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   638
	local file_patt = "^.*@"..(self.host:gsub("%p", "%%%1")).."%.xml$";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   639
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   640
	local f, s, filename = lfs.dir(prosody.paths.data);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   641
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   642
	return function ()
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   643
		filename = f(s, filename);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   644
		while filename and not filename:match(file_patt) do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   645
			filename = f(s, filename);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   646
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   647
		if not filename then return nil; end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   648
		return filename:match("^[^@]+");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   649
	end;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   650
end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   651
8355
6ff50541d2a6 mod_storage_xep0227: Ignore unused 'self' argument [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8354
diff changeset
   652
function driver:open(datastore, typ) -- luacheck: ignore 212/self
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   653
	if typ and typ ~= "keyval" and typ ~= "archive" then return nil, "unsupported-store"; end
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   654
	local handler = handlers[datastore];
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   655
	if not handler and datastore:match("^pep_") then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   656
		handler = handlers.pep_;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   657
	end
6704
88a25c364a14 mod_storage_xep0227: Update open method for current API
Kim Alvefur <zash@zash.se>
parents: 6703
diff changeset
   658
	if not handler then return nil, "unsupported-datastore"; end
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   659
	local instance = setmetatable({
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   660
			host = module.host;
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   661
			datastore = datastore;
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   662
			users = users;
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   663
			_get_user_xml = assert(default_get_user_xml);
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   664
			_set_user_xml = default_set_user_xml;
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   665
		}, {
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   666
			__index = handler;
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   667
		}
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   668
	);
3414
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   669
	if instance.init then instance:init(); end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   670
	return instance;
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   671
end
9a1f6239b63c storage/mod_xep0227: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
   672
12177
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   673
-- Custom API that allows some configuration
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   674
function driver:open_xep0227(datastore, typ, options)
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   675
	local instance, err = self:open(datastore, typ);
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   676
	if not instance then
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   677
		return instance, err;
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   678
	end
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   679
	if options then
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   680
		instance._set_user_xml = assert(options.set_user_xml);
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   681
		instance._get_user_xml = assert(options.get_user_xml);
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   682
	end
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   683
	return instance;
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   684
end
270047afa6af mod_storage_xep0227: Allow overriding the input/output layer for XEP-0227 data
Matthew Wild <mwild1@gmail.com>
parents: 12086
diff changeset
   685
11793
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   686
local function get_store_names(self, path)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   687
	local stores = set.new();
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   688
	local f, err = io_open(paths.join(prosody.paths.data, path));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   689
	if not f then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   690
		module:log("warn", "Unable to load XML file for <%s>: %s", "store listing", err);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   691
		return stores;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   692
	end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   693
	module:log("info", "Loaded %s", path);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   694
	local s = f:read("*a");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   695
	f:close();
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   696
	local xml = parse_xml_real(s);
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   697
	for _, handler_funcs in pairs(handlers) do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   698
		if handler_funcs._stores then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   699
			stores:include(handler_funcs._stores(self, xml));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   700
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   701
	end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   702
	return stores;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   703
end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   704
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   705
function driver:stores(username)
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   706
	local store_dir = prosody.paths.data;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   707
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   708
	local mode, err = lfs.attributes(store_dir, "mode");
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   709
	if not mode then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   710
		return function() module:log("debug", "Could not iterate over stores in %s: %s", store_dir, err); end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   711
	end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   712
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   713
	local file_patt = "^.*@"..(module.host:gsub("%p", "%%%1")).."%.xml$";
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   714
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   715
	local all_users = username == true;
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   716
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   717
	local store_names = set.new();
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   718
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   719
	for filename in lfs.dir(prosody.paths.data) do
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   720
		if filename:match(file_patt) then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   721
			if all_users or filename == username.."@"..module.host..".xml" then
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   722
				store_names:include(get_store_names(self, filename));
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   723
				if not all_users then break; end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   724
			end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   725
		end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   726
	end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   727
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   728
	return store_names:items();
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   729
end
f3085620b6ff mod_storage_xep0227: Update for XEP-0227 r1.1: Support for SCRAM, MAM, PEP
Matthew Wild <mwild1@gmail.com>
parents: 8355
diff changeset
   730
5121
b5a5643f8572 core.storagemanager, mod_storage_*: "data-driver" -> "storage-provider", to allow using module:provides().
Waqas Hussain <waqas20@gmail.com>
parents: 3414
diff changeset
   731
module:provides("storage", driver);