2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
13 local datamanager
= require
"util.datamanager";
14 local jid_prep
= require
"util.jid".prep
;
16 local module_host
= module
:get_host();
18 function inject_roster_contacts(event
)
19 local username
, host
= event
.username
, event
.host
;
20 --module:log("debug", "Injecting group members to roster");
21 local bare_jid
= username
.."@"..host
;
22 if not members
[bare_jid
] and not members
[false] then return; end -- Not a member of any groups
24 local roster
= event
.roster
;
25 local function import_jids_to_roster(group_name
)
26 for jid
in pairs(groups
[group_name
]) do
28 --module:log("debug", "processing jid %s in group %s", tostring(jid), tostring(group_name));
29 if jid
~= bare_jid
then
30 if not roster
[jid
] then roster
[jid
] = {}; end
31 roster
[jid
].subscription
= "both";
32 if groups
[group_name
][jid
] then
33 roster
[jid
].name
= groups
[group_name
][jid
];
35 if not roster
[jid
].groups
then
36 roster
[jid
].groups
= { [group_name
] = true };
38 roster
[jid
].groups
[group_name
] = true;
39 roster
[jid
].persist
= false;
44 -- Find groups this JID is a member of
45 if members
[bare_jid
] then
46 for _
, group_name
in ipairs(members
[bare_jid
]) do
47 --module:log("debug", "Importing group %s", group_name);
48 import_jids_to_roster(group_name
);
52 -- Import public groups
53 if members
[false] then
54 for _
, group_name
in ipairs(members
[false]) do
55 --module:log("debug", "Importing group %s", group_name);
56 import_jids_to_roster(group_name
);
61 roster
[false].version
= true;
65 function remove_virtual_contacts(username
, host
, datastore
, data
)
66 if host
== module_host
and datastore
== "roster" then
67 local new_roster
= {};
68 for jid
, contact
in pairs(data
) do
69 if contact
.persist
~= false then
70 new_roster
[jid
] = contact
;
73 if new_roster
[false] then
74 new_roster
[false].version
= nil; -- Version is void
76 return username
, host
, datastore
, new_roster
;
79 return username
, host
, datastore
, data
;
82 function module
.load()
83 local groups_file
= module
:get_option_path("groups_file", nil, "config");
84 if not groups_file
then return; end
86 module
:hook("roster-load", inject_roster_contacts
);
87 datamanager
.add_callback(remove_virtual_contacts
);
89 groups
= { default
= {} };
91 local curr_group
= "default";
92 for line
in io
.lines(groups_file
) do
93 if line
:match("^%s*%[.-%]%s*$") then
94 curr_group
= line
:match("^%s*%[(.-)%]%s*$");
95 if curr_group
:match("^%+") then
96 curr_group
= curr_group
:gsub("^%+", "");
97 if not members
[false] then
100 members
[false][#members
[false]+1] = curr_group
; -- Is a public group
102 module
:log("debug", "New group: %s", tostring(curr_group
));
103 groups
[curr_group
] = groups
[curr_group
] or {};
106 local entryjid
, name
= line
:match("([^=]*)=?(.*)");
107 module
:log("debug", "entryjid = '%s', name = '%s'", entryjid
, name
);
109 jid
= jid_prep(entryjid
:match("%S+"));
111 module
:log("debug", "New member of %s: %s", tostring(curr_group
), tostring(jid
));
112 groups
[curr_group
][jid
] = name
or false;
113 members
[jid
] = members
[jid
] or {};
114 members
[jid
][#members
[jid
]+1] = curr_group
;
115 elseif entryjid
:match("%S") then
116 module
:log("warn", "Invalid JID: %q", entryjid
);
120 module
:log("info", "Groups loaded successfully");
123 function module
.unload()
124 datamanager
.remove_callback(remove_virtual_contacts
);
127 -- Public for other modules to access
128 function group_contains(group_name
, jid
)
129 return groups
[group_name
][jid
];