From e498488bde119a98ecfc27880a2a9c9e9adfb223 Mon Sep 17 00:00:00 2001
From: Joshua Colp <jcolp@digium.com>
Date: Mon, 16 Jul 2007 18:24:29 +0000
Subject: [PATCH] For my next trick I will make it so dialplan functions no
 longer need to call ast_module_user_add and ast_module_user_remove. These are
 now called in the ast_func_read and ast_func_write functions outside of the
 module.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@75255 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 funcs/func_curl.c      |  5 -----
 funcs/func_cut.c       | 10 ----------
 funcs/func_enum.c      | 18 ------------------
 funcs/func_rand.c      |  5 -----
 funcs/func_realtime.c  | 15 ++-------------
 funcs/func_version.c   |  5 -----
 funcs/func_vmcount.c   |  5 -----
 include/asterisk/pbx.h | 10 ++++++++--
 main/pbx.c             | 28 +++++++++++++++++++++++-----
 9 files changed, 33 insertions(+), 68 deletions(-)

diff --git a/funcs/func_curl.c b/funcs/func_curl.c
index 802bbb4a07..b32d5f748e 100644
--- a/funcs/func_curl.c
+++ b/funcs/func_curl.c
@@ -135,7 +135,6 @@ static int curl_internal(struct MemoryStruct *chunk, char *url, char *post)
 
 static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info, char *buf, size_t len)
 {
-	struct ast_module_user *u;
 	struct MemoryStruct chunk = { NULL, 0 };
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(url);
@@ -149,8 +148,6 @@ static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info,
 		return -1;
 	}
 
-	u = ast_module_user_add(chan);
-
 	AST_STANDARD_APP_ARGS(args, info);	
 
 	if (!curl_internal(&chunk, args.url, args.postdata)) {
@@ -166,8 +163,6 @@ static int acf_curl_exec(struct ast_channel *chan, const char *cmd, char *info,
 		ast_log(LOG_ERROR, "Cannot allocate curl structure\n");
 	}
 
-	ast_module_user_remove(u);
-
 	return 0;
 }
 
diff --git a/funcs/func_cut.c b/funcs/func_cut.c
index 7afd978cab..41710b978c 100644
--- a/funcs/func_cut.c
+++ b/funcs/func_cut.c
@@ -222,11 +222,8 @@ static int cut_internal(struct ast_channel *chan, char *data, char *buffer, size
 
 static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 {
-	struct ast_module_user *u;
 	int ret = -1;
 
-	u = ast_module_user_add(chan);
-
 	switch (sort_internal(chan, data, buf, len)) {
 	case ERROR_NOARG:
 		ast_log(LOG_ERROR, "SORT() requires an argument\n");
@@ -241,17 +238,12 @@ static int acf_sort_exec(struct ast_channel *chan, const char *cmd, char *data,
 		ast_log(LOG_ERROR, "Unknown internal error\n");
 	}
 
-	ast_module_user_remove(u);
-
 	return ret;
 }
 
 static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 {
 	int ret = -1;
-	struct ast_module_user *u;
-
-	u = ast_module_user_add(chan);
 
 	switch (cut_internal(chan, data, buf, len)) {
 	case ERROR_NOARG:
@@ -270,8 +262,6 @@ static int acf_cut_exec(struct ast_channel *chan, const char *cmd, char *data, c
 		ast_log(LOG_ERROR, "Unknown internal error\n");
 	}
 
-	ast_module_user_remove(u);
-
 	return ret;
 }
 
diff --git a/funcs/func_enum.c b/funcs/func_enum.c
index ff7ab4f213..581d68a7a7 100644
--- a/funcs/func_enum.c
+++ b/funcs/func_enum.c
@@ -67,7 +67,6 @@ static int function_enum(struct ast_channel *chan, const char *cmd, char *data,
 	int res = 0;
 	char tech[80];
 	char dest[256] = "", tmp[2] = "", num[AST_MAX_EXTENSION] = "";
-	struct ast_module_user *u;
 	char *s, *p;
 	unsigned int record = 1;
 
@@ -85,8 +84,6 @@ static int function_enum(struct ast_channel *chan, const char *cmd, char *data,
 		return -1;
 	}
 
-	u = ast_module_user_add(chan);
-
 	ast_copy_string(tech, args.tech ? args.tech : "sip", sizeof(tech));
 
 	if (!args.zone)
@@ -115,8 +112,6 @@ static int function_enum(struct ast_channel *chan, const char *cmd, char *data,
 	else
 		ast_copy_string(buf, dest, len);
 
-	ast_module_user_remove(u);
-
 	return 0;
 }
 
@@ -156,7 +151,6 @@ static int enum_query_read(struct ast_channel *chan, const char *cmd, char *data
 {
 	struct enum_result_datastore *erds;
 	struct ast_datastore *datastore;
-	struct ast_module_user *u;
 	char *parse, tech[128], dest[128];
 	int res = -1;
 
@@ -166,8 +160,6 @@ static int enum_query_read(struct ast_channel *chan, const char *cmd, char *data
 		AST_APP_ARG(zone);
 	);
 
-	u = ast_module_user_add(chan);
-
 	if (ast_strlen_zero(data)) {
 		ast_log(LOG_WARNING, "ENUMQUERY requires at least a number as an argument...\n");
 		goto finish;
@@ -214,14 +206,12 @@ static int enum_query_read(struct ast_channel *chan, const char *cmd, char *data
 	res = 0;
     
 finish:
-	ast_module_user_remove(u);
 
 	return res;
 }
 
 static int enum_result_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 {
-	struct ast_module_user *u;
 	struct enum_result_datastore *erds;
 	struct ast_datastore *datastore;
 	char *parse, *p;
@@ -232,8 +222,6 @@ static int enum_result_read(struct ast_channel *chan, const char *cmd, char *dat
 		AST_APP_ARG(resultnum);
 	);
 
-	u = ast_module_user_add(chan);
-
 	if (ast_strlen_zero(data)) {
 		ast_log(LOG_WARNING, "ENUMRESULT requires two arguments (id and resultnum)\n");
 		goto finish;
@@ -298,7 +286,6 @@ static int enum_result_read(struct ast_channel *chan, const char *cmd, char *dat
 	res = 0;
 
 finish:
-	ast_module_user_remove(u);
 
 	return res;
 }
@@ -349,7 +336,6 @@ static int function_txtcidname(struct ast_channel *chan, const char *cmd,
 	char tech[80];
 	char txt[256] = "";
 	char dest[80];
-	struct ast_module_user *u;
 
 	buf[0] = '\0';
 
@@ -359,16 +345,12 @@ static int function_txtcidname(struct ast_channel *chan, const char *cmd,
 		return -1;
 	}
 
-	u = ast_module_user_add(chan);
-
 	res = ast_get_txt(chan, data, dest, sizeof(dest), tech, sizeof(tech), txt,
 			  sizeof(txt));
 
 	if (!ast_strlen_zero(txt))
 		ast_copy_string(buf, txt, len);
 
-	ast_module_user_remove(u);
-
 	return 0;
 }
 
diff --git a/funcs/func_rand.c b/funcs/func_rand.c
index e92f16e959..8885904a94 100644
--- a/funcs/func_rand.c
+++ b/funcs/func_rand.c
@@ -44,15 +44,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 static int acf_rand_exec(struct ast_channel *chan, const char *cmd,
 			 char *parse, char *buffer, size_t buflen)
 {
-	struct ast_module_user *u;
 	int min_int, response_int, max_int;
 	AST_DECLARE_APP_ARGS(args,
 			     AST_APP_ARG(min);
 			     AST_APP_ARG(max);
 	);
 
-	u = ast_module_user_add(chan);
-
 	AST_STANDARD_APP_ARGS(args, parse);
 
 	if (ast_strlen_zero(args.min) || sscanf(args.min, "%d", &min_int) != 1)
@@ -73,8 +70,6 @@ static int acf_rand_exec(struct ast_channel *chan, const char *cmd,
 	ast_debug(1, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int);
 	snprintf(buffer, buflen, "%d", response_int);
 
-	ast_module_user_remove(u);
-
 	return 0;
 }
 
diff --git a/funcs/func_realtime.c b/funcs/func_realtime.c
index 7ffe8122cb..611e6ecc64 100644
--- a/funcs/func_realtime.c
+++ b/funcs/func_realtime.c
@@ -48,7 +48,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) 
 {
 	struct ast_variable *var, *head;
-	struct ast_module_user *u;
 	struct ast_str *out;
 	size_t resultslen;
 	int n;
@@ -65,8 +64,6 @@ static int function_realtime_read(struct ast_channel *chan, const char *cmd, cha
 		return -1;
 	}
 
-	u = ast_module_user_add(chan);
-
 	AST_STANDARD_APP_ARGS(args, data);
 
 	if (!args.delim1)
@@ -76,10 +73,9 @@ static int function_realtime_read(struct ast_channel *chan, const char *cmd, cha
 
 	head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, NULL);
 
-	if (!head) {
-		ast_module_user_remove(u);
+	if (!head)
 		return -1;
-	}
+
 	resultslen = 0;
 	n = 0;
 	for (var = head; var; n++, var = var->next)
@@ -92,14 +88,11 @@ static int function_realtime_read(struct ast_channel *chan, const char *cmd, cha
 		ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1);
 	ast_copy_string(buf, out->str, len);
 
-	ast_module_user_remove(u);
-
 	return 0;
 }
 
 static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
 {
-	struct ast_module_user *u;
 	int res = 0;
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(family);
@@ -113,8 +106,6 @@ static int function_realtime_write(struct ast_channel *chan, const char *cmd, ch
 		return -1;
 	}
 
-	u = ast_module_user_add(chan);
-
 	AST_STANDARD_APP_ARGS(args, data);
 
 	res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, NULL);
@@ -123,8 +114,6 @@ static int function_realtime_write(struct ast_channel *chan, const char *cmd, ch
 		ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n");
 	}
 
-	ast_module_user_remove(u);
-
 	return 0;
 }
 
diff --git a/funcs/func_version.c b/funcs/func_version.c
index 88ad138e77..7c7ed38020 100644
--- a/funcs/func_version.c
+++ b/funcs/func_version.c
@@ -47,14 +47,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 static int acf_version_exec(struct ast_channel *chan, const char *cmd,
 			 char *parse, char *buffer, size_t buflen)
 {
-	struct ast_module_user *u;
 	char *response_char = ASTERISK_VERSION;
 	AST_DECLARE_APP_ARGS(args,
 			     AST_APP_ARG(info);
 	);
 
-	u = ast_module_user_add(chan);
-
 	AST_STANDARD_APP_ARGS(args, parse);
 
 	if (!ast_strlen_zero(args.info) ) {
@@ -79,8 +76,6 @@ static int acf_version_exec(struct ast_channel *chan, const char *cmd,
 	ast_debug(1, "VERSION returns %s result, given %s argument\n", response_char, args.info);
 	snprintf(buffer, buflen, "%s", response_char);
 
-	ast_module_user_remove(u);
-
 	return 0;
 }
 
diff --git a/funcs/func_vmcount.c b/funcs/func_vmcount.c
index 7f0282c323..8183b8c74e 100644
--- a/funcs/func_vmcount.c
+++ b/funcs/func_vmcount.c
@@ -47,15 +47,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 static int acf_vmcount_exec(struct ast_channel *chan, const char *cmd, char *argsstr, char *buf, size_t len)
 {
-	struct ast_module_user *u;
 	char *context;
 	AST_DECLARE_APP_ARGS(args,
 		AST_APP_ARG(vmbox);
 		AST_APP_ARG(folder);
 	);
 
-	u = ast_module_user_add(chan);
-
 	buf[0] = '\0';
 
 	AST_STANDARD_APP_ARGS(args, argsstr);
@@ -72,8 +69,6 @@ static int acf_vmcount_exec(struct ast_channel *chan, const char *cmd, char *arg
 	}
 
 	snprintf(buf, len, "%d", ast_app_messagecount(context, args.vmbox, args.folder));
-
-	ast_module_user_remove(u);
 	
 	return 0;
 }
diff --git a/include/asterisk/pbx.h b/include/asterisk/pbx.h
index 70373999eb..41dd0d4415 100644
--- a/include/asterisk/pbx.h
+++ b/include/asterisk/pbx.h
@@ -76,6 +76,7 @@ struct ast_custom_function {
 	const char *syntax;		/*!< Syntax description */
 	int (*read)(struct ast_channel *, const char *, char *, char *, size_t);	/*!< Read function, if read is supported */
 	int (*write)(struct ast_channel *, const char *, char *, const char *);		/*!< Write function, if write is supported */
+	struct ast_module *mod;         /*!< Module this custom function belongs to */
 	AST_RWLIST_ENTRY(ast_custom_function) acflist;
 };
 
@@ -862,9 +863,14 @@ struct ast_custom_function* ast_custom_function_find(const char *name);
 int ast_custom_function_unregister(struct ast_custom_function *acf);
 
 /*!
- * \brief Reigster a custom function
+ * \brief Register a custom function
  */
-int ast_custom_function_register(struct ast_custom_function *acf);
+#define ast_custom_function_register(acf) ast_custom_function_register2(acf, ast_module_info->self)
+
+/*!
+ * \brief Register a custom function
+ */
+int ast_custom_function_register2(struct ast_custom_function *acf, struct ast_module *mod);
 
 /*! 
  * \brief Retrieve the number of active calls
diff --git a/main/pbx.c b/main/pbx.c
index d47e0e8ccc..ee88a06159 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -1410,13 +1410,15 @@ int ast_custom_function_unregister(struct ast_custom_function *acf)
 	return acf ? 0 : -1;
 }
 
-int ast_custom_function_register(struct ast_custom_function *acf)
+int ast_custom_function_register2(struct ast_custom_function *acf, struct ast_module *mod)
 {
 	struct ast_custom_function *cur;
 
 	if (!acf)
 		return -1;
 
+	acf->mod = mod;
+
 	AST_RWLIST_WRLOCK(&acf_root);
 
 	AST_RWLIST_TRAVERSE(&acf_root, cur, acflist) {
@@ -1476,8 +1478,16 @@ int ast_func_read(struct ast_channel *chan, const char *function, char *workspac
 		ast_log(LOG_ERROR, "Function %s not registered\n", copy);
 	else if (!acfptr->read)
 		ast_log(LOG_ERROR, "Function %s cannot be read\n", copy);
-	else
-		return acfptr->read(chan, copy, args, workspace, len);
+	else {
+		int res;
+		struct ast_module_user *u = NULL;
+		if (acfptr->mod)
+			u = __ast_module_user_add(acfptr->mod, chan);
+		res = acfptr->read(chan, copy, args, workspace, len);
+		if (acfptr->mod && u)
+			__ast_module_user_remove(acfptr->mod, u);
+		return res;
+	}
 	return -1;
 }
 
@@ -1491,8 +1501,16 @@ int ast_func_write(struct ast_channel *chan, const char *function, const char *v
 		ast_log(LOG_ERROR, "Function %s not registered\n", copy);
 	else if (!acfptr->write)
 		ast_log(LOG_ERROR, "Function %s cannot be written to\n", copy);
-	else
-		return acfptr->write(chan, copy, args, value);
+	else {
+		int res;
+		struct ast_module_user *u = NULL;
+		if (acfptr->mod)
+			u = __ast_module_user_add(acfptr->mod, chan);
+		res = acfptr->write(chan, copy, args, value);
+		if (acfptr->mod && u)
+			__ast_module_user_remove(acfptr->mod, u);
+		return res;
+	}
 
 	return -1;
 }
-- 
GitLab