Comparison

plugins/mod_groups.lua @ 1388:546caa44620c

mod_groups: Support for public groups, and extra logging
author Matthew Wild <mwild1@gmail.com>
date Mon, 22 Jun 2009 22:02:04 +0100
parent 1383:8774c5cbf147
child 1522:569d58d21612
comparison
equal deleted inserted replaced
1387:64f18488b275 1388:546caa44620c
1 1
2 local groups = { default = {} }; 2 local groups = { default = {} };
3 local members = {}; 3 local members = { [false] = {} };
4 4
5 local groups_file; 5 local groups_file;
6 6
7 local jid, datamanager = require "util.jid", require "util.datamanager"; 7 local jid, datamanager = require "util.jid", require "util.datamanager";
8 local jid_bare, jid_prep = jid.bare, jid.prep; 8 local jid_bare, jid_prep = jid.bare, jid.prep;
12 function inject_roster_contacts(username, host, roster) 12 function inject_roster_contacts(username, host, roster)
13 module:log("warn", "Injecting group members to roster"); 13 module:log("warn", "Injecting group members to roster");
14 local bare_jid = username.."@"..host; 14 local bare_jid = username.."@"..host;
15 if not members[bare_jid] then return; end -- Not a member of any groups 15 if not members[bare_jid] then return; end -- Not a member of any groups
16 16
17 -- Find groups this JID is a member of 17 local function import_jids_to_roster(group_name)
18 for _, group_name in ipairs(members[bare_jid]) do
19 -- Find other people in the same group
20 for jid in pairs(groups[group_name]) do 18 for jid in pairs(groups[group_name]) do
21 -- Add them to roster 19 -- Add them to roster
22 --module:log("debug", "processing jid %s in group %s", tostring(jid), tostring(group_name)); 20 --module:log("debug", "processing jid %s in group %s", tostring(jid), tostring(group_name));
23 if jid ~= bare_jid then 21 if jid ~= bare_jid then
24 if not roster[jid] then roster[jid] = {}; end 22 if not roster[jid] then roster[jid] = {}; end
28 end 26 end
29 roster[jid].groups[group_name] = true; 27 roster[jid].groups[group_name] = true;
30 roster[jid].persist = false; 28 roster[jid].persist = false;
31 end 29 end
32 end 30 end
31 end
32
33 -- Find groups this JID is a member of
34 for _, group_name in ipairs(members[bare_jid]) do
35 import_jids_to_roster(group_name);
36 end
37
38 -- Import public groups
39 for _, group_name in ipairs(members[false]) do
40 import_jids_to_roster(group_name);
33 end 41 end
34 end 42 end
35 43
36 function remove_virtual_contacts(username, host, datastore, data) 44 function remove_virtual_contacts(username, host, datastore, data)
37 if host == module_host and datastore == "roster" then 45 if host == module_host and datastore == "roster" then
53 61
54 module:hook("roster-load", inject_roster_contacts); 62 module:hook("roster-load", inject_roster_contacts);
55 datamanager.add_callback(remove_virtual_contacts); 63 datamanager.add_callback(remove_virtual_contacts);
56 64
57 groups = { default = {} }; 65 groups = { default = {} };
58 66 members = { [false] = {} };
59 local curr_group = "default"; 67 local curr_group = "default";
60 for line in io.lines(groups_file) do 68 for line in io.lines(groups_file) do
61 if line:match("^%[%w+%]$") then 69 if line:match("^%s*%[.-%]%s*$") then
62 curr_group = line:match("^%[(%w+)%]$"); 70 curr_group = line:match("^%s*%[(.-)%]%s*$");
71 if curr_group:match("^%+") then
72 curr_group = curr_group:gsub("^%+", "");
73 members[false][#members[false]+1] = curr_group; -- Is a public group
74 end
75 module:log("debug", "New group: %s", tostring(curr_group));
63 groups[curr_group] = groups[curr_group] or {}; 76 groups[curr_group] = groups[curr_group] or {};
64 else 77 else
65 -- Add JID 78 -- Add JID
66 local jid = jid_prep(line); 79 local jid = jid_prep(line);
67 if jid then 80 if jid then
81 module:log("debug", "New member of %s: %s", tostring(curr_group), tostring(jid));
68 groups[curr_group][jid] = true; 82 groups[curr_group][jid] = true;
69 members[jid] = members[jid] or {}; 83 members[jid] = members[jid] or {};
70 members[jid][#members[jid]+1] = curr_group; 84 members[jid][#members[jid]+1] = curr_group;
71 end 85 end
72 end 86 end