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