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 log("debug", "load_roster: asked for: "..username.."@"..host);
86 if hosts[host] and hosts[host].sessions[username] then
87 roster = hosts[host].sessions[username].roster;
89 log("debug", "load_roster: loading for new user: "..username.."@"..host);
90 roster = datamanager.load(username, host, "roster") or {};
91 if not roster[false] then roster[false] = { }; end
92 hosts[host].sessions[username].roster = roster;
93 hosts[host].events.fire_event("roster-load", username, host, roster);
98 -- Attempt to load roster for non-loaded user
99 log("debug", "load_roster: loading for offline user: "..username.."@"..host);
100 roster = datamanager.load(username, host, "roster") or {};
101 hosts[host].events.fire_event("roster-load", username, host, roster);
105 function save_roster(username, host)
106 log("debug", "save_roster: saving roster for "..username.."@"..host);
107 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
108 local roster = hosts[host].sessions[username].roster;
109 roster[false].version = (roster[false].version or 1) + 1;
110 return datamanager.store(username, host, "roster", hosts[host].sessions[username].roster);
112 log("warn", "save_roster: user had no roster to save");
116 function process_inbound_subscription_approval(username, host, jid)
117 local roster = load_roster(username, host);
118 local item = roster[jid];
119 if item and item.ask then
120 if item.subscription == "none" then
121 item.subscription = "to";
122 else -- subscription == from
123 item.subscription = "both";
126 return datamanager.store(username, host, "roster", roster);
130 function process_inbound_subscription_cancellation(username, host, jid)
131 local roster = load_roster(username, host);
132 local item = roster[jid];
134 if is_contact_pending_out(username, host, jid) then
139 if item.subscription == "to" then
140 item.subscription = "none";
142 elseif item.subscription == "both" then
143 item.subscription = "from";
148 return datamanager.store(username, host, "roster", roster);
152 function process_inbound_unsubscribe(username, host, jid)
153 local roster = load_roster(username, host);
154 local item = roster[jid];
156 if is_contact_pending_in(username, host, jid) then
157 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
161 if item.subscription == "from" then
162 item.subscription = "none";
164 elseif item.subscription == "both" then
165 item.subscription = "to";
170 return datamanager.store(username, host, "roster", roster);
174 function is_contact_subscribed(username, host, jid)
175 local roster = load_roster(username, host);
176 local item = roster[jid];
177 return item and (item.subscription == "from" or item.subscription == "both");
180 function is_contact_pending_in(username, host, jid)
181 local roster = load_roster(username, host);
182 return roster.pending and roster.pending[jid];
184 function set_contact_pending_in(username, host, jid, pending)
185 local roster = load_roster(username, host);
186 local item = roster[jid];
187 if item and (item.subscription == "from" or item.subscription == "both") then
190 if not roster.pending then roster.pending = {}; end
191 roster.pending[jid] = true;
192 return datamanager.store(username, host, "roster", roster);
194 function is_contact_pending_out(username, host, jid)
195 local roster = load_roster(username, host);
196 local item = roster[jid];
197 return item and item.ask;
199 function set_contact_pending_out(username, host, jid) -- subscribe
200 local roster = load_roster(username, host);
201 local item = roster[jid];
202 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
206 item = {subscription = "none", groups = {}};
209 item.ask = "subscribe";
210 log("debug", "set_contact_pending_out: saving roster; set "..username.."@"..host..".roster["..jid.."].ask=subscribe");
211 return datamanager.store(username, host, "roster", roster);
213 function unsubscribe(username, host, jid)
214 local roster = load_roster(username, host);
215 local item = roster[jid];
216 if not item then return false; end
217 if (item.subscription == "from" or item.subscription == "none") and not item.ask then
221 if item.subscription == "both" then
222 item.subscription = "from";
223 elseif item.subscription == "to" then
224 item.subscription = "none";
226 return datamanager.store(username, host, "roster", roster);
228 function subscribed(username, host, jid)
229 if is_contact_pending_in(username, host, jid) then
230 local roster = load_roster(username, host);
231 local item = roster[jid];
232 if not item then -- FIXME should roster item be auto-created?
233 item = {subscription = "none", groups = {}};
236 if item.subscription == "none" then
237 item.subscription = "from";
238 else -- subscription == to
239 item.subscription = "both";
241 roster.pending[jid] = nil;
242 -- TODO maybe remove roster.pending if empty
243 return datamanager.store(username, host, "roster", roster);
244 end -- TODO else implement optional feature pre-approval (ask = subscribed)
246 function unsubscribed(username, host, jid)
247 local roster = load_roster(username, host);
248 local item = roster[jid];
249 local pending = is_contact_pending_in(username, host, jid);
251 if is_contact_pending_in(username, host, jid) then
252 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
256 if item.subscription == "from" then
257 item.subscription = "none";
259 elseif item.subscription == "both" then
260 item.subscription = "to";
265 return datamanager.store(username, host, "roster", roster);
269 function process_outbound_subscription_request(username, host, jid)
270 local roster = load_roster(username, host);
271 local item = roster[jid];
272 if item and (item.subscription == "none" or item.subscription == "from") then
273 item.ask = "subscribe";
274 return datamanager.store(username, host, "roster", roster);
278 --[[function process_outbound_subscription_approval(username, host, jid)
279 local roster = load_roster(username, host);
280 local item = roster[jid];
281 if item and (item.subscription == "none" or item.subscription == "from" then
282 item.ask = "subscribe";
283 return datamanager.store(username, host, "roster", roster);