Software /
code /
prosody
Annotate
plugins/mod_groups.lua @ 1487:66f18c18befa
Merge with main branch.
author | Tobias Markmann <tm@ayena.de> |
---|---|
date | Sun, 05 Jul 2009 19:05:25 +0200 |
parent | 1388:546caa44620c |
child | 1522:569d58d21612 |
rev | line source |
---|---|
1383
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local groups = { default = {} }; |
1388
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
3 local members = { [false] = {} }; |
1383
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local groups_file; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local jid, datamanager = require "util.jid", require "util.datamanager"; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local jid_bare, jid_prep = jid.bare, jid.prep; |
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 module_host = module:get_host(); |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 function inject_roster_contacts(username, host, roster) |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 module:log("warn", "Injecting group members to roster"); |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local bare_jid = username.."@"..host; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 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
|
16 |
1388
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
17 local function import_jids_to_roster(group_name) |
1383
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 for jid in pairs(groups[group_name]) do |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 -- Add them to roster |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 --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
|
21 if jid ~= bare_jid then |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 if not roster[jid] then roster[jid] = {}; end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 roster[jid].subscription = "both"; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 if not roster[jid].groups then |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 roster[jid].groups = { [group_name] = true }; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 roster[jid].groups[group_name] = true; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 roster[jid].persist = false; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 end |
1388
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
32 |
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
33 -- 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
|
34 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
|
35 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
|
36 end |
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
37 |
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
38 -- Import public groups |
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
39 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
|
40 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
|
41 end |
1383
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 function remove_virtual_contacts(username, host, datastore, data) |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 if host == module_host and datastore == "roster" then |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local new_roster = {}; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 for jid, contact in pairs(data) do |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 if contact.persist ~= false then |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 new_roster[jid] = contact; |
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 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 return username, host, datastore, new_roster; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 return username, host, datastore, data; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 function module.load() |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 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
|
60 if not groups_file then return; 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 module:hook("roster-load", inject_roster_contacts); |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 datamanager.add_callback(remove_virtual_contacts); |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 groups = { default = {} }; |
1388
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
66 members = { [false] = {} }; |
1383
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 local curr_group = "default"; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 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
|
69 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
|
70 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
|
71 if curr_group:match("^%+") then |
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
72 curr_group = curr_group:gsub("^%+", ""); |
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
73 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
|
74 end |
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
75 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
|
76 groups[curr_group] = groups[curr_group] or {}; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 else |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 -- Add JID |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 local jid = jid_prep(line); |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 if jid then |
1388
546caa44620c
mod_groups: Support for public groups, and extra logging
Matthew Wild <mwild1@gmail.com>
parents:
1383
diff
changeset
|
81 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
|
82 groups[curr_group][jid] = true; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 members[jid] = members[jid] or {}; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 members[jid][#members[jid]+1] = curr_group; |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 module:log("info", "Groups loaded successfully"); |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 end |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 function module.unload() |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 datamanager.remove_callback(remove_virtual_contacts); |
8774c5cbf147
mod_groups: Experimental shared roster support
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 end |