3 local log = require "util.logger".init("rostermanager");
5 local setmetatable = setmetatable;
6 local format = string.format;
7 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
8 local pairs, ipairs = pairs, ipairs;
12 local datamanager = require "util.datamanager"
13 local st = require "util.stanza";
15 module "rostermanager"
17 function add_to_roster(session, jid, item)
18 if session.roster then
19 local old_item = session.roster[jid];
20 session.roster[jid] = item;
21 if save_roster(session.username, session.host) then
24 session.roster[jid] = old_item;
25 return nil, "wait", "internal-server-error", "Unable to save roster";
28 return nil, "auth", "not-authorized", "Session's roster not loaded";
32 function remove_from_roster(session, jid)
33 if session.roster then
34 local old_item = session.roster[jid];
35 session.roster[jid] = nil;
36 if save_roster(session.username, session.host) then
39 session.roster[jid] = old_item;
40 return nil, "wait", "internal-server-error", "Unable to save roster";
43 return nil, "auth", "not-authorized", "Session's roster not loaded";
47 function roster_push(username, host, jid)
48 if jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
49 local item = hosts[host].sessions[username].roster[jid];
50 local stanza = st.iq({type="set"});
51 stanza:tag("query", {xmlns = "jabber:iq:roster"});
53 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask});
54 for group in pairs(item.groups) do
55 stanza:tag("group"):text(group):up();
58 stanza:tag("item", {jid = jid, subscription = "remove"});
60 stanza:up(); -- move out from item
61 stanza:up(); -- move out from stanza
63 for _, session in pairs(hosts[host].sessions[username].sessions) do
64 if session.interested then
65 -- FIXME do we need to set stanza.attr.to?
72 function load_roster(username, host)
73 log("debug", "load_roster: asked for: "..username.."@"..host);
74 if hosts[host] and hosts[host].sessions[username] then
75 local roster = hosts[host].sessions[username].roster;
77 log("debug", "load_roster: loading for new user: "..username.."@"..host);
78 roster = datamanager.load(username, host, "roster") or {};
79 hosts[host].sessions[username].roster = roster;
83 -- Attempt to load roster for non-loaded user
84 log("debug", "load_roster: loading for offline user: "..username.."@"..host);
85 return datamanager.load(username, host, "roster") or {};
88 function save_roster(username, host)
89 log("debug", "save_roster: saving roster for "..username.."@"..host);
90 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
91 return datamanager.store(username, host, "roster", hosts[host].sessions[username].roster);
96 function process_inbound_subscription_approval(username, host, jid)
97 local roster = load_roster(username, host);
98 local item = roster[jid];
99 if item and item.ask then
100 if item.subscription == "none" then
101 item.subscription = "to";
102 else -- subscription == from
103 item.subscription = "both";
106 return datamanager.store(username, host, "roster", roster);
110 function process_inbound_subscription_cancellation(username, host, jid)
111 local roster = load_roster(username, host);
112 local item = roster[jid];
114 if is_contact_pending_out(username, host, jid) then
119 if item.subscription == "to" then
120 item.subscription = "none";
122 elseif item.subscription == "both" then
123 item.subscription = "from";
128 return datamanager.store(username, host, "roster", roster);
132 function process_inbound_unsubscribe(username, host, jid)
133 local roster = load_roster(username, host);
134 local item = roster[jid];
136 if is_contact_pending_in(username, host, jid) then
137 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
141 if item.subscription == "from" then
142 item.subscription = "none";
144 elseif item.subscription == "both" then
145 item.subscription = "to";
150 return datamanager.store(username, host, "roster", roster);
154 function is_contact_subscribed(username, host, jid)
155 local roster = load_roster(username, host);
156 local item = roster[jid];
157 return item and (item.subscription == "from" or item.subscription == "both");
160 function is_contact_pending_in(username, host, jid)
161 local roster = load_roster(username, host);
162 return roster.pending and roster.pending[jid];
164 function set_contact_pending_in(username, host, jid, pending)
165 local roster = load_roster(username, host);
166 local item = roster[jid];
167 if item and (item.subscription == "from" or item.subscription == "both") then
170 if not roster.pending then roster.pending = {}; end
171 roster.pending[jid] = true;
172 return datamanager.store(username, host, "roster", roster);
174 function is_contact_pending_out(username, host, jid)
175 local roster = load_roster(username, host);
176 local item = roster[jid];
177 return item and item.ask;
179 function set_contact_pending_out(username, host, jid) -- subscribe
180 local roster = load_roster(username, host);
181 local item = roster[jid];
182 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
186 item = {subscription = "none", groups = {}};
189 item.ask = "subscribe";
190 log("debug", "set_contact_pending_out: saving roster; set "..username.."@"..host..".roster["..jid.."].ask=subscribe");
191 return datamanager.store(username, host, "roster", roster);
193 function unsubscribe(username, host, jid)
194 local roster = load_roster(username, host);
195 local item = roster[jid];
196 if not item then return false; end
197 if (item.subscription == "from" or item.subscription == "none") and not item.ask then
201 if item.subscription == "both" then
202 item.subscription = "from";
203 elseif item.subscription == "to" then
204 item.subscription = "none";
206 return datamanager.store(username, host, "roster", roster);
208 function subscribed(username, host, jid)
209 if is_contact_pending_in(username, host, jid) then
210 local roster = load_roster(username, host);
211 local item = roster[jid];
212 if item.subscription == "none" then
213 item.subscription = "from";
214 else -- subscription == to
215 item.subscription = "both";
217 roster.pending[jid] = nil;
218 -- TODO maybe remove roster.pending if empty
219 return datamanager.store(username, host, "roster", roster);
220 end -- TODO else implement optional feature pre-approval (ask = subscribed)
222 function unsubscribed(username, host, jid)
223 local roster = load_roster(username, host);
224 local item = roster[jid];
225 local pending = is_contact_pending_in(username, host, jid);
227 if is_contact_pending_in(username, host, jid) then
228 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
232 if item.subscription == "from" then
233 item.subscription = "none";
235 elseif item.subscription == "both" then
236 item.subscription = "to";
241 return datamanager.store(username, host, "roster", roster);
245 function process_outbound_subscription_request(username, host, jid)
246 local roster = load_roster(username, host);
247 local item = roster[jid];
248 if item and (item.subscription == "none" or item.subscription == "from") then
249 item.ask = "subscribe";
250 return datamanager.store(username, host, "roster", roster);
254 --[[function process_outbound_subscription_approval(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);