Software /
code /
prosody-modules
Annotate
mod_groups_internal/mod_groups_internal.lua @ 5708:9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
This was a feature request for Snikket.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 02 Nov 2023 16:59:44 +0000 |
parent | 4707:27f7ed9f50cd |
child | 5818:5533c577dd02 |
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"; |
5708
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 |
5708
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. |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 local function subscribe(user, user_jid, contact, contact_jid) |
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 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 -- 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
|
32 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
|
33 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
|
34 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 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
|
37 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
|
38 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 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
|
41 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
|
42 if not members then return; end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 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
|
44 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
|
45 if membername ~= username then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 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
|
47 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
|
48 module:log("debug", "[group %s] Subscribing %s to %s", member_jid, user_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 subscribe(membername, member_jid, username, user_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 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
|
52 module:log("debug", "[group %s] Subscribing %s to %s", user_jid, member_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 subscribe(username, user_jid, membername, member_jid); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 end |
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 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 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
|
60 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
|
61 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
|
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_group(group_id) |
4419
1185acb2ab91
mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents:
4418
diff
changeset
|
66 local members = get_members(group_id) |
1185acb2ab91
mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents:
4418
diff
changeset
|
67 if not members then |
1185acb2ab91
mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents:
4418
diff
changeset
|
68 return |
1185acb2ab91
mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents:
4418
diff
changeset
|
69 end |
1185acb2ab91
mod_groups_internal: handle empty groups correctly
Jonas Schäfer <jonas@wielicki.name>
parents:
4418
diff
changeset
|
70 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
|
71 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
|
72 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 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
|
76 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
|
77 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
|
78 end); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 |
5708
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
80 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
|
81 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
|
82 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
|
83 return nil, "service-unavailable" |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
84 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
89 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
90 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
|
91 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
|
92 if not room then |
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 ok = pcall(function () |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
97 room:set_public(false); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
98 room:set_persistent(true); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
99 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
|
100 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
|
101 room:set_moderated(false); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
102 room:set_whois("anyone"); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
103 room:set_name(name); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
104 end); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
105 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
106 if not ok then |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
107 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
|
108 room:destroy(); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
109 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
|
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 return muc_jid, room; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
113 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
114 |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 --luacheck: ignore 131 |
5708
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 end |
4385
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
120 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
|
121 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
|
122 return nil, "conflict" |
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
123 end |
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
124 else |
dfb34cc97028
mod_groups_internal: allow specifying a group_id on create
Jonas Schäfer <jonas@wielicki.name>
parents:
4383
diff
changeset
|
125 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
|
126 end |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 |
4388
7de3c955cfe2
mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents:
4386
diff
changeset
|
128 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
|
129 local room = nil |
5708
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 -- 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
|
134 delete(group_id) |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
135 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
|
136 end |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 }); |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 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
|
144 if room then |
4423
ca821df93cb9
mod_groups_internal: correctly destroy MUC associated with group
Jonas Schäfer <jonas@wielicki.name>
parents:
4419
diff
changeset
|
145 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
|
146 end |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 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
|
148 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
|
149 |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 return group_id; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 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
|
154 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
|
155 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 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
|
158 if not info then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 return nil, "bad-request" |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 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
|
163 return nil, "bad-request" |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 |
4415
c7424b96c75e
mod_groups_internal: maintain the name of the associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4407
diff
changeset
|
166 -- 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
|
167 -- an existing group. |
c7424b96c75e
mod_groups_internal: maintain the name of the associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4407
diff
changeset
|
168 if info.muc_jid then |
4706
428861d1d1e4
mod_groups_internal: Fix accidental global [luacheck]
Kim Alvefur <zash@zash.se>
parents:
4430
diff
changeset
|
169 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
|
170 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
|
171 end |
c7424b96c75e
mod_groups_internal: maintain the name of the associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4407
diff
changeset
|
172 |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 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
|
174 if not ok then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 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
|
176 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 return true |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 function get_members(group_id) |
5708
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
181 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
|
182 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 function exists(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 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
|
186 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 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
|
189 local groups = {}; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 do |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 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
|
192 if group_set then |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 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
|
194 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
|
195 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 return groups; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 function delete(group_id) |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 if group_members_store:set(group_id, nil) then |
4407
105586ca9a79
mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents:
4406
diff
changeset
|
203 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
|
204 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
|
205 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
|
206 if room then |
ca821df93cb9
mod_groups_internal: correctly destroy MUC associated with group
Jonas Schäfer <jonas@wielicki.name>
parents:
4419
diff
changeset
|
207 room:destroy() |
ca821df93cb9
mod_groups_internal: correctly destroy MUC associated with group
Jonas Schäfer <jonas@wielicki.name>
parents:
4419
diff
changeset
|
208 end |
4389
6cfa313cd524
mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4388
diff
changeset
|
209 end |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 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
|
211 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 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
|
213 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 |
4386
6357ac65b4eb
mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents:
4385
diff
changeset
|
215 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
|
216 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
|
217 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
|
218 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
|
219 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 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
|
221 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
|
222 end |
4389
6cfa313cd524
mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4388
diff
changeset
|
223 if group_info.muc_jid then |
6cfa313cd524
mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4388
diff
changeset
|
224 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
|
225 if room then |
6cfa313cd524
mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4388
diff
changeset
|
226 local user_jid = username .. "@" .. host; |
4407
105586ca9a79
mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents:
4406
diff
changeset
|
227 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
|
228 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
|
229 { 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
|
230 ):tag("x", { |
846b7af5588c
mod_groups_internal: send invite to new members of groups
Jonas Schäfer <jonas@wielicki.name>
parents:
4392
diff
changeset
|
231 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
|
232 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
|
233 }):up()); |
4407
105586ca9a79
mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents:
4406
diff
changeset
|
234 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
|
235 else |
4418
8c31d4b872c3
mod_groups_internal: fix log message not appearing
Jonas Schäfer <jonas@wielicki.name>
parents:
4417
diff
changeset
|
236 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
|
237 end |
6cfa313cd524
mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4388
diff
changeset
|
238 end |
4424
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
239 module:fire_event( |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
240 "group-user-added", |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
241 { |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
242 id = group_id, |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
243 user = username, |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
244 host = host, |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
245 group_info = group_info, |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
246 } |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
247 ) |
4386
6357ac65b4eb
mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents:
4385
diff
changeset
|
248 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
|
249 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
|
250 end |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 return true; |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 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
|
255 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
|
256 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
|
257 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
|
258 end |
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
259 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
|
260 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
|
261 end |
4389
6cfa313cd524
mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4388
diff
changeset
|
262 if group_info.muc_jid then |
6cfa313cd524
mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4388
diff
changeset
|
263 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
|
264 if room then |
6cfa313cd524
mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4388
diff
changeset
|
265 local user_jid = username .. "@" .. host; |
4407
105586ca9a79
mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents:
4406
diff
changeset
|
266 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
|
267 else |
4418
8c31d4b872c3
mod_groups_internal: fix log message not appearing
Jonas Schäfer <jonas@wielicki.name>
parents:
4417
diff
changeset
|
268 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
|
269 end |
6cfa313cd524
mod_groups_internal: manage associated MUC
Jonas Schäfer <jonas@wielicki.name>
parents:
4388
diff
changeset
|
270 end |
4424
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
271 module:fire_event( |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
272 "group-user-removed", |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
273 { |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
274 id = group_id, |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
275 user = username, |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
276 host = host, |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
277 group_info = group_info, |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
278 } |
679be18e6a5e
mod_groups_internal: emit events for group membership changes
Jonas Schäfer <jonas@wielicki.name>
parents:
4423
diff
changeset
|
279 ) |
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 |
4386
6357ac65b4eb
mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents:
4385
diff
changeset
|
283 function sync(group_id) |
4407
105586ca9a79
mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents:
4406
diff
changeset
|
284 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
|
285 end |
6357ac65b4eb
mod_groups_internal: allow delay of roster sync on group change
Jonas Schäfer <jonas@wielicki.name>
parents:
4385
diff
changeset
|
286 |
5708
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
287 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
|
288 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
|
289 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
|
290 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
291 -- Create the MUC |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
292 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
|
293 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
|
294 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
|
295 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
|
296 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
|
297 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
298 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
|
299 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
|
300 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
|
301 room:destroy(); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
302 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
|
303 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
304 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
305 -- 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
|
306 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
|
307 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
|
308 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
|
309 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
|
310 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
311 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
312 -- 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
|
313 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
|
314 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
|
315 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
|
316 module:send(st.message( |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
317 { 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
|
318 ):tag("x", { |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
319 xmlns = "jabber:x:conference", |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
320 jid = muc_jid |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
321 }):up()); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
322 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
|
323 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
324 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
325 -- 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
|
326 local muc = { |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
327 jid = muc_jid; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
328 name = name; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
329 }; |
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 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
|
332 group_id = group_id; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
333 group_info = group_info; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
334 muc = muc; |
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 return muc; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
338 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
339 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
340 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
|
341 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
|
342 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
|
343 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
|
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 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
|
347 if not mucs then |
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.muc_jid then |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
349 return true; |
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 -- 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
|
352 mucs = {}; |
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 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
|
355 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
|
356 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
357 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
358 local removed; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
359 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
|
360 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
|
361 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
|
362 break; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
363 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
364 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
365 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
366 if removed then |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
367 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
|
368 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
|
369 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
370 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
371 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
|
372 -- 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
|
373 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
|
374 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
|
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 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
|
378 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
379 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
|
380 if room then |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
381 room:destroy(); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
382 else |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
383 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
|
384 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
385 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
386 module:fire_event( |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
387 "group-chat-removed", |
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 group_id = group_id; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
390 group_info = group_info; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
391 muc = { |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
392 id = muc_id; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
393 jid = 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 } |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
396 ); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
397 else |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
398 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
|
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 return true; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
402 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
403 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
404 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
|
405 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
|
406 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
|
407 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
|
408 return nil; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
409 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
410 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
411 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
|
412 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
413 -- 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
|
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 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
|
416 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
417 |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
418 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
|
419 return { |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
420 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
|
421 jid = muc_jid; |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
422 name = muc_host.get_room_from_jid(muc_jid):get_name(); |
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 end); |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
425 end |
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
426 |
4430
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
427 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
|
428 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
|
429 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
|
430 return false, err |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
431 end |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
432 |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
433 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
|
434 module:fire_event( |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
435 "group-user-added", |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
436 { |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
437 id = group_id, |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
438 user = username, |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
439 host = host, |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
440 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
|
441 } |
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 end |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
444 |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
445 return true |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
446 end |
71c495fa03f3
mod_groups_internal: allow force-emitting group member addition events
Jonas Schäfer <jonas@wielicki.name>
parents:
4424
diff
changeset
|
447 |
4383
1e7406b85add
mod_groups_internal: new module for grouping beyond mod_adhoc_groups
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
448 -- 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
|
449 function groups() |
5708
9edc698848e9
mod_groups_internal: Update to support multiple MUCs per group
Matthew Wild <mwild1@gmail.com>
parents:
4707
diff
changeset
|
450 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
|
451 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
|
452 |
4417
7e379ccb8ed6
mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents:
4415
diff
changeset
|
453 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
|
454 if not muc_host_name then |
4407
105586ca9a79
mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents:
4406
diff
changeset
|
455 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
|
456 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
|
457 end |
e5792ca1d704
mod_groups_internal: fix default value and handling of groups_muc_host
Jonas Schäfer <jonas@wielicki.name>
parents:
4389
diff
changeset
|
458 |
4407
105586ca9a79
mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents:
4406
diff
changeset
|
459 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
|
460 if not target_module then |
4407
105586ca9a79
mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents:
4406
diff
changeset
|
461 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
|
462 else |
4407
105586ca9a79
mod_groups_internal: Style
Matthew Wild <mwild1@gmail.com>
parents:
4406
diff
changeset
|
463 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
|
464 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
|
465 end |
7de3c955cfe2
mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents:
4386
diff
changeset
|
466 end |
7de3c955cfe2
mod_groups_internal: allow creating MUCs if a MUC host is linked
Jonas Schäfer <jonas@wielicki.name>
parents:
4386
diff
changeset
|
467 |
4707
27f7ed9f50cd
mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents:
4706
diff
changeset
|
468 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
|
469 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
|
470 local username = event.username; |
27f7ed9f50cd
mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents:
4706
diff
changeset
|
471 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
|
472 remove_member(group_id, username); |
27f7ed9f50cd
mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents:
4706
diff
changeset
|
473 end |
27f7ed9f50cd
mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents:
4706
diff
changeset
|
474 end); |
27f7ed9f50cd
mod_groups_internal: Remove deleted user from groups
Kim Alvefur <zash@zash.se>
parents:
4706
diff
changeset
|
475 |
4417
7e379ccb8ed6
mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents:
4415
diff
changeset
|
476 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
|
477 setup(); |
7e379ccb8ed6
mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents:
4415
diff
changeset
|
478 else |
7e379ccb8ed6
mod_groups_internal: make robust against module reloads
Jonas Schäfer <jonas@wielicki.name>
parents:
4415
diff
changeset
|
479 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
|
480 end |