Mercurial > prosody-modules
comparison mod_clean_roster/mod_clean_roster.lua @ 5085:e384b91d0aa7
mod_clean_roster: Clean out invalid characters from roster entires
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 22 Nov 2022 16:59:52 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5084:dda2af7ed02f | 5085:e384b91d0aa7 |
---|---|
1 local s_find = string.find; | |
2 | |
3 local pctl = require "util.prosodyctl"; | |
4 | |
5 local rostermanager = require "core.rostermanager"; | |
6 local storagemanager = require "core.storagemanager"; | |
7 local usermanager = require "core.usermanager"; | |
8 | |
9 -- copypaste from util.stanza | |
10 local function valid_xml_cdata(str, attr) | |
11 return not s_find(str, attr and "[^\1\9\10\13\20-~\128-\247]" or "[^\9\10\13\20-~\128-\247]"); | |
12 end | |
13 | |
14 function module.command(_arg) | |
15 if select(2, pctl.isrunning()) then | |
16 pctl.show_warning("Stop Prosody before running this command"); | |
17 return 1; | |
18 end | |
19 | |
20 for hostname, host in pairs(prosody.hosts) do | |
21 if hostname ~= "*" then | |
22 if host.users.name == "null" then | |
23 storagemanager.initialize_host(hostname); | |
24 usermanager.initialize_host(hostname); | |
25 end | |
26 local fixes = 0; | |
27 for username in host.users.users() do | |
28 local roster = rostermanager.load_roster(username, hostname); | |
29 local changed = false; | |
30 for contact, item in pairs(roster) do | |
31 if contact ~= false then | |
32 if item.name and not valid_xml_cdata(item.name, false) then | |
33 item.name = item.name:gsub("[^\9\10\13\20-~\128-\247]", "�"); | |
34 fixes = fixes + 1; | |
35 changed = true; | |
36 end | |
37 local clean_groups = {}; | |
38 for group in pairs(item.groups) do | |
39 if valid_xml_cdata(group, false) then | |
40 clean_groups[group] = true; | |
41 else | |
42 clean_groups[group:gsub("[^\9\10\13\20-~\128-\247]", "�")] = true; | |
43 fixes = fixes + 1; | |
44 changed = true; | |
45 end | |
46 end | |
47 item.groups = clean_groups; | |
48 else | |
49 -- pending entries etc | |
50 end | |
51 end | |
52 if changed then | |
53 assert(rostermanager.save_roster(username, hostname, roster)); | |
54 end | |
55 end | |
56 pctl.show_message("Fixed %d items on host %s", fixes, hostname); | |
57 end | |
58 end | |
59 return 0; | |
60 end |