Annotate

plugins/mod_groups.lua @ 3072:6f68b1700e02

Merge 0.7->trunk
author Matthew Wild <mwild1@gmail.com>
date Fri, 21 May 2010 19:28:56 +0100
parent 3054:05b45b1281aa
child 3125:f2d1079a758d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1388
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1388
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1388
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1388
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1388
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1388
diff changeset
8
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
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)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
10 local groups;
6706a02df271 mod_groups: Only create group lists when necessary (now we can reliably detect when a user is in /any/ group, including public ones)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
11 local members;
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local groups_file;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local jid, datamanager = require "util.jid", require "util.datamanager";
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local jid_bare, jid_prep = jid.bare, jid.prep;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local module_host = module:get_host();
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 function inject_roster_contacts(username, host, roster)
3054
05b45b1281aa mod_groups: Remove potentially verbose per-contact logging (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
21 --module:log("debug", "Injecting group members to roster");
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local bare_jid = username.."@"..host;
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)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
23 if not members[bare_jid] and not members[false] then return; end -- Not a member of any groups
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
25 local function import_jids_to_roster(group_name)
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 for jid in pairs(groups[group_name]) do
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 -- Add them to roster
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 --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
29 if jid ~= bare_jid then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 if not roster[jid] then roster[jid] = {}; end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 roster[jid].subscription = "both";
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if not roster[jid].groups then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 roster[jid].groups = { [group_name] = true };
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 roster[jid].groups[group_name] = true;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 roster[jid].persist = false;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
40
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
41 -- Find groups this JID is a member of
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)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
42 if members[bare_jid] then
6706a02df271 mod_groups: Only create group lists when necessary (now we can reliably detect when a user is in /any/ group, including public ones)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
43 for _, group_name in ipairs(members[bare_jid]) do
3054
05b45b1281aa mod_groups: Remove potentially verbose per-contact logging (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
44 --module:log("debug", "Importing group %s", group_name);
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)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
45 import_jids_to_roster(group_name);
6706a02df271 mod_groups: Only create group lists when necessary (now we can reliably detect when a user is in /any/ group, including public ones)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
46 end
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
47 end
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
48
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
49 -- Import public groups
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)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
50 if members[false] then
6706a02df271 mod_groups: Only create group lists when necessary (now we can reliably detect when a user is in /any/ group, including public ones)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
51 for _, group_name in ipairs(members[false]) do
3054
05b45b1281aa mod_groups: Remove potentially verbose per-contact logging (thanks Flo)
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
52 --module:log("debug", "Importing group %s", group_name);
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)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
53 import_jids_to_roster(group_name);
6706a02df271 mod_groups: Only create group lists when necessary (now we can reliably detect when a user is in /any/ group, including public ones)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
54 end
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
55 end
2912
f5a5317f3485 mod_groups: Fixes to make compatible with roster versioning - set version to 'true' to indicate that the roster is not being versioned
Matthew Wild <mwild1@gmail.com>
parents: 2911
diff changeset
56
f5a5317f3485 mod_groups: Fixes to make compatible with roster versioning - set version to 'true' to indicate that the roster is not being versioned
Matthew Wild <mwild1@gmail.com>
parents: 2911
diff changeset
57 if roster[false] then
f5a5317f3485 mod_groups: Fixes to make compatible with roster versioning - set version to 'true' to indicate that the roster is not being versioned
Matthew Wild <mwild1@gmail.com>
parents: 2911
diff changeset
58 roster[false].version = true;
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
59 end
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 function remove_virtual_contacts(username, host, datastore, data)
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 if host == module_host and datastore == "roster" then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local new_roster = {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 for jid, contact in pairs(data) do
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 if contact.persist ~= false then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 new_roster[jid] = contact;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
2912
f5a5317f3485 mod_groups: Fixes to make compatible with roster versioning - set version to 'true' to indicate that the roster is not being versioned
Matthew Wild <mwild1@gmail.com>
parents: 2911
diff changeset
70 new_roster[false].version = nil; -- Version is void
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 return username, host, datastore, new_roster;
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
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return username, host, datastore, data;
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.load()
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 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
79 if not groups_file then return; end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 module:hook("roster-load", inject_roster_contacts);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 datamanager.add_callback(remove_virtual_contacts);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 groups = { default = {} };
2911
30895e419e92 mod_groups: Missed hunk from last commit, don't create the list of public group members unless necessary
Matthew Wild <mwild1@gmail.com>
parents: 2910
diff changeset
85 members = { };
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 local curr_group = "default";
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 for line in io.lines(groups_file) do
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
88 if line:match("^%s*%[.-%]%s*$") then
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
89 curr_group = line:match("^%s*%[(.-)%]%s*$");
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
90 if curr_group:match("^%+") then
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
91 curr_group = curr_group:gsub("^%+", "");
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)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
92 if not members[false] then
6706a02df271 mod_groups: Only create group lists when necessary (now we can reliably detect when a user is in /any/ group, including public ones)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
93 members[false] = {};
6706a02df271 mod_groups: Only create group lists when necessary (now we can reliably detect when a user is in /any/ group, including public ones)
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
94 end
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
95 members[false][#members[false]+1] = curr_group; -- Is a public group
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
96 end
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
97 module:log("debug", "New group: %s", tostring(curr_group));
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 groups[curr_group] = groups[curr_group] or {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 else
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 -- Add JID
2913
3d4e814cadfa mod_groups: Ignore whitespace on group member JID lines (thanks Luis!)
Matthew Wild <mwild1@gmail.com>
parents: 2912
diff changeset
101 local jid = jid_prep(line:match("%S+"));
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 if jid then
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
103 module:log("debug", "New member of %s: %s", tostring(curr_group), tostring(jid));
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 groups[curr_group][jid] = true;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 members[jid] = members[jid] or {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 members[jid][#members[jid]+1] = curr_group;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 module:log("info", "Groups loaded successfully");
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 function module.unload()
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 datamanager.remove_callback(remove_virtual_contacts);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end