plugins/mod_mimicking.lua
author Kim Alvefur <zash@zash.se>
Mon, 29 Apr 2019 17:26:56 +0200
changeset 9989 46c038969eb3
parent 9988 bbabd35b30ae
child 9990 9cb639ef5c72
permissions -rw-r--r--
mod_mimicking: Use more intuitive term "mimicry index" for skeletons Fits better with the module name too.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
-- Prosody IM
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
-- Copyright (C) 2012 Florian Zeitz
9985
8758febbaca2 mod_mimicking: Import skeleton() from current location
Kim Alvefur <zash@zash.se>
parents: 9984
diff changeset
     3
-- Copyright (C) 2019 Kim Alvefur
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
--
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
-- COPYING file in the source package for more information.
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
--
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
9985
8758febbaca2 mod_mimicking: Import skeleton() from current location
Kim Alvefur <zash@zash.se>
parents: 9984
diff changeset
     9
local encodings = require "util.encodings";
8758febbaca2 mod_mimicking: Import skeleton() from current location
Kim Alvefur <zash@zash.se>
parents: 9984
diff changeset
    10
assert(encodings.confusable, "This module requires that Prosody be built with ICU");
8758febbaca2 mod_mimicking: Import skeleton() from current location
Kim Alvefur <zash@zash.se>
parents: 9984
diff changeset
    11
local skeleton = encodings.confusable.skeleton;
8758febbaca2 mod_mimicking: Import skeleton() from current location
Kim Alvefur <zash@zash.se>
parents: 9984
diff changeset
    12
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
local usage = require "util.prosodyctl".show_usage;
9988
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    14
local usermanager = require "core.usermanager";
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    15
local storagemanager = require "core.storagemanager";
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
9988
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    17
local skeletons
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    18
function module.load()
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    19
	if module.host ~= "*" then
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    20
		skeletons = module:open_store("skeletons");
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    21
	end
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    22
end
9987
f7d11503fdce mod_mimicking: Use new storage API
Kim Alvefur <zash@zash.se>
parents: 9986
diff changeset
    23
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
module:hook("user-registered", function(user)
9987
f7d11503fdce mod_mimicking: Use new storage API
Kim Alvefur <zash@zash.se>
parents: 9986
diff changeset
    25
	skeletons:set(skeleton(user.username), { username = user.username });
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
end);
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
9986
d09575dae242 mod_mimicking: Hook the correct event names
Kim Alvefur <zash@zash.se>
parents: 9985
diff changeset
    28
module:hook("user-deleted", function(user)
9987
f7d11503fdce mod_mimicking: Use new storage API
Kim Alvefur <zash@zash.se>
parents: 9986
diff changeset
    29
	skeletons:set(skeleton(user.username), nil);
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
end);
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
9986
d09575dae242 mod_mimicking: Hook the correct event names
Kim Alvefur <zash@zash.se>
parents: 9985
diff changeset
    32
module:hook("user-registering", function(user)
9987
f7d11503fdce mod_mimicking: Use new storage API
Kim Alvefur <zash@zash.se>
parents: 9986
diff changeset
    33
	if skeletons:get(skeleton(user.username)) then
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
		user.allowed = false;
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
	end
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
end);
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
function module.command(arg)
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    39
	if (arg[1] ~= "bootstrap" or not arg[2]) then
9989
46c038969eb3 mod_mimicking: Use more intuitive term "mimicry index" for skeletons
Kim Alvefur <zash@zash.se>
parents: 9988
diff changeset
    40
		usage("mod_mimicking bootstrap <host>", "Initialize username mimicry index");
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
		return;
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
	end
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    43
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
	local host = arg[2];
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    45
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    46
	local host_session = prosody.hosts[host];
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    47
	if not host_session then
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
		return "No such host";
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
	end
9988
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    50
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
	storagemanager.initialize_host(host);
9988
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    52
	usermanager.initialize_host(host);
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
9988
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    54
	skeletons = storagemanager.open(host, "skeletons");
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    55
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    56
	for user in usermanager.users(host) do
bbabd35b30ae mod_mimicking: Update command to work with current code
Kim Alvefur <zash@zash.se>
parents: 9987
diff changeset
    57
		skeletons:set(skeleton(user), { username = user });
9984
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    58
	end
73a447249fe4 mod_mimicking: Prevents registration of confusable usernames (by Florob) (fixes #1347)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    59
end