annotate mod_groups_internal/mod_groups_internal.lua @ 5825:865c77b5c6dc

mod_groups_internal: Add flag to indicate when a linked MUC has been deleted In theory this shouldn't happen, but it could (and apparently does).
author Matthew Wild <mwild1@gmail.com>
date Fri, 12 Jan 2024 18:20:40 +0000
parents f1e816df1f73
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local rostermanager = require"core.rostermanager";
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
2 local modulemanager = require"core.modulemanager";
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
3 local array = require "util.array";
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local id = require "util.id";
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local jid = require "util.jid";
4399
846b7af5588c mod_groups_internal: send invite to new members of groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4392
diff changeset
6 local st = require "util.stanza";
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local jid_join = jid.join;
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local host = module.host;
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
10 local group_info_store = module:open_store("group_info", "keyval+");
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local group_members_store = module:open_store("groups");
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local group_memberships = module:open_store("groups", "map");
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
4392
e5792ca1d704 mod_groups_internal: fix default value and handling of groups_muc_host
Jonas Schäfer <jonas@wielicki.name>
parents: 4389
diff changeset
14 local muc_host_name = module:get_option("groups_muc_host", "groups."..host);
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
15 local muc_host = nil;
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
16
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local is_contact_subscribed = rostermanager.is_contact_subscribed;
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 -- Make a *one-way* subscription. User will see when contact is online,
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 -- contact will not see when user is online.
5800
8566a423da88 mod_groups_internal: Set group names as roster groups
Kim Alvefur <zash@zash.se>
parents: 5795
diff changeset
21 local function subscribe(user, user_jid, contact, contact_jid, group_name)
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 -- Update user's roster to say subscription request is pending...
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 rostermanager.set_contact_pending_out(user, host, contact_jid);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 -- Update contact's roster to say subscription request is pending...
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 rostermanager.set_contact_pending_in(contact, host, user_jid);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 -- Update contact's roster to say subscription request approved...
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 rostermanager.subscribed(contact, host, user_jid);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 -- Update user's roster to say subscription request approved...
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 rostermanager.process_inbound_subscription_approval(user, host, contact_jid);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
5800
8566a423da88 mod_groups_internal: Set group names as roster groups
Kim Alvefur <zash@zash.se>
parents: 5795
diff changeset
31 if group_name then
8566a423da88 mod_groups_internal: Set group names as roster groups
Kim Alvefur <zash@zash.se>
parents: 5795
diff changeset
32 local user_roster = rostermanager.load_roster(user, host);
8566a423da88 mod_groups_internal: Set group names as roster groups
Kim Alvefur <zash@zash.se>
parents: 5795
diff changeset
33 user_roster[contact_jid].groups[group_name] = true;
8566a423da88 mod_groups_internal: Set group names as roster groups
Kim Alvefur <zash@zash.se>
parents: 5795
diff changeset
34 end
8566a423da88 mod_groups_internal: Set group names as roster groups
Kim Alvefur <zash@zash.se>
parents: 5795
diff changeset
35
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 -- Push updates to both rosters
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 rostermanager.roster_push(user, host, contact_jid);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 rostermanager.roster_push(contact, host, user_jid);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local function user_groups(username)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 return pairs(group_memberships:get_all(username) or {});
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local function do_single_group_subscriptions(username, group_id)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local members = group_members_store:get(group_id);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if not members then return; end
5800
8566a423da88 mod_groups_internal: Set group names as roster groups
Kim Alvefur <zash@zash.se>
parents: 5795
diff changeset
48 local group_name = group_info_store:get_key(group_id, "name");
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local user_jid = jid_join(username, host);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 for membername in pairs(members) do
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if membername ~= username then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local member_jid = jid_join(membername, host);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if not is_contact_subscribed(username, host, member_jid) then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 module:log("debug", "[group %s] Subscribing %s to %s", member_jid, user_jid);
5800
8566a423da88 mod_groups_internal: Set group names as roster groups
Kim Alvefur <zash@zash.se>
parents: 5795
diff changeset
55 subscribe(membername, member_jid, username, user_jid, group_name);
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if not is_contact_subscribed(membername, host, user_jid) then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 module:log("debug", "[group %s] Subscribing %s to %s", user_jid, member_jid);
5800
8566a423da88 mod_groups_internal: Set group names as roster groups
Kim Alvefur <zash@zash.se>
parents: 5795
diff changeset
59 subscribe(username, user_jid, membername, member_jid, group_name);
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 local function do_all_group_subscriptions_by_user(username)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 for group_id in user_groups(username) do
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 do_single_group_subscriptions(username, group_id);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 local function do_all_group_subscriptions_by_group(group_id)
4419
1185acb2ab91 mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents: 4418
diff changeset
72 local members = get_members(group_id)
1185acb2ab91 mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents: 4418
diff changeset
73 if not members then
1185acb2ab91 mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents: 4418
diff changeset
74 return
1185acb2ab91 mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents: 4418
diff changeset
75 end
1185acb2ab91 mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents: 4418
diff changeset
76 for membername in pairs(members) do
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 do_single_group_subscriptions(membername, group_id);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 module:hook("resource-bind", function(event)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 module:log("debug", "Updating group subscriptions...");
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 do_all_group_subscriptions_by_user(event.session.username);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
86 local function _create_muc_room(name)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
87 if not muc_host_name then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
88 module:log("error", "cannot create group MUC: no MUC host configured")
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
89 return nil, "service-unavailable"
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
90 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
91 if not muc_host then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
92 module:log("error", "cannot create group MUC: MUC host %s not configured properly", muc_host_name)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
93 return nil, "internal-server-error"
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
94 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
95
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
96 local muc_jid = jid.prep(id.short() .. "@" .. muc_host_name);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
97 local room = muc_host.create_room(muc_jid)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
98 if not room then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
99 return nil, "internal-server-error"
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
100 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
101
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
102 local ok = pcall(function ()
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
103 room:set_public(false);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
104 room:set_persistent(true);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
105 room:set_members_only(true);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
106 room:set_allow_member_invites(false);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
107 room:set_moderated(false);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
108 room:set_whois("anyone");
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
109 room:set_name(name);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
110 end);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
111
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
112 if not ok then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
113 module:log("error", "Failed to configure group MUC %s", muc_jid);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
114 room:destroy();
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
115 return nil, "internal-server-error";
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
116 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
117
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
118 return muc_jid, room;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
119 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
120
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 --luacheck: ignore 131
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
122 function create(group_info, create_default_muc, group_id)
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 if not group_info.name then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 return nil, "group-name-required";
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
4385
dfb34cc97028 mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents: 4383
diff changeset
126 if group_id then
dfb34cc97028 mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents: 4383
diff changeset
127 if exists(group_id) then
dfb34cc97028 mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents: 4383
diff changeset
128 return nil, "conflict"
dfb34cc97028 mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents: 4383
diff changeset
129 end
dfb34cc97028 mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents: 4383
diff changeset
130 else
dfb34cc97028 mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents: 4383
diff changeset
131 group_id = id.short();
dfb34cc97028 mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents: 4383
diff changeset
132 end
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
134 local muc_jid = nil
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
135 local room = nil
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
136 if create_default_muc then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
137 muc_jid, room = _create_muc_room(group_info.name);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
138 if not muc_jid then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
139 -- MUC creation failed, fail to create group
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
140 delete(group_id)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
141 return nil, room;
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
142 end
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 local ok = group_info_store:set(group_id, {
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 name = group_info.name;
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
147 muc_jid = muc_jid;
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 });
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 if not ok then
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
150 if room then
4423
ca821df93cb9 mod_groups_internal: correctly destroy MUC associated with group
Jonas Schäfer <jonas@wielicki.name>
parents: 4419
diff changeset
151 room:destroy()
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
152 end
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 return nil, "internal-server-error";
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 end
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
155
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 return group_id;
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 function get_info(group_id)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 return group_info_store:get(group_id);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 function set_info(group_id, info)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 if not info then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 return nil, "bad-request"
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 if not info.name or #info.name == 0 then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 return nil, "bad-request"
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
4415
c7424b96c75e mod_groups_internal: maintain the name of the associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4407
diff changeset
172 -- TODO: we should probably prohibit changing/removing the MUC JID of
c7424b96c75e mod_groups_internal: maintain the name of the associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4407
diff changeset
173 -- an existing group.
c7424b96c75e mod_groups_internal: maintain the name of the associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4407
diff changeset
174 if info.muc_jid then
4706
428861d1d1e4 mod_groups_internal: Fix accidental global [luacheck]
Kim Alvefur <zash@zash.se>
parents: 4430
diff changeset
175 local room = muc_host.get_room_from_jid(info.muc_jid);
4415
c7424b96c75e mod_groups_internal: maintain the name of the associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4407
diff changeset
176 room:set_name(info.name);
c7424b96c75e mod_groups_internal: maintain the name of the associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4407
diff changeset
177 end
c7424b96c75e mod_groups_internal: maintain the name of the associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4407
diff changeset
178
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 local ok = group_info_store:set(group_id, info);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 if not ok then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 return nil, "internal-server-error";
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 return true
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 function get_members(group_id)
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
187 return group_members_store:get(group_id) or {};
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 function exists(group_id)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 return not not get_info(group_id);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 function get_user_groups(username)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 local groups = {};
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 do
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 local group_set = group_memberships:get_all(username);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 if group_set then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 for group_id in pairs(group_set) do
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 table.insert(groups, group_id);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 return groups;
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 function delete(group_id)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 if group_members_store:set(group_id, nil) then
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
209 local group_info = get_info(group_id);
4406
d86592775a20 mod_groups_internal: Fix unintended global variable (thanks luacheck)
Matthew Wild <mwild1@gmail.com>
parents: 4405
diff changeset
210 if group_info and group_info.muc_jid then
4423
ca821df93cb9 mod_groups_internal: correctly destroy MUC associated with group
Jonas Schäfer <jonas@wielicki.name>
parents: 4419
diff changeset
211 local room = muc_host.get_room_from_jid(group_info.muc_jid)
ca821df93cb9 mod_groups_internal: correctly destroy MUC associated with group
Jonas Schäfer <jonas@wielicki.name>
parents: 4419
diff changeset
212 if room then
ca821df93cb9 mod_groups_internal: correctly destroy MUC associated with group
Jonas Schäfer <jonas@wielicki.name>
parents: 4419
diff changeset
213 room:destroy()
ca821df93cb9 mod_groups_internal: correctly destroy MUC associated with group
Jonas Schäfer <jonas@wielicki.name>
parents: 4419
diff changeset
214 end
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
215 end
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 return group_info_store:set(group_id, nil);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 return nil, "internal-server-error";
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220
4386
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
221 function add_member(group_id, username, delay_update)
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 local group_info = group_info_store:get(group_id);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 if not group_info then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 return nil, "group-not-found";
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 if not group_memberships:set(group_id, username, {}) then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 return nil, "internal-server-error";
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 end
5822
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
229
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
230 if group_info.muc_jid then
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
231 local room = muc_host.get_room_from_jid(group_info.muc_jid);
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
232 if room then
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
233 local user_jid = username .. "@" .. host;
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
234 room:set_affiliation(true, user_jid, "member");
4399
846b7af5588c mod_groups_internal: send invite to new members of groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4392
diff changeset
235 module:send(st.message(
846b7af5588c mod_groups_internal: send invite to new members of groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4392
diff changeset
236 { from = group_info.muc_jid, to = user_jid }
846b7af5588c mod_groups_internal: send invite to new members of groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4392
diff changeset
237 ):tag("x", {
846b7af5588c mod_groups_internal: send invite to new members of groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4392
diff changeset
238 xmlns = "jabber:x:conference",
846b7af5588c mod_groups_internal: send invite to new members of groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4392
diff changeset
239 jid = group_info.muc_jid
846b7af5588c mod_groups_internal: send invite to new members of groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4392
diff changeset
240 }):up());
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
241 module:log("debug", "set user %s to be member in %s and sent invite", username, group_info.muc_jid);
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
242 else
4418
8c31d4b872c3 mod_groups_internal: fix log message not appearing
Jonas Schäfer <jonas@wielicki.name>
parents: 4417
diff changeset
243 module:log("warn", "failed to update affiliation for %s in %s", username, group_info.muc_jid);
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
244 end
5822
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
245 elseif group_info.mucs then
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
246 local user_jid = username .. "@" .. host;
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
247 for i = #group_info.mucs, 1, -1 do
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
248 local muc_jid = group_info.mucs[i];
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
249 local room = muc_host.get_room_from_jid(muc_jid);
5824
f1e816df1f73 mod_groups_internal: Also remove MUCs that still exist, but have been destroyed
Matthew Wild <mwild1@gmail.com>
parents: 5822
diff changeset
250 if not room or room._data.destroyed then
5822
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
251 -- MUC no longer available, for some reason
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
252 -- Let's remove it from the circle metadata...
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
253 table.remove(group_info.mucs, i);
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
254 group_info_store:set_key(group_id, "mucs", group_info.mucs);
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
255 else
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
256 room:set_affiliation(true, user_jid, "member");
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
257 module:send(st.message(
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
258 { from = muc_jid, to = user_jid }
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
259 ):tag("x", {
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
260 xmlns = "jabber:x:conference",
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
261 jid = muc_jid
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
262 }):up());
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
263 module:log("debug", "set user %s to be member in %s and sent invite", username, muc_jid);
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
264 end
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
265 end
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
266 end
5822
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
267
4424
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
268 module:fire_event(
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
269 "group-user-added",
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
270 {
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
271 id = group_id,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
272 user = username,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
273 host = host,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
274 group_info = group_info,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
275 }
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
276 )
4386
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
277 if not delay_update then
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
278 do_all_group_subscriptions_by_group(group_id);
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
279 end
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 return true;
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 function remove_member(group_id, username)
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 local group_info = group_info_store:get(group_id);
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 if not group_info then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 return nil, "group-not-found";
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 if not group_memberships:set(group_id, username, nil) then
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289 return nil, "internal-server-error";
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 end
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
291 if group_info.muc_jid then
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
292 local room = muc_host.get_room_from_jid(group_info.muc_jid);
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
293 if room then
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
294 local user_jid = username .. "@" .. host;
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
295 room:set_affiliation(true, user_jid, nil);
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
296 else
4418
8c31d4b872c3 mod_groups_internal: fix log message not appearing
Jonas Schäfer <jonas@wielicki.name>
parents: 4417
diff changeset
297 module:log("warn", "failed to update affiliation for %s in %s", username, group_info.muc_jid);
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
298 end
5822
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
299 elseif group_info.mucs then
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
300 local user_jid = username .. "@" .. host;
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
301 for _, muc_jid in ipairs(group_info.mucs) do
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
302 local room = muc_host.get_room_from_jid(muc_jid);
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
303 if room then
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
304 room:set_affiliation(true, user_jid, nil);
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
305 else
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
306 module:log("warn", "failed to update affiliation for %s in %s", username, muc_jid);
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
307 end
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
308 end
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
309 end
5822
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
310
4424
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
311 module:fire_event(
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
312 "group-user-removed",
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
313 {
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
314 id = group_id,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
315 user = username,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
316 host = host,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
317 group_info = group_info,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
318 }
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
319 )
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 return true;
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
321 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322
4386
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
323 function sync(group_id)
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
324 do_all_group_subscriptions_by_group(group_id);
4386
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
325 end
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
326
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
327 function add_group_chat(group_id, name)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
328 local group_info = group_info_store:get(group_id);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
329 local mucs = group_info.mucs or {};
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
330
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
331 -- Create the MUC
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
332 local muc_jid, room = _create_muc_room(name);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
333 if not muc_jid then return nil, room; end
5821
c9279845fc41 mod_groups_internal: Save MUC room after creation to commit it to storage
Matthew Wild <mwild1@gmail.com>
parents: 5820
diff changeset
334 room:save(); -- This ensures the room is committed to storage
c9279845fc41 mod_groups_internal: Save MUC room after creation to commit it to storage
Matthew Wild <mwild1@gmail.com>
parents: 5820
diff changeset
335
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
336 table.insert(mucs, muc_jid);
5822
fe3bde6ef95a mod_groups_internal: Sync MUC affiliations for multi-MUC groups
Matthew Wild <mwild1@gmail.com>
parents: 5821
diff changeset
337
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
338 if group_info.muc_jid then -- COMPAT include old muc_jid into array
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
339 table.insert(mucs, group_info.muc_jid);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
340 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
341 local store_ok, store_err = group_info_store:set_key(group_id, "mucs", mucs);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
342 if not store_ok then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
343 module:log("error", "Failed to store new MUC association: %s", store_err);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
344 room:destroy();
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
345 return nil, "internal-server-error";
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
346 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
347
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
348 -- COMPAT: clear old muc_jid (it's now in mucs array)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
349 if group_info.muc_jid then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
350 module:log("debug", "Clearing old single-MUC JID");
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
351 group_info.muc_jid = nil;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
352 group_info_store:set_key(group_id, "muc_jid", nil);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
353 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
354
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
355 -- Make existing group members, members of the MUC
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
356 for username in pairs(get_members(group_id)) do
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
357 local user_jid = username .. "@" ..module.host;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
358 room:set_affiliation(true, user_jid, "member");
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
359 module:send(st.message(
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
360 { from = muc_jid, to = user_jid }
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
361 ):tag("x", {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
362 xmlns = "jabber:x:conference",
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
363 jid = muc_jid
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
364 }):up());
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
365 module:log("debug", "set user %s to be member in %s and sent invite", user_jid, muc_jid);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
366 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
367
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
368 -- Notify other modules (such as mod_groups_muc_bookmarks)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
369 local muc = {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
370 jid = muc_jid;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
371 name = name;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
372 };
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
373
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
374 module:fire_event("group-chat-added", {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
375 group_id = group_id;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
376 group_info = group_info;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
377 muc = muc;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
378 });
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
379
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
380 return muc;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
381 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
382
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
383 function remove_group_chat(group_id, muc_id)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
384 local group_info = group_info_store:get(group_id);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
385 if not group_info then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
386 return nil, "group-not-found";
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
387 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
388
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
389 local mucs = group_info.mucs;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
390 if not mucs then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
391 if not group_info.muc_jid then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
392 return true;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
393 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
394 -- COMPAT with old single-MUC groups - upgrade to new format
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
395 mucs = {};
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
396 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
397 if group_info.muc_jid then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
398 table.insert(mucs, group_info.muc_jid);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
399 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
400
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
401 local removed;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
402 for i, muc_jid in ipairs(mucs) do
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
403 if muc_id == jid.node(muc_jid) then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
404 removed = table.remove(mucs, i);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
405 break;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
406 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
407 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
408
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
409 if removed then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
410 if not group_info_store:set_key(group_id, "mucs", mucs) then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
411 return nil, "internal-server-error";
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
412 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
413
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
414 if group_info.muc_jid then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
415 -- COMPAT: Now we've set the array, clean up muc_jid
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
416 group_info.muc_jid = nil;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
417 group_info_store:set_key(group_id, "muc_jid", nil);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
418 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
419
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
420 module:log("debug", "Updated group MUC list");
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
421
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
422 local room = muc_host.get_room_from_jid(removed);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
423 if room then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
424 room:destroy();
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
425 else
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
426 module:log("warn", "Removing a group chat, but associated MUC not found (%s)", removed);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
427 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
428
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
429 module:fire_event(
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
430 "group-chat-removed",
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
431 {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
432 group_id = group_id;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
433 group_info = group_info;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
434 muc = {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
435 id = muc_id;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
436 jid = removed;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
437 };
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
438 }
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
439 );
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
440 else
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
441 module:log("warn", "Removal of a group chat that can't be found - %s", muc_id);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
442 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
443
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
444 return true;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
445 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
446
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
447 function get_group_chats(group_id)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
448 local group_info, err = group_info_store:get(group_id);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
449 if not group_info then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
450 module:log("debug", "Unable to load group info: %s - %s", group_id, err);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
451 return nil;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
452 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
453
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
454 local mucs = group_info.mucs or {};
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
455
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
456 -- COMPAT with single-MUC groups
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
457 if group_info.muc_jid then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
458 table.insert(mucs, group_info.muc_jid);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
459 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
460
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
461 return array.map(mucs, function (muc_jid)
5820
742142f9771e mod_groups_internal: Fix traceback when room doesn't exist
Matthew Wild <mwild1@gmail.com>
parents: 5800
diff changeset
462 local room = muc_host.get_room_from_jid(muc_jid);
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
463 return {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
464 id = jid.node(muc_jid);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
465 jid = muc_jid;
5820
742142f9771e mod_groups_internal: Fix traceback when room doesn't exist
Matthew Wild <mwild1@gmail.com>
parents: 5800
diff changeset
466 name = room and room:get_name() or group_info.name;
5825
865c77b5c6dc mod_groups_internal: Add flag to indicate when a linked MUC has been deleted
Matthew Wild <mwild1@gmail.com>
parents: 5824
diff changeset
467 deleted = not room or room._data.destroyed;
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
468 };
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
469 end);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
470 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
471
4430
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
472 function emit_member_events(group_id)
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
473 local group_info, err = get_info(group_id)
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
474 if group_info == nil then
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
475 return false, err
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
476 end
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
477
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
478 for username in pairs(get_members(group_id)) do
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
479 module:fire_event(
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
480 "group-user-added",
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
481 {
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
482 id = group_id,
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
483 user = username,
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
484 host = host,
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
485 group_info = group_info,
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
486 }
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
487 )
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
488 end
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
489
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
490 return true
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
491 end
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
492
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
493 -- Returns iterator over group ids
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
494 function groups()
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
495 return group_info_store:items();
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
496 end
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
497
4417
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
498 local function setup()
4392
e5792ca1d704 mod_groups_internal: fix default value and handling of groups_muc_host
Jonas Schäfer <jonas@wielicki.name>
parents: 4389
diff changeset
499 if not muc_host_name then
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
500 module:log("info", "MUC management disabled (groups_muc_host set to nil)");
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
501 return;
4392
e5792ca1d704 mod_groups_internal: fix default value and handling of groups_muc_host
Jonas Schäfer <jonas@wielicki.name>
parents: 4389
diff changeset
502 end
e5792ca1d704 mod_groups_internal: fix default value and handling of groups_muc_host
Jonas Schäfer <jonas@wielicki.name>
parents: 4389
diff changeset
503
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
504 local target_module = modulemanager.get_module(muc_host_name, "muc");
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
505 if not target_module then
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
506 module:log("error", "host %s is not a MUC host -- group management will not work correctly; check your groups_muc_host setting!", muc_host_name);
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
507 else
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
508 module:log("debug", "found MUC host at %s", muc_host_name);
4388
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
509 muc_host = target_module;
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
510 end
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
511 end
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
512
4707
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
513 module:hook_global("user-deleted", function(event)
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
514 if event.host ~= module.host then return end
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
515 local username = event.username;
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
516 for group_id in user_groups(username) do
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
517 remove_member(group_id, username);
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
518 end
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
519 end);
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
520
4417
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
521 if prosody.start_time then -- server already started
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
522 setup();
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
523 else
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
524 module:hook_global("server-started", setup);
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
525 end