Annotate

core/rostermanager.lua @ 5554:e91db0aac408

Merge 0.9->trunk
author Matthew Wild <mwild1@gmail.com>
date Wed, 01 May 2013 13:45:42 +0100
parent 5429:25333de6e7c7
child 5747:23076ee191d3
child 7777:65836cf2d057
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1380
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 268
diff changeset
9
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
10
263
75275b6b4e03 Update rostermanager to use new logger
Matthew Wild <mwild1@gmail.com>
parents: 193
diff changeset
11
75275b6b4e03 Update rostermanager to use new logger
Matthew Wild <mwild1@gmail.com>
parents: 193
diff changeset
12 local log = require "util.logger".init("rostermanager");
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
13
5348
ca0d820e9318 rostermanager: Remove unused imports
Kim Alvefur <zash@zash.se>
parents: 5060
diff changeset
14 local pairs = pairs;
916
f0743928ef7e core.rostermanager/mod_roster: Support for roster versioning
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
15 local tostring = tostring;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
16
103
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
17 local hosts = hosts;
2050
adc1b92b8c6b rostermanager: Fixed a global access.
Waqas Hussain <waqas20@gmail.com>
parents: 2049
diff changeset
18 local bare_sessions = bare_sessions;
103
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
19
183
403db3b5c3c2 Combined some lines
Waqas Hussain <waqas20@gmail.com>
parents: 177
diff changeset
20 local datamanager = require "util.datamanager"
5429
25333de6e7c7 rostermanager: do not save rosters for unexistant users.
Marco Cirillo <maranda@lightwitch.org>
parents: 5348
diff changeset
21 local um_user_exists = require "core.usermanager".user_exists;
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
22 local st = require "util.stanza";
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
23
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
24 module "rostermanager"
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
25
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
26 function add_to_roster(session, jid, item)
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
27 if session.roster then
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
28 local old_item = session.roster[jid];
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
29 session.roster[jid] = item;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
30 if save_roster(session.username, session.host) then
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
31 return true;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
32 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
33 session.roster[jid] = old_item;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
34 return nil, "wait", "internal-server-error", "Unable to save roster";
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
35 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
36 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
37 return nil, "auth", "not-authorized", "Session's roster not loaded";
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
38 end
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
39 end
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
40
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
41 function remove_from_roster(session, jid)
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
42 if session.roster then
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
43 local old_item = session.roster[jid];
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
44 session.roster[jid] = nil;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
45 if save_roster(session.username, session.host) then
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
46 return true;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
47 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
48 session.roster[jid] = old_item;
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
49 return nil, "wait", "internal-server-error", "Unable to save roster";
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
50 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
51 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
52 return nil, "auth", "not-authorized", "Session's roster not loaded";
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
53 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
54 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
55
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
56 function roster_push(username, host, jid)
916
f0743928ef7e core.rostermanager/mod_roster: Support for roster versioning
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
57 local roster = jid and jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
f0743928ef7e core.rostermanager/mod_roster: Support for roster versioning
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
58 if roster then
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
59 local item = hosts[host].sessions[username].roster[jid];
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
60 local stanza = st.iq({type="set"});
916
f0743928ef7e core.rostermanager/mod_roster: Support for roster versioning
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
61 stanza:tag("query", {xmlns = "jabber:iq:roster", ver = tostring(roster[false].version or "1") });
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
62 if item then
132
9a4aa57af367 Roster updates
Waqas Hussain <waqas20@gmail.com>
parents: 114
diff changeset
63 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask});
110
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
64 for group in pairs(item.groups) do
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
65 stanza:tag("group"):text(group):up();
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
66 end
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
67 else
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
68 stanza:tag("item", {jid = jid, subscription = "remove"});
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
69 end
193
13ac34255c37 mod_roster fix
Waqas Hussain <waqas20@gmail.com>
parents: 183
diff changeset
70 stanza:up(); -- move out from item
13ac34255c37 mod_roster fix
Waqas Hussain <waqas20@gmail.com>
parents: 183
diff changeset
71 stanza:up(); -- move out from stanza
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
72 -- stanza ready
110
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
73 for _, session in pairs(hosts[host].sessions[username].sessions) do
114
bed2a8508cf5 Added session property for interested resources
Waqas Hussain <waqas20@gmail.com>
parents: 110
diff changeset
74 if session.interested then
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
75 -- FIXME do we need to set stanza.attr.to?
110
bb5ac5976a97 Fixed: Roster support
Waqas Hussain <waqas20@gmail.com>
parents: 107
diff changeset
76 session.send(stanza);
107
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
77 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
78 end
8d8debda3df2 Added: Roster manipulation functions to core.rostermanager
Waqas Hussain <waqas20@gmail.com>
parents: 103
diff changeset
79 end
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
80 end
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
81
103
ebdb7875443e Fixed: Typos caused by lack of sleep.
Waqas Hussain <waqas20@gmail.com>
parents: 101
diff changeset
82 function load_roster(username, host)
2048
d420722519e1 rostermanager: Log a warning when a self-contact is detected.
Waqas Hussain <waqas20@gmail.com>
parents: 2047
diff changeset
83 local jid = username.."@"..host;
4993
5243b74a4cbb Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents: 3129
diff changeset
84 log("debug", "load_roster: asked for: %s", jid);
2049
227f3fd2caaf rostermanager: Refactored roster loading to remove duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents: 2048
diff changeset
85 local user = bare_sessions[jid];
1380
9b3b43b8963a rostermanager: Fire event on roster load
Matthew Wild <mwild1@gmail.com>
parents: 925
diff changeset
86 local roster;
2049
227f3fd2caaf rostermanager: Refactored roster loading to remove duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents: 2048
diff changeset
87 if user then
227f3fd2caaf rostermanager: Refactored roster loading to remove duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents: 2048
diff changeset
88 roster = user.roster;
227f3fd2caaf rostermanager: Refactored roster loading to remove duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents: 2048
diff changeset
89 if roster then return roster; end
4993
5243b74a4cbb Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents: 3129
diff changeset
90 log("debug", "load_roster: loading for new user: %s@%s", username, host);
2049
227f3fd2caaf rostermanager: Refactored roster loading to remove duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents: 2048
diff changeset
91 else -- Attempt to load roster for non-loaded user
4993
5243b74a4cbb Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents: 3129
diff changeset
92 log("debug", "load_roster: loading for offline user: %s@%s", username, host);
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
93 end
3087
9b31e36528e0 rostermanager: When loading the roster, also return a datamanager error if available.
Waqas Hussain <waqas20@gmail.com>
parents: 2925
diff changeset
94 local data, err = datamanager.load(username, host, "roster");
9b31e36528e0 rostermanager: When loading the roster, also return a datamanager error if available.
Waqas Hussain <waqas20@gmail.com>
parents: 2925
diff changeset
95 roster = data or {};
2049
227f3fd2caaf rostermanager: Refactored roster loading to remove duplicate code.
Waqas Hussain <waqas20@gmail.com>
parents: 2048
diff changeset
96 if user then user.roster = roster; end
3110
7426727548ed rostermanager: Mark rosters which fail to load as broken, and never save them.
Waqas Hussain <waqas20@gmail.com>
parents: 3090
diff changeset
97 if not roster[false] then roster[false] = { broken = err or nil }; end
2048
d420722519e1 rostermanager: Log a warning when a self-contact is detected.
Waqas Hussain <waqas20@gmail.com>
parents: 2047
diff changeset
98 if roster[jid] then
d420722519e1 rostermanager: Log a warning when a self-contact is detected.
Waqas Hussain <waqas20@gmail.com>
parents: 2047
diff changeset
99 roster[jid] = nil;
4993
5243b74a4cbb Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents: 3129
diff changeset
100 log("warn", "roster for %s has a self-contact", jid);
2048
d420722519e1 rostermanager: Log a warning when a self-contact is detected.
Waqas Hussain <waqas20@gmail.com>
parents: 2047
diff changeset
101 end
3090
f14d2962f32c rostermanager: Don't fire roster-load event if there was an error loading the roster
Matthew Wild <mwild1@gmail.com>
parents: 3087
diff changeset
102 if not err then
f14d2962f32c rostermanager: Don't fire roster-load event if there was an error loading the roster
Matthew Wild <mwild1@gmail.com>
parents: 3087
diff changeset
103 hosts[host].events.fire_event("roster-load", username, host, roster);
f14d2962f32c rostermanager: Don't fire roster-load event if there was an error loading the roster
Matthew Wild <mwild1@gmail.com>
parents: 3087
diff changeset
104 end
3087
9b31e36528e0 rostermanager: When loading the roster, also return a datamanager error if available.
Waqas Hussain <waqas20@gmail.com>
parents: 2925
diff changeset
105 return roster, err;
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
106 end
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
107
1897
6d81e74310a7 rostermanager: Added support for saving rosters of offline users to the save_roster function.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
108 function save_roster(username, host, roster)
5429
25333de6e7c7 rostermanager: do not save rosters for unexistant users.
Marco Cirillo <maranda@lightwitch.org>
parents: 5348
diff changeset
109 if not um_user_exists(username, host) then
25333de6e7c7 rostermanager: do not save rosters for unexistant users.
Marco Cirillo <maranda@lightwitch.org>
parents: 5348
diff changeset
110 log("debug", "not saving roster for %s@%s: the user doesn't exist", username, host);
25333de6e7c7 rostermanager: do not save rosters for unexistant users.
Marco Cirillo <maranda@lightwitch.org>
parents: 5348
diff changeset
111 return nil;
25333de6e7c7 rostermanager: do not save rosters for unexistant users.
Marco Cirillo <maranda@lightwitch.org>
parents: 5348
diff changeset
112 end
25333de6e7c7 rostermanager: do not save rosters for unexistant users.
Marco Cirillo <maranda@lightwitch.org>
parents: 5348
diff changeset
113
4993
5243b74a4cbb Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents: 3129
diff changeset
114 log("debug", "save_roster: saving roster for %s@%s", username, host);
1897
6d81e74310a7 rostermanager: Added support for saving rosters of offline users to the save_roster function.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
115 if not roster then
6d81e74310a7 rostermanager: Added support for saving rosters of offline users to the save_roster function.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
116 roster = hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
6d81e74310a7 rostermanager: Added support for saving rosters of offline users to the save_roster function.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
117 --if not roster then
6d81e74310a7 rostermanager: Added support for saving rosters of offline users to the save_roster function.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
118 -- --roster = load_roster(username, host);
6d81e74310a7 rostermanager: Added support for saving rosters of offline users to the save_roster function.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
119 -- return true; -- roster unchanged, no reason to save
6d81e74310a7 rostermanager: Added support for saving rosters of offline users to the save_roster function.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
120 --end
6d81e74310a7 rostermanager: Added support for saving rosters of offline users to the save_roster function.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
121 end
6d81e74310a7 rostermanager: Added support for saving rosters of offline users to the save_roster function.
Waqas Hussain <waqas20@gmail.com>
parents: 1523
diff changeset
122 if roster then
2909
bcfd76cc9b7d rostermanager: Don't bump the roster version if it is == true (ie. we are not versioning the roster)
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
123 local metadata = roster[false];
bcfd76cc9b7d rostermanager: Don't bump the roster version if it is == true (ie. we are not versioning the roster)
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
124 if not metadata then
bcfd76cc9b7d rostermanager: Don't bump the roster version if it is == true (ie. we are not versioning the roster)
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
125 metadata = {};
bcfd76cc9b7d rostermanager: Don't bump the roster version if it is == true (ie. we are not versioning the roster)
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
126 roster[false] = metadata;
bcfd76cc9b7d rostermanager: Don't bump the roster version if it is == true (ie. we are not versioning the roster)
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
127 end
bcfd76cc9b7d rostermanager: Don't bump the roster version if it is == true (ie. we are not versioning the roster)
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
128 if metadata.version ~= true then
bcfd76cc9b7d rostermanager: Don't bump the roster version if it is == true (ie. we are not versioning the roster)
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
129 metadata.version = (metadata.version or 0) + 1;
bcfd76cc9b7d rostermanager: Don't bump the roster version if it is == true (ie. we are not versioning the roster)
Matthew Wild <mwild1@gmail.com>
parents: 2050
diff changeset
130 end
3110
7426727548ed rostermanager: Mark rosters which fail to load as broken, and never save them.
Waqas Hussain <waqas20@gmail.com>
parents: 3090
diff changeset
131 if roster[false].broken then return nil, "Not saving broken roster" end
1947
ff2b86076e9c rostermanager: Fixed a traceback on roster save.
Waqas Hussain <waqas20@gmail.com>
parents: 1898
diff changeset
132 return datamanager.store(username, host, "roster", roster);
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
133 end
916
f0743928ef7e core.rostermanager/mod_roster: Support for roster versioning
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
134 log("warn", "save_roster: user had no roster to save");
101
c690fa382743 Added some roster management functions
Waqas Hussain <waqas20@gmail.com>
parents: 80
diff changeset
135 return nil;
0
3e3171b59028 First commit, where do you want to go tomorrow?
matthew
parents:
diff changeset
136 end
80
523ac742cc19 Fixed: rostermanager.lua now returns the module
Waqas Hussain <waqas20@gmail.com>
parents: 6
diff changeset
137
171
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
138 function process_inbound_subscription_approval(username, host, jid)
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
139 local roster = load_roster(username, host);
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
140 local item = roster[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
141 if item and item.ask then
171
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
142 if item.subscription == "none" then
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
143 item.subscription = "to";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
144 else -- subscription == from
171
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
145 item.subscription = "both";
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
146 end
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
147 item.ask = nil;
1898
1ce02e2f5a3f rostermanager: Fixed: Roster version was not being properly updated in some edge cases.
Waqas Hussain <waqas20@gmail.com>
parents: 1897
diff changeset
148 return save_roster(username, host, roster);
171
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
149 end
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
150 end
28f420d057a0 Inbound subscription approval
Waqas Hussain <waqas20@gmail.com>
parents: 132
diff changeset
151
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
152 function process_inbound_subscription_cancellation(username, host, jid)
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
153 local roster = load_roster(username, host);
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
154 local item = roster[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
155 local changed = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
156 if is_contact_pending_out(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
157 item.ask = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
158 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
159 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
160 if item then
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
161 if item.subscription == "to" then
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
162 item.subscription = "none";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
163 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
164 elseif item.subscription == "both" then
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
165 item.subscription = "from";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
166 changed = true;
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
167 end
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
168 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
169 if changed then
1898
1ce02e2f5a3f rostermanager: Fixed: Roster version was not being properly updated in some edge cases.
Waqas Hussain <waqas20@gmail.com>
parents: 1897
diff changeset
170 return save_roster(username, host, roster);
172
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
171 end
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
172 end
71247788c7c7 Inbound subscription cancellation
Waqas Hussain <waqas20@gmail.com>
parents: 171
diff changeset
173
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
174 function process_inbound_unsubscribe(username, host, jid)
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
175 local roster = load_roster(username, host);
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
176 local item = roster[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
177 local changed = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
178 if is_contact_pending_in(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
179 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
180 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
181 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
182 if item then
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
183 if item.subscription == "from" then
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
184 item.subscription = "none";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
185 changed = true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
186 elseif item.subscription == "both" then
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
187 item.subscription = "to";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
188 changed = true;
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
189 end
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
190 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
191 if changed then
1898
1ce02e2f5a3f rostermanager: Fixed: Roster version was not being properly updated in some edge cases.
Waqas Hussain <waqas20@gmail.com>
parents: 1897
diff changeset
192 return save_roster(username, host, roster);
173
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
193 end
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
194 end
f3a2af4d2ff2 Inbound unsubscribe
Waqas Hussain <waqas20@gmail.com>
parents: 172
diff changeset
195
3129
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
196 local function _get_online_roster_subscription(jidA, jidB)
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
197 local user = bare_sessions[jidA];
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
198 local item = user and (user.roster[jidB] or { subscription = "none" });
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
199 return item and item.subscription;
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
200 end
174
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
201 function is_contact_subscribed(username, host, jid)
3129
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
202 do
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
203 local selfjid = username.."@"..host;
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
204 local subscription = _get_online_roster_subscription(selfjid, jid);
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
205 if subscription then return (subscription == "both" or subscription == "from"); end
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
206 local subscription = _get_online_roster_subscription(jid, selfjid);
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
207 if subscription then return (subscription == "both" or subscription == "to"); end
125f03db0b1a rostermanager: Optimisation to avoid unnecessarily loading rosters for offline contacts on probes, etc.
Waqas Hussain <waqas20@gmail.com>
parents: 3111
diff changeset
208 end
3111
826cb5f1859b rostermanager: Return an error string when subscription test fails due to an error.
Waqas Hussain <waqas20@gmail.com>
parents: 3110
diff changeset
209 local roster, err = load_roster(username, host);
174
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
210 local item = roster[jid];
3111
826cb5f1859b rostermanager: Return an error string when subscription test fails due to an error.
Waqas Hussain <waqas20@gmail.com>
parents: 3110
diff changeset
211 return item and (item.subscription == "from" or item.subscription == "both"), err;
174
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
212 end
f9aff1fc7e99 Inbound subscription request
Waqas Hussain <waqas20@gmail.com>
parents: 173
diff changeset
213
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
214 function is_contact_pending_in(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
215 local roster = load_roster(username, host);
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 176
diff changeset
216 return roster.pending and roster.pending[jid];
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
217 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
218 function set_contact_pending_in(username, host, jid, pending)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
219 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
220 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
221 if item and (item.subscription == "from" or item.subscription == "both") then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
222 return; -- false
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
223 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
224 if not roster.pending then roster.pending = {}; end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
225 roster.pending[jid] = true;
1898
1ce02e2f5a3f rostermanager: Fixed: Roster version was not being properly updated in some edge cases.
Waqas Hussain <waqas20@gmail.com>
parents: 1897
diff changeset
226 return save_roster(username, host, roster);
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
227 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
228 function is_contact_pending_out(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
229 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
230 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
231 return item and item.ask;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
232 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
233 function set_contact_pending_out(username, host, jid) -- subscribe
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
234 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
235 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
236 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
237 return true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
238 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
239 if not item then
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 176
diff changeset
240 item = {subscription = "none", groups = {}};
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
241 roster[jid] = item;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
242 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
243 item.ask = "subscribe";
4993
5243b74a4cbb Hopefully inert commit to clean up logging across a number of modules, removing all cases of concatenation when building log messages
Matthew Wild <mwild1@gmail.com>
parents: 3129
diff changeset
244 log("debug", "set_contact_pending_out: saving roster; set %s@%s.roster[%q].ask=subscribe", username, host, jid);
1898
1ce02e2f5a3f rostermanager: Fixed: Roster version was not being properly updated in some edge cases.
Waqas Hussain <waqas20@gmail.com>
parents: 1897
diff changeset
245 return save_roster(username, host, roster);
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
246 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
247 function unsubscribe(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
248 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
249 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
250 if not item then return false; end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
251 if (item.subscription == "from" or item.subscription == "none") and not item.ask then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
252 return true;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
253 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
254 item.ask = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
255 if item.subscription == "both" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
256 item.subscription = "from";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
257 elseif item.subscription == "to" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
258 item.subscription = "none";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
259 end
1898
1ce02e2f5a3f rostermanager: Fixed: Roster version was not being properly updated in some edge cases.
Waqas Hussain <waqas20@gmail.com>
parents: 1897
diff changeset
260 return save_roster(username, host, roster);
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
261 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
262 function subscribed(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
263 if is_contact_pending_in(username, host, jid) then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
264 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
265 local item = roster[jid];
925
4861bcf0afa4 Fixed: rostermanager: Create new roster item if one doesn't exist on subscription approval (fixes issue #77)
Waqas Hussain <waqas20@gmail.com>
parents: 916
diff changeset
266 if not item then -- FIXME should roster item be auto-created?
4861bcf0afa4 Fixed: rostermanager: Create new roster item if one doesn't exist on subscription approval (fixes issue #77)
Waqas Hussain <waqas20@gmail.com>
parents: 916
diff changeset
267 item = {subscription = "none", groups = {}};
4861bcf0afa4 Fixed: rostermanager: Create new roster item if one doesn't exist on subscription approval (fixes issue #77)
Waqas Hussain <waqas20@gmail.com>
parents: 916
diff changeset
268 roster[jid] = item;
4861bcf0afa4 Fixed: rostermanager: Create new roster item if one doesn't exist on subscription approval (fixes issue #77)
Waqas Hussain <waqas20@gmail.com>
parents: 916
diff changeset
269 end
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
270 if item.subscription == "none" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
271 item.subscription = "from";
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
272 else -- subscription == to
177
606c433955e7 Bug fixes and checks for presence subscriptions, etc
Waqas Hussain <waqas20@gmail.com>
parents: 176
diff changeset
273 item.subscription = "both";
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
274 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
275 roster.pending[jid] = nil;
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
276 -- TODO maybe remove roster.pending if empty
1898
1ce02e2f5a3f rostermanager: Fixed: Roster version was not being properly updated in some edge cases.
Waqas Hussain <waqas20@gmail.com>
parents: 1897
diff changeset
277 return save_roster(username, host, roster);
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
278 end -- TODO else implement optional feature pre-approval (ask = subscribed)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
279 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
280 function unsubscribed(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
281 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
282 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
283 local pending = is_contact_pending_in(username, host, jid);
5060
b0e36777f715 mod_presence, rostermanager: Bring outbound subscription cancellation in line with RFC6121.
Waqas Hussain <waqas20@gmail.com>
parents: 5024
diff changeset
284 if pending then
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
285 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
286 end
5060
b0e36777f715 mod_presence, rostermanager: Bring outbound subscription cancellation in line with RFC6121.
Waqas Hussain <waqas20@gmail.com>
parents: 5024
diff changeset
287 local subscribed;
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
288 if item then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
289 if item.subscription == "from" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
290 item.subscription = "none";
5060
b0e36777f715 mod_presence, rostermanager: Bring outbound subscription cancellation in line with RFC6121.
Waqas Hussain <waqas20@gmail.com>
parents: 5024
diff changeset
291 subscribed = true;
267
6303b7454ad0 Some bugs fixed
Waqas Hussain <waqas20@gmail.com>
parents: 193
diff changeset
292 elseif item.subscription == "both" then
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
293 item.subscription = "to";
5060
b0e36777f715 mod_presence, rostermanager: Bring outbound subscription cancellation in line with RFC6121.
Waqas Hussain <waqas20@gmail.com>
parents: 5024
diff changeset
294 subscribed = true;
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
295 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
296 end
5060
b0e36777f715 mod_presence, rostermanager: Bring outbound subscription cancellation in line with RFC6121.
Waqas Hussain <waqas20@gmail.com>
parents: 5024
diff changeset
297 local success = (pending or subscribed) and save_roster(username, host, roster);
b0e36777f715 mod_presence, rostermanager: Bring outbound subscription cancellation in line with RFC6121.
Waqas Hussain <waqas20@gmail.com>
parents: 5024
diff changeset
298 return success, pending, subscribed;
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
299 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
300
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
301 function process_outbound_subscription_request(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
302 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
303 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
304 if item and (item.subscription == "none" or item.subscription == "from") then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
305 item.ask = "subscribe";
1898
1ce02e2f5a3f rostermanager: Fixed: Roster version was not being properly updated in some edge cases.
Waqas Hussain <waqas20@gmail.com>
parents: 1897
diff changeset
306 return save_roster(username, host, roster);
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
307 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
308 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
309
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
310 --[[function process_outbound_subscription_approval(username, host, jid)
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
311 local roster = load_roster(username, host);
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
312 local item = roster[jid];
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
313 if item and (item.subscription == "none" or item.subscription == "from" then
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
314 item.ask = "subscribe";
1898
1ce02e2f5a3f rostermanager: Fixed: Roster version was not being properly updated in some edge cases.
Waqas Hussain <waqas20@gmail.com>
parents: 1897
diff changeset
315 return save_roster(username, host, roster);
176
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
316 end
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
317 end]]
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
318
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
319
e5cd2a03891d Outbound presence subscription
Waqas Hussain <waqas20@gmail.com>
parents: 174
diff changeset
320
916
f0743928ef7e core.rostermanager/mod_roster: Support for roster versioning
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
321 return _M;