Merge 0.9->trunk
[prosody.git] / plugins / mod_groups.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local groups;
11 local members;
12
13 local groups_file;
14
15 local jid, datamanager = require "util.jid", require "util.datamanager";
16 local jid_prep = jid.prep;
17
18 local module_host = module:get_host();
19
20 function inject_roster_contacts(event)
21         local username, host= event.username, event.host;
22         --module:log("debug", "Injecting group members to roster");
23         local bare_jid = username.."@"..host;
24         if not members[bare_jid] and not members[false] then return; end -- Not a member of any groups
25
26         local roster = event.roster;
27         local function import_jids_to_roster(group_name)
28                 for jid in pairs(groups[group_name]) do
29                         -- Add them to roster
30                         --module:log("debug", "processing jid %s in group %s", tostring(jid), tostring(group_name));
31                         if jid ~= bare_jid then
32                                 if not roster[jid] then roster[jid] = {}; end
33                                 roster[jid].subscription = "both";
34                                 if groups[group_name][jid] then
35                                         roster[jid].name = groups[group_name][jid];
36                                 end
37                                 if not roster[jid].groups then
38                                         roster[jid].groups = { [group_name] = true };
39                                 end
40                                 roster[jid].groups[group_name] = true;
41                                 roster[jid].persist = false;
42                         end
43                 end
44         end
45
46         -- Find groups this JID is a member of
47         if members[bare_jid] then
48                 for _, group_name in ipairs(members[bare_jid]) do
49                         --module:log("debug", "Importing group %s", group_name);
50                         import_jids_to_roster(group_name);
51                 end
52         end
53
54         -- Import public groups
55         if members[false] then
56                 for _, group_name in ipairs(members[false]) do
57                         --module:log("debug", "Importing group %s", group_name);
58                         import_jids_to_roster(group_name);
59                 end
60         end
61
62         if roster[false] then
63                 roster[false].version = true;
64         end
65 end
66
67 function remove_virtual_contacts(username, host, datastore, data)
68         if host == module_host and datastore == "roster" then
69                 local new_roster = {};
70                 for jid, contact in pairs(data) do
71                         if contact.persist ~= false then
72                                 new_roster[jid] = contact;
73                         end
74                 end
75                 if new_roster[false] then
76                         new_roster[false].version = nil; -- Version is void
77                 end
78                 return username, host, datastore, new_roster;
79         end
80
81         return username, host, datastore, data;
82 end
83
84 function module.load()
85         groups_file = module:get_option_string("groups_file");
86         if not groups_file then return; end
87
88         module:hook("roster-load", inject_roster_contacts);
89         datamanager.add_callback(remove_virtual_contacts);
90
91         groups = { default = {} };
92         members = { };
93         local curr_group = "default";
94         for line in io.lines(groups_file) do
95                 if line:match("^%s*%[.-%]%s*$") then
96                         curr_group = line:match("^%s*%[(.-)%]%s*$");
97                         if curr_group:match("^%+") then
98                                 curr_group = curr_group:gsub("^%+", "");
99                                 if not members[false] then
100                                         members[false] = {};
101                                 end
102                                 members[false][#members[false]+1] = curr_group; -- Is a public group
103                         end
104                         module:log("debug", "New group: %s", tostring(curr_group));
105                         groups[curr_group] = groups[curr_group] or {};
106                 else
107                         -- Add JID
108                         local entryjid, name = line:match("([^=]*)=?(.*)");
109                         module:log("debug", "entryjid = '%s', name = '%s'", entryjid, name);
110                         local jid;
111                         jid = jid_prep(entryjid:match("%S+"));
112                         if jid then
113                                 module:log("debug", "New member of %s: %s", tostring(curr_group), tostring(jid));
114                                 groups[curr_group][jid] = name or false;
115                                 members[jid] = members[jid] or {};
116                                 members[jid][#members[jid]+1] = curr_group;
117                         end
118                 end
119         end
120         module:log("info", "Groups loaded successfully");
121 end
122
123 function module.unload()
124         datamanager.remove_callback(remove_virtual_contacts);
125 end
126
127 -- Public for other modules to access
128 function group_contains(group_name, jid)
129         return groups[group_name][jid];
130 end