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