2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
12 local log = require "util.logger".init("rostermanager");
15 local tostring = tostring;
19 local bare_sessions = prosody.bare_sessions;
21 local datamanager = require "util.datamanager"
22 local um_user_exists = require "core.usermanager".user_exists;
23 local st = require "util.stanza";
27 local function add_to_roster(session, jid, item)
28 if session.roster then
29 local old_item = session.roster[jid];
30 session.roster[jid] = item;
31 if save_roster(session.username, session.host) then
34 session.roster[jid] = old_item;
35 return nil, "wait", "internal-server-error", "Unable to save roster";
38 return nil, "auth", "not-authorized", "Session's roster not loaded";
42 local function remove_from_roster(session, jid)
43 if session.roster then
44 local old_item = session.roster[jid];
45 session.roster[jid] = nil;
46 if save_roster(session.username, session.host) then
49 session.roster[jid] = old_item;
50 return nil, "wait", "internal-server-error", "Unable to save roster";
53 return nil, "auth", "not-authorized", "Session's roster not loaded";
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;
60 local item = hosts[host].sessions[username].roster[jid];
61 local stanza = st.iq({type="set"});
62 stanza:tag("query", {xmlns = "jabber:iq:roster", ver = tostring(roster[false].version or "1") });
64 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask});
65 for group in pairs(item.groups) do
66 stanza:tag("group"):text(group):up();
69 stanza:tag("item", {jid = jid, subscription = "remove"});
71 stanza:up(); -- move out from item
72 stanza:up(); -- move out from stanza
74 for _, session in pairs(hosts[host].sessions[username].sessions) do
75 if session.interested then
76 -- FIXME do we need to set stanza.attr.to?
83 local function roster_metadata(roster, err)
84 local metadata = roster[false];
86 metadata = { broken = err or nil };
87 roster[false] = metadata;
89 if roster.pending and type(roster.pending.subscription) ~= "string" then
90 metadata.pending = roster.pending;
92 elseif not metadata.pending then
93 metadata.pending = {};
98 local function load_roster(username, host)
99 local jid = username.."@"..host;
100 log("debug", "load_roster: asked for: %s", jid);
101 local user = bare_sessions[jid];
104 roster = user.roster;
105 if roster then return roster; end
106 log("debug", "load_roster: loading for new user: %s@%s", username, host);
107 else -- Attempt to load roster for non-loaded user
108 log("debug", "load_roster: loading for offline user: %s@%s", username, host);
110 local data, err = datamanager.load(username, host, "roster");
112 if user then user.roster = roster; end
113 roster_metadata(roster, err);
116 log("warn", "roster for %s has a self-contact", jid);
119 hosts[host].events.fire_event("roster-load", { username = username, host = host, roster = roster });
124 local function save_roster(username, host, roster)
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);
130 log("debug", "save_roster: saving roster for %s@%s", username, host);
132 roster = hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
134 -- --roster = load_roster(username, host);
135 -- return true; -- roster unchanged, no reason to save
139 local metadata = roster_metadata(roster);
140 if metadata.version ~= true then
141 metadata.version = (metadata.version or 0) + 1;
143 if metadata.broken then return nil, "Not saving broken roster" end
144 return datamanager.store(username, host, "roster", roster);
146 log("warn", "save_roster: user had no roster to save");
150 local function process_inbound_subscription_approval(username, host, jid)
151 local roster = load_roster(username, host);
152 local item = roster[jid];
153 if item and item.ask then
154 if item.subscription == "none" then
155 item.subscription = "to";
156 else -- subscription == from
157 item.subscription = "both";
160 return save_roster(username, host, roster);
164 local function process_inbound_subscription_cancellation(username, host, jid)
165 local roster = load_roster(username, host);
166 local item = roster[jid];
168 if is_contact_pending_out(username, host, jid) then
173 if item.subscription == "to" then
174 item.subscription = "none";
176 elseif item.subscription == "both" then
177 item.subscription = "from";
182 return save_roster(username, host, roster);
186 local function process_inbound_unsubscribe(username, host, jid)
187 local roster = load_roster(username, host);
188 local item = roster[jid];
190 if is_contact_pending_in(username, host, jid) then
191 roster[false].pending[jid] = nil;
195 if item.subscription == "from" then
196 item.subscription = "none";
198 elseif item.subscription == "both" then
199 item.subscription = "to";
204 return save_roster(username, host, roster);
208 local function _get_online_roster_subscription(jidA, jidB)
209 local user = bare_sessions[jidA];
210 local item = user and (user.roster[jidB] or { subscription = "none" });
211 return item and item.subscription;
213 local function is_contact_subscribed(username, host, jid)
215 local selfjid = username.."@"..host;
216 local user_subscription = _get_online_roster_subscription(selfjid, jid);
217 if user_subscription then return (user_subscription == "both" or user_subscription == "from"); end
218 local contact_subscription = _get_online_roster_subscription(jid, selfjid);
219 if contact_subscription then return (contact_subscription == "both" or contact_subscription == "to"); end
221 local roster, err = load_roster(username, host);
222 local item = roster[jid];
223 return item and (item.subscription == "from" or item.subscription == "both"), err;
226 local function is_contact_pending_in(username, host, jid)
227 local roster = load_roster(username, host);
228 return roster[false].pending[jid];
230 local function set_contact_pending_in(username, host, jid)
231 local roster = load_roster(username, host);
232 local item = roster[jid];
233 if item and (item.subscription == "from" or item.subscription == "both") then
236 roster[false].pending[jid] = true;
237 return save_roster(username, host, roster);
239 local function is_contact_pending_out(username, host, jid)
240 local roster = load_roster(username, host);
241 local item = roster[jid];
242 return item and item.ask;
244 local function set_contact_pending_out(username, host, jid) -- subscribe
245 local roster = load_roster(username, host);
246 local item = roster[jid];
247 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
251 item = {subscription = "none", groups = {}};
254 item.ask = "subscribe";
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);
258 local function unsubscribe(username, host, jid)
259 local roster = load_roster(username, host);
260 local item = roster[jid];
261 if not item then return false; end
262 if (item.subscription == "from" or item.subscription == "none") and not item.ask then
266 if item.subscription == "both" then
267 item.subscription = "from";
268 elseif item.subscription == "to" then
269 item.subscription = "none";
271 return save_roster(username, host, roster);
273 local function subscribed(username, host, jid)
274 if is_contact_pending_in(username, host, jid) then
275 local roster = load_roster(username, host);
276 local item = roster[jid];
277 if not item then -- FIXME should roster item be auto-created?
278 item = {subscription = "none", groups = {}};
281 if item.subscription == "none" then
282 item.subscription = "from";
283 else -- subscription == to
284 item.subscription = "both";
286 roster[false].pending[jid] = nil;
287 return save_roster(username, host, roster);
288 end -- TODO else implement optional feature pre-approval (ask = subscribed)
290 local function unsubscribed(username, host, jid)
291 local roster = load_roster(username, host);
292 local item = roster[jid];
293 local pending = is_contact_pending_in(username, host, jid);
295 roster[false].pending[jid] = nil;
299 if item.subscription == "from" then
300 item.subscription = "none";
302 elseif item.subscription == "both" then
303 item.subscription = "to";
307 local success = (pending or subscribed) and save_roster(username, host, roster);
308 return success, pending, subscribed;
311 local function process_outbound_subscription_request(username, host, jid)
312 local roster = load_roster(username, host);
313 local item = roster[jid];
314 if item and (item.subscription == "none" or item.subscription == "from") then
315 item.ask = "subscribe";
316 return save_roster(username, host, roster);
320 --[[function process_outbound_subscription_approval(username, host, jid)
321 local roster = load_roster(username, host);
322 local item = roster[jid];
323 if item and (item.subscription == "none" or item.subscription == "from" then
324 item.ask = "subscribe";
325 return save_roster(username, host, roster);
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;