+function process_inbound_subscription_approval(username, host, jid)
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ if item and item.ask then
+ if item.subscription == "none" then
+ item.subscription = "to";
+ else -- subscription == from
+ item.subscription = "both";
+ end
+ item.ask = nil;
+ return datamanager.store(username, host, "roster", roster);
+ end
+end
+
+function process_inbound_subscription_cancellation(username, host, jid)
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ local changed = nil;
+ if is_contact_pending_out(username, host, jid) then
+ item.ask = nil;
+ changed = true;
+ end
+ if item then
+ if item.subscription == "to" then
+ item.subscription = "none";
+ changed = true;
+ elseif item.subscription == "both" then
+ item.subscription = "from";
+ changed = true;
+ end
+ end
+ if changed then
+ return datamanager.store(username, host, "roster", roster);
+ end
+end
+
+function process_inbound_unsubscribe(username, host, jid)
+ local roster = load_roster(username, host);
+ local item = roster[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
+ if item.subscription == "from" then
+ item.subscription = "none";
+ changed = true;
+ elseif item.subscription == "both" then
+ item.subscription = "to";
+ changed = true;
+ end
+ end
+ if changed then
+ return datamanager.store(username, host, "roster", roster);
+ end
+end
+
+function is_contact_subscribed(username, host, jid)
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ return item and (item.subscription == "from" or item.subscription == "both");
+end
+
+function is_contact_pending_in(username, host, jid)
+ local roster = load_roster(username, host);
+ return roster.pending and roster.pending[jid];
+end
+function set_contact_pending_in(username, host, jid, pending)
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ if item and (item.subscription == "from" or item.subscription == "both") then
+ return; -- false
+ end
+ if not roster.pending then roster.pending = {}; end
+ roster.pending[jid] = true;
+ return datamanager.store(username, host, "roster", 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 datamanager.store(username, host, "roster", 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 datamanager.store(username, host, "roster", 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 datamanager.store(username, host, "roster", 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
+ if item.subscription == "from" then
+ item.subscription = "none";
+ changed = true;
+ elseif item.subscription == "both" then
+ item.subscription = "to";
+ changed = true;
+ end
+ end
+ if changed then
+ return datamanager.store(username, host, "roster", roster);
+ end
+end
+
+function process_outbound_subscription_request(username, host, jid)
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ if item and (item.subscription == "none" or item.subscription == "from") then
+ item.ask = "subscribe";
+ return datamanager.store(username, host, "roster", roster);
+ end
+end
+
+--[[function process_outbound_subscription_approval(username, host, jid)
+ local roster = load_roster(username, host);
+ local item = roster[jid];
+ if item and (item.subscription == "none" or item.subscription == "from" then
+ item.ask = "subscribe";
+ return datamanager.store(username, host, "roster", roster);
+ end
+end]]
+
+
+
+return _M;