Commit 6ad71cbb57 for freeswitch.com

commit 6ad71cbb57241adbfd34936c5239e50d16674cdb
Author: Dmitry Verenitsin <morbit85@gmail.com>
Date:   Thu Dec 3 21:47:19 2026 +0500

    [core] Add switch_channel_expand_variables_callback (#3331)

    Expands variables like `switch_channel_expand_variables_check()`, but
    passes each top-level `${...}` to a `switch_expand_variables_cb_t`
    before it is appended. The callback gets the variable or API name and
    the final value, after recursive expansion, `:offset` slicing and
    `[index]` lookup, and writes the replacement into a stream owned by the
    expander. The recursive expansions behind a value do not call it. A
    callback failure fails the whole expansion and returns NULL.

    `switch_channel_expand_variables_check()` now wraps the same body
    without a callback, so existing callers are unchanged.

    Four `switch_core_session` cases cover the names and values passed to
    the callback for variables and an API call, an input with nothing to
    expand, and callback failure.

diff --git a/src/include/switch_channel.h b/src/include/switch_channel.h
index 352e399d2a..0caf3a94fd 100644
--- a/src/include/switch_channel.h
+++ b/src/include/switch_channel.h
@@ -653,6 +653,19 @@ SWITCH_DECLARE(void) switch_channel_event_set_extended_data(_In_ switch_channel_
 SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *channel, const char *in, switch_event_t *var_list, switch_event_t *api_list, uint32_t recur);
 #define switch_channel_expand_variables(_channel, _in) switch_channel_expand_variables_check(_channel, _in, NULL, NULL, 0)

+/*!
+  \brief Expand variables like switch_channel_expand_variables_check, passing each substituted value through a callback
+  \param channel channel to expand the variables from
+  \param in the original string
+  \param var_list optional variable permission list
+  \param api_list optional API permission list
+  \param cb called for each top-level ${...} with its final value, not for the recursive expansions behind it
+  \param user_data pointer passed to cb
+  \return the original string if no expansion takes place, otherwise a new string that must be freed; NULL if cb failed
+*/
+SWITCH_DECLARE(char *) switch_channel_expand_variables_callback(switch_channel_t *channel, const char *in, switch_event_t *var_list, switch_event_t *api_list,
+																 switch_expand_variables_cb_t cb, void *user_data);
+
 #define switch_channel_inbound_display(_channel) ((switch_channel_direction(_channel) == SWITCH_CALL_DIRECTION_INBOUND && !switch_channel_test_flag(_channel, CF_BLEG)) || (switch_channel_direction(_channel) == SWITCH_CALL_DIRECTION_OUTBOUND && switch_channel_test_flag(_channel, CF_DIALPLAN)))

 #define switch_channel_outbound_display(_channel) ((switch_channel_direction(_channel) == SWITCH_CALL_DIRECTION_INBOUND && switch_channel_test_flag(_channel, CF_BLEG)) || (switch_channel_direction(_channel) == SWITCH_CALL_DIRECTION_OUTBOUND && !switch_channel_test_flag(_channel, CF_DIALPLAN)))
diff --git a/src/include/switch_types.h b/src/include/switch_types.h
index cd47eb0a0e..2d165b1dd5 100644
--- a/src/include/switch_types.h
+++ b/src/include/switch_types.h
@@ -2497,6 +2497,18 @@ typedef uint8_t * (*switch_stream_handle_read_function_t) (switch_stream_handle_
 typedef switch_status_t (*switch_stream_handle_write_function_t) (switch_stream_handle_t *handle, const char *fmt, ...);
 typedef switch_status_t (*switch_stream_handle_raw_write_function_t) (switch_stream_handle_t *handle, uint8_t *data, switch_size_t datalen);

+/*!
+ * \brief Callback for switch_channel_expand_variables_callback, called for each top-level ${...}, not for the recursive expansions behind it
+ * \param name the variable name, without any :offset or [index], or the API name
+ * \param value the value that would be substituted, "" if it expanded to nothing
+ * \param stream empty stream; what the callback writes to it is substituted instead of value.
+ *        Write value bytes with raw_write_function, or with write_function and a "%s" format.
+ *        The output must not contain NUL bytes; the substitution ends at the first one.
+ * \param user_data pointer to the user provided extra data
+ * \return SWITCH_STATUS_SUCCESS to substitute the stream content, anything else to fail the whole expansion
+ */
+typedef switch_status_t (*switch_expand_variables_cb_t)(const char *name, const char *value, switch_stream_handle_t *stream, void *user_data);
+
 typedef switch_status_t (*switch_api_function_t) (_In_opt_z_ const char *cmd, _In_opt_ switch_core_session_t *session,
 												  _In_ switch_stream_handle_t *stream);

diff --git a/src/switch_channel.c b/src/switch_channel.c
index 9798ec0cbb..d22888d8ef 100644
--- a/src/switch_channel.c
+++ b/src/switch_channel.c
@@ -3985,13 +3985,15 @@ SWITCH_DECLARE(switch_status_t) switch_channel_perform_answer(switch_channel_t *
 	memset(c, 0, olen - cpos);\
 	}}                           \

-SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *channel, const char *in, switch_event_t *var_list, switch_event_t *api_list, uint32_t recur)
+static char *channel_expand_variables(switch_channel_t *channel, const char *in, switch_event_t *var_list, switch_event_t *api_list, uint32_t recur,
+									  switch_expand_variables_cb_t cb, void *user_data)
 {
 	char *p, *c = NULL;
 	char *data, *indup, *endof_indup;
 	size_t sp = 0, len = 0, olen = 0, vtype = 0, br = 0, cpos, block = 128;
 	char *cloned_sub_val = NULL, *sub_val = NULL, *expanded_sub_val = NULL;
 	char *func_val = NULL, *sb = NULL;
+	switch_stream_handle_t cb_stream = { 0 };
 	int nv = 0;

 	if (recur > 100) {
@@ -4014,6 +4016,10 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c
 	indup = strdup(in);
 	endof_indup = end_of_p(indup) + 1;

+	if (cb) {
+		SWITCH_STANDARD_STREAM(cb_stream);
+	}
+
 	if ((data = malloc(olen))) {
 		memset(data, 0, olen);
 		c = data;
@@ -4072,7 +4078,7 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c
 			}

 			if (vtype) {
-				char *s = p, *e, *vname, *vval = NULL;
+				char *s = p, *e, *vname, *vval = NULL, *expanded_name = NULL;
 				size_t nlen;

 				s++;
@@ -4139,16 +4145,15 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c
 				}

 				if (vtype == 1 || vtype == 3) {
-					char *expanded = NULL;
 					int offset = 0;
 					int ooffset = 0;
 					char *ptr;
 					int idx = -1;

-					if ((expanded = switch_channel_expand_variables_check(channel, (char *) vname, var_list, api_list, recur+1)) == vname) {
-						expanded = NULL;
+					if ((expanded_name = switch_channel_expand_variables_check(channel, (char *) vname, var_list, api_list, recur+1)) == vname) {
+						expanded_name = NULL;
 					} else {
-						vname = expanded;
+						vname = expanded_name;
 					}

 					if ((ptr = strchr(vname, ':'))) {
@@ -4198,8 +4203,6 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c
 							}
 						}
 					}
-
-					switch_safe_free(expanded);
 				} else {
 					switch_stream_handle_t stream = { 0 };
 					char *expanded = NULL;
@@ -4207,12 +4210,10 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c
 					SWITCH_STANDARD_STREAM(stream);

 					if (stream.data) {
-						char *expanded_vname = NULL;
-
-						if ((expanded_vname = switch_channel_expand_variables_check(channel, (char *) vname, var_list, api_list, recur+1)) == vname) {
-							expanded_vname = NULL;
+						if ((expanded_name = switch_channel_expand_variables_check(channel, (char *) vname, var_list, api_list, recur+1)) == vname) {
+							expanded_name = NULL;
 						} else {
-							vname = expanded_vname;
+							vname = expanded_name;
 						}

 						if ((expanded = switch_channel_expand_variables_check(channel, vval, var_list, api_list, recur+1)) == vval) {
@@ -4235,15 +4236,38 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c
 						}

 						switch_safe_free(expanded);
-						switch_safe_free(expanded_vname);

 					} else {
 						switch_log_printf(SWITCH_CHANNEL_CHANNEL_LOG(channel), SWITCH_LOG_CRIT, "Memory Error!\n");
+						switch_safe_free(expanded_name);
+						switch_safe_free(cb_stream.data);
 						free(data);
 						free(indup);
+
 						return (char *) in;
 					}
 				}
+
+				if (cb) {
+					cb_stream.data_len = 0;
+					cb_stream.end = cb_stream.data;
+					*(char *) cb_stream.data = '\0';
+
+					if (cb(vname, sub_val ? sub_val : "", &cb_stream, user_data) != SWITCH_STATUS_SUCCESS) {
+						switch_safe_free(func_val);
+						switch_safe_free(cloned_sub_val);
+						switch_safe_free(expanded_sub_val);
+						switch_safe_free(expanded_name);
+						switch_safe_free(cb_stream.data);
+						switch_safe_free(data);
+						switch_safe_free(indup);
+
+						return NULL;
+					}
+
+					sub_val = (char *) cb_stream.data;
+				}
+
 				if ((nlen = sub_val ? strlen(sub_val) : 0)) {
 					if (len + nlen >= olen) {
 						resize(nlen);
@@ -4257,6 +4281,7 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c
 				switch_safe_free(func_val);
 				switch_safe_free(cloned_sub_val);
 				switch_safe_free(expanded_sub_val);
+				switch_safe_free(expanded_name);
 				sub_val = NULL;
 				vname = NULL;
 				br = 0;
@@ -4284,11 +4309,23 @@ SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *c
 			}
 		}
 	}
+	switch_safe_free(cb_stream.data);
 	free(indup);

 	return data;
 }

+SWITCH_DECLARE(char *) switch_channel_expand_variables_check(switch_channel_t *channel, const char *in, switch_event_t *var_list, switch_event_t *api_list, uint32_t recur)
+{
+	return channel_expand_variables(channel, in, var_list, api_list, recur, NULL, NULL);
+}
+
+SWITCH_DECLARE(char *) switch_channel_expand_variables_callback(switch_channel_t *channel, const char *in, switch_event_t *var_list, switch_event_t *api_list,
+																 switch_expand_variables_cb_t cb, void *user_data)
+{
+	return channel_expand_variables(channel, in, var_list, api_list, 0, cb, user_data);
+}
+
 SWITCH_DECLARE(char *) switch_channel_build_param_string(switch_channel_t *channel, switch_caller_profile_t *caller_profile, const char *prefix)
 {
 	switch_stream_handle_t stream = { 0 };
diff --git a/tests/unit/switch_core_session.c b/tests/unit/switch_core_session.c
index f1595397b0..016351c89c 100644
--- a/tests/unit/switch_core_session.c
+++ b/tests/unit/switch_core_session.c
@@ -57,6 +57,57 @@ static const char *partner_uuid_copy_repeat(switch_core_session_t *session, char
 	return uuid;
 }

+#define EXPAND_CB_MAX_VALUES 8
+
+typedef struct {
+	int calls;
+	char names[EXPAND_CB_MAX_VALUES][64];
+	char values[EXPAND_CB_MAX_VALUES][64];
+} expand_cb_record_t;
+
+/* Records each name and value it receives and writes the value enclosed in [] */
+static switch_status_t expand_bracket_callback(const char *name, const char *value, switch_stream_handle_t *stream, void *user_data)
+{
+	expand_cb_record_t *rec = (expand_cb_record_t *) user_data;
+
+	if (rec->calls < EXPAND_CB_MAX_VALUES) {
+		switch_copy_string(rec->names[rec->calls], name, sizeof(rec->names[0]));
+		switch_copy_string(rec->values[rec->calls], value, sizeof(rec->values[0]));
+	}
+
+	rec->calls++;
+
+	return stream->write_function(stream, "[%s]", value);
+}
+
+/* Writes each value unchanged */
+static switch_status_t expand_same_callback(const char *name, const char *value, switch_stream_handle_t *stream, void *user_data)
+{
+	int *calls = (int *) user_data;
+
+	(*calls)++;
+
+	return stream->raw_write_function(stream, (uint8_t *) value, strlen(value));
+}
+
+/* Fails every value */
+static switch_status_t expand_fail_callback(const char *name, const char *value, switch_stream_handle_t *stream, void *user_data)
+{
+	return SWITCH_STATUS_FALSE;
+}
+
+/* Writes the first value unchanged and fails the second */
+static switch_status_t expand_fail_second_callback(const char *name, const char *value, switch_stream_handle_t *stream, void *user_data)
+{
+	int *calls = (int *) user_data;
+
+	if (++(*calls) == 2) {
+		return SWITCH_STATUS_FALSE;
+	}
+
+	return stream->raw_write_function(stream, (uint8_t *) value, strlen(value));
+}
+
 FST_CORE_BEGIN("./conf")
 {
 	FST_SUITE_BEGIN(switch_core_session)
@@ -139,6 +190,136 @@ FST_CORE_BEGIN("./conf")
 			switch_channel_set_variable(fst_channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, NULL);
 		}
 		FST_SESSION_END()
+
+		FST_SESSION_BEGIN(expand_variables_callback_values)
+		{
+			const char *in = "<${cbt_a}|${cbt_b}|${${cbt_n}}|${cbt_a:1}|${cbt_a:-2}|${cbt_arr[1]}|${cbt_unset}>";
+			expand_cb_record_t rec = { 0 };
+			char *plain = NULL, *out = NULL;
+
+			switch_channel_set_variable(fst_channel, "cbt_a", "abc");
+			switch_channel_set_variable_var_check(fst_channel, "cbt_b", "${cbt_a}d", SWITCH_FALSE);
+			switch_channel_set_variable(fst_channel, "cbt_n", "cbt_a");
+			switch_channel_add_variable_var_check(fst_channel, "cbt_arr", "first", SWITCH_TRUE, SWITCH_STACK_PUSH);
+			switch_channel_add_variable_var_check(fst_channel, "cbt_arr", "second", SWITCH_TRUE, SWITCH_STACK_PUSH);
+
+			plain = switch_channel_expand_variables(fst_channel, in);
+			fst_check_string_equals(plain, "<abc|abcd|abc|bc|bc|second|>");
+
+			/* each value reaches the callback after nested expansion, offset slicing and indexing; an unset variable arrives as "" */
+			out = switch_channel_expand_variables_callback(fst_channel, in, NULL, NULL, expand_bracket_callback, &rec);
+			fst_check_string_equals(out, "<[abc]|[abcd]|[abc]|[bc]|[bc]|[second]|[]>");
+
+			/* one call per substituted value: neither the template text nor the nested ${cbt_a} and ${cbt_n} reach it */
+			fst_xcheck(rec.calls == 7, "callback called once per substituted value");
+			fst_check_string_equals(rec.values[0], "abc");
+			fst_check_string_equals(rec.values[1], "abcd");
+			fst_check_string_equals(rec.values[2], "abc");
+			fst_check_string_equals(rec.values[3], "bc");
+			fst_check_string_equals(rec.values[4], "bc");
+			fst_check_string_equals(rec.values[5], "second");
+			fst_check_string_equals(rec.values[6], "");
+
+			/* the name is the resolved variable name, without :offset or [index] */
+			fst_check_string_equals(rec.names[0], "cbt_a");
+			fst_check_string_equals(rec.names[1], "cbt_b");
+			fst_check_string_equals(rec.names[2], "cbt_a");
+			fst_check_string_equals(rec.names[3], "cbt_a");
+			fst_check_string_equals(rec.names[4], "cbt_a");
+			fst_check_string_equals(rec.names[5], "cbt_arr");
+			fst_check_string_equals(rec.names[6], "cbt_unset");
+
+			if (plain != in) {
+				switch_safe_free(plain);
+			}
+
+			if (out != in) {
+				switch_safe_free(out);
+			}
+		}
+		FST_SESSION_END()
+
+		FST_SESSION_BEGIN(expand_variables_callback_unchanged)
+		{
+			const char *in = "'${cbt_a}',${cbt_unset},\"${cbt_q}\"";
+			const char *no_vars = "no variables here";
+			char *plain = NULL, *out = NULL;
+			int calls = 0;
+
+			switch_channel_set_variable(fst_channel, "cbt_a", "abc");
+			switch_channel_set_variable(fst_channel, "cbt_q", "x\"y'z");
+
+			/* a callback returning each value unchanged gives the same result as plain expansion */
+			plain = switch_channel_expand_variables(fst_channel, in);
+			out = switch_channel_expand_variables_callback(fst_channel, in, NULL, NULL, expand_same_callback, &calls);
+			fst_check_string_equals(out, plain);
+			fst_xcheck(calls == 3, "callback called for each of the three variables");
+
+			if (plain != in) {
+				switch_safe_free(plain);
+			}
+
+			if (out != in) {
+				switch_safe_free(out);
+			}
+
+			/* no variables: the input is returned as is and the callback is not called */
+			calls = 0;
+			out = switch_channel_expand_variables_callback(fst_channel, no_vars, NULL, NULL, expand_same_callback, &calls);
+			fst_xcheck(out == no_vars, "input without variables is returned as is");
+			fst_xcheck(calls == 0, "callback not called without variables");
+		}
+		FST_SESSION_END()
+
+		FST_SESSION_BEGIN(expand_variables_callback_fail)
+		{
+			char *out = NULL;
+			int calls = 0;
+
+			switch_channel_set_variable(fst_channel, "cbt_a", "abc");
+
+			/* a callback failure fails the whole expansion */
+			out = switch_channel_expand_variables_callback(fst_channel, "x ${cbt_a} y", NULL, NULL, expand_fail_callback, NULL);
+			fst_xcheck(out == NULL, "expansion fails when the callback fails");
+
+			/* a failure after an earlier value was substituted fails the expansion too, and no later value is passed */
+			out = switch_channel_expand_variables_callback(fst_channel, "${cbt_a} ${cbt_a:1} ${cbt_a}", NULL, NULL, expand_fail_second_callback, &calls);
+			fst_xcheck(out == NULL, "expansion fails when the second callback call fails");
+			fst_xcheck(calls == 2, "callback not called after it failed");
+		}
+		FST_SESSION_END()
+
+		FST_SESSION_BEGIN(expand_variables_callback_api)
+		{
+			const char *in = "<${strepoch 2020-01-01 00:00:00}>";
+			expand_cb_record_t rec = { 0 };
+			char *plain = NULL, *out = NULL, *expected = NULL;
+
+			/* an API call reaches the callback once, with the API name and the API output */
+			plain = switch_channel_expand_variables(fst_channel, in);
+			out = switch_channel_expand_variables_callback(fst_channel, in, NULL, NULL, expand_bracket_callback, &rec);
+
+			fst_xcheck(rec.calls == 1, "callback called once for the API call");
+			fst_check_string_equals(rec.names[0], "strepoch");
+			fst_xcheck(!zstr(rec.values[0]), "API output passed to the callback");
+
+			expected = switch_mprintf("<%s>", rec.values[0]);
+			fst_check_string_equals(plain, expected);
+			switch_safe_free(expected);
+
+			expected = switch_mprintf("<[%s]>", rec.values[0]);
+			fst_check_string_equals(out, expected);
+			switch_safe_free(expected);
+
+			if (plain != in) {
+				switch_safe_free(plain);
+			}
+
+			if (out != in) {
+				switch_safe_free(out);
+			}
+		}
+		FST_SESSION_END()
 	}
 	FST_SUITE_END()
 }