channel: Clear channel flag in error branch.
[asterisk/asterisk.git] / funcs / func_pjsip_aor.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2015, 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 Get information about a PJSIP AOR
22  *
23  * \author \verbatim Joshua Colp <jcolp@digium.com> \endverbatim
24  *
25  * \ingroup functions
26  *
27  */
28
29 /*** MODULEINFO
30         <support_level>core</support_level>
31         <depend>pjproject</depend>
32         <depend>res_pjsip</depend>
33  ***/
34
35 #include "asterisk.h"
36
37 #include <pjsip.h>
38 #include <pjlib.h>
39
40 #include "asterisk/app.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/sorcery.h"
44 #include "asterisk/res_pjsip.h"
45
46 /*** DOCUMENTATION
47         <function name="PJSIP_AOR" language="en_US">
48                 <synopsis>
49                         Get information about a PJSIP AOR
50                 </synopsis>
51                 <syntax>
52                         <parameter name="name" required="true">
53                                 <para>The name of the AOR to query.</para>
54                         </parameter>
55                         <parameter name="field" required="true">
56                                 <para>The configuration option for the AOR to query for.
57                                 Supported options are those fields on the
58                                 <replaceable>aor</replaceable> object in
59                                 <filename>pjsip.conf</filename>.</para>
60                                 <enumlist>
61                                         <configOptionToEnum>
62                                                 <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption)"/>
63                                         </configOptionToEnum>
64                                 </enumlist>
65                         </parameter>
66                 </syntax>
67         </function>
68 ***/
69
70 static int pjsip_aor_function_read(struct ast_channel *chan,
71         const char *cmd, char *data, struct ast_str **buf, ssize_t len)
72 {
73         struct ast_sorcery *pjsip_sorcery;
74         char *parsed_data = ast_strdupa(data);
75         RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
76         int res = 0;
77
78         AST_DECLARE_APP_ARGS(args,
79                 AST_APP_ARG(aor_name);
80                 AST_APP_ARG(field_name);
81         );
82
83         /* Check for zero arguments */
84         if (ast_strlen_zero(parsed_data)) {
85                 ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
86                 return -1;
87         }
88
89         AST_STANDARD_APP_ARGS(args, parsed_data);
90
91         if (ast_strlen_zero(args.aor_name)) {
92                 ast_log(AST_LOG_ERROR, "Cannot call %s without an AOR name to query\n", cmd);
93                 return -1;
94         }
95
96         if (ast_strlen_zero(args.field_name)) {
97                 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
98                 return -1;
99         }
100
101         pjsip_sorcery = ast_sip_get_sorcery();
102         if (!pjsip_sorcery) {
103                 ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
104                 return -1;
105         }
106
107         aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", args.aor_name);
108         if (!aor_obj) {
109                 ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s'\n", args.aor_name);
110                 return -1;
111         }
112
113         if (!strcmp(args.field_name, "contact")) {
114                 /* The multiple fields handler for contact does not provide a list of contact object names, which is what we want, so we
115                  * handle contact specifically to provide this.
116                  */
117                 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
118                 struct ao2_iterator i;
119                 struct ast_sip_contact *contact;
120                 int first = 1;
121
122                 contacts = ast_sip_location_retrieve_aor_contacts(aor_obj);
123                 if (!contacts) {
124                         ast_log(LOG_WARNING, "Failed to retrieve contacts for AOR '%s'\n", args.aor_name);
125                         return -1;
126                 }
127
128                 i = ao2_iterator_init(contacts, 0);
129                 while ((contact = ao2_iterator_next(&i))) {
130                         if (!first) {
131                                 ast_str_append(buf, len, "%s", ",");
132                         }
133
134                         ast_str_append(buf, len, "%s", ast_sorcery_object_get_id(contact));
135                         first = 0;
136
137                         ao2_ref(contact, -1);
138                 }
139                 ao2_iterator_destroy(&i);
140         } else {
141                 struct ast_variable *change_set;
142                 struct ast_variable *it_change_set;
143
144                 change_set = ast_sorcery_objectset_create(pjsip_sorcery, aor_obj);
145                 if (!change_set) {
146                         ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s': change set is NULL\n", args.aor_name);
147                         return -1;
148                 }
149
150                 for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
151                         if (!strcmp(it_change_set->name, args.field_name)) {
152                                 ast_str_set(buf, len, "%s", it_change_set->value);
153                                 break;
154                         }
155                 }
156
157                 if (!it_change_set) {
158                         ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP AOR\n", args.field_name);
159                         res = 1;
160                 }
161
162                 ast_variables_destroy(change_set);
163         }
164
165         return res;
166 }
167
168
169 static struct ast_custom_function pjsip_aor_function = {
170         .name = "PJSIP_AOR",
171         .read2 = pjsip_aor_function_read,
172 };
173
174 static int unload_module(void)
175 {
176         return ast_custom_function_unregister(&pjsip_aor_function);
177 }
178
179 static int load_module(void)
180 {
181         return ast_custom_function_register(&pjsip_aor_function);
182 }
183
184 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP AOR");