2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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");
14 local setmetatable = setmetatable;
15 local format = string.format;
16 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
17 local pairs, ipairs = pairs, ipairs;
18 local tostring = tostring;
22 local datamanager = require "util.datamanager"
23 local st = require "util.stanza";
25 module "rostermanager"
27 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 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 function roster_push(username, host, jid)
58 local roster = jid and jid ~= "pending" 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 function load_roster(username, host)
84 local jid = username.."@"..host;
85 log("debug", "load_roster: asked for: "..jid);
86 local user = bare_sessions[jid];
90 if roster then return roster; end
91 log("debug", "load_roster: loading for new user: "..username.."@"..host);
92 else -- Attempt to load roster for non-loaded user
93 log("debug", "load_roster: loading for offline user: "..username.."@"..host);
95 roster = datamanager.load(username, host, "roster") or {};
96 if user then user.roster = roster; end
97 if not roster[false] then roster[false] = { }; end
100 log("warn", "roster for "..jid.." has a self-contact");
102 hosts[host].events.fire_event("roster-load", username, host, roster);
106 function save_roster(username, host, roster)
107 log("debug", "save_roster: saving roster for "..username.."@"..host);
109 roster = hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster;
111 -- --roster = load_roster(username, host);
112 -- return true; -- roster unchanged, no reason to save
116 if not roster[false] then roster[false] = {}; end
117 roster[false].version = (roster[false].version or 0) + 1;
118 return datamanager.store(username, host, "roster", roster);
120 log("warn", "save_roster: user had no roster to save");
124 function process_inbound_subscription_approval(username, host, jid)
125 local roster = load_roster(username, host);
126 local item = roster[jid];
127 if item and item.ask then
128 if item.subscription == "none" then
129 item.subscription = "to";
130 else -- subscription == from
131 item.subscription = "both";
134 return save_roster(username, host, roster);
138 function process_inbound_subscription_cancellation(username, host, jid)
139 local roster = load_roster(username, host);
140 local item = roster[jid];
142 if is_contact_pending_out(username, host, jid) then
147 if item.subscription == "to" then
148 item.subscription = "none";
150 elseif item.subscription == "both" then
151 item.subscription = "from";
156 return save_roster(username, host, roster);
160 function process_inbound_unsubscribe(username, host, jid)
161 local roster = load_roster(username, host);
162 local item = roster[jid];
164 if is_contact_pending_in(username, host, jid) then
165 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
169 if item.subscription == "from" then
170 item.subscription = "none";
172 elseif item.subscription == "both" then
173 item.subscription = "to";
178 return save_roster(username, host, roster);
182 function is_contact_subscribed(username, host, jid)
183 local roster = load_roster(username, host);
184 local item = roster[jid];
185 return item and (item.subscription == "from" or item.subscription == "both");
188 function is_contact_pending_in(username, host, jid)
189 local roster = load_roster(username, host);
190 return roster.pending and roster.pending[jid];
192 function set_contact_pending_in(username, host, jid, pending)
193 local roster = load_roster(username, host);
194 local item = roster[jid];
195 if item and (item.subscription == "from" or item.subscription == "both") then
198 if not roster.pending then roster.pending = {}; end
199 roster.pending[jid] = true;
200 return save_roster(username, host, roster);
202 function is_contact_pending_out(username, host, jid)
203 local roster = load_roster(username, host);
204 local item = roster[jid];
205 return item and item.ask;
207 function set_contact_pending_out(username, host, jid) -- subscribe
208 local roster = load_roster(username, host);
209 local item = roster[jid];
210 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
214 item = {subscription = "none", groups = {}};
217 item.ask = "subscribe";
218 log("debug", "set_contact_pending_out: saving roster; set "..username.."@"..host..".roster["..jid.."].ask=subscribe");
219 return save_roster(username, host, roster);
221 function unsubscribe(username, host, jid)
222 local roster = load_roster(username, host);
223 local item = roster[jid];
224 if not item then return false; end
225 if (item.subscription == "from" or item.subscription == "none") and not item.ask then
229 if item.subscription == "both" then
230 item.subscription = "from";
231 elseif item.subscription == "to" then
232 item.subscription = "none";
234 return save_roster(username, host, roster);
236 function subscribed(username, host, jid)
237 if is_contact_pending_in(username, host, jid) then
238 local roster = load_roster(username, host);
239 local item = roster[jid];
240 if not item then -- FIXME should roster item be auto-created?
241 item = {subscription = "none", groups = {}};
244 if item.subscription == "none" then
245 item.subscription = "from";
246 else -- subscription == to
247 item.subscription = "both";
249 roster.pending[jid] = nil;
250 -- TODO maybe remove roster.pending if empty
251 return save_roster(username, host, roster);
252 end -- TODO else implement optional feature pre-approval (ask = subscribed)
254 function unsubscribed(username, host, jid)
255 local roster = load_roster(username, host);
256 local item = roster[jid];
257 local pending = is_contact_pending_in(username, host, jid);
259 if is_contact_pending_in(username, host, jid) then
260 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
264 if item.subscription == "from" then
265 item.subscription = "none";
267 elseif item.subscription == "both" then
268 item.subscription = "to";
273 return save_roster(username, host, roster);
277 function process_outbound_subscription_request(username, host, jid)
278 local roster = load_roster(username, host);
279 local item = roster[jid];
280 if item and (item.subscription == "none" or item.subscription == "from") then
281 item.ask = "subscribe";
282 return save_roster(username, host, roster);
286 --[[function process_outbound_subscription_approval(username, host, jid)
287 local roster = load_roster(username, host);
288 local item = roster[jid];
289 if item and (item.subscription == "none" or item.subscription == "from" then
290 item.ask = "subscribe";
291 return save_roster(username, host, roster);