app_dial: Allow macro/gosub pre-bridge execution to occur on priorities
[asterisk/asterisk.git] / main / sorcery.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Sorcery Data Access Layer API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/logger.h"
35 #include "asterisk/sorcery.h"
36 #include "asterisk/astobj2.h"
37 #include "asterisk/format.h"
38 #include "asterisk/format_cap.h"
39 #include "asterisk/strings.h"
40 #include "asterisk/config_options.h"
41 #include "asterisk/netsock2.h"
42 #include "asterisk/module.h"
43 #include "asterisk/taskprocessor.h"
44 #include "asterisk/threadpool.h"
45 #include "asterisk/json.h"
46 #include "asterisk/format_pref.h"
47
48 /* To prevent DEBUG_FD_LEAKS from interfering with things we undef open and close */
49 #undef open
50 #undef close
51
52 /*! \brief Number of buckets for wizards (should be prime for performance reasons) */
53 #define WIZARD_BUCKETS 7
54
55 /*! \brief Number of buckets for types (should be prime for performance reasons) */
56 #define TYPE_BUCKETS 53
57
58 /*! \brief Thread pool for observers */
59 static struct ast_threadpool *threadpool;
60
61 /*! \brief Structure for internal sorcery object information */
62 struct ast_sorcery_object {
63         /*! \brief Unique identifier of this object */
64         char *id;
65
66         /*! \brief Type of object */
67         char type[MAX_OBJECT_TYPE];
68
69         /*! \brief Optional object destructor */
70         ao2_destructor_fn destructor;
71
72         /*! \brief Extended object fields */
73         struct ast_variable *extended;
74 };
75
76 /*! \brief Structure for registered object type */
77 struct ast_sorcery_object_type {
78         /*! \brief Unique name of the object type */
79         char name[MAX_OBJECT_TYPE];
80
81         /*! \brief Optional transformation callback */
82         sorcery_transform_handler transform;
83
84         /*! \brief Optional object set apply callback */
85         sorcery_apply_handler apply;
86
87         /*! \brief Optional object copy callback */
88         sorcery_copy_handler copy;
89
90         /*! \brief Optional object diff callback */
91         sorcery_diff_handler diff;
92
93         /*! \brief Wizard instances */
94         struct ao2_container *wizards;
95
96         /*! \brief Object fields */
97         struct ao2_container *fields;
98
99         /*! \brief Configuration framework general information */
100         struct aco_info *info;
101
102         /*! \brief Configuration framework file information */
103         struct aco_file *file;
104
105         /*! \brief Type details */
106         struct aco_type type;
107
108         /*! \brief Observers */
109         struct ao2_container *observers;
110
111         /*! \brief Serializer for observers */
112         struct ast_taskprocessor *serializer;
113
114         /*! \brief Specifies if object type is reloadable or not */
115         unsigned int reloadable:1;
116 };
117
118 /*! \brief Structure for registered object type observer */
119 struct ast_sorcery_object_type_observer {
120         /*! \brief Pointer to the observer implementation */
121         const struct ast_sorcery_observer *callbacks;
122 };
123
124 /*! \brief Structure used for observer invocations */
125 struct sorcery_observer_invocation {
126         /*! \brief Pointer to the object type */
127         struct ast_sorcery_object_type *object_type;
128
129         /*! \brief Pointer to the object */
130         void *object;
131 };
132
133 /*! \brief Structure for registered object field */
134 struct ast_sorcery_object_field {
135         /*! \brief Name of the field */
136         char name[MAX_OBJECT_FIELD];
137
138         /*! \brief Callback function for translation of a single value */
139         sorcery_field_handler handler;
140
141         /*! \brief Callback function for translation of multiple values */
142         sorcery_fields_handler multiple_handler;
143
144         /*! \brief Position of the field */
145         intptr_t args[];
146 };
147
148 /*! \brief Structure for a wizard instance which operates on objects */
149 struct ast_sorcery_object_wizard {
150         /*! \brief Wizard interface itself */
151         struct ast_sorcery_wizard *wizard;
152
153         /*! \brief Unique data for the wizard */
154         void *data;
155
156         /*! \brief Wizard is acting as an object cache */
157         unsigned int caching:1;
158 };
159
160 /*! \brief Full structure for sorcery */
161 struct ast_sorcery {
162         /*! \brief Container for known object types */
163         struct ao2_container *types;
164 };
165
166 /*! \brief Structure for passing load/reload details */
167 struct sorcery_load_details {
168         /*! \brief Sorcery structure in use */
169         const struct ast_sorcery *sorcery;
170
171         /*! \brief Type of object being loaded */
172         const char *type;
173
174         /*! \brief Whether this is a reload or not */
175         unsigned int reload:1;
176 };
177
178 /*! \brief Registered sorcery wizards */
179 struct ao2_container *wizards;
180
181 static int int_handler_fn(const void *obj, const intptr_t *args, char **buf)
182 {
183         int *field = (int *)(obj + args[0]);
184         return (ast_asprintf(buf, "%d", *field) < 0) ? -1 : 0;
185 }
186
187 static int uint_handler_fn(const void *obj, const intptr_t *args, char **buf)
188 {
189         unsigned int *field = (unsigned int *)(obj + args[0]);
190         return (ast_asprintf(buf, "%u", *field) < 0) ? -1 : 0;
191 }
192
193 static int double_handler_fn(const void *obj, const intptr_t *args, char **buf)
194 {
195         double *field = (double *)(obj + args[0]);
196         return (ast_asprintf(buf, "%f", *field) < 0) ? -1 : 0;
197 }
198
199 static int stringfield_handler_fn(const void *obj, const intptr_t *args, char **buf)
200 {
201         ast_string_field *field = (const char **)(obj + args[0]);
202         return !(*buf = ast_strdup(*field)) ? -1 : 0;
203 }
204
205 static int bool_handler_fn(const void *obj, const intptr_t *args, char **buf)
206 {
207         unsigned int *field = (unsigned int *)(obj + args[0]);
208         return !(*buf = ast_strdup(*field ? "true" : "false")) ? -1 : 0;
209 }
210
211 static int sockaddr_handler_fn(const void *obj, const intptr_t *args, char **buf)
212 {
213         struct ast_sockaddr *field = (struct ast_sockaddr *)(obj + args[0]);
214         return !(*buf = ast_strdup(ast_sockaddr_stringify(field))) ? -1 : 0;
215 }
216
217 static int chararray_handler_fn(const void *obj, const intptr_t *args, char **buf)
218 {
219         char *field = (char *)(obj + args[0]);
220         return !(*buf = ast_strdup(field)) ? -1 : 0;
221 }
222
223 static int codec_handler_fn(const void *obj, const intptr_t *args, char **buf)
224 {
225         char tmp_buf[256];
226         struct ast_codec_pref *pref = (struct ast_codec_pref *)(obj + args[0]);
227         ast_codec_pref_string(pref, tmp_buf, sizeof(tmp_buf));
228         return !(*buf = ast_strdup(tmp_buf));
229 }
230
231 static sorcery_field_handler sorcery_field_default_handler(enum aco_option_type type)
232 {
233         switch(type) {
234         case OPT_BOOL_T: return bool_handler_fn;
235         case OPT_CHAR_ARRAY_T: return chararray_handler_fn;
236         case OPT_CODEC_T: return codec_handler_fn;
237         case OPT_DOUBLE_T: return double_handler_fn;
238         case OPT_INT_T: return int_handler_fn;
239         case OPT_SOCKADDR_T: return sockaddr_handler_fn;
240         case OPT_STRINGFIELD_T: return stringfield_handler_fn;
241         case OPT_UINT_T: return uint_handler_fn;
242
243         default:
244         case OPT_CUSTOM_T: return NULL;
245         }
246
247         return NULL;
248 }
249
250 /*! \brief Hashing function for sorcery wizards */
251 static int sorcery_wizard_hash(const void *obj, const int flags)
252 {
253         const struct ast_sorcery_wizard *wizard = obj;
254         const char *name = obj;
255
256         return ast_str_hash(flags & OBJ_KEY ? name : wizard->name);
257 }
258
259 /*! \brief Comparator function for sorcery wizards */
260 static int sorcery_wizard_cmp(void *obj, void *arg, int flags)
261 {
262         struct ast_sorcery_wizard *wizard1 = obj, *wizard2 = arg;
263         const char *name = arg;
264
265         return !strcmp(wizard1->name, flags & OBJ_KEY ? name : wizard2->name) ? CMP_MATCH | CMP_STOP : 0;
266 }
267
268 /*! \brief Cleanup function */
269 static void sorcery_exit(void)
270 {
271         ast_threadpool_shutdown(threadpool);
272         threadpool = NULL;
273 }
274
275 /*! \brief Cleanup function for graceful shutdowns */
276 static void sorcery_cleanup(void)
277 {
278         ao2_cleanup(wizards);
279 }
280
281 int ast_sorcery_init(void)
282 {
283         struct ast_threadpool_options options = {
284                 .version = AST_THREADPOOL_OPTIONS_VERSION,
285                 .auto_increment = 1,
286                 .max_size = 0,
287                 .idle_timeout = 60,
288                 .initial_size = 0,
289         };
290         ast_assert(wizards == NULL);
291
292         if (!(threadpool = ast_threadpool_create("Sorcery", NULL, &options))) {
293                 threadpool = NULL;
294                 return -1;
295         }
296
297         if (!(wizards = ao2_container_alloc(WIZARD_BUCKETS, sorcery_wizard_hash, sorcery_wizard_cmp))) {
298                 ast_threadpool_shutdown(threadpool);
299                 return -1;
300         }
301
302         ast_register_cleanup(sorcery_cleanup);
303         ast_register_atexit(sorcery_exit);
304
305         return 0;
306 }
307
308 int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
309 {
310         struct ast_sorcery_wizard *wizard;
311         int res = -1;
312
313         ast_assert(!ast_strlen_zero(interface->name));
314
315         ao2_lock(wizards);
316
317         if ((wizard = ao2_find(wizards, interface->name, OBJ_KEY | OBJ_NOLOCK))) {
318                 ast_log(LOG_WARNING, "Attempted to register sorcery wizard '%s' twice\n",
319                         interface->name);
320                 goto done;
321         }
322
323         if (!(wizard = ao2_alloc(sizeof(*wizard), NULL))) {
324                 goto done;
325         }
326
327         *wizard = *interface;
328         wizard->module = module;
329
330         ao2_link_flags(wizards, wizard, OBJ_NOLOCK);
331         res = 0;
332
333         ast_verb(2, "Sorcery registered wizard '%s'\n", interface->name);
334
335 done:
336         ao2_cleanup(wizard);
337         ao2_unlock(wizards);
338
339         return res;
340 }
341
342 int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
343 {
344         RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, interface->name, OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
345
346         if (wizard) {
347                 ast_verb(2, "Sorcery unregistered wizard '%s'\n", interface->name);
348                 return 0;
349         } else {
350                 return -1;
351         }
352 }
353
354 /*! \brief Destructor called when sorcery structure is destroyed */
355 static void sorcery_destructor(void *obj)
356 {
357         struct ast_sorcery *sorcery = obj;
358
359         ao2_cleanup(sorcery->types);
360 }
361
362 /*! \brief Hashing function for sorcery types */
363 static int sorcery_type_hash(const void *obj, const int flags)
364 {
365         const struct ast_sorcery_object_type *type = obj;
366         const char *name = obj;
367
368         return ast_str_hash(flags & OBJ_KEY ? name : type->name);
369 }
370
371 /*! \brief Comparator function for sorcery types */
372 static int sorcery_type_cmp(void *obj, void *arg, int flags)
373 {
374         struct ast_sorcery_object_type *type1 = obj, *type2 = arg;
375         const char *name = arg;
376
377         return !strcmp(type1->name, flags & OBJ_KEY ? name : type2->name) ? CMP_MATCH | CMP_STOP : 0;
378 }
379
380 struct ast_sorcery *ast_sorcery_open(void)
381 {
382         struct ast_sorcery *sorcery;
383
384         if (!(sorcery = ao2_alloc(sizeof(*sorcery), sorcery_destructor))) {
385                 return NULL;
386         }
387
388         if (!(sorcery->types = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, TYPE_BUCKETS, sorcery_type_hash, sorcery_type_cmp))) {
389                 ao2_ref(sorcery, -1);
390                 sorcery = NULL;
391         }
392
393         return sorcery;
394 }
395
396 /*! \brief Destructor function for object types */
397 static void sorcery_object_type_destructor(void *obj)
398 {
399         struct ast_sorcery_object_type *object_type = obj;
400
401         ao2_cleanup(object_type->wizards);
402         ao2_cleanup(object_type->fields);
403         ao2_cleanup(object_type->observers);
404
405         if (object_type->info) {
406                 aco_info_destroy(object_type->info);
407                 ast_free(object_type->info);
408         }
409
410         ast_free(object_type->file);
411
412         ast_taskprocessor_unreference(object_type->serializer);
413 }
414
415 /*! \brief Internal function which allocates an object type structure */
416 static struct ast_sorcery_object_type *sorcery_object_type_alloc(const char *type, const char *module)
417 {
418         struct ast_sorcery_object_type *object_type;
419         char uuid[AST_UUID_STR_LEN];
420
421         if (!(object_type = ao2_alloc(sizeof(*object_type), sorcery_object_type_destructor))) {
422                 return NULL;
423         }
424
425         /* Order matters for object wizards */
426         if (!(object_type->wizards = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
427                 ao2_ref(object_type, -1);
428                 return NULL;
429         }
430
431         if (!(object_type->fields = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
432                 ao2_ref(object_type, -1);
433                 return NULL;
434         }
435
436         if (!(object_type->observers = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, 1, NULL, NULL))) {
437                 ao2_ref(object_type, -1);
438                 return NULL;
439         }
440
441         if (!(object_type->info = ast_calloc(1, sizeof(*object_type->info) + 2 * sizeof(object_type->info->files[0])))) {
442                 ao2_ref(object_type, -1);
443                 return NULL;
444         }
445
446         if (!(object_type->file = ast_calloc(1, sizeof(*object_type->file) + 2 * sizeof(object_type->file->types[0])))) {
447                 ao2_ref(object_type, -1);
448                 return NULL;
449         }
450
451         if (!ast_uuid_generate_str(uuid, sizeof(uuid))) {
452                 ao2_ref(object_type, -1);
453                 return NULL;
454         }
455
456         if (!(object_type->serializer = ast_threadpool_serializer(uuid, threadpool))) {
457                 ao2_ref(object_type, -1);
458                 return NULL;
459         }
460
461         object_type->info->files[0] = object_type->file;
462         object_type->info->files[1] = NULL;
463         object_type->info->module = module;
464
465         ast_copy_string(object_type->name, type, sizeof(object_type->name));
466
467         return object_type;
468 }
469
470 /*! \brief Object wizard destructor */
471 static void sorcery_object_wizard_destructor(void *obj)
472 {
473         struct ast_sorcery_object_wizard *object_wizard = obj;
474
475         if (object_wizard->data) {
476                 object_wizard->wizard->close(object_wizard->data);
477         }
478
479         if (object_wizard->wizard) {
480                 ast_module_unref(object_wizard->wizard->module);
481         }
482 }
483
484 /*! \brief Internal function which creates an object type and adds a wizard mapping */
485 static int sorcery_apply_wizard_mapping(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data, unsigned int caching)
486 {
487         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
488         RAII_VAR(struct ast_sorcery_wizard *, wizard, ao2_find(wizards, name, OBJ_KEY), ao2_cleanup);
489         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, ao2_alloc(sizeof(*object_wizard), sorcery_object_wizard_destructor), ao2_cleanup);
490         int created = 0;
491
492         if (!wizard || !object_wizard) {
493                 return -1;
494         }
495
496         if (!object_type) {
497                 if (!(object_type = sorcery_object_type_alloc(type, module))) {
498                         return -1;
499                 }
500                 created = 1;
501         }
502
503         if (wizard->open && !(object_wizard->data = wizard->open(data))) {
504                 return -1;
505         }
506
507         ast_module_ref(wizard->module);
508
509         object_wizard->wizard = wizard;
510         object_wizard->caching = caching;
511
512         ao2_link(object_type->wizards, object_wizard);
513
514         if (created) {
515                 ao2_link(sorcery->types, object_type);
516         }
517
518         return 0;
519 }
520
521 int __ast_sorcery_apply_config(struct ast_sorcery *sorcery, const char *name, const char *module)
522 {
523         struct ast_flags flags = { 0 };
524         struct ast_config *config = ast_config_load2("sorcery.conf", "sorcery", flags);
525         struct ast_variable *mapping;
526         int res = 0;
527
528         if (!config || config == CONFIG_STATUS_FILEINVALID) {
529                 return -1;
530         }
531
532         for (mapping = ast_variable_browse(config, name); mapping; mapping = mapping->next) {
533                 RAII_VAR(char *, mapping_name, ast_strdup(mapping->name), ast_free);
534                 RAII_VAR(char *, mapping_value, ast_strdup(mapping->value), ast_free);
535                 char *options = mapping_name;
536                 char *type = strsep(&options, "/");
537                 char *data = mapping_value;
538                 char *wizard = strsep(&data, ",");
539                 unsigned int caching = 0;
540
541                 /* If no object type or wizard exists just skip, nothing we can do */
542                 if (ast_strlen_zero(type) || ast_strlen_zero(wizard)) {
543                         continue;
544                 }
545
546                 /* If the wizard is configured as a cache treat it as such */
547                 if (!ast_strlen_zero(options) && strstr(options, "cache")) {
548                         caching = 1;
549                 }
550
551                 /* Any error immediately causes us to stop */
552                 if (sorcery_apply_wizard_mapping(sorcery, type, module, wizard, data, caching)) {
553                         res = -1;
554                         break;
555                 }
556         }
557
558         ast_config_destroy(config);
559
560         return res;
561 }
562
563 int __ast_sorcery_apply_default(struct ast_sorcery *sorcery, const char *type, const char *module, const char *name, const char *data)
564 {
565         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
566
567         /* Defaults can not be added if any existing mapping exists */
568         if (object_type) {
569                 return -1;
570         }
571
572         return sorcery_apply_wizard_mapping(sorcery, type, module, name, data, 0);
573 }
574
575 static int sorcery_extended_config_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
576 {
577         return ast_sorcery_object_set_extended(obj, var->name, var->value);
578 }
579
580 static int sorcery_extended_fields_handler(const void *obj, struct ast_variable **fields)
581 {
582         const struct ast_sorcery_object_details *details = obj;
583
584         if (details->object->extended) {
585                 *fields = ast_variables_dup(details->object->extended);
586         } else {
587                 *fields = NULL;
588         }
589
590         return 0;
591 }
592
593 int __ast_sorcery_object_register(struct ast_sorcery *sorcery, const char *type, unsigned int hidden, unsigned int reloadable, aco_type_item_alloc alloc, sorcery_transform_handler transform, sorcery_apply_handler apply)
594 {
595         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
596
597         if (!object_type || object_type->type.item_alloc) {
598                 return -1;
599         }
600
601         object_type->type.name = object_type->name;
602         object_type->type.type = ACO_ITEM;
603         object_type->type.category = ".?";
604         object_type->type.item_alloc = alloc;
605         object_type->type.hidden = hidden;
606
607         object_type->reloadable = reloadable;
608         object_type->transform = transform;
609         object_type->apply = apply;
610         object_type->file->types[0] = &object_type->type;
611         object_type->file->types[1] = NULL;
612
613         if (aco_info_init(object_type->info)) {
614                 return -1;
615         }
616
617         if (ast_sorcery_object_fields_register(sorcery, type, "^@", sorcery_extended_config_handler, sorcery_extended_fields_handler)) {
618                 return -1;
619         }
620
621         return 0;
622 }
623
624 void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
625 {
626         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
627
628         if (!object_type) {
629                 return;
630         }
631
632         object_type->copy = copy;
633 }
634
635 void ast_sorcery_object_set_diff_handler(struct ast_sorcery *sorcery, const char *type, sorcery_diff_handler diff)
636 {
637         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
638
639         if (!object_type) {
640                 return;
641         }
642
643         object_type->diff = diff;
644 }
645
646 int ast_sorcery_object_fields_register(struct ast_sorcery *sorcery, const char *type, const char *regex, aco_option_handler config_handler, sorcery_fields_handler sorcery_handler)
647 {
648         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
649         RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
650
651         if (!object_type || !object_type->type.item_alloc || !config_handler || !(object_field = ao2_alloc(sizeof(*object_field), NULL))) {
652                 return -1;
653         }
654
655         ast_copy_string(object_field->name, regex, sizeof(object_field->name));
656         object_field->multiple_handler = sorcery_handler;
657
658         ao2_link(object_type->fields, object_field);
659         __aco_option_register(object_type->info, regex, ACO_REGEX, object_type->file->types, "", OPT_CUSTOM_T, config_handler, 0, 1, 0);
660
661         return 0;
662 }
663
664 int __ast_sorcery_object_field_register(struct ast_sorcery *sorcery, const char *type, const char *name, const char *default_val, enum aco_option_type opt_type,
665                                         aco_option_handler config_handler, sorcery_field_handler sorcery_handler, unsigned int flags, unsigned int no_doc, size_t argc, ...)
666 {
667         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
668         RAII_VAR(struct ast_sorcery_object_field *, object_field, NULL, ao2_cleanup);
669         int pos;
670         va_list args;
671
672         if (!strcmp(type, "id") || !object_type || !object_type->type.item_alloc) {
673                 return -1;
674         }
675
676         if (!sorcery_handler) {
677                 sorcery_handler = sorcery_field_default_handler(opt_type);
678         }
679
680         if (!(object_field = ao2_alloc(sizeof(*object_field) + argc * sizeof(object_field->args[0]), NULL))) {
681                 return -1;
682         }
683
684         ast_copy_string(object_field->name, name, sizeof(object_field->name));
685         object_field->handler = sorcery_handler;
686
687         va_start(args, argc);
688         for (pos = 0; pos < argc; pos++) {
689                 object_field->args[pos] = va_arg(args, size_t);
690         }
691         va_end(args);
692
693         ao2_link(object_type->fields, object_field);
694
695         /* TODO: Improve this hack */
696         if (!argc) {
697                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc);
698         } else if (argc == 1) {
699                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
700                         object_field->args[0]);
701         } else if (argc == 2) {
702                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
703                         object_field->args[0], object_field->args[1]);
704         } else if (argc == 3) {
705                 __aco_option_register(object_type->info, name, ACO_EXACT, object_type->file->types, default_val, opt_type, config_handler, flags, no_doc, argc,
706                         object_field->args[0], object_field->args[1], object_field->args[2]);
707         } else {
708                 ast_assert(0); /* The hack... she does us no good for this */
709         }
710
711         return 0;
712 }
713
714 /*! \brief Retrieves whether or not the type is reloadable */
715 static int sorcery_reloadable(const struct ast_sorcery *sorcery, const char *type)
716 {
717         RAII_VAR(struct ast_sorcery_object_type *, object_type,
718                  ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
719         return object_type && object_type->reloadable;
720 }
721
722 static int sorcery_wizard_load(void *obj, void *arg, int flags)
723 {
724         struct ast_sorcery_object_wizard *wizard = obj;
725         struct sorcery_load_details *details = arg;
726         void (*load)(void *data, const struct ast_sorcery *sorcery, const char *type);
727
728         if (details->reload && !sorcery_reloadable(details->sorcery, details->type)) {
729                 ast_log(LOG_NOTICE, "Type '%s' is not reloadable, "
730                         "maintaining previous values\n", details->type);
731                 return 0;
732         }
733
734         load = !details->reload ? wizard->wizard->load : wizard->wizard->reload;
735
736         if (load) {
737                 load(wizard->data, details->sorcery, details->type);
738         }
739
740         return 0;
741 }
742
743 /*! \brief Destructor for observer invocation */
744 static void sorcery_observer_invocation_destroy(void *obj)
745 {
746         struct sorcery_observer_invocation *invocation = obj;
747
748         ao2_cleanup(invocation->object_type);
749         ao2_cleanup(invocation->object);
750 }
751
752 /*! \brief Allocator function for observer invocation */
753 static struct sorcery_observer_invocation *sorcery_observer_invocation_alloc(struct ast_sorcery_object_type *object_type, void *object)
754 {
755         struct sorcery_observer_invocation *invocation = ao2_alloc(sizeof(*invocation), sorcery_observer_invocation_destroy);
756
757         if (!invocation) {
758                 return NULL;
759         }
760
761         ao2_ref(object_type, +1);
762         invocation->object_type = object_type;
763
764         if (object) {
765                 ao2_ref(object, +1);
766                 invocation->object = object;
767         }
768
769         return invocation;
770 }
771
772 /*! \brief Internal callback function which notifies an individual observer that an object type has been loaded */
773 static int sorcery_observer_notify_loaded(void *obj, void *arg, int flags)
774 {
775         const struct ast_sorcery_object_type_observer *observer = obj;
776
777         if (observer->callbacks->loaded) {
778                 observer->callbacks->loaded(arg);
779         }
780
781         return 0;
782 }
783
784 /*! \brief Internal callback function which notifies observers that an object type has been loaded */
785 static int sorcery_observers_notify_loaded(void *data)
786 {
787         struct sorcery_observer_invocation *invocation = data;
788
789         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_loaded, invocation->object_type->name);
790         ao2_cleanup(invocation);
791
792         return 0;
793 }
794
795 static int sorcery_object_load(void *obj, void *arg, int flags)
796 {
797         struct ast_sorcery_object_type *type = obj;
798         struct sorcery_load_details *details = arg;
799
800         details->type = type->name;
801         ao2_callback(type->wizards, OBJ_NODATA, sorcery_wizard_load, details);
802
803         if (ao2_container_count(type->observers)) {
804                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(type, NULL);
805
806                 if (invocation && ast_taskprocessor_push(type->serializer, sorcery_observers_notify_loaded, invocation)) {
807                         ao2_cleanup(invocation);
808                 }
809         }
810
811         return 0;
812 }
813
814 void ast_sorcery_load(const struct ast_sorcery *sorcery)
815 {
816         struct sorcery_load_details details = {
817                 .sorcery = sorcery,
818                 .reload = 0,
819         };
820
821         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
822 }
823
824 void ast_sorcery_load_object(const struct ast_sorcery *sorcery, const char *type)
825 {
826         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
827         struct sorcery_load_details details = {
828                 .sorcery = sorcery,
829                 .reload = 0,
830         };
831
832         if (!object_type) {
833                 return;
834         }
835
836         sorcery_object_load(object_type, &details, 0);
837 }
838
839 void ast_sorcery_reload(const struct ast_sorcery *sorcery)
840 {
841         struct sorcery_load_details details = {
842                 .sorcery = sorcery,
843                 .reload = 1,
844         };
845
846         ao2_callback(sorcery->types, OBJ_NODATA, sorcery_object_load, &details);
847 }
848
849 void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
850 {
851         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
852         struct sorcery_load_details details = {
853                 .sorcery = sorcery,
854                 .reload = 1,
855         };
856
857         if (!object_type) {
858                 return;
859         }
860
861         sorcery_object_load(object_type, &details, 0);
862 }
863
864 void ast_sorcery_ref(struct ast_sorcery *sorcery)
865 {
866         ao2_ref(sorcery, +1);
867 }
868
869 struct ast_variable *ast_sorcery_objectset_create(const struct ast_sorcery *sorcery, const void *object)
870 {
871         const struct ast_sorcery_object_details *details = object;
872         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
873         struct ao2_iterator i;
874         struct ast_sorcery_object_field *object_field;
875         struct ast_variable *fields = NULL;
876         int res = 0;
877
878         if (!object_type) {
879                 return NULL;
880         }
881
882         i = ao2_iterator_init(object_type->fields, 0);
883
884         for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
885                 struct ast_variable *tmp = NULL;
886
887                 if (object_field->multiple_handler) {
888                         if ((res = object_field->multiple_handler(object, &tmp))) {
889                                 ast_variables_destroy(tmp);
890                         }
891                 } else if (object_field->handler) {
892                         char *buf = NULL;
893
894                         if ((res = object_field->handler(object, object_field->args, &buf)) ||
895                                 !(tmp = ast_variable_new(object_field->name, S_OR(buf, ""), ""))) {
896                                 res = -1;
897                         }
898
899                         ast_free(buf);
900                 } else {
901                         continue;
902                 }
903
904                 if (!res && tmp) {
905                         tmp->next = fields;
906                         fields = tmp;
907                 }
908         }
909
910         ao2_iterator_destroy(&i);
911
912         /* If any error occurs we destroy all fields handled before so a partial objectset is not returned */
913         if (res) {
914                 ast_variables_destroy(fields);
915                 fields = NULL;
916         }
917
918         return fields;
919 }
920
921 struct ast_json *ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
922 {
923         const struct ast_sorcery_object_details *details = object;
924         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
925         struct ao2_iterator i;
926         struct ast_sorcery_object_field *object_field;
927         struct ast_json *json = ast_json_object_create();
928         int res = 0;
929
930         if (!object_type || !json) {
931                 return NULL;
932         }
933
934         i = ao2_iterator_init(object_type->fields, 0);
935
936         for (; (object_field = ao2_iterator_next(&i)) && !res; ao2_ref(object_field, -1)) {
937                 if (object_field->multiple_handler) {
938                         struct ast_variable *tmp = NULL;
939                         struct ast_variable *field;
940
941                         if ((res = object_field->multiple_handler(object, &tmp))) {
942                                 ao2_ref(object_field, -1);
943                                 break;
944                         }
945
946                         for (field = tmp; field; field = field->next) {
947                                 struct ast_json *value = ast_json_string_create(field->value);
948
949                                 if (value && ast_json_object_set(json, field->name, value)) {
950                                         ast_json_unref(value);
951                                         res = -1;
952                                 }
953                         }
954
955                         ast_variables_destroy(tmp);
956                 } else if (object_field->handler) {
957                         char *buf = NULL;
958                         struct ast_json *value = NULL;
959
960                         if ((res = object_field->handler(object, object_field->args, &buf)) ||
961                                 !(value = ast_json_string_create(buf)) ||
962                                 ast_json_object_set(json, object_field->name, value)) {
963                                 ast_json_unref(value);
964                                 res = -1;
965                         }
966
967                         ast_free(buf);
968                 } else {
969                         continue;
970                 }
971         }
972
973         ao2_iterator_destroy(&i);
974
975         /* If any error occurs we destroy the JSON object so a partial objectset is not returned */
976         if (res) {
977                 ast_json_unref(json);
978                 json = NULL;
979         }
980
981         return json;
982 }
983
984 int ast_sorcery_objectset_apply(const struct ast_sorcery *sorcery, void *object, struct ast_variable *objectset)
985 {
986         const struct ast_sorcery_object_details *details = object;
987         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
988         RAII_VAR(struct ast_variable *, transformed, NULL, ast_variables_destroy);
989         struct ast_variable *field;
990         int res = 0;
991
992         if (!object_type) {
993                 return -1;
994         }
995
996         if (object_type->transform && (transformed = object_type->transform(objectset))) {
997                 field = transformed;
998         } else {
999                 field = objectset;
1000         }
1001
1002         for (; field; field = field->next) {
1003                 if ((res = aco_process_var(&object_type->type, details->object->id, field, object))) {
1004                         break;
1005                 }
1006         }
1007
1008         if (!res && object_type->apply) {
1009                 res = object_type->apply(sorcery, object);
1010         }
1011
1012         return res;
1013 }
1014
1015 static const struct ast_variable *sorcery_find_field(const struct ast_variable *fields, const char *name)
1016 {
1017         const struct ast_variable *field;
1018
1019         /* Search the linked list of fields to find the correct one */
1020         for (field = fields; field; field = field->next) {
1021                 if (!strcmp(field->name, name)) {
1022                         return field;
1023                 }
1024         }
1025
1026         return NULL;
1027 }
1028
1029 int ast_sorcery_changeset_create(const struct ast_variable *original, const struct ast_variable *modified, struct ast_variable **changes)
1030 {
1031         const struct ast_variable *field;
1032         int res = 0;
1033
1034         *changes = NULL;
1035
1036         /* Unless the ast_variable list changes when examined... it can't differ from itself */
1037         if (original == modified) {
1038                 return 0;
1039         }
1040
1041         for (field = modified; field; field = field->next) {
1042                 const struct ast_variable *old = sorcery_find_field(original, field->name);
1043
1044                 if (!old || strcmp(old->value, field->value)) {
1045                         struct ast_variable *tmp;
1046
1047                         if (!(tmp = ast_variable_new(field->name, field->value, ""))) {
1048                                 res = -1;
1049                                 break;
1050                         }
1051
1052                         tmp->next = *changes;
1053                         *changes = tmp;
1054                 }
1055         }
1056
1057         /* If an error occurred do not return a partial changeset */
1058         if (res) {
1059                 ast_variables_destroy(*changes);
1060                 *changes = NULL;
1061         }
1062
1063         return res;
1064 }
1065
1066 static void sorcery_object_destructor(void *object)
1067 {
1068         struct ast_sorcery_object_details *details = object;
1069
1070         if (details->object->destructor) {
1071                 details->object->destructor(object);
1072         }
1073
1074         ast_variables_destroy(details->object->extended);
1075         ast_free(details->object->id);
1076 }
1077
1078 void *ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
1079 {
1080         void *object = ao2_alloc_options(size + sizeof(struct ast_sorcery_object), sorcery_object_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
1081         struct ast_sorcery_object_details *details = object;
1082
1083         if (!object) {
1084                 return NULL;
1085         }
1086
1087         details->object = object + size;
1088         details->object->destructor = destructor;
1089
1090         return object;
1091 }
1092
1093 void *ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
1094 {
1095         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1096         struct ast_sorcery_object_details *details;
1097
1098         if (!object_type || !object_type->type.item_alloc ||
1099                 !(details = object_type->type.item_alloc(id))) {
1100                 return NULL;
1101         }
1102
1103         if (ast_strlen_zero(id)) {
1104                 char uuid[AST_UUID_STR_LEN];
1105
1106                 ast_uuid_generate_str(uuid, sizeof(uuid));
1107                 details->object->id = ast_strdup(uuid);
1108         } else {
1109                 details->object->id = ast_strdup(id);
1110         }
1111
1112         ast_copy_string(details->object->type, type, sizeof(details->object->type));
1113
1114         if (aco_set_defaults(&object_type->type, id, details)) {
1115                 ao2_ref(details, -1);
1116                 return NULL;
1117         }
1118
1119         return details;
1120 }
1121
1122 void *ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
1123 {
1124         const struct ast_sorcery_object_details *details = object;
1125         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1126         struct ast_sorcery_object_details *copy = ast_sorcery_alloc(sorcery, details->object->type, details->object->id);
1127         RAII_VAR(struct ast_variable *, objectset, NULL, ast_variables_destroy);
1128         int res = 0;
1129
1130         if (!copy) {
1131                 return NULL;
1132         } else if (object_type->copy) {
1133                 res = object_type->copy(object, copy);
1134         } else if ((objectset = ast_sorcery_objectset_create(sorcery, object))) {
1135                 res = ast_sorcery_objectset_apply(sorcery, copy, objectset);
1136         } else {
1137                 /* No native copy available and could not create an objectset, this copy has failed */
1138                 res = -1;
1139         }
1140
1141         if (res) {
1142                 ao2_cleanup(copy);
1143                 copy = NULL;
1144         }
1145
1146         return copy;
1147 }
1148
1149 int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
1150 {
1151         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, ast_sorcery_object_get_type(original), OBJ_KEY), ao2_cleanup);
1152
1153         *changes = NULL;
1154
1155         if (strcmp(ast_sorcery_object_get_type(original), ast_sorcery_object_get_type(modified))) {
1156                 return -1;
1157         }
1158
1159         if (original == modified) {
1160                 return 0;
1161         } else if (!object_type->diff) {
1162                 RAII_VAR(struct ast_variable *, objectset1, NULL, ast_variables_destroy);
1163                 RAII_VAR(struct ast_variable *, objectset2, NULL, ast_variables_destroy);
1164
1165                 objectset1 = ast_sorcery_objectset_create(sorcery, original);
1166                 objectset2 = ast_sorcery_objectset_create(sorcery, modified);
1167
1168                 return ast_sorcery_changeset_create(objectset1, objectset2, changes);
1169         } else {
1170                 return object_type->diff(original, modified, changes);
1171         }
1172 }
1173
1174 /*! \brief Structure used when calling create, update, or delete */
1175 struct sorcery_details {
1176         /*! \brief Pointer to the sorcery instance */
1177         const struct ast_sorcery *sorcery;
1178         /*! \brief Pointer to the object itself */
1179         void *obj;
1180 };
1181
1182 /*! \brief Internal function used to create an object in caching wizards */
1183 static int sorcery_cache_create(void *obj, void *arg, int flags)
1184 {
1185         const struct ast_sorcery_object_wizard *object_wizard = obj;
1186         const struct sorcery_details *details = arg;
1187
1188         if (!object_wizard->caching || !object_wizard->wizard->create) {
1189                 return 0;
1190         }
1191
1192         object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj);
1193
1194         return 0;
1195 }
1196
1197 void *ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
1198 {
1199         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1200         void *object = NULL;
1201         struct ao2_iterator i;
1202         struct ast_sorcery_object_wizard *wizard;
1203         unsigned int cached = 0;
1204
1205         if (!object_type || ast_strlen_zero(id)) {
1206                 return NULL;
1207         }
1208
1209         i = ao2_iterator_init(object_type->wizards, 0);
1210         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1211                 if (wizard->wizard->retrieve_id &&
1212                         !(object = wizard->wizard->retrieve_id(sorcery, wizard->data, object_type->name, id))) {
1213                         continue;
1214                 }
1215
1216                 cached = wizard->caching;
1217
1218                 ao2_ref(wizard, -1);
1219                 break;
1220         }
1221         ao2_iterator_destroy(&i);
1222
1223         if (!cached && object) {
1224                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1225         }
1226
1227         return object;
1228 }
1229
1230 void *ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
1231 {
1232         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1233         void *object = NULL;
1234         struct ao2_iterator i;
1235         struct ast_sorcery_object_wizard *wizard;
1236         unsigned int cached = 0;
1237
1238         if (!object_type) {
1239                 return NULL;
1240         }
1241
1242         /* If returning multiple objects create a container to store them in */
1243         if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1244                 if (!(object = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1245                         return NULL;
1246                 }
1247         }
1248
1249         /* Inquire with the available wizards for retrieval */
1250         i = ao2_iterator_init(object_type->wizards, 0);
1251         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1252                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE)) {
1253                         if (wizard->wizard->retrieve_multiple) {
1254                                 wizard->wizard->retrieve_multiple(sorcery, wizard->data, object_type->name, object, fields);
1255                         }
1256                 } else if (fields && wizard->wizard->retrieve_fields) {
1257                         if (wizard->wizard->retrieve_fields) {
1258                                 object = wizard->wizard->retrieve_fields(sorcery, wizard->data, object_type->name, fields);
1259                         }
1260                 }
1261
1262                 if ((flags & AST_RETRIEVE_FLAG_MULTIPLE) || !object) {
1263                         continue;
1264                 }
1265
1266                 cached = wizard->caching;
1267
1268                 ao2_ref(wizard, -1);
1269                 break;
1270         }
1271         ao2_iterator_destroy(&i);
1272
1273         /* If we are returning a single object and it came from a non-cache source create it in any caches */
1274         if (!(flags & AST_RETRIEVE_FLAG_MULTIPLE) && !cached && object) {
1275                 ao2_callback(object_type->wizards, 0, sorcery_cache_create, object);
1276         }
1277
1278         return object;
1279 }
1280
1281 struct ao2_container *ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
1282 {
1283         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1284         struct ao2_container *objects;
1285         struct ao2_iterator i;
1286         struct ast_sorcery_object_wizard *wizard;
1287
1288         if (!object_type || !(objects = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) {
1289                 return NULL;
1290         }
1291
1292         i = ao2_iterator_init(object_type->wizards, 0);
1293         for (; (wizard = ao2_iterator_next(&i)); ao2_ref(wizard, -1)) {
1294                 if (!wizard->wizard->retrieve_regex) {
1295                         continue;
1296                 }
1297
1298                 wizard->wizard->retrieve_regex(sorcery, wizard->data, object_type->name, objects, regex);
1299         }
1300         ao2_iterator_destroy(&i);
1301
1302         return objects;
1303 }
1304
1305 /*! \brief Internal function which returns if the wizard has created the object */
1306 static int sorcery_wizard_create(void *obj, void *arg, int flags)
1307 {
1308         const struct ast_sorcery_object_wizard *object_wizard = obj;
1309         const struct sorcery_details *details = arg;
1310
1311         return (!object_wizard->caching && !object_wizard->wizard->create(details->sorcery, object_wizard->data, details->obj)) ? CMP_MATCH | CMP_STOP : 0;
1312 }
1313
1314 /*! \brief Internal callback function which notifies an individual observer that an object has been created */
1315 static int sorcery_observer_notify_create(void *obj, void *arg, int flags)
1316 {
1317         const struct ast_sorcery_object_type_observer *observer = obj;
1318
1319         if (observer->callbacks->created) {
1320                 observer->callbacks->created(arg);
1321         }
1322
1323         return 0;
1324 }
1325
1326 /*! \brief Internal callback function which notifies observers that an object has been created */
1327 static int sorcery_observers_notify_create(void *data)
1328 {
1329         struct sorcery_observer_invocation *invocation = data;
1330
1331         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_create, invocation->object);
1332         ao2_cleanup(invocation);
1333
1334         return 0;
1335 }
1336
1337 int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
1338 {
1339         const struct ast_sorcery_object_details *details = object;
1340         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1341         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1342         struct sorcery_details sdetails = {
1343                 .sorcery = sorcery,
1344                 .obj = object,
1345         };
1346
1347         if (!object_type) {
1348                 return -1;
1349         }
1350
1351         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_create, &sdetails)) &&
1352                 ao2_container_count(object_type->observers)) {
1353                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1354
1355                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_create, invocation)) {
1356                         ao2_cleanup(invocation);
1357                 }
1358         }
1359
1360         return object_wizard ? 0 : -1;
1361 }
1362
1363 /*! \brief Internal callback function which notifies an individual observer that an object has been updated */
1364 static int sorcery_observer_notify_update(void *obj, void *arg, int flags)
1365 {
1366         const struct ast_sorcery_object_type_observer *observer = obj;
1367
1368         if (observer->callbacks->updated) {
1369                 observer->callbacks->updated(arg);
1370         }
1371
1372         return 0;
1373 }
1374
1375 /*! \brief Internal callback function which notifies observers that an object has been updated */
1376 static int sorcery_observers_notify_update(void *data)
1377 {
1378         struct sorcery_observer_invocation *invocation = data;
1379
1380         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_update, invocation->object);
1381         ao2_cleanup(invocation);
1382
1383         return 0;
1384 }
1385
1386 /*! \brief Internal function which returns if a wizard has updated the object */
1387 static int sorcery_wizard_update(void *obj, void *arg, int flags)
1388 {
1389         const struct ast_sorcery_object_wizard *object_wizard = obj;
1390         const struct sorcery_details *details = arg;
1391
1392         return (object_wizard->wizard->update && !object_wizard->wizard->update(details->sorcery, object_wizard->data, details->obj) &&
1393                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1394 }
1395
1396 int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
1397 {
1398         const struct ast_sorcery_object_details *details = object;
1399         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1400         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1401         struct sorcery_details sdetails = {
1402                 .sorcery = sorcery,
1403                 .obj = object,
1404         };
1405
1406         if (!object_type) {
1407                 return -1;
1408         }
1409
1410         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_update, &sdetails)) &&
1411                 ao2_container_count(object_type->observers)) {
1412                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1413
1414                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_update, invocation)) {
1415                         ao2_cleanup(invocation);
1416                 }
1417         }
1418
1419         return object_wizard ? 0 : -1;
1420 }
1421
1422 /*! \brief Internal callback function which notifies an individual observer that an object has been deleted */
1423 static int sorcery_observer_notify_delete(void *obj, void *arg, int flags)
1424 {
1425         const struct ast_sorcery_object_type_observer *observer = obj;
1426
1427         if (observer->callbacks->deleted) {
1428                 observer->callbacks->deleted(arg);
1429         }
1430
1431         return 0;
1432 }
1433
1434 /*! \brief Internal callback function which notifies observers that an object has been deleted */
1435 static int sorcery_observers_notify_delete(void *data)
1436 {
1437         struct sorcery_observer_invocation *invocation = data;
1438
1439         ao2_callback(invocation->object_type->observers, OBJ_NODATA, sorcery_observer_notify_delete, invocation->object);
1440         ao2_cleanup(invocation);
1441
1442         return 0;
1443 }
1444
1445 /*! \brief Internal function which returns if a wizard has deleted the object */
1446 static int sorcery_wizard_delete(void *obj, void *arg, int flags)
1447 {
1448         const struct ast_sorcery_object_wizard *object_wizard = obj;
1449         const struct sorcery_details *details = arg;
1450
1451         return (object_wizard->wizard->delete && !object_wizard->wizard->delete(details->sorcery, object_wizard->data, details->obj) &&
1452                 !object_wizard->caching) ? CMP_MATCH | CMP_STOP : 0;
1453 }
1454
1455 int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
1456 {
1457         const struct ast_sorcery_object_details *details = object;
1458         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, details->object->type, OBJ_KEY), ao2_cleanup);
1459         RAII_VAR(struct ast_sorcery_object_wizard *, object_wizard, NULL, ao2_cleanup);
1460         struct sorcery_details sdetails = {
1461                 .sorcery = sorcery,
1462                 .obj = object,
1463         };
1464
1465         if (!object_type) {
1466                 return -1;
1467         }
1468
1469         if ((object_wizard = ao2_callback(object_type->wizards, 0, sorcery_wizard_delete, &sdetails)) &&
1470                 ao2_container_count(object_type->observers)) {
1471                 struct sorcery_observer_invocation *invocation = sorcery_observer_invocation_alloc(object_type, object);
1472
1473                 if (invocation && ast_taskprocessor_push(object_type->serializer, sorcery_observers_notify_delete, invocation)) {
1474                         ao2_cleanup(invocation);
1475                 }
1476         }
1477
1478         return object_wizard ? 0 : -1;
1479 }
1480
1481 void ast_sorcery_unref(struct ast_sorcery *sorcery)
1482 {
1483         ao2_cleanup(sorcery);
1484 }
1485
1486 const char *ast_sorcery_object_get_id(const void *object)
1487 {
1488         const struct ast_sorcery_object_details *details = object;
1489         return details->object->id;
1490 }
1491
1492 const char *ast_sorcery_object_get_type(const void *object)
1493 {
1494         const struct ast_sorcery_object_details *details = object;
1495         return details->object->type;
1496 }
1497
1498 const char *ast_sorcery_object_get_extended(const void *object, const char *name)
1499 {
1500         const struct ast_sorcery_object_details *details = object;
1501         struct ast_variable *field;
1502
1503         for (field = details->object->extended; field; field = field->next) {
1504                 if (!strcmp(field->name + 1, name)) {
1505                         return field->value;
1506                 }
1507         }
1508
1509         return NULL;
1510 }
1511
1512 int ast_sorcery_object_set_extended(const void *object, const char *name, const char *value)
1513 {
1514         RAII_VAR(struct ast_variable *, field, NULL, ast_variables_destroy);
1515         struct ast_variable *extended = ast_variable_new(name, value, ""), *previous = NULL;
1516         const struct ast_sorcery_object_details *details = object;
1517
1518         if (!extended) {
1519                 return -1;
1520         }
1521
1522         for (field = details->object->extended; field; previous = field, field = field->next) {
1523                 if (!strcmp(field->name, name)) {
1524                         if (previous) {
1525                                 previous->next = field->next;
1526                         } else {
1527                                 details->object->extended = field->next;
1528                         }
1529                         field->next = NULL;
1530                         break;
1531                 }
1532         }
1533
1534         extended->next = details->object->extended;
1535         details->object->extended = extended;
1536
1537         return 0;
1538 }
1539
1540 int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1541 {
1542         RAII_VAR(struct ast_sorcery_object_type *, object_type, ao2_find(sorcery->types, type, OBJ_KEY), ao2_cleanup);
1543         struct ast_sorcery_object_type_observer *observer;
1544         int res;
1545
1546         if (!object_type || !callbacks) {
1547                 return -1;
1548         }
1549
1550         if (!(observer = ao2_alloc(sizeof(*observer), NULL))) {
1551                 return -1;
1552         }
1553
1554         observer->callbacks = callbacks;
1555         res = 0;
1556         if (!ao2_link(object_type->observers, observer)) {
1557                 res = -1;
1558         }
1559         ao2_ref(observer, -1);
1560
1561         return res;
1562 }
1563
1564 /*! \brief Internal callback function for removing an observer */
1565 static int sorcery_observer_remove(void *obj, void *arg, int flags)
1566 {
1567         const struct ast_sorcery_object_type_observer *observer = obj;
1568
1569         return (observer->callbacks == arg) ? CMP_MATCH | CMP_STOP : 0;
1570 }
1571
1572 void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
1573 {
1574         RAII_VAR(struct ast_sorcery_object_type *, object_type, NULL, ao2_cleanup);
1575         struct ast_sorcery_observer *cbs = (struct ast_sorcery_observer *) callbacks;/* Remove const for traversal. */
1576
1577         if (!sorcery) {
1578                 return;
1579         }
1580         object_type = ao2_find(sorcery->types, type, OBJ_KEY);
1581         if (!object_type) {
1582                 return;
1583         }
1584
1585         ao2_callback(object_type->observers, OBJ_NODATA | OBJ_UNLINK,
1586                 sorcery_observer_remove, cbs);
1587 }
1588
1589 int ast_sorcery_object_id_compare(const void *obj_left, const void *obj_right, int flags)
1590 {
1591         if (!obj_left || !obj_right) {
1592                 return 0;
1593         }
1594         return strcmp(ast_sorcery_object_get_id(obj_left), ast_sorcery_object_get_id(obj_right));
1595 }