mod_pinger/mod_pinger.lua
changeset 3107 5bf79bb3cf7e
parent 2678 c971b2cee2cc
child 3117 8298b06e6603
equal deleted inserted replaced
3106:f04dbfad5407 3107:5bf79bb3cf7e
    11 	return data;
    11 	return data;
    12 end
    12 end
    13 
    13 
    14 function check_session(watchdog)
    14 function check_session(watchdog)
    15 	local session = watchdog.session;
    15 	local session = watchdog.session;
       
    16 	if session.smacks then
       
    17 		unwatch_session(session);
       
    18 		return;
       
    19 	end
    16 	if not session.idle_pinged then
    20 	if not session.idle_pinged then
    17 		session.idle_pinged = true;
    21 		session.idle_pinged = true;
    18 		if session.smacks then
    22 		session.send(st.iq({ type = "get", from = module.host, id = "idle-check" })
    19 			if not session.awaiting_ack then
    23 				:tag("ping", { xmlns = "urn:xmpp:ping" }));
    20 				session.send(st.stanza("r", { xmlns = session.smacks }))
       
    21 			end
       
    22 		else
       
    23 			session.send(st.iq({ type = "get", from = module.host, id = "idle-check" })
       
    24 					:tag("ping", { xmlns = "urn:xmpp:ping" }));
       
    25 		end
       
    26 		return ping_timeout; -- Call us again after ping_timeout
    24 		return ping_timeout; -- Call us again after ping_timeout
    27 	else
    25 	else
    28 		module:log("info", "Client %q silent for too long, closing...", session.full_jid);
    26 		module:log("info", "Client %q silent for too long, closing...", session.full_jid);
    29 		session:close("connection-timeout");
    27 		session:close("connection-timeout");
    30 	end
    28 	end
    40 	end
    38 	end
    41 end
    39 end
    42 
    40 
    43 function unwatch_session(session)
    41 function unwatch_session(session)
    44 	if session.idle_watchdog then
    42 	if session.idle_watchdog then
       
    43 		filters.remove_filter(session, "bytes/in", update_watchdog);
    45 		session.idle_watchdog:cancel();
    44 		session.idle_watchdog:cancel();
    46 		session.idle_watchdog = nil;
    45 		session.idle_watchdog = nil;
    47 		filters.remove_filter(session, "bytes/in", update_watchdog);
       
    48 	end
    46 	end
    49 end
    47 end
    50 
    48 
    51 module:hook("resource-bind", function (event) watch_session(event.session); end);
    49 module:hook("resource-bind", function (event) watch_session(event.session); end);
    52 module:hook("resource-unbind", function (event) unwatch_session(event.session); end);
    50 module:hook("resource-unbind", function (event) unwatch_session(event.session); end);
    53 
       
    54 -- handle smacks sessions properly (not pinging in hibernated state)
       
    55 module:hook("smacks-hibernation-start", function (event) unwatch_session(event.origin); end);
       
    56 module:hook("smacks-hibernation-end", function (event) watch_session(event.resumed); end);