Annotate

plugins/mod_groups.lua @ 2053:f5a198127dd3

MUC: Fixed: affiliation='none' was omitted from some presence broadcasts.
author Waqas Hussain <waqas20@gmail.com>
date Sun, 25 Oct 2009 21:07:32 +0500
parent 1522:569d58d21612
child 2910:6706a02df271
child 2923:b7049746bd29
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
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1388
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1388
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
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
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local groups = { default = {} };
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
11 local members = { [false] = {} };
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)
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 module:log("warn", "Injecting group members to roster");
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local bare_jid = username.."@"..host;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 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
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
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
42 for _, group_name in ipairs(members[bare_jid]) do
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
43 import_jids_to_roster(group_name);
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
44 end
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
45
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
46 -- Import public groups
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
47 for _, group_name in ipairs(members[false]) do
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
48 import_jids_to_roster(group_name);
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
49 end
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 function remove_virtual_contacts(username, host, datastore, data)
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 if host == module_host and datastore == "roster" then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local new_roster = {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 for jid, contact in pairs(data) do
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 if contact.persist ~= false then
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 new_roster[jid] = contact;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return username, host, datastore, new_roster;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return username, host, datastore, data;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 function module.load()
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 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
68 if not groups_file then return; end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 module:hook("roster-load", inject_roster_contacts);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 datamanager.add_callback(remove_virtual_contacts);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 groups = { default = {} };
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
74 members = { [false] = {} };
1383
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local curr_group = "default";
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 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
77 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
78 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
79 if curr_group:match("^%+") then
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
80 curr_group = curr_group:gsub("^%+", "");
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
81 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
82 end
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
83 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
84 groups[curr_group] = groups[curr_group] or {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 else
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 -- Add JID
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 local jid = jid_prep(line);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if jid then
1388
546caa44620c mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents: 1383
diff changeset
89 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
90 groups[curr_group][jid] = true;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 members[jid] = members[jid] or {};
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 members[jid][#members[jid]+1] = curr_group;
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 module:log("info", "Groups loaded successfully");
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 function module.unload()
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 datamanager.remove_callback(remove_virtual_contacts);
8774c5cbf147 mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end