core/rostermanager.lua
changeset 6790 ad64e2eacd0e
parent 6782 6236668da30a
child 7067 b98006bfd97a
equal deleted inserted replaced
6789:3deeb38d79ab 6790:ad64e2eacd0e
    21 local datamanager = require "util.datamanager"
    21 local datamanager = require "util.datamanager"
    22 local um_user_exists = require "core.usermanager".user_exists;
    22 local um_user_exists = require "core.usermanager".user_exists;
    23 local st = require "util.stanza";
    23 local st = require "util.stanza";
    24 
    24 
    25 local _ENV = nil;
    25 local _ENV = nil;
       
    26 
       
    27 local save_roster; -- forward declaration
    26 
    28 
    27 local function add_to_roster(session, jid, item)
    29 local function add_to_roster(session, jid, item)
    28 	if session.roster then
    30 	if session.roster then
    29 		local old_item = session.roster[jid];
    31 		local old_item = session.roster[jid];
    30 		session.roster[jid] = item;
    32 		session.roster[jid] = item;
   119 		hosts[host].events.fire_event("roster-load", { username = username, host = host, roster = roster });
   121 		hosts[host].events.fire_event("roster-load", { username = username, host = host, roster = roster });
   120 	end
   122 	end
   121 	return roster, err;
   123 	return roster, err;
   122 end
   124 end
   123 
   125 
   124 local function save_roster(username, host, roster)
   126 function save_roster(username, host, roster)
   125 	if not um_user_exists(username, host) then
   127 	if not um_user_exists(username, host) then
   126 		log("debug", "not saving roster for %s@%s: the user doesn't exist", username, host);
   128 		log("debug", "not saving roster for %s@%s: the user doesn't exist", username, host);
   127 		return nil;
   129 		return nil;
   128 	end
   130 	end
   129 
   131 
   159 		item.ask = nil;
   161 		item.ask = nil;
   160 		return save_roster(username, host, roster);
   162 		return save_roster(username, host, roster);
   161 	end
   163 	end
   162 end
   164 end
   163 
   165 
       
   166 local is_contact_pending_out -- forward declaration
       
   167 
   164 local function process_inbound_subscription_cancellation(username, host, jid)
   168 local function process_inbound_subscription_cancellation(username, host, jid)
   165 	local roster = load_roster(username, host);
   169 	local roster = load_roster(username, host);
   166 	local item = roster[jid];
   170 	local item = roster[jid];
   167 	local changed = nil;
   171 	local changed = nil;
   168 	if is_contact_pending_out(username, host, jid) then
   172 	if is_contact_pending_out(username, host, jid) then
   181 	if changed then
   185 	if changed then
   182 		return save_roster(username, host, roster);
   186 		return save_roster(username, host, roster);
   183 	end
   187 	end
   184 end
   188 end
   185 
   189 
       
   190 local is_contact_pending_in -- forward declaration
       
   191 
   186 local function process_inbound_unsubscribe(username, host, jid)
   192 local function process_inbound_unsubscribe(username, host, jid)
   187 	local roster = load_roster(username, host);
   193 	local roster = load_roster(username, host);
   188 	local item = roster[jid];
   194 	local item = roster[jid];
   189 	local changed = nil;
   195 	local changed = nil;
   190 	if is_contact_pending_in(username, host, jid) then
   196 	if is_contact_pending_in(username, host, jid) then
   221 	local roster, err = load_roster(username, host);
   227 	local roster, err = load_roster(username, host);
   222 	local item = roster[jid];
   228 	local item = roster[jid];
   223 	return item and (item.subscription == "from" or item.subscription == "both"), err;
   229 	return item and (item.subscription == "from" or item.subscription == "both"), err;
   224 end
   230 end
   225 
   231 
   226 local function is_contact_pending_in(username, host, jid)
   232 function is_contact_pending_in(username, host, jid)
   227 	local roster = load_roster(username, host);
   233 	local roster = load_roster(username, host);
   228 	return roster[false].pending[jid];
   234 	return roster[false].pending[jid];
   229 end
   235 end
   230 local function set_contact_pending_in(username, host, jid)
   236 local function set_contact_pending_in(username, host, jid)
   231 	local roster = load_roster(username, host);
   237 	local roster = load_roster(username, host);
   234 		return; -- false
   240 		return; -- false
   235 	end
   241 	end
   236 	roster[false].pending[jid] = true;
   242 	roster[false].pending[jid] = true;
   237 	return save_roster(username, host, roster);
   243 	return save_roster(username, host, roster);
   238 end
   244 end
   239 local function is_contact_pending_out(username, host, jid)
   245 function is_contact_pending_out(username, host, jid)
   240 	local roster = load_roster(username, host);
   246 	local roster = load_roster(username, host);
   241 	local item = roster[jid];
   247 	local item = roster[jid];
   242 	return item and item.ask;
   248 	return item and item.ask;
   243 end
   249 end
   244 local function set_contact_pending_out(username, host, jid) -- subscribe
   250 local function set_contact_pending_out(username, host, jid) -- subscribe