annotate mod_muc_members_json/mod_muc_members_json.lua @ 6549:d2e50b587721

mod_firewall: marks: switch to keyval+ store API
author Matthew Wild <mwild1@gmail.com>
date Sun, 24 May 2026 10:49:09 +0100
parents 9a89cb9810a7
children 228859ea4781
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6431
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
1 local async = require "util.async";
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local http = require "net.http";
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local json = require "util.json";
6431
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
4 local promise = require "util.promise";
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
5 local set = require "util.set";
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local json_url = assert(module:get_option_string("muc_members_json_url"), "muc_members_json_url required");
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local managed_mucs = module:get_option("muc_members_json_mucs");
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
6431
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
10 local dry_run_mode = module:get_option_boolean("muc_members_json_dry_run", false);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
11
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local mod_muc = module:depends("muc");
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 --[[
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 xsf = {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 team_hats = {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 board = {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 id = "xmpp:xmpp.org/hats/board";
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 title = "Board";
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 };
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 };
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 member_hat = {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 id = "xmpp:xmpp.org/hats/member";
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 title = "XSF member";
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 };
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 };
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 iteam = {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 team_hats = {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 iteam = {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 id = "xmpp:xmpp.org/hats/iteam";
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 title = "Infra team";
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 };
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 };
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 };
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 }
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 --]]
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local function get_hats(member_info, muc_config)
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local hats = {};
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 if muc_config.member_hat then
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 hats[muc_config.member_hat.id] = {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 title = muc_config.member_hat.title;
5587
dd4df71166ea mod_muc_members_json: Set imported hats to active by default
Matthew Wild <mwild1@gmail.com>
parents: 5586
diff changeset
44 active = true;
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 };
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if muc_config.team_hats and member_info.roles then
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 for _, role in ipairs(member_info.roles) do
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local hat = muc_config.team_hats[role];
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 if hat then
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 hats[hat.id] = {
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 title = hat.title;
5587
dd4df71166ea mod_muc_members_json: Set imported hats to active by default
Matthew Wild <mwild1@gmail.com>
parents: 5586
diff changeset
53 active = true;
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 };
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 end
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 return hats;
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60
6431
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
61 local function async_as_promise(f, p)
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
62 return promise.new(function (resolve, reject)
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
63 local r = async.runner(f);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
64 r:onready(function ()
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
65 resolve(true);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
66 end);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
67 r:onerror(function (err)
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
68 reject(err);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
69 end);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
70 if p == nil then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
71 p = true;
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
72 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
73 local ok, state = r:run(p);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
74 if ok and state == "ready" then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
75 resolve(true);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
76 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
77 end);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
78 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
79
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
80 function update_members(dry_run)
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
81 module:log("debug", "Updating... (dry run: %s)", dry_run and "no" or "yes");
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
82 return http.request(json_url)
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 :next(function (result)
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return json.decode(result.body);
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end)
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 :next(function (data)
6433
9a89cb9810a7 mod_muc_members_json: Use logging stacks own serialization
Kim Alvefur <zash@zash.se>
parents: 6431
diff changeset
87 module:log("debug", "DATA: %q", data);
6431
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
88 return async_as_promise(function ()
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
89 for name, muc_config in pairs(managed_mucs) do
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
90 local muc_jid = name.."@"..module.host;
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
91 local muc = mod_muc.get_room_from_jid(muc_jid);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
92 if muc then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
93 local owner_roles = set.new(muc_config.owner_roles);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
94 local admin_roles = set.new(muc_config.admin_roles);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
95 local member_roles = set.new(muc_config.member_roles);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
96 local affiliated_roles = owner_roles + admin_roles + member_roles;
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
97 local is_private = muc_config.private;
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
98 local include_all_members = muc_config.member_roles == nil;
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99
6431
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
100 if is_private and not muc:get_members_only() then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
101 if not dry_run then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
102 module:log("info", "Setting %s to members-only...", muc_jid);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
103 muc:set_members_only(true);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
104 else
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
105 module:log("info", "DRY RUN: Would set %s to members-only...", muc_jid);
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 end
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
6431
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
108
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
109 local jids = {};
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
110 for _, member_info in ipairs(data.members) do
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
111 local roles = set.new(member_info.roles);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
112 for _, member_jid in ipairs(member_info.jids) do
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
113 if include_all_members or not set.intersection(affiliated_roles, roles):empty() then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
114 jids[member_jid] = true;
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
115 local is_owner = not set.intersection(owner_roles, roles):empty();
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
116 local is_admin = not set.intersection(admin_roles, roles):empty();
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
117 local expected_affiliation = (is_owner and "owner") or (is_admin and "admin") or "member";
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
118 local affiliation = muc:get_affiliation(member_jid);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
119 if affiliation ~= expected_affiliation then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
120 if not dry_run then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
121 muc:set_affiliation(true, member_jid, expected_affiliation, "imported membership");
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
122 muc:set_affiliation_data(member_jid, "source", module.name);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
123 module:log("info", "Set affiliation of %s in %s, %s -> %s", member_jid, muc_jid, affiliation, expected_affiliation);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
124 else
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
125 module:log("info", "DRY RUN: Would set affiliation of %s in %s, %s -> %s", member_jid, muc_jid, affiliation, expected_affiliation);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
126 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
127 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
128 if not dry_run then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
129 muc:set_affiliation_data(member_jid, "hats", get_hats(member_info, muc_config));
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
130 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
131 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
132 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
133 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
134 -- Remove affiliation from folk who weren't in the source data but previously were
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
135 for jid, aff, data in muc:each_affiliation() do
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
136 if not jids[jid] and data and data.source == module.name then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
137 if not dry_run then
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
138 module:log("info", "Removing affiliation of %s in %s", jid, muc_jid);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
139 muc:set_affiliation(true, jid, "none", "imported membership lost");
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
140 else
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
141 module:log("info", "DRY RUN: Would remove affiliation of %s in %s", jid, muc_jid);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
142 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
143 end
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 end
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
6431
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
147 end);
5586
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 end):catch(function (err)
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 module:log("error", "FAILED: %s", err);
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 end);
30b9f78b5058 mod_muc_members_json: New module to import MUC membership from a JSON URL
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 end
6431
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
152
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
153 function module.load()
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
154 update_members(dry_run_mode);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
155 end
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
156
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
157 module:hourly(function ()
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
158 update_members(dry_run_mode);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
159 end);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
160
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
161 module:add_item("shell-command", {
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
162 section = "members_json";
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
163 section_desc = "Commands related to mod_muc_members_json";
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
164 name = "update";
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
165 desc = "Force an update of the members data";
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
166 args = {
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
167 { name = "host", type = "string" };
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
168 };
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
169 host_selector = "host";
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
170 flags = {
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
171 bool_params = { dry_run = true };
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
172 };
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
173 handler = function (shell, host, opts)
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
174 return update_members(opts.dry_run)
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
175 :next(function ()
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
176 return opts.dry_run and "Updated (dry-run only, check logs)" or "Updated";
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
177 end);
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
178 end;
459cb2c2789b mod_muc_members_json: Support private MUCs, non-member affiliation grants, async storage, dry-run mode
Matthew Wild <mwild1@gmail.com>
parents: 5657
diff changeset
179 });