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);
85 if hosts[host] and hosts[host].sessions[username] then
86 local roster = hosts[host].sessions[username].roster;
88 log("debug", "load_roster: loading for new user: "..username.."@"..host);
89 roster = datamanager.load(username, host, "roster") or {};
90 if not roster[false] then roster[false] = { }; end
91 hosts[host].sessions[username].roster = roster;
95 -- Attempt to load roster for non-loaded user
96 log("debug", "load_roster: loading for offline user: "..username.."@"..host);
97 return datamanager.load(username, host, "roster") or {};
100 function save_roster(username, host)
101 log("debug", "save_roster: saving roster for "..username.."@"..host);
102 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
103 local roster = hosts[host].sessions[username].roster;
104 roster[false].version = (roster[false].version or 1) + 1;
105 return datamanager.store(username, host, "roster", hosts[host].sessions[username].roster);
107 log("warn", "save_roster: user had no roster to save");
111 function process_inbound_subscription_approval(username, host, jid)
112 local roster = load_roster(username, host);
113 local item = roster[jid];
114 if item and item.ask then
115 if item.subscription == "none" then
116 item.subscription = "to";
117 else -- subscription == from
118 item.subscription = "both";
121 return datamanager.store(username, host, "roster", roster);
125 function process_inbound_subscription_cancellation(username, host, jid)
126 local roster = load_roster(username, host);
127 local item = roster[jid];
129 if is_contact_pending_out(username, host, jid) then
134 if item.subscription == "to" then
135 item.subscription = "none";
137 elseif item.subscription == "both" then
138 item.subscription = "from";
143 return datamanager.store(username, host, "roster", roster);
147 function process_inbound_unsubscribe(username, host, jid)
148 local roster = load_roster(username, host);
149 local item = roster[jid];
151 if is_contact_pending_in(username, host, jid) then
152 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
156 if item.subscription == "from" then
157 item.subscription = "none";
159 elseif item.subscription == "both" then
160 item.subscription = "to";
165 return datamanager.store(username, host, "roster", roster);
169 function is_contact_subscribed(username, host, jid)
170 local roster = load_roster(username, host);
171 local item = roster[jid];
172 return item and (item.subscription == "from" or item.subscription == "both");
175 function is_contact_pending_in(username, host, jid)
176 local roster = load_roster(username, host);
177 return roster.pending and roster.pending[jid];
179 function set_contact_pending_in(username, host, jid, pending)
180 local roster = load_roster(username, host);
181 local item = roster[jid];
182 if item and (item.subscription == "from" or item.subscription == "both") then
185 if not roster.pending then roster.pending = {}; end
186 roster.pending[jid] = true;
187 return datamanager.store(username, host, "roster", roster);
189 function is_contact_pending_out(username, host, jid)
190 local roster = load_roster(username, host);
191 local item = roster[jid];
192 return item and item.ask;
194 function set_contact_pending_out(username, host, jid) -- subscribe
195 local roster = load_roster(username, host);
196 local item = roster[jid];
197 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
201 item = {subscription = "none", groups = {}};
204 item.ask = "subscribe";
205 log("debug", "set_contact_pending_out: saving roster; set "..username.."@"..host..".roster["..jid.."].ask=subscribe");
206 return datamanager.store(username, host, "roster", roster);
208 function unsubscribe(username, host, jid)
209 local roster = load_roster(username, host);
210 local item = roster[jid];
211 if not item then return false; end
212 if (item.subscription == "from" or item.subscription == "none") and not item.ask then
216 if item.subscription == "both" then
217 item.subscription = "from";
218 elseif item.subscription == "to" then
219 item.subscription = "none";
221 return datamanager.store(username, host, "roster", roster);
223 function subscribed(username, host, jid)
224 if is_contact_pending_in(username, host, jid) then
225 local roster = load_roster(username, host);
226 local item = roster[jid];
227 if not item then -- FIXME should roster item be auto-created?
228 item = {subscription = "none", groups = {}};
231 if item.subscription == "none" then
232 item.subscription = "from";
233 else -- subscription == to
234 item.subscription = "both";
236 roster.pending[jid] = nil;
237 -- TODO maybe remove roster.pending if empty
238 return datamanager.store(username, host, "roster", roster);
239 end -- TODO else implement optional feature pre-approval (ask = subscribed)
241 function unsubscribed(username, host, jid)
242 local roster = load_roster(username, host);
243 local item = roster[jid];
244 local pending = is_contact_pending_in(username, host, jid);
246 if is_contact_pending_in(username, host, jid) then
247 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
251 if item.subscription == "from" then
252 item.subscription = "none";
254 elseif item.subscription == "both" then
255 item.subscription = "to";
260 return datamanager.store(username, host, "roster", roster);
264 function process_outbound_subscription_request(username, host, jid)
265 local roster = load_roster(username, host);
266 local item = roster[jid];
267 if item and (item.subscription == "none" or item.subscription == "from") then
268 item.ask = "subscribe";
269 return datamanager.store(username, host, "roster", roster);
273 --[[function process_outbound_subscription_approval(username, host, jid)
274 local roster = load_roster(username, host);
275 local item = roster[jid];
276 if item and (item.subscription == "none" or item.subscription == "from" then
277 item.ask = "subscribe";
278 return datamanager.store(username, host, "roster", roster);