2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 local log = require "util.logger".init("rostermanager");
25 local setmetatable = setmetatable;
26 local format = string.format;
27 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
28 local pairs, ipairs = pairs, ipairs;
32 local datamanager = require "util.datamanager"
33 local st = require "util.stanza";
35 module "rostermanager"
37 function add_to_roster(session, jid, item)
38 if session.roster then
39 local old_item = session.roster[jid];
40 session.roster[jid] = item;
41 if save_roster(session.username, session.host) then
44 session.roster[jid] = old_item;
45 return nil, "wait", "internal-server-error", "Unable to save roster";
48 return nil, "auth", "not-authorized", "Session's roster not loaded";
52 function remove_from_roster(session, jid)
53 if session.roster then
54 local old_item = session.roster[jid];
55 session.roster[jid] = nil;
56 if save_roster(session.username, session.host) then
59 session.roster[jid] = old_item;
60 return nil, "wait", "internal-server-error", "Unable to save roster";
63 return nil, "auth", "not-authorized", "Session's roster not loaded";
67 function roster_push(username, host, jid)
68 if jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
69 local item = hosts[host].sessions[username].roster[jid];
70 local stanza = st.iq({type="set"});
71 stanza:tag("query", {xmlns = "jabber:iq:roster"});
73 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask});
74 for group in pairs(item.groups) do
75 stanza:tag("group"):text(group):up();
78 stanza:tag("item", {jid = jid, subscription = "remove"});
80 stanza:up(); -- move out from item
81 stanza:up(); -- move out from stanza
83 for _, session in pairs(hosts[host].sessions[username].sessions) do
84 if session.interested then
85 -- FIXME do we need to set stanza.attr.to?
92 function load_roster(username, host)
93 log("debug", "load_roster: asked for: "..username.."@"..host);
94 if hosts[host] and hosts[host].sessions[username] then
95 local roster = hosts[host].sessions[username].roster;
97 log("debug", "load_roster: loading for new user: "..username.."@"..host);
98 roster = datamanager.load(username, host, "roster") or {};
99 hosts[host].sessions[username].roster = roster;
103 -- Attempt to load roster for non-loaded user
104 log("debug", "load_roster: loading for offline user: "..username.."@"..host);
105 return datamanager.load(username, host, "roster") or {};
108 function save_roster(username, host)
109 log("debug", "save_roster: saving roster for "..username.."@"..host);
110 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
111 return datamanager.store(username, host, "roster", hosts[host].sessions[username].roster);
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 item.subscription == "none" then
233 item.subscription = "from";
234 else -- subscription == to
235 item.subscription = "both";
237 roster.pending[jid] = nil;
238 -- TODO maybe remove roster.pending if empty
239 return datamanager.store(username, host, "roster", roster);
240 end -- TODO else implement optional feature pre-approval (ask = subscribed)
242 function unsubscribed(username, host, jid)
243 local roster = load_roster(username, host);
244 local item = roster[jid];
245 local pending = is_contact_pending_in(username, host, jid);
247 if is_contact_pending_in(username, host, jid) then
248 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
252 if item.subscription == "from" then
253 item.subscription = "none";
255 elseif item.subscription == "both" then
256 item.subscription = "to";
261 return datamanager.store(username, host, "roster", roster);
265 function process_outbound_subscription_request(username, host, jid)
266 local roster = load_roster(username, host);
267 local item = roster[jid];
268 if item and (item.subscription == "none" or item.subscription == "from") then
269 item.ask = "subscribe";
270 return datamanager.store(username, host, "roster", roster);
274 --[[function process_outbound_subscription_approval(username, host, jid)
275 local roster = load_roster(username, host);
276 local item = roster[jid];
277 if item and (item.subscription == "none" or item.subscription == "from" then
278 item.ask = "subscribe";
279 return datamanager.store(username, host, "roster", roster);