Comparison

plugins/mod_groups.lua @ 2910:6706a02df271

mod_groups: Only create group lists when necessary (now we can reliably detect when a user is in /any/ group, including public ones)
author Matthew Wild <mwild1@gmail.com>
date Mon, 22 Mar 2010 14:32:23 +0000
parent 1522:569d58d21612
child 2911:30895e419e92
comparison
equal deleted inserted replaced
2909:bcfd76cc9b7d 2910:6706a02df271
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 9
10 local groups = { default = {} }; 10 local groups;
11 local members = { [false] = {} }; 11 local members;
12 12
13 local groups_file; 13 local groups_file;
14 14
15 local jid, datamanager = require "util.jid", require "util.datamanager"; 15 local jid, datamanager = require "util.jid", require "util.datamanager";
16 local jid_bare, jid_prep = jid.bare, jid.prep; 16 local jid_bare, jid_prep = jid.bare, jid.prep;
18 local module_host = module:get_host(); 18 local module_host = module:get_host();
19 19
20 function inject_roster_contacts(username, host, roster) 20 function inject_roster_contacts(username, host, roster)
21 module:log("warn", "Injecting group members to roster"); 21 module:log("warn", "Injecting group members to roster");
22 local bare_jid = username.."@"..host; 22 local bare_jid = username.."@"..host;
23 if not members[bare_jid] then return; end -- Not a member of any groups 23 if not members[bare_jid] and not members[false] then return; end -- Not a member of any groups
24 24
25 local function import_jids_to_roster(group_name) 25 local function import_jids_to_roster(group_name)
26 for jid in pairs(groups[group_name]) do 26 for jid in pairs(groups[group_name]) do
27 -- Add them to roster 27 -- Add them to roster
28 --module:log("debug", "processing jid %s in group %s", tostring(jid), tostring(group_name)); 28 --module:log("debug", "processing jid %s in group %s", tostring(jid), tostring(group_name));
37 end 37 end
38 end 38 end
39 end 39 end
40 40
41 -- Find groups this JID is a member of 41 -- Find groups this JID is a member of
42 for _, group_name in ipairs(members[bare_jid]) do 42 if members[bare_jid] then
43 import_jids_to_roster(group_name); 43 for _, group_name in ipairs(members[bare_jid]) do
44 module:log("debug", "Importing group %s", group_name);
45 import_jids_to_roster(group_name);
46 end
44 end 47 end
45 48
46 -- Import public groups 49 -- Import public groups
47 for _, group_name in ipairs(members[false]) do 50 if members[false] then
48 import_jids_to_roster(group_name); 51 for _, group_name in ipairs(members[false]) do
52 module:log("debug", "Importing group %s", group_name);
53 import_jids_to_roster(group_name);
54 end
49 end 55 end
50 end 56 end
51 57
52 function remove_virtual_contacts(username, host, datastore, data) 58 function remove_virtual_contacts(username, host, datastore, data)
53 if host == module_host and datastore == "roster" then 59 if host == module_host and datastore == "roster" then
76 for line in io.lines(groups_file) do 82 for line in io.lines(groups_file) do
77 if line:match("^%s*%[.-%]%s*$") then 83 if line:match("^%s*%[.-%]%s*$") then
78 curr_group = line:match("^%s*%[(.-)%]%s*$"); 84 curr_group = line:match("^%s*%[(.-)%]%s*$");
79 if curr_group:match("^%+") then 85 if curr_group:match("^%+") then
80 curr_group = curr_group:gsub("^%+", ""); 86 curr_group = curr_group:gsub("^%+", "");
87 if not members[false] then
88 members[false] = {};
89 end
81 members[false][#members[false]+1] = curr_group; -- Is a public group 90 members[false][#members[false]+1] = curr_group; -- Is a public group
82 end 91 end
83 module:log("debug", "New group: %s", tostring(curr_group)); 92 module:log("debug", "New group: %s", tostring(curr_group));
84 groups[curr_group] = groups[curr_group] or {}; 93 groups[curr_group] = groups[curr_group] or {};
85 else 94 else