Comparison

core/rostermanager.lua @ 6779:6236668da30a

core.*: Remove use of module() function
author Kim Alvefur <zash@zash.se>
date Sat, 21 Feb 2015 10:42:19 +0100
parent 6662:5ef319efedba
child 6787:ad64e2eacd0e
comparison
equal deleted inserted replaced
6778:4009ae66e0f0 6779:6236668da30a
20 20
21 local datamanager = require "util.datamanager" 21 local datamanager = require "util.datamanager"
22 local um_user_exists = require "core.usermanager".user_exists; 22 local um_user_exists = require "core.usermanager".user_exists;
23 local st = require "util.stanza"; 23 local st = require "util.stanza";
24 24
25 module "rostermanager" 25 local _ENV = nil;
26 26
27 function add_to_roster(session, jid, item) 27 local function add_to_roster(session, jid, item)
28 if session.roster then 28 if session.roster then
29 local old_item = session.roster[jid]; 29 local old_item = session.roster[jid];
30 session.roster[jid] = item; 30 session.roster[jid] = item;
31 if save_roster(session.username, session.host) then 31 if save_roster(session.username, session.host) then
32 return true; 32 return true;
37 else 37 else
38 return nil, "auth", "not-authorized", "Session's roster not loaded"; 38 return nil, "auth", "not-authorized", "Session's roster not loaded";
39 end 39 end
40 end 40 end
41 41
42 function remove_from_roster(session, jid) 42 local function remove_from_roster(session, jid)
43 if session.roster then 43 if session.roster then
44 local old_item = session.roster[jid]; 44 local old_item = session.roster[jid];
45 session.roster[jid] = nil; 45 session.roster[jid] = nil;
46 if save_roster(session.username, session.host) then 46 if save_roster(session.username, session.host) then
47 return true; 47 return true;
52 else 52 else
53 return nil, "auth", "not-authorized", "Session's roster not loaded"; 53 return nil, "auth", "not-authorized", "Session's roster not loaded";
54 end 54 end
55 end 55 end
56 56
57 function roster_push(username, host, jid) 57 local function roster_push(username, host, jid)
58 local roster = jid and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster; 58 local roster = jid and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
59 if roster then 59 if roster then
60 local item = hosts[host].sessions[username].roster[jid]; 60 local item = hosts[host].sessions[username].roster[jid];
61 local stanza = st.iq({type="set"}); 61 local stanza = st.iq({type="set"});
62 stanza:tag("query", {xmlns = "jabber:iq:roster", ver = tostring(roster[false].version or "1") }); 62 stanza:tag("query", {xmlns = "jabber:iq:roster", ver = tostring(roster[false].version or "1") });
93 metadata.pending = {}; 93 metadata.pending = {};
94 end 94 end
95 return metadata; 95 return metadata;
96 end 96 end
97 97
98 function load_roster(username, host) 98 local function load_roster(username, host)
99 local jid = username.."@"..host; 99 local jid = username.."@"..host;
100 log("debug", "load_roster: asked for: %s", jid); 100 log("debug", "load_roster: asked for: %s", jid);
101 local user = bare_sessions[jid]; 101 local user = bare_sessions[jid];
102 local roster; 102 local roster;
103 if user then 103 if user then
119 hosts[host].events.fire_event("roster-load", { username = username, host = host, roster = roster }); 119 hosts[host].events.fire_event("roster-load", { username = username, host = host, roster = roster });
120 end 120 end
121 return roster, err; 121 return roster, err;
122 end 122 end
123 123
124 function save_roster(username, host, roster) 124 local function save_roster(username, host, roster)
125 if not um_user_exists(username, host) then 125 if not um_user_exists(username, host) then
126 log("debug", "not saving roster for %s@%s: the user doesn't exist", username, host); 126 log("debug", "not saving roster for %s@%s: the user doesn't exist", username, host);
127 return nil; 127 return nil;
128 end 128 end
129 129
145 end 145 end
146 log("warn", "save_roster: user had no roster to save"); 146 log("warn", "save_roster: user had no roster to save");
147 return nil; 147 return nil;
148 end 148 end
149 149
150 function process_inbound_subscription_approval(username, host, jid) 150 local function process_inbound_subscription_approval(username, host, jid)
151 local roster = load_roster(username, host); 151 local roster = load_roster(username, host);
152 local item = roster[jid]; 152 local item = roster[jid];
153 if item and item.ask then 153 if item and item.ask then
154 if item.subscription == "none" then 154 if item.subscription == "none" then
155 item.subscription = "to"; 155 item.subscription = "to";
159 item.ask = nil; 159 item.ask = nil;
160 return save_roster(username, host, roster); 160 return save_roster(username, host, roster);
161 end 161 end
162 end 162 end
163 163
164 function process_inbound_subscription_cancellation(username, host, jid) 164 local function process_inbound_subscription_cancellation(username, host, jid)
165 local roster = load_roster(username, host); 165 local roster = load_roster(username, host);
166 local item = roster[jid]; 166 local item = roster[jid];
167 local changed = nil; 167 local changed = nil;
168 if is_contact_pending_out(username, host, jid) then 168 if is_contact_pending_out(username, host, jid) then
169 item.ask = nil; 169 item.ask = nil;
181 if changed then 181 if changed then
182 return save_roster(username, host, roster); 182 return save_roster(username, host, roster);
183 end 183 end
184 end 184 end
185 185
186 function process_inbound_unsubscribe(username, host, jid) 186 local function process_inbound_unsubscribe(username, host, jid)
187 local roster = load_roster(username, host); 187 local roster = load_roster(username, host);
188 local item = roster[jid]; 188 local item = roster[jid];
189 local changed = nil; 189 local changed = nil;
190 if is_contact_pending_in(username, host, jid) then 190 if is_contact_pending_in(username, host, jid) then
191 roster[false].pending[jid] = nil; 191 roster[false].pending[jid] = nil;
208 local function _get_online_roster_subscription(jidA, jidB) 208 local function _get_online_roster_subscription(jidA, jidB)
209 local user = bare_sessions[jidA]; 209 local user = bare_sessions[jidA];
210 local item = user and (user.roster[jidB] or { subscription = "none" }); 210 local item = user and (user.roster[jidB] or { subscription = "none" });
211 return item and item.subscription; 211 return item and item.subscription;
212 end 212 end
213 function is_contact_subscribed(username, host, jid) 213 local function is_contact_subscribed(username, host, jid)
214 do 214 do
215 local selfjid = username.."@"..host; 215 local selfjid = username.."@"..host;
216 local user_subscription = _get_online_roster_subscription(selfjid, jid); 216 local user_subscription = _get_online_roster_subscription(selfjid, jid);
217 if user_subscription then return (user_subscription == "both" or user_subscription == "from"); end 217 if user_subscription then return (user_subscription == "both" or user_subscription == "from"); end
218 local contact_subscription = _get_online_roster_subscription(jid, selfjid); 218 local contact_subscription = _get_online_roster_subscription(jid, selfjid);
221 local roster, err = load_roster(username, host); 221 local roster, err = load_roster(username, host);
222 local item = roster[jid]; 222 local item = roster[jid];
223 return item and (item.subscription == "from" or item.subscription == "both"), err; 223 return item and (item.subscription == "from" or item.subscription == "both"), err;
224 end 224 end
225 225
226 function is_contact_pending_in(username, host, jid) 226 local function is_contact_pending_in(username, host, jid)
227 local roster = load_roster(username, host); 227 local roster = load_roster(username, host);
228 return roster[false].pending[jid]; 228 return roster[false].pending[jid];
229 end 229 end
230 function set_contact_pending_in(username, host, jid) 230 local function set_contact_pending_in(username, host, jid)
231 local roster = load_roster(username, host); 231 local roster = load_roster(username, host);
232 local item = roster[jid]; 232 local item = roster[jid];
233 if item and (item.subscription == "from" or item.subscription == "both") then 233 if item and (item.subscription == "from" or item.subscription == "both") then
234 return; -- false 234 return; -- false
235 end 235 end
236 roster[false].pending[jid] = true; 236 roster[false].pending[jid] = true;
237 return save_roster(username, host, roster); 237 return save_roster(username, host, roster);
238 end 238 end
239 function is_contact_pending_out(username, host, jid) 239 local function is_contact_pending_out(username, host, jid)
240 local roster = load_roster(username, host); 240 local roster = load_roster(username, host);
241 local item = roster[jid]; 241 local item = roster[jid];
242 return item and item.ask; 242 return item and item.ask;
243 end 243 end
244 function set_contact_pending_out(username, host, jid) -- subscribe 244 local function set_contact_pending_out(username, host, jid) -- subscribe
245 local roster = load_roster(username, host); 245 local roster = load_roster(username, host);
246 local item = roster[jid]; 246 local item = roster[jid];
247 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then 247 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
248 return true; 248 return true;
249 end 249 end
253 end 253 end
254 item.ask = "subscribe"; 254 item.ask = "subscribe";
255 log("debug", "set_contact_pending_out: saving roster; set %s@%s.roster[%q].ask=subscribe", username, host, jid); 255 log("debug", "set_contact_pending_out: saving roster; set %s@%s.roster[%q].ask=subscribe", username, host, jid);
256 return save_roster(username, host, roster); 256 return save_roster(username, host, roster);
257 end 257 end
258 function unsubscribe(username, host, jid) 258 local function unsubscribe(username, host, jid)
259 local roster = load_roster(username, host); 259 local roster = load_roster(username, host);
260 local item = roster[jid]; 260 local item = roster[jid];
261 if not item then return false; end 261 if not item then return false; end
262 if (item.subscription == "from" or item.subscription == "none") and not item.ask then 262 if (item.subscription == "from" or item.subscription == "none") and not item.ask then
263 return true; 263 return true;
268 elseif item.subscription == "to" then 268 elseif item.subscription == "to" then
269 item.subscription = "none"; 269 item.subscription = "none";
270 end 270 end
271 return save_roster(username, host, roster); 271 return save_roster(username, host, roster);
272 end 272 end
273 function subscribed(username, host, jid) 273 local function subscribed(username, host, jid)
274 if is_contact_pending_in(username, host, jid) then 274 if is_contact_pending_in(username, host, jid) then
275 local roster = load_roster(username, host); 275 local roster = load_roster(username, host);
276 local item = roster[jid]; 276 local item = roster[jid];
277 if not item then -- FIXME should roster item be auto-created? 277 if not item then -- FIXME should roster item be auto-created?
278 item = {subscription = "none", groups = {}}; 278 item = {subscription = "none", groups = {}};
285 end 285 end
286 roster[false].pending[jid] = nil; 286 roster[false].pending[jid] = nil;
287 return save_roster(username, host, roster); 287 return save_roster(username, host, roster);
288 end -- TODO else implement optional feature pre-approval (ask = subscribed) 288 end -- TODO else implement optional feature pre-approval (ask = subscribed)
289 end 289 end
290 function unsubscribed(username, host, jid) 290 local function unsubscribed(username, host, jid)
291 local roster = load_roster(username, host); 291 local roster = load_roster(username, host);
292 local item = roster[jid]; 292 local item = roster[jid];
293 local pending = is_contact_pending_in(username, host, jid); 293 local pending = is_contact_pending_in(username, host, jid);
294 if pending then 294 if pending then
295 roster[false].pending[jid] = nil; 295 roster[false].pending[jid] = nil;
306 end 306 end
307 local success = (pending or subscribed) and save_roster(username, host, roster); 307 local success = (pending or subscribed) and save_roster(username, host, roster);
308 return success, pending, subscribed; 308 return success, pending, subscribed;
309 end 309 end
310 310
311 function process_outbound_subscription_request(username, host, jid) 311 local function process_outbound_subscription_request(username, host, jid)
312 local roster = load_roster(username, host); 312 local roster = load_roster(username, host);
313 local item = roster[jid]; 313 local item = roster[jid];
314 if item and (item.subscription == "none" or item.subscription == "from") then 314 if item and (item.subscription == "none" or item.subscription == "from") then
315 item.ask = "subscribe"; 315 item.ask = "subscribe";
316 return save_roster(username, host, roster); 316 return save_roster(username, host, roster);
326 end 326 end
327 end]] 327 end]]
328 328
329 329
330 330
331 return _M; 331 return {
332 add_to_roster = add_to_roster;
333 remove_from_roster = remove_from_roster;
334 roster_push = roster_push;
335 load_roster = load_roster;
336 save_roster = save_roster;
337 process_inbound_subscription_approval = process_inbound_subscription_approval;
338 process_inbound_subscription_cancellation = process_inbound_subscription_cancellation;
339 process_inbound_unsubscribe = process_inbound_unsubscribe;
340 is_contact_subscribed = is_contact_subscribed;
341 is_contact_pending_in = is_contact_pending_in;
342 set_contact_pending_in = set_contact_pending_in;
343 is_contact_pending_out = is_contact_pending_out;
344 set_contact_pending_out = set_contact_pending_out;
345 unsubscribe = unsubscribe;
346 subscribed = subscribed;
347 unsubscribed = unsubscribed;
348 process_outbound_subscription_request = process_outbound_subscription_request;
349 };