[Asterisk-code-review] func_lookup: Fork of HINT function (asterisk[master])

N A asteriskteam at digium.com
Mon Jun 21 07:51:36 CDT 2021


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/16075 )


Change subject: func_lookup: Fork of HINT function
......................................................................

func_lookup: Fork of HINT function

This is a generic key-value lookup function
that is similar to HINT, except it references
priorities that are not used for device state,
allowing for access to these extension values
without adding overhead for device state.

This provides a fast, efficient, and convenient
mechanism in certain circumstances for accessing
key-values in the dialplan while being able to
use native extension pattern matching on the keys.

ASTERISK-29486

Change-Id: Iad81019689674c9f4ac77d235f5d7234adbb1432
---
A doc/CHANGES-staging/func_lookup.txt
A funcs/func_lookup.c
M pbx/pbx_config.c
3 files changed, 151 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/75/16075/1

diff --git a/doc/CHANGES-staging/func_lookup.txt b/doc/CHANGES-staging/func_lookup.txt
new file mode 100644
index 0000000..2e3d948
--- /dev/null
+++ b/doc/CHANGES-staging/func_lookup.txt
@@ -0,0 +1,7 @@
+Subject: func_lookup
+
+This is a new function that works like
+HINT, except the referenced priorities
+are not used for device state, ditching
+unneeded overhead while allowing for
+key value lookups using pattern matching.
diff --git a/funcs/func_lookup.c b/funcs/func_lookup.c
new file mode 100644
index 0000000..97377c7
--- /dev/null
+++ b/funcs/func_lookup.c
@@ -0,0 +1,128 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2021, Naveen Albert
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Dialplan lookup function
+ *
+ * \author Naveen Albert <asterisk at phreaknet.org>
+ *
+ * \ingroup functions
+ */
+
+/*** MODULEINFO
+	<support_level>extended</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/utils.h"
+#include "asterisk/app.h"
+
+/*** DOCUMENTATION
+	<function name="LOOKUP" language="en_US">
+		<synopsis>
+			Get the value set for a static dialplan lookup extension.
+		</synopsis>
+		<syntax>
+			<parameter name="extension" required="true" argsep="@">
+				<argument name="extension" required="true" />
+				<argument name="context" />
+			</parameter>
+		</syntax>
+		<description>
+			<para>The LOOKUP function can be used to retrieve arbitrary values
+			from a static dialplan lookup, similar to using a hint. For example:</para>
+			<para>NoOp(Bill calls from 1234 to ${LOOKUP(1234 at billing-tel-num)})</para>
+			<para>LOOKUP is like the HINT function without device state, providing
+			an easy and efficient way to access static values in the dialplan and
+			enables key-value lookups to be performed with pattern matching.</para>
+		</description>
+	</function>
+ ***/
+
+#define PRIORITY_LOOKUP	-2	/*!< Special Priority for a lookup */
+
+static struct ast_exten *ast_lookup_extension(struct ast_channel *c, const char *context, const char *exten)
+{
+	struct ast_exten *e;
+	struct pbx_find_info q = { .stacklen = 0 }; /* the rest is set in pbx_find_context */
+	ast_rdlock_contexts();
+	e = pbx_find_extension(c, NULL, &q, context, exten, PRIORITY_LOOKUP, NULL, "", E_MATCH);
+	ast_unlock_contexts();
+	return e;
+}
+
+static int ast_get_lookup(char *lookup, int lookupsize, char *name, int namesize, struct ast_channel *c, const char *context, const char *exten)
+{
+	struct ast_exten *e = ast_lookup_extension(c, context, exten);
+	if (e) {
+		if (name) {
+			const char *tmp = ast_get_extension_app(e);
+			if (tmp)
+				ast_copy_string(name, tmp, namesize);
+		}
+		return 0;
+	}
+	return -1;
+}
+
+static int lookup_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
+{
+	char *exten, *context;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(exten);
+	);
+
+	if (ast_strlen_zero(data)) {
+		ast_log(LOG_WARNING, "The LOOKUP function requires an extension\n");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, data);
+
+	if (ast_strlen_zero(args.exten)) {
+		ast_log(LOG_WARNING, "The LOOKUP function requires an extension\n");
+		return -1;
+	}
+
+	context = exten = args.exten;
+	strsep(&context, "@");
+	if (ast_strlen_zero(context))
+		context = "default";
+
+	return ast_get_lookup(NULL, 0, buf, len, chan, context, exten);
+}
+
+static struct ast_custom_function lookup_function = {
+	.name = "LOOKUP",
+	.read = lookup_read,
+};
+
+static int unload_module(void)
+{
+	return ast_custom_function_unregister(&lookup_function);
+}
+
+static int load_module(void)
+{
+	return ast_custom_function_register(&lookup_function);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Arbitrary static dialplan lookup function");
diff --git a/pbx/pbx_config.c b/pbx/pbx_config.c
index f6af7bf..39aa6ea 100644
--- a/pbx/pbx_config.c
+++ b/pbx/pbx_config.c
@@ -446,6 +446,8 @@
 		 */
 		if (!strcmp("hint", c))
 			removing_priority = PRIORITY_HINT;
+		else if (!strcmp("lookup", c))
+			removing_priority = PRIORITY_LOOKUP;
 		else {
 			while (*c && isdigit(*c))
 				c++;
@@ -526,6 +528,8 @@
 		ipriority = 0;
 	} else if (!strcmp("hint", priority)) {
 		ipriority = PRIORITY_HINT;
+	} else if (!strcmp("lookup", priority)) {
+		ipriority = PRIORITY_LOOKUP;
 	} else if ((sscanf(priority, "%30d", &ipriority) != 1) || ipriority <= 0) {
 		astman_send_error(s, m, "The priority specified was invalid.");
 		return 0;
@@ -963,7 +967,11 @@
 					fprintf(output, "exten => %s,hint,%s\n",
 						    ast_get_extension_name(p),
 						    ast_get_extension_app(p));
-				} else {
+				} else if (ast_get_extension_priority(p) == PRIORITY_LOOKUP) { /* easy */
+                                        fprintf(output, "exten => %s,lookup,%s\n",
+                                                    ast_get_extension_name(p),
+                                                    ast_get_extension_app(p));
+                                }  else {
 					const char *sep, *cid;
 					const char *el = ast_get_extension_label(p);
 					char label[128] = "";
@@ -1112,6 +1120,8 @@
 	if (prior) {
 		if (!strcmp(prior, "hint")) {
 			iprior = PRIORITY_HINT;
+		} else if (!strcmp(prior, "lookup")) {
+			iprior = PRIORITY_LOOKUP;
 		} else {
 			if (sscanf(prior, "%30d", &iprior) != 1) {
 				ast_cli(a->fd, "'%s' is not a valid priority\n", prior);
@@ -1215,7 +1225,9 @@
 	/* Priority conversion/validation */
 	if (!strcmp(priority, "hint")) {
 		ipriority = PRIORITY_HINT;
-	} else if ((sscanf(priority, "%30d", &ipriority) != 1) || (ipriority < 0)) {
+	} else if (!strcmp(priority, "lookup")) {
+                ipriority = PRIORITY_LOOKUP;
+        }  else if ((sscanf(priority, "%30d", &ipriority) != 1) || (ipriority < 0)) {
 		astman_send_error(s, m, "The priority specified was invalid.");
 		return 0;
 	}
@@ -1799,6 +1811,8 @@
 				}
 				if (!strcmp(pri,"hint")) {
 					ipri = PRIORITY_HINT;
+				} else if (!strcmp(pri,"lookup")) {
+					ipri = PRIORITY_LOOKUP;
 				} else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
 					if (lastpri > -2) {
 						ipri = lastpri + 1;

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16075
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Iad81019689674c9f4ac77d235f5d7234adbb1432
Gerrit-Change-Number: 16075
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210621/1161a6be/attachment-0001.html>


More information about the asterisk-code-review mailing list