Merge "BuildSystem: Really do not pass unknown-warning options to the compiler."
[asterisk/asterisk.git] / funcs / func_pjsip_endpoint.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Matt Jordan <mjordan@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 endpoint
22  *
23  * \author \verbatim Matt Jordan <mjordan@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/channel.h"
44 #include "asterisk/sorcery.h"
45 #include "asterisk/res_pjsip.h"
46
47 /*** DOCUMENTATION
48         <function name="PJSIP_ENDPOINT" language="en_US">
49                 <synopsis>
50                         Get information about a PJSIP endpoint
51                 </synopsis>
52                 <syntax>
53                         <parameter name="name" required="true">
54                                 <para>The name of the endpoint to query.</para>
55                         </parameter>
56                         <parameter name="field" required="true">
57                                 <para>The configuration option for the endpoint to query for.
58                                 Supported options are those fields on the
59                                 <replaceable>endpoint</replaceable> object in
60                                 <filename>pjsip.conf</filename>.</para>
61                                 <enumlist>
62                                         <configOptionToEnum>
63                                                 <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='endpoint']/configOption)"/>
64                                         </configOptionToEnum>
65                                 </enumlist>
66                         </parameter>
67                 </syntax>
68         </function>
69 ***/
70
71 static int pjsip_endpoint_function_read(struct ast_channel *chan,
72         const char *cmd, char *data, struct ast_str **buf, ssize_t len)
73 {
74         struct ast_sorcery *pjsip_sorcery;
75         char *parsed_data = ast_strdupa(data);
76         RAII_VAR(void *, endpoint_obj, NULL, ao2_cleanup);
77         struct ast_variable *change_set;
78         struct ast_variable *it_change_set;
79         int res;
80
81         AST_DECLARE_APP_ARGS(args,
82                 AST_APP_ARG(endpoint_name);
83                 AST_APP_ARG(field_name);
84         );
85
86         /* Check for zero arguments */
87         if (ast_strlen_zero(parsed_data)) {
88                 ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
89                 return -1;
90         }
91
92         AST_STANDARD_APP_ARGS(args, parsed_data);
93
94         if (ast_strlen_zero(args.endpoint_name)) {
95                 ast_log(AST_LOG_ERROR, "Cannot call %s without an endpoint name to query\n", cmd);
96                 return -1;
97         }
98
99         if (ast_strlen_zero(args.field_name)) {
100                 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
101                 return -1;
102         }
103
104         pjsip_sorcery = ast_sip_get_sorcery();
105         if (!pjsip_sorcery) {
106                 ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
107                 return -1;
108         }
109
110         endpoint_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "endpoint", args.endpoint_name);
111         if (!endpoint_obj) {
112                 ast_log(AST_LOG_WARNING, "Failed to retrieve information for endpoint '%s'\n", args.endpoint_name);
113                 return -1;
114         }
115
116         change_set = ast_sorcery_objectset_create(pjsip_sorcery, endpoint_obj);
117         if (!change_set) {
118                 ast_log(AST_LOG_WARNING, "Failed to retrieve information for endpoint '%s': change set is NULL\n", args.endpoint_name);
119                 return -1;
120         }
121
122         for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
123                 if (!strcmp(it_change_set->name, args.field_name)) {
124                         if (!strcmp(it_change_set->name, "disallow")) {
125                                 ast_str_set(buf, len, "!%s", it_change_set->value);
126                         } else {
127                                 ast_str_set(buf, len, "%s", it_change_set->value);
128                         }
129                         break;
130                 }
131         }
132
133         res = it_change_set ? 0 : 1;
134         if (res) {
135                 ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP endpoint\n", args.field_name);
136         }
137
138         ast_variables_destroy(change_set);
139
140         return res;
141 }
142
143
144 static struct ast_custom_function pjsip_endpoint_function = {
145         .name = "PJSIP_ENDPOINT",
146         .read2 = pjsip_endpoint_function_read,
147 };
148
149 static int unload_module(void)
150 {
151         return ast_custom_function_unregister(&pjsip_endpoint_function);
152 }
153
154 static int load_module(void)
155 {
156         return ast_custom_function_register(&pjsip_endpoint_function);
157 }
158
159 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP endpoint");