3 local function log(type, message)
4 mainlog(type, "rostermanager", message);
7 local setmetatable = setmetatable;
8 local format = string.format;
9 local loadfile, setfenv, pcall = loadfile, setfenv, pcall;
10 local pairs, ipairs = pairs, ipairs;
14 require "util.datamanager"
16 local datamanager = datamanager;
17 local st = require "util.stanza";
19 module "rostermanager"
21 function add_to_roster(session, jid, item)
22 if session.roster then
23 local old_item = session.roster[jid];
24 session.roster[jid] = item;
25 if save_roster(session.username, session.host) then
28 session.roster[jid] = old_item;
29 return nil, "wait", "internal-server-error", "Unable to save roster";
32 return nil, "auth", "not-authorized", "Session's roster not loaded";
36 function remove_from_roster(session, jid)
37 if session.roster then
38 local old_item = session.roster[jid];
39 session.roster[jid] = nil;
40 if save_roster(session.username, session.host) then
43 session.roster[jid] = old_item;
44 return nil, "wait", "internal-server-error", "Unable to save roster";
47 return nil, "auth", "not-authorized", "Session's roster not loaded";
51 function roster_push(username, host, jid)
52 if jid ~= "pending" and hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
53 local item = hosts[host].sessions[username].roster[jid];
54 local stanza = st.iq({type="set"});
55 stanza:tag("query", {xmlns = "jabber:iq:roster"});
57 stanza:tag("item", {jid = jid, subscription = item.subscription, name = item.name, ask = item.ask});
58 for group in pairs(item.groups) do
59 stanza:tag("group"):text(group):up();
62 stanza:tag("item", {jid = jid, subscription = "remove"});
67 for _, session in pairs(hosts[host].sessions[username].sessions) do
68 if session.interested then
69 -- FIXME do we need to set stanza.attr.to?
76 function load_roster(username, host)
77 log("debug", "load_roster: asked for: "..username.."@"..host);
78 if hosts[host] and hosts[host].sessions[username] then
79 local roster = hosts[host].sessions[username].roster;
81 log("debug", "load_roster: loading for new user: "..username.."@"..host);
82 roster = datamanager.load(username, host, "roster") or {};
83 hosts[host].sessions[username].roster = roster;
87 -- Attempt to load roster for non-loaded user
88 log("debug", "load_roster: loading for offline user: "..username.."@"..host);
89 return datamanager.load(username, host, "roster") or {};
92 function save_roster(username, host)
93 log("debug", "save_roster: saving roster for "..username.."@"..host);
94 if hosts[host] and hosts[host].sessions[username] and hosts[host].sessions[username].roster then
95 return datamanager.store(username, host, "roster", hosts[host].sessions[username].roster);
100 function process_inbound_subscription_approval(username, host, jid)
101 local roster = load_roster(username, host);
102 local item = roster[jid];
103 if item and item.ask then
104 if item.subscription == "none" then
105 item.subscription = "to";
106 else -- subscription == from
107 item.subscription = "both";
110 return datamanager.store(username, host, "roster", roster);
114 function process_inbound_subscription_cancellation(username, host, jid)
115 local roster = load_roster(username, host);
116 local item = roster[jid];
118 if is_contact_pending_out(username, host, jid) then
123 if item.subscription == "to" then
124 item.subscription = "none";
126 elseif item.subscription == "both" then
127 item.subscription = "from";
132 return datamanager.store(username, host, "roster", roster);
136 function process_inbound_unsubscribe(username, host, jid)
137 local roster = load_roster(username, host);
138 local item = roster[jid];
140 if is_contact_pending_in(username, host, jid) then
141 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
145 if item.subscription == "from" then
146 item.subscription = "none";
148 elseif item.subscription == "both" then
149 item.subscription = "to";
154 return datamanager.store(username, host, "roster", roster);
158 function is_contact_subscribed(username, host, jid)
159 local roster = load_roster(username, host);
160 local item = roster[jid];
161 return item and (item.subscription == "from" or item.subscription == "both");
164 function is_contact_pending_in(username, host, jid)
165 local roster = load_roster(username, host);
166 return roster.pending and roster.pending[jid];
168 function set_contact_pending_in(username, host, jid, pending)
169 local roster = load_roster(username, host);
170 local item = roster[jid];
171 if item and (item.subscription == "from" or item.subscription == "both") then
174 if not roster.pending then roster.pending = {}; end
175 roster.pending[jid] = true;
176 return datamanager.store(username, host, "roster", roster);
178 function is_contact_pending_out(username, host, jid)
179 local roster = load_roster(username, host);
180 local item = roster[jid];
181 return item and item.ask;
183 function set_contact_pending_out(username, host, jid) -- subscribe
184 local roster = load_roster(username, host);
185 local item = roster[jid];
186 if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
190 item = {subscription = "none", groups = {}};
193 item.ask = "subscribe";
194 log("debug", "set_contact_pending_out: saving roster; set "..username.."@"..host..".roster["..jid.."].ask=subscribe");
195 return datamanager.store(username, host, "roster", roster);
197 function unsubscribe(username, host, jid)
198 local roster = load_roster(username, host);
199 local item = roster[jid];
200 if not item then return false; end
201 if (item.subscription == "from" or item.subscription == "none") and not item.ask then
205 if item.subscription == "both" then
206 item.subscription = "from";
207 elseif item.subscription == "to" then
208 item.subscription = "none";
210 return datamanager.store(username, host, "roster", roster);
212 function subscribed(username, host, jid)
213 if is_contact_pending_in(username, host, jid) then
214 local roster = load_roster(username, host);
215 local item = roster[jid];
216 if item.subscription == "none" then
217 item.subscription = "from";
218 else -- subscription == to
219 item.subscription = "both";
221 roster.pending[jid] = nil;
222 -- TODO maybe remove roster.pending if empty
223 return datamanager.store(username, host, "roster", roster);
224 end -- TODO else implement optional feature pre-approval (ask = subscribed)
226 function unsubscribed(username, host, jid)
227 local roster = load_roster(username, host);
228 local item = roster[jid];
229 local pending = is_contact_pending_in(username, host, jid);
231 if is_contact_pending_in(username, host, jid) then
232 roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
236 if item.subscription == "from" then
237 item.subscription = "none";
239 elseif item.subscription == both then
240 item.subscription = "to";
245 return datamanager.store(username, host, "roster", roster);
249 function process_outbound_subscription_request(username, host, jid)
250 local roster = load_roster(username, host);
251 local item = roster[jid];
252 if item and (item.subscription == "none" or item.subscription == "from") then
253 item.ask = "subscribe";
254 return datamanager.store(username, host, "roster", roster);
258 --[[function process_outbound_subscription_approval(username, host, jid)
259 local roster = load_roster(username, host);
260 local item = roster[jid];
261 if item and (item.subscription == "none" or item.subscription == "from" then
262 item.ask = "subscribe";
263 return datamanager.store(username, host, "roster", roster);