summaryrefslogtreecommitdiff
path: root/res/res_sorcery_realtime.c
blob: 101034009f2c9f55bbc49ce7a4c4f5e7833a51ff (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * Joshua Colp <jcolp@digium.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 Sorcery Realtime Object Wizard
 *
 * \author Joshua Colp <jcolp@digium.com>
 */

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

#include "asterisk.h"

#include <regex.h>

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

/*! \brief They key field used to store the unique identifier for the object */
#define UUID_FIELD "id"

enum unqualified_fetch {
	UNQUALIFIED_FETCH_NO,
	UNQUALIFIED_FETCH_WARN,
	UNQUALIFIED_FETCH_YES,
	UNQUALIFIED_FETCH_ERROR,
};

struct sorcery_config {
	enum unqualified_fetch fetch;
	char family[];
};

static void *sorcery_realtime_open(const char *data);
static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object);
static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id);
static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields);
static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects,
					     const struct ast_variable *fields);
static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex);
static void sorcery_realtime_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type,
					     struct ao2_container *objects, const char *prefix, const size_t prefix_len);
static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object);
static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object);
static void sorcery_realtime_close(void *data);

static struct ast_sorcery_wizard realtime_object_wizard = {
	.name = "realtime",
	.open = sorcery_realtime_open,
	.create = sorcery_realtime_create,
	.retrieve_id = sorcery_realtime_retrieve_id,
	.retrieve_fields = sorcery_realtime_retrieve_fields,
	.retrieve_multiple = sorcery_realtime_retrieve_multiple,
	.retrieve_regex = sorcery_realtime_retrieve_regex,
	.retrieve_prefix = sorcery_realtime_retrieve_prefix,
	.update = sorcery_realtime_update,
	.delete = sorcery_realtime_delete,
	.close = sorcery_realtime_close,
};

static int sorcery_realtime_create(const struct ast_sorcery *sorcery, void *data, void *object)
{
	struct sorcery_config *config = data;
	RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);
	struct ast_variable *id = ast_variable_new(UUID_FIELD, ast_sorcery_object_get_id(object), "");

	if (!fields || !id) {
		ast_variables_destroy(id);
		return -1;
	}

	/* Place the identifier at the front for sanity sake */
	id->next = fields;
	fields = id;

	return (ast_store_realtime_fields(config->family, fields) <= 0) ? -1 : 0;
}

/*! \brief Internal helper function which returns a filtered objectset.
 *
 * The following are filtered out of the objectset:
 * \li The id field. This is returned to the caller in an out parameter.
 * \li Fields that are not registered with sorcery.
 *
 * \param objectset Objectset to filter.
 * \param[out] id The ID of the sorcery object, as found in the objectset.
 * \param sorcery The sorcery instance that is requesting an objectset.
 * \param type The object type
 *
 * \return The filtered objectset
 */
static struct ast_variable *sorcery_realtime_filter_objectset(struct ast_variable *objectset, struct ast_variable **id,
		const struct ast_sorcery *sorcery, const char *type)
{
	struct ast_variable *previous = NULL, *field = objectset;
	struct ast_sorcery_object_type *object_type;

	object_type = ast_sorcery_get_object_type(sorcery, type);
	if (!object_type) {
		ast_log(LOG_WARNING, "Unknown sorcery object type %s. Expect errors\n", type);
		/* Continue since we still want to filter out the id */
	}

	while (field) {
		int remove_field = 0;
		int delete_field = 0;

		if (!strcmp(field->name, UUID_FIELD)) {
			*id = field;
			remove_field = 1;
		} else if (object_type &&
				!ast_sorcery_is_object_field_registered(object_type, field->name)) {
			ast_debug(1, "Filtering out realtime field '%s' from retrieval\n", field->name);
			remove_field = 1;
			delete_field = 1;
		}

		if (remove_field) {
			struct ast_variable *removed;

			if (previous) {
				previous->next = field->next;
			} else {
				objectset = field->next;
			}

			removed = field;
			field = field->next;
			removed->next = NULL;
			if (delete_field) {
				ast_variables_destroy(removed);
			}
		} else {
			previous = field;
			field = field->next;
		}
	}

	ao2_cleanup(object_type);

	return objectset;
}

static void *sorcery_realtime_retrieve_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
{
	struct sorcery_config *config = data;
	RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
	RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
	void *object = NULL;

	if (!(objectset = ast_load_realtime_fields(config->family, fields))) {
		return NULL;
	}

	objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);

	if (!id
		|| !(object = ast_sorcery_alloc(sorcery, type, id->value))
		|| ast_sorcery_objectset_apply(sorcery, object, objectset)) {
		ao2_cleanup(object);
		return NULL;
	}

	return object;
}

static void *sorcery_realtime_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
{
	RAII_VAR(struct ast_variable *, fields, ast_variable_new(UUID_FIELD, id, ""), ast_variables_destroy);

	return sorcery_realtime_retrieve_fields(sorcery, data, type, fields);
}

static void sorcery_realtime_retrieve_multiple(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
{
	struct sorcery_config *config = data;
	RAII_VAR(struct ast_config *, rows, NULL, ast_config_destroy);
	RAII_VAR(struct ast_variable *, all, NULL, ast_variables_destroy);
	struct ast_category *row = NULL;

	if (!fields) {
		char field[strlen(UUID_FIELD) + 6], value[2];

		if (config->fetch == UNQUALIFIED_FETCH_NO) {
			return;
		}
		if (config->fetch == UNQUALIFIED_FETCH_ERROR) {
			ast_log(LOG_ERROR, "Unqualified fetch prevented on %s\n", config->family);
			return;
		}
		if (config->fetch == UNQUALIFIED_FETCH_WARN) {
			ast_log(LOG_WARNING, "Unqualified fetch attempted on %s\n", config->family);
			return;
		}

		/* If no fields have been specified we want all rows, so trick realtime into doing it */
		snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
		snprintf(value, sizeof(value), "%%");

		if (!(all = ast_variable_new(field, value, ""))) {
			return;
		}

		fields = all;
	}

	if (!(rows = ast_load_realtime_multientry_fields(config->family, fields))) {
		return;
	}

	while ((row = ast_category_browse_filtered(rows, NULL, row, NULL))) {
		struct ast_variable *objectset = ast_category_detach_variables(row);
		RAII_VAR(struct ast_variable *, id, NULL, ast_variables_destroy);
		RAII_VAR(void *, object, NULL, ao2_cleanup);

		objectset = sorcery_realtime_filter_objectset(objectset, &id, sorcery, type);

		if (id
			&& (object = ast_sorcery_alloc(sorcery, type, id->value))
			&& !ast_sorcery_objectset_apply(sorcery, object, objectset)) {
			ao2_link(objects, object);
		}

		ast_variables_destroy(objectset);
	}
}

static void sorcery_realtime_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
{
	char field[strlen(UUID_FIELD) + 6], value[strlen(regex) + 3];
	RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);

	if (!ast_strlen_zero(regex)) {
		/* The realtime API provides no direct ability to do regex so for now we support a limited subset using pattern matching */
		snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
		if (regex[0] == '^') {
			snprintf(value, sizeof(value), "%s%%", regex + 1);
		} else {
			snprintf(value, sizeof(value), "%%%s%%", regex);
		}

		if (!(fields = ast_variable_new(field, value, ""))) {
			return;
		}
	}

	sorcery_realtime_retrieve_multiple(sorcery, data, type, objects, fields);
}

static void sorcery_realtime_retrieve_prefix(const struct ast_sorcery *sorcery, void *data, const char *type,
					     struct ao2_container *objects, const char *prefix, const size_t prefix_len)
{
	char field[strlen(UUID_FIELD) + 6], value[prefix_len + 2];
	RAII_VAR(struct ast_variable *, fields, NULL, ast_variables_destroy);

	if (prefix_len) {
		snprintf(field, sizeof(field), "%s LIKE", UUID_FIELD);
		snprintf(value, sizeof(value), "%.*s%%", (int) prefix_len, prefix);
		if (!(fields = ast_variable_new(field, value, ""))) {
			return;
		}
	}

	sorcery_realtime_retrieve_multiple(sorcery, data, type, objects, fields);
}

static int sorcery_realtime_update(const struct ast_sorcery *sorcery, void *data, void *object)
{
	struct sorcery_config *config = data;
	RAII_VAR(struct ast_variable *, fields, ast_sorcery_objectset_create(sorcery, object), ast_variables_destroy);

	if (!fields) {
		return -1;
	}

	return (ast_update_realtime_fields(config->family, UUID_FIELD, ast_sorcery_object_get_id(object), fields) < 0) ? -1 : 0;
}

static int sorcery_realtime_delete(const struct ast_sorcery *sorcery, void *data, void *object)
{
	struct sorcery_config *config = data;

	return (ast_destroy_realtime_fields(config->family, UUID_FIELD, ast_sorcery_object_get_id(object), NULL) <= 0) ? -1 : 0;
}

static void *sorcery_realtime_open(const char *data)
{
	struct sorcery_config *config;
	char *tmp;
	char *family;
	char *option;

	/* We require a prefix for family string generation, or else stuff could mix together */
	if (ast_strlen_zero(data)) {
		return NULL;
	}

	tmp = ast_strdupa(data);
	family = strsep(&tmp, ",");

	if (!ast_realtime_is_mapping_defined(family)) {
		return NULL;
	}

	config = ast_calloc(1, sizeof(*config) + strlen(family) + 1);
	if (!config) {
		return NULL;
	}

	strcpy(config->family, family); /* Safe */
	config->fetch = UNQUALIFIED_FETCH_YES;

	while ((option = strsep(&tmp, ","))) {
		char *name = strsep(&option, "=");
		char *value = option;

		if (!strcasecmp(name, "allow_unqualified_fetch")) {
			if (ast_strlen_zero(value) || !strcasecmp(value, "yes")) {
				config->fetch = UNQUALIFIED_FETCH_YES;
			} else if (!strcasecmp(value, "no")) {
				config->fetch = UNQUALIFIED_FETCH_NO;
			} else if (!strcasecmp(value, "warn")) {
				config->fetch = UNQUALIFIED_FETCH_WARN;
			} else if (!strcasecmp(value, "error")) {
				config->fetch = UNQUALIFIED_FETCH_ERROR;
			} else {
				ast_log(LOG_ERROR, "Unrecognized value in %s:%s: '%s'\n", family, name, value);
				return NULL;
			}
		} else {
			ast_log(LOG_ERROR, "Unrecognized option in %s: '%s'\n", family, name);
			return NULL;
		}
	}

	return config;
}

static void sorcery_realtime_close(void *data)
{
	ast_free(data);
}

static int load_module(void)
{
	if (ast_sorcery_wizard_register(&realtime_object_wizard)) {
		return AST_MODULE_LOAD_DECLINE;
	}

	return AST_MODULE_LOAD_SUCCESS;
}

static int unload_module(void)
{
	ast_sorcery_wizard_unregister(&realtime_object_wizard);
	return 0;
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Sorcery Realtime Object Wizard",
	.support_level = AST_MODULE_SUPPORT_CORE,
	.load = load_module,
	.unload = unload_module,
	.load_pri = AST_MODPRI_REALTIME_DRIVER,
);