view mod_stanza_counter/mod_stanza_counter.lua @ 4738:5aee8d86629a

mod_bookmarks2: Fix handling of nick and password elements This form of child retrieval fails when the stanza elements internally don't have an 'xmlns' attribute, which can happen sometimes for some reason, including when they have been constructed via the stanza builder API. When that is the case then the explicit namespace arguemnt does not match the nil value of the internal attribute. Calling `:get_child()` without the namespace argument does the right thing here, with both nil and the parent namespace as valid values for the internal attribute.
author Kim Alvefur <zash@zash.se>
date Wed, 03 Nov 2021 21:11:55 +0100
parents 7dbde05b48a9
children
line wrap: on
line source

-- (C) 2011, Marco Cirillo (LW.Org)
-- General Stanzas' Counter.

local jid_bare = require "util.jid".bare

-- Setup, Init functions.
-- initialize function counter table on the global object on start
local function init_counter()
	prosody.stanza_counter = {
		iq = { incoming=0, outgoing=0 },
		message = { incoming=0, outgoing=0 },
		presence = { incoming=0, outgoing=0 }
	}
end

-- Setup on server start
local function setup() init_counter() end

-- Basic Stanzas' Counters
local function iq_callback(check)
	return function(self)
		local origin, stanza = self.origin, self.stanza
		if not prosody.stanza_counter then init_counter() end
		if check then
			if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil
			else
				prosody.stanza_counter.iq["outgoing"] = prosody.stanza_counter.iq["outgoing"] + 1
			end
		else
			prosody.stanza_counter.iq["incoming"] = prosody.stanza_counter.iq["incoming"] + 1
		end
	end
end

local function mes_callback(check)
	return function(self)
		local origin, stanza = self.origin, self.stanza
		if not prosody.stanza_counter then init_counter() end
		if check then
			if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil
			else
				prosody.stanza_counter.message["outgoing"] = prosody.stanza_counter.message["outgoing"] + 1
			end
		else
			prosody.stanza_counter.message["incoming"] = prosody.stanza_counter.message["incoming"] + 1
		end
	end
end

local function pre_callback(check)
	return function(self)
		local origin, stanza = self.origin, self.stanza
		if not prosody.stanza_counter then init_counter() end
		if check then
			if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil
			else
				prosody.stanza_counter.presence["outgoing"] = prosody.stanza_counter.presence["outgoing"] + 1
			end
		else
			prosody.stanza_counter.presence["incoming"] = prosody.stanza_counter.presence["incoming"] + 1
		end
	end
end

-- Hook all pre-stanza events.
module:hook("pre-iq/bare", iq_callback(true), 140)
module:hook("pre-iq/full", iq_callback(true), 140)
module:hook("pre-iq/host", iq_callback(true), 140)

module:hook("pre-message/bare", mes_callback(true), 140)
module:hook("pre-message/full", mes_callback(true), 140)
module:hook("pre-message/host", mes_callback(true), 140)

module:hook("pre-presence/bare", pre_callback(true), 140)
module:hook("pre-presence/full", pre_callback(true), 140)
module:hook("pre-presence/host", pre_callback(true), 140)

-- Hook all stanza events.
module:hook("iq/bare", iq_callback(false), 140)
module:hook("iq/full", iq_callback(false), 140)
module:hook("iq/host", iq_callback(false), 140)

module:hook("message/bare", mes_callback(false), 140)
module:hook("message/full", mes_callback(false), 140)
module:hook("message/host", mes_callback(false), 140)

module:hook("presence/bare", pre_callback(false), 140)
module:hook("presence/full", pre_callback(false), 140)
module:hook("presence/host", pre_callback(false), 140)

-- Hook server start to initialize the counter.
module:hook("server-started", setup)