annotate mod_groups_internal/mod_groups_internal.lua @ 5813:d38772479891

mod_pubsub_serverinfo: Some logging improvements
author Matthew Wild <mwild1@gmail.com>
date Mon, 08 Jan 2024 15:51:52 +0000
parents 8566a423da88
children 742142f9771e
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
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
229 if group_info.muc_jid then
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
230 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
231 if room then
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
232 local user_jid = username .. "@" .. host;
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
233 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
234 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
235 { 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
236 ):tag("x", {
846b7af5588c mod_groups_internal: send invite to new members of groups
Jonas Schäfer <jonas@wielicki.name>
parents: 4392
diff changeset
237 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
238 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
239 }):up());
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
240 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
241 else
4418
8c31d4b872c3 mod_groups_internal: fix log message not appearing
Jonas Schäfer <jonas@wielicki.name>
parents: 4417
diff changeset
242 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
243 end
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
244 end
4424
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
245 module:fire_event(
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
246 "group-user-added",
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
247 {
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
248 id = group_id,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
249 user = username,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
250 host = host,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
251 group_info = group_info,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
252 }
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
253 )
4386
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
254 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
255 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
256 end
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 return true;
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 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
261 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
262 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
263 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
264 end
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 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
266 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
267 end
4389
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
268 if group_info.muc_jid then
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
269 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
270 if room then
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
271 local user_jid = username .. "@" .. host;
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
272 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
273 else
4418
8c31d4b872c3 mod_groups_internal: fix log message not appearing
Jonas Schäfer <jonas@wielicki.name>
parents: 4417
diff changeset
274 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
275 end
6cfa313cd524 mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents: 4388
diff changeset
276 end
4424
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
277 module:fire_event(
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
278 "group-user-removed",
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
279 {
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
280 id = group_id,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
281 user = username,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
282 host = host,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
283 group_info = group_info,
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
284 }
679be18e6a5e mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents: 4423
diff changeset
285 )
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 return true;
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
4386
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
289 function sync(group_id)
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
290 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
291 end
6357ac65b4eb mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents: 4385
diff changeset
292
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
293 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
294 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
295 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
296
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
297 -- Create the MUC
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
298 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
299 if not muc_jid then return nil, room; end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
300 table.insert(mucs, muc_jid);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
301 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
302 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
303 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
304 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
305 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
306 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
307 room:destroy();
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
308 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
309 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
310
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
311 -- 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
312 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
313 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
314 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
315 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
316 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
317
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
318 -- 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
319 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
320 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
321 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
322 module:send(st.message(
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
323 { 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
324 ):tag("x", {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
325 xmlns = "jabber:x:conference",
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
326 jid = muc_jid
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
327 }):up());
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
328 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
329 end
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 -- 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
332 local muc = {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
333 jid = muc_jid;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
334 name = name;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
335 };
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
336
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
337 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
338 group_id = group_id;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
339 group_info = group_info;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
340 muc = muc;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
341 });
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
342
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
343 return muc;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
344 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
345
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
346 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
347 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
348 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
349 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
350 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
351
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
352 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
353 if not mucs then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
354 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
355 return true;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
356 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
357 -- 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
358 mucs = {};
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
359 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
360 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
361 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
362 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
363
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
364 local removed;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
365 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
366 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
367 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
368 break;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
369 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
370 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
371
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
372 if removed then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
373 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
374 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
375 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
376
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
377 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
378 -- 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
379 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
380 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
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 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
384
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
385 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
386 if room then
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
387 room:destroy();
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
388 else
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
389 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
390 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
391
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
392 module:fire_event(
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
393 "group-chat-removed",
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
394 {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
395 group_id = group_id;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
396 group_info = group_info;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
397 muc = {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
398 id = muc_id;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
399 jid = removed;
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 }
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
402 );
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
403 else
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
404 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
405 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
406
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
407 return true;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
408 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
409
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
410 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
411 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
412 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
413 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
414 return nil;
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
415 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
416
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
417 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
418
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
419 -- 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
420 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
421 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
422 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
423
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
424 return array.map(mucs, function (muc_jid)
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
425 return {
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
426 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
427 jid = muc_jid;
5795
5533c577dd02 mod_groups_internal: Return group name instead of MUC name if MUC has no name
Matthew Wild <mwild1@gmail.com>
parents: 5685
diff changeset
428 name = muc_host.get_room_from_jid(muc_jid):get_name() or group_info.name;
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
429 };
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
430 end);
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
431 end
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
432
4430
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
433 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
434 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
435 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
436 return false, err
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
437 end
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
438
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
439 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
440 module:fire_event(
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
441 "group-user-added",
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
442 {
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
443 id = group_id,
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
444 user = username,
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
445 host = host,
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
446 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
447 }
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
448 )
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
449 end
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
450
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
451 return true
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
452 end
71c495fa03f3 mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents: 4424
diff changeset
453
4383
1e7406b85add mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
454 -- 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
455 function groups()
5685
9edc698848e9 mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents: 4707
diff changeset
456 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
457 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
458
4417
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
459 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
460 if not muc_host_name then
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
461 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
462 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
463 end
e5792ca1d704 mod_groups_internal: fix default value and handling of groups_muc_host
Jonas Schäfer <jonas@wielicki.name>
parents: 4389
diff changeset
464
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
465 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
466 if not target_module then
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
467 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
468 else
4407
105586ca9a79 mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents: 4406
diff changeset
469 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
470 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
471 end
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
472 end
7de3c955cfe2 mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents: 4386
diff changeset
473
4707
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
474 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
475 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
476 local username = event.username;
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
477 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
478 remove_member(group_id, username);
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
479 end
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
480 end);
27f7ed9f50cd mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents: 4706
diff changeset
481
4417
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
482 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
483 setup();
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
484 else
7e379ccb8ed6 mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents: 4415
diff changeset
485 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
486 end