+ return; -- false
+ end
+ if not roster.pending then roster.pending = {}; end
+ roster.pending[jid] = true;
+ return save_roster(username, host, roster);
+end
+function is_contact_pending_out(username, host, jid)
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ return item and item.ask;
+end
+function set_contact_pending_out(username, host, jid) -- subscribe
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ if item and (item.ask or item.subscription == "to" or item.subscription == "both") then
+ return true;
+ end
+ if not item then
+ item = {subscription = "none", groups = {}};
+ roster[jid] = item;
+ end
+ item.ask = "subscribe";
+ log("debug", "set_contact_pending_out: saving roster; set "..username.."@"..host..".roster["..jid.."].ask=subscribe");
+ return save_roster(username, host, roster);
+end
+function unsubscribe(username, host, jid)
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ if not item then return false; end
+ if (item.subscription == "from" or item.subscription == "none") and not item.ask then
+ return true;
+ end
+ item.ask = nil;
+ if item.subscription == "both" then
+ item.subscription = "from";
+ elseif item.subscription == "to" then
+ item.subscription = "none";
+ end
+ return save_roster(username, host, roster);
+end
+function subscribed(username, host, jid)
+ if is_contact_pending_in(username, host, jid) then
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ if not item then -- FIXME should roster item be auto-created?
+ item = {subscription = "none", groups = {}};
+ roster[jid] = item;
+ end
+ if item.subscription == "none" then
+ item.subscription = "from";
+ else -- subscription == to
+ item.subscription = "both";
+ end
+ roster.pending[jid] = nil;
+ -- TODO maybe remove roster.pending if empty
+ return save_roster(username, host, roster);
+ end -- TODO else implement optional feature pre-approval (ask = subscribed)
+end
+function unsubscribed(username, host, jid)
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ local pending = is_contact_pending_in(username, host, jid);
+ local changed = nil;
+ if is_contact_pending_in(username, host, jid) then
+ roster.pending[jid] = nil; -- TODO maybe delete roster.pending if empty?
+ changed = true;
+ end
+ if item then