Annotate

plugins/mod_groups.lua @ 1383:8774c5cbf147

mod_groups: Experimental shared roster support
author Matthew Wild <mwild1@gmail.com>
date Mon, 22 Jun 2009 14:22:24 +0100
child 1388:546caa44620c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local groups = { default = {} };
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local members = {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local groups_file;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local jid, datamanager = require "util.jid", require "util.datamanager";
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local jid_bare, jid_prep = jid.bare, jid.prep;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local module_host = module:get_host();
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 function inject_roster_contacts(username, host, roster)
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 module:log("warn", "Injecting group members to roster");
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local bare_jid = username.."@"..host;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 if not members[bare_jid] then return; end -- Not a member of any groups
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 -- Find groups this JID is a member of
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 for _, group_name in ipairs(members[bare_jid]) do
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 -- Find other people in the same group
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 for jid in pairs(groups[group_name]) do
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 -- Add them to roster
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 --module:log("debug", "processing jid %s in group %s", tostring(jid), tostring(group_name));
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if jid ~= bare_jid then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 if not roster[jid] then roster[jid] = {}; end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 roster[jid].subscription = "both";
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if not roster[jid].groups then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 roster[jid].groups = { [group_name] = true };
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 roster[jid].groups[group_name] = true;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 roster[jid].persist = false;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 function remove_virtual_contacts(username, host, datastore, data)
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 if host == module_host and datastore == "roster" then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local new_roster = {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 for jid, contact in pairs(data) do
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if contact.persist ~= false then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 new_roster[jid] = contact;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return username, host, datastore, new_roster;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 return username, host, datastore, data;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 function module.load()
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 groups_file = config.get(module:get_host(), "core", "groups_file");
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 if not groups_file then return; end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 module:hook("roster-load", inject_roster_contacts);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 datamanager.add_callback(remove_virtual_contacts);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 groups = { default = {} };
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local curr_group = "default";
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 for line in io.lines(groups_file) do
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 if line:match("^%[%w+%]$") then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 curr_group = line:match("^%[(%w+)%]$");
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 groups[curr_group] = groups[curr_group] or {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 else
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 -- Add JID
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 local jid = jid_prep(line);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if jid then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 groups[curr_group][jid] = true;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 members[jid] = members[jid] or {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 members[jid][#members[jid]+1] = curr_group;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 module:log("info", "Groups loaded successfully");
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 function module.unload()
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 datamanager.remove_callback(remove_virtual_contacts);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end