Comparison

plugins/mod_mimicking.lua @ 10563:e8db377a2983

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Tue, 24 Dec 2019 00:39:45 +0100
parent 9986:9cb639ef5c72
child 11848:b4d4f0339e16
comparison
equal deleted inserted replaced
10562:670afc079f68 10563:e8db377a2983
1 -- Prosody IM
2 -- Copyright (C) 2012 Florian Zeitz
3 -- Copyright (C) 2019 Kim Alvefur
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local encodings = require "util.encodings";
10 assert(encodings.confusable, "This module requires that Prosody be built with ICU");
11 local skeleton = encodings.confusable.skeleton;
12
13 local usage = require "util.prosodyctl".show_usage;
14 local usermanager = require "core.usermanager";
15 local storagemanager = require "core.storagemanager";
16
17 local skeletons
18 function module.load()
19 if module.host ~= "*" then
20 skeletons = module:open_store("skeletons");
21 end
22 end
23
24 module:hook("user-registered", function(user)
25 local skel = skeleton(user.username);
26 local ok, err = skeletons:set(skel, { username = user.username });
27 if not ok then
28 module:log("error", "Unable to store mimicry data (%q => %q): %s", user.username, skel, err);
29 end
30 end);
31
32 module:hook("user-deleted", function(user)
33 local skel = skeleton(user.username);
34 local ok, err = skeletons:set(skel, nil);
35 if not ok and err then
36 module:log("error", "Unable to clear mimicry data (%q): %s", skel, err);
37 end
38 end);
39
40 module:hook("user-registering", function(user)
41 local existing, err = skeletons:get(skeleton(user.username));
42 if existing then
43 module:log("debug", "Attempt to register username '%s' which could be confused with '%s'", user.username, existing.username);
44 user.allowed = false;
45 elseif err then
46 module:log("error", "Unable to check if new username '%s' can be confused with any existing user: %s", err);
47 end
48 end);
49
50 function module.command(arg)
51 if (arg[1] ~= "bootstrap" or not arg[2]) then
52 usage("mod_mimicking bootstrap <host>", "Initialize username mimicry database");
53 return;
54 end
55
56 local host = arg[2];
57
58 local host_session = prosody.hosts[host];
59 if not host_session then
60 return "No such host";
61 end
62
63 storagemanager.initialize_host(host);
64 usermanager.initialize_host(host);
65
66 skeletons = storagemanager.open(host, "skeletons");
67
68 local count = 0;
69 for user in usermanager.users(host) do
70 local skel = skeleton(user);
71 local existing, err = skeletons:get(skel);
72 if existing and existing.username ~= user then
73 module:log("warn", "Existing usernames '%s' and '%s' are confusable", existing.username, user);
74 elseif err then
75 module:log("error", "Error checking for existing mimicry data (%q = %q): %s", user, skel, err);
76 end
77 local ok, err = skeletons:set(skel, { username = user });
78 if ok then
79 count = count + 1;
80 elseif err then
81 module:log("error", "Unable to store mimicry data (%q => %q): %s", user, skel, err);
82 end
83 end
84 module:log("info", "%d usernames indexed", count);
85 end