summaryrefslogtreecommitdiff
path: root/funcs/func_sorcery.c
blob: 7703e2be9320bdb683a9e2fdeaf748ef69aa726e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Fairview 5 Engineering, LLC
 *
 * George Joseph <george.joseph@fairview5.com>
 *
 * 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 Get a field from a sorcery object
 *
 * \author \verbatim George Joseph <george.joseph@fairview5.com> \endverbatim
 *
 * \ingroup functions
 *
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/app.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/sorcery.h"

/*** DOCUMENTATION
	<function name="AST_SORCERY" language="en_US">
		<synopsis>
			Get a field from a sorcery object
		</synopsis>
		<syntax>
			<parameter name="module_name" required="true">
				<para>The name of the module owning the sorcery instance.</para>
			</parameter>
			<parameter name="object_type" required="true">
				<para>The type of object to query.</para>
			</parameter>
			<parameter name="object_id" required="true">
				<para>The id of the object to query.</para>
			</parameter>
			<parameter name="field_name" required="true">
				<para>The name of the field.</para>
			</parameter>
			<parameter name="retrieval_method" required="false">
				<para>Fields that have multiple occurrences may be retrieved in two ways.</para>
				<enumlist>
					<enum name="concat"><para>Returns all matching fields concatenated
					in a single string separated by <replaceable>separator</replaceable>
					which defaults to <literal>,</literal>.</para></enum>

					<enum name="single"><para>Returns the nth occurrence of the field
					as specified by <replaceable>occurrence_number</replaceable> which defaults to <literal>1</literal>.
					</para></enum>
				</enumlist>
				<para>The default is <literal>concat</literal> with separator <literal>,</literal>.</para>
			</parameter>
			<parameter name="retrieval_details" required="false">
				<para>Specifies either the separator for <literal>concat</literal>
				or the occurrence number for <literal>single</literal>.</para>
			</parameter>
		</syntax>
	</function>
***/

static int sorcery_function_read(struct ast_channel *chan,
	const char *cmd, char *data, struct ast_str **buf, ssize_t len)
{
	char *parsed_data = ast_strdupa(data);
	RAII_VAR(struct ast_sorcery *, sorcery, NULL, ast_sorcery_unref);
	RAII_VAR(void *, sorcery_obj, NULL, ao2_cleanup);
	struct ast_variable *change_set;
	struct ast_variable *it_change_set;
	int found, field_number = 1, ix, method;
	char *separator = ",";

	enum methods {
		CONCAT,
		SINGLE,
	};

	AST_DECLARE_APP_ARGS(args,
		AST_APP_ARG(module_name);
		AST_APP_ARG(object_type);
		AST_APP_ARG(object_id);
		AST_APP_ARG(field_name);
		AST_APP_ARG(method);
		AST_APP_ARG(method_arg);
	);

	/* Check for zero arguments */
	if (ast_strlen_zero(parsed_data)) {
		ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
		return -1;
	}

	AST_STANDARD_APP_ARGS(args, parsed_data);

	if (ast_strlen_zero(args.module_name)) {
		ast_log(AST_LOG_ERROR, "Cannot call %s without a module name to query\n", cmd);
		return -1;
	}

	if (ast_strlen_zero(args.object_type)) {
		ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object type\n", cmd);
		return -1;
	}

	if (ast_strlen_zero(args.object_id)) {
		ast_log(AST_LOG_ERROR, "Cannot call %s with an empty object name\n", cmd);
		return -1;
	}

	if (ast_strlen_zero(args.field_name)) {
		ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name\n", cmd);
		return -1;
	}

	if (ast_strlen_zero(args.method)) {
		method = CONCAT;
	} else {
		if (strcmp(args.method, "concat") == 0) {
			method = CONCAT;
			if (ast_strlen_zero(args.method_arg)) {
				separator = ",";
			} else {
				separator = args.method_arg;
			}

		} else if (strcmp(args.method, "single") == 0) {
			method = SINGLE;
			if (!ast_strlen_zero(args.method_arg)) {
				if (sscanf(args.method_arg, "%30d", &field_number) <= 0 || field_number <= 0 ) {
					ast_log(AST_LOG_ERROR, "occurrence_number must be a positive integer\n");
					return -1;
				}
			}
		} else {
			ast_log(AST_LOG_ERROR, "Retrieval method must be 'concat' or 'single'\n");
			return -1;
		}
	}

	sorcery = ast_sorcery_retrieve_by_module_name(args.module_name);
	if (!sorcery) {
		ast_log(AST_LOG_ERROR, "Failed to retrieve sorcery instance for module %s\n", args.module_name);
		return -1;
	}

	sorcery_obj = ast_sorcery_retrieve_by_id(sorcery, args.object_type, args.object_id);
	if (!sorcery_obj) {
		return -1;
	}

	change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
	if (!change_set) {
		return -1;
	}

	ix=1;
	found = 0;
	for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {

		if (method == CONCAT && strcmp(it_change_set->name, args.field_name) == 0) {
			ast_str_append(buf, 0, "%s%s", it_change_set->value, separator);
			found = 1;
			continue;
		}

		if (method == SINGLE && strcmp(it_change_set->name, args.field_name) == 0  && ix++ == field_number) {
			ast_str_set(buf, len, "%s", it_change_set->value);
			found = 1;
			break;
		}
	}

	ast_variables_destroy(change_set);

	if (!found) {
		return -1;
	}

	if (method == CONCAT) {
		ast_str_truncate(*buf, -1);
	}

	return 0;
}

static struct ast_custom_function sorcery_function = {
	.name = "AST_SORCERY",
	.read2 = sorcery_function_read,
};

static int unload_module(void)
{
	return ast_custom_function_unregister(&sorcery_function);
}

static int load_module(void)
{
	return ast_custom_function_register(&sorcery_function);
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get a field from a sorcery object");