2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 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;
21 local datamanager = require "util.datamanager"
22 local st = require "util.stanza";
24 module "rostermanager"
26 function add_to_roster(session, jid, item)
27 if session.roster then
28 local old_item = session.roster[jid];
29 session.roster[jid] = item;
30 if save_roster(session.username, session.host) then
33 session.roster[jid] = old_item;
34 return nil, "wait", "internal-server-error", "Unable to save roster";
37 return nil, "auth", "not-authorized", "Session's roster not loaded";
41 function remove_from_roster(session, jid)
42 if session.roster then
43 local old_item = session.roster[jid];
44 session.roster[jid] = nil;
45 if save_roster(session.username, session.host) then
48 session.roster[jid] = old_item;
49 return nil, "wait", "internal-server-error", "Unable to save roster";
52 return nil, "auth", "not-authorized", "Session's roster not loaded";
56 function roster_push(username, host, jid)
57 if jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
58 local item = hosts[host].sessions[username].roster[jid];
59 local stanza = st.iq({type="set"});
60 stanza:tag("query", {xmlns = "jabber:iq:roster"});
62 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask});
63 for group in pairs(item.groups) do
64 stanza:tag("group"):text(group):up();
67 stanza:tag("item", {jid = jid, subscription = "remove"});
69 stanza:up(); -- move out from item
70 stanza:up(); -- move out from stanza
72 for _, session in pairs(hosts[host].sessions[username].sessions) do
73 if session.interested then
74 -- FIXME do we need to set stanza.attr.to?
81 function load_roster(username, host)
82 log("debug", "load_roster: asked for: "..username.."@"..host);
83 if hosts[host] and hosts[host].sessions[username] then
84 local roster = hosts[host].sessions[username].roster;
86 log("debug", "load_roster: loading for new user: "..username.."@"..host);
87 roster = datamanager.load(username, host, "roster") or {};
88 hosts[host].sessions[username].roster = roster;
92 -- Attempt to load roster for non-loaded user
93 log("debug", "load_roster: loading for offline user: "..username.."@"..host);
94 return datamanager.load(username, host, "roster") or {};
97 function save_roster(username, host)
98 log("debug", "save_roster: saving roster for "..username.."@"..host);
99 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
100 return datamanager.store(username, host, "roster", hosts[host].sessions[username].roster);
105 function process_inbound_subscription_approval(username, host, jid)
106 local roster = load_roster(username, host);
107 local item = roster[jid];
108 if item and item.ask then
109 if item.subscription == "none" then
110 item.subscription = "to";
111 else -- subscription == from
112 item.subscription = "both";
115 return datamanager.store(username, host, "roster", roster);
119 function process_inbound_subscription_cancellation(username, host, jid)
120 local roster = load_roster(username, host);
121 local item = roster[jid];
123 if is_contact_pending_out(username, host, jid) then
128 if item.subscription == "to" then
129 item.subscription = "none";
131 elseif item.subscription == "both" then
132 item.subscription = "from";
137 return datamanager.store(username, host, "roster", roster);
141 function process_inbound_unsubscribe(username, host, jid)
142 local roster = load_roster(username, host);
143 local item = roster[jid];
145 if is_contact_pending_in(username, host, jid) then
146 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
150 if item.subscription == "from" then
151 item.subscription = "none";
153 elseif item.subscription == "both" then
154 item.subscription = "to";
159 return datamanager.store(username, host, "roster", roster);
163 function is_contact_subscribed(username, host, jid)
164 local roster = load_roster(username, host);
165 local item = roster[jid];
166 return item and (item.subscription == "from" or item.subscription == "both");
169 function is_contact_pending_in(username, host, jid)
170 local roster = load_roster(username, host);
171 return roster.pending and roster.pending[jid];
173 function set_contact_pending_in(username, host, jid, pending)
174 local roster = load_roster(username, host);
175 local item = roster[jid];
176 if item and (item.subscription == "from" or item.subscription == "both") then
179 if not roster.pending then roster.pending = {}; end
180 roster.pending[jid] = true;
181 return datamanager.store(username, host, "roster", roster);
183 function is_contact_pending_out(username, host, jid)
184 local roster = load_roster(username, host);
185 local item = roster[jid];
186 return item and item.ask;
188 function set_contact_pending_out(username, host, jid) -- subscribe
189 local roster = load_roster(username, host);
190 local item = roster[jid];
191 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
195 item = {subscription = "none", groups = {}};
198 item.ask = "subscribe";
199 log("debug", "set_contact_pending_out: saving roster; set "..username.."@"..host..".roster["..jid.."].ask=subscribe");
200 return datamanager.store(username, host, "roster", roster);
202 function unsubscribe(username, host, jid)
203 local roster = load_roster(username, host);
204 local item = roster[jid];
205 if not item then return false; end
206 if (item.subscription == "from" or item.subscription == "none") and not item.ask then
210 if item.subscription == "both" then
211 item.subscription = "from";
212 elseif item.subscription == "to" then
213 item.subscription = "none";
215 return datamanager.store(username, host, "roster", roster);
217 function subscribed(username, host, jid)
218 if is_contact_pending_in(username, host, jid) then
219 local roster = load_roster(username, host);
220 local item = roster[jid];
221 if item.subscription == "none" then
222 item.subscription = "from";
223 else -- subscription == to
224 item.subscription = "both";
226 roster.pending[jid] = nil;
227 -- TODO maybe remove roster.pending if empty
228 return datamanager.store(username, host, "roster", roster);
229 end -- TODO else implement optional feature pre-approval (ask = subscribed)
231 function unsubscribed(username, host, jid)
232 local roster = load_roster(username, host);
233 local item = roster[jid];
234 local pending = is_contact_pending_in(username, host, jid);
236 if is_contact_pending_in(username, host, jid) then
237 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
241 if item.subscription == "from" then
242 item.subscription = "none";
244 elseif item.subscription == "both" then
245 item.subscription = "to";
250 return datamanager.store(username, host, "roster", roster);
254 function process_outbound_subscription_request(username, host, jid)
255 local roster = load_roster(username, host);
256 local item = roster[jid];
257 if item and (item.subscription == "none" or item.subscription == "from") then
258 item.ask = "subscribe";
259 return datamanager.store(username, host, "roster", roster);
263 --[[function process_outbound_subscription_approval(username, host, jid)
264 local roster = load_roster(username, host);
265 local item = roster[jid];
266 if item and (item.subscription == "none" or item.subscription == "from" then
267 item.ask = "subscribe";
268 return datamanager.store(username, host, "roster", roster);