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