Comparison

core/rostermanager.lua @ 6613:2aae36312eb9

rostermanager, mod_roster, mod_presence: Move pending roster items to roster metadata field
author Kim Alvefur <zash@zash.se>
date Fri, 03 Apr 2015 06:38:22 +0200
parent 6550:a335d02951b3
child 6616:5d2346e72716
comparison
equal deleted inserted replaced
6612:6cc48b51d699 6613:2aae36312eb9
52 return nil, "auth", "not-authorized", "Session's roster not loaded"; 52 return nil, "auth", "not-authorized", "Session's roster not loaded";
53 end 53 end
54 end 54 end
55 55
56 function roster_push(username, host, jid) 56 function roster_push(username, host, jid)
57 local roster = jid and jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster; 57 local roster = jid and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
58 if roster then 58 if roster then
59 local item = hosts[host].sessions[username].roster[jid]; 59 local item = hosts[host].sessions[username].roster[jid];
60 local stanza = st.iq({type="set"}); 60 local stanza = st.iq({type="set"});
61 stanza:tag("query", {xmlns = "jabber:iq:roster", ver = tostring(roster[false].version or "1") }); 61 stanza:tag("query", {xmlns = "jabber:iq:roster", ver = tostring(roster[false].version or "1") });
62 if item then 62 if item then
77 end 77 end
78 end 78 end
79 end 79 end
80 end 80 end
81 81
82 local function roster_metadata(roster, err)
83 local metadata = roster[false];
84 if not metadata then
85 metadata = { broken = err or nil };
86 roster[false] = metadata;
87 end
88 if not metadata.pending then
89 if roster.pending and not type(roster.pending.subscription) == "string" then
90 metadata.pending, roster.pending = roster.pending, nil;
91 else
92 metadata.pending = {};
93 end
94 end
95 return metadata;
96 end
97
82 function load_roster(username, host) 98 function load_roster(username, host)
83 local jid = username.."@"..host; 99 local jid = username.."@"..host;
84 log("debug", "load_roster: asked for: %s", jid); 100 log("debug", "load_roster: asked for: %s", jid);
85 local user = bare_sessions[jid]; 101 local user = bare_sessions[jid];
86 local roster; 102 local roster;
92 log("debug", "load_roster: loading for offline user: %s@%s", username, host); 108 log("debug", "load_roster: loading for offline user: %s@%s", username, host);
93 end 109 end
94 local data, err = datamanager.load(username, host, "roster"); 110 local data, err = datamanager.load(username, host, "roster");
95 roster = data or {}; 111 roster = data or {};
96 if user then user.roster = roster; end 112 if user then user.roster = roster; end
97 if not roster[false] then roster[false] = { broken = err or nil }; end 113 roster_metadata(roster, err);
98 if roster[jid] then 114 if roster[jid] then
99 roster[jid] = nil; 115 roster[jid] = nil;
100 log("warn", "roster for %s has a self-contact", jid); 116 log("warn", "roster for %s has a self-contact", jid);
101 end 117 end
102 if not err then 118 if not err then
118 -- --roster = load_roster(username, host); 134 -- --roster = load_roster(username, host);
119 -- return true; -- roster unchanged, no reason to save 135 -- return true; -- roster unchanged, no reason to save
120 --end 136 --end
121 end 137 end
122 if roster then 138 if roster then
123 local metadata = roster[false]; 139 local metadata = roster_metadata(roster);
124 if not metadata then
125 metadata = {};
126 roster[false] = metadata;
127 end
128 if metadata.version ~= true then 140 if metadata.version ~= true then
129 metadata.version = (metadata.version or 0) + 1; 141 metadata.version = (metadata.version or 0) + 1;
130 end 142 end
131 if roster[false].broken then return nil, "Not saving broken roster" end 143 if metadata.broken then return nil, "Not saving broken roster" end
132 return datamanager.store(username, host, "roster", roster); 144 return datamanager.store(username, host, "roster", roster);
133 end 145 end
134 log("warn", "save_roster: user had no roster to save"); 146 log("warn", "save_roster: user had no roster to save");
135 return nil; 147 return nil;
136 end 148 end
174 function process_inbound_unsubscribe(username, host, jid) 186 function process_inbound_unsubscribe(username, host, jid)
175 local roster = load_roster(username, host); 187 local roster = load_roster(username, host);
176 local item = roster[jid]; 188 local item = roster[jid];
177 local changed = nil; 189 local changed = nil;
178 if is_contact_pending_in(username, host, jid) then 190 if is_contact_pending_in(username, host, jid) then
179 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty? 191 roster[false].pending[jid] = nil;
180 changed = true; 192 changed = true;
181 end 193 end
182 if item then 194 if item then
183 if item.subscription == "from" then 195 if item.subscription == "from" then
184 item.subscription = "none"; 196 item.subscription = "none";
211 return item and (item.subscription == "from" or item.subscription == "both"), err; 223 return item and (item.subscription == "from" or item.subscription == "both"), err;
212 end 224 end
213 225
214 function is_contact_pending_in(username, host, jid) 226 function is_contact_pending_in(username, host, jid)
215 local roster = load_roster(username, host); 227 local roster = load_roster(username, host);
216 return roster.pending and roster.pending[jid]; 228 return roster[false].pending[jid];
217 end 229 end
218 function set_contact_pending_in(username, host, jid, pending) 230 function set_contact_pending_in(username, host, jid, pending)
219 local roster = load_roster(username, host); 231 local roster = load_roster(username, host);
220 local item = roster[jid]; 232 local item = roster[jid];
221 if item and (item.subscription == "from" or item.subscription == "both") then 233 if item and (item.subscription == "from" or item.subscription == "both") then
222 return; -- false 234 return; -- false
223 end 235 end
224 if not roster.pending then roster.pending = {}; end 236 roster[false].pending[jid] = true;
225 roster.pending[jid] = true;
226 return save_roster(username, host, roster); 237 return save_roster(username, host, roster);
227 end 238 end
228 function is_contact_pending_out(username, host, jid) 239 function is_contact_pending_out(username, host, jid)
229 local roster = load_roster(username, host); 240 local roster = load_roster(username, host);
230 local item = roster[jid]; 241 local item = roster[jid];
270 if item.subscription == "none" then 281 if item.subscription == "none" then
271 item.subscription = "from"; 282 item.subscription = "from";
272 else -- subscription == to 283 else -- subscription == to
273 item.subscription = "both"; 284 item.subscription = "both";
274 end 285 end
275 roster.pending[jid] = nil; 286 roster[false].pending[jid] = nil;
276 -- TODO maybe remove roster.pending if empty
277 return save_roster(username, host, roster); 287 return save_roster(username, host, roster);
278 end -- TODO else implement optional feature pre-approval (ask = subscribed) 288 end -- TODO else implement optional feature pre-approval (ask = subscribed)
279 end 289 end
280 function unsubscribed(username, host, jid) 290 function unsubscribed(username, host, jid)
281 local roster = load_roster(username, host); 291 local roster = load_roster(username, host);
282 local item = roster[jid]; 292 local item = roster[jid];
283 local pending = is_contact_pending_in(username, host, jid); 293 local pending = is_contact_pending_in(username, host, jid);
284 if pending then 294 if pending then
285 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty? 295 roster[false].pending[jid] = nil;
286 end 296 end
287 local subscribed; 297 local subscribed;
288 if item then 298 if item then
289 if item.subscription == "from" then 299 if item.subscription == "from" then
290 item.subscription = "none"; 300 item.subscription = "none";