0f5a204b883f868bc0edcec35079457216f8f4c2
[asterisk/asterisk.git] / funcs / func_pjsip_contact.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 contact
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 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/sorcery.h"
46 #include "asterisk/res_pjsip.h"
47
48 /*** DOCUMENTATION
49         <function name="PJSIP_CONTACT" language="en_US">
50                 <synopsis>
51                         Get information about a PJSIP contact
52                 </synopsis>
53                 <syntax>
54                         <parameter name="name" required="true">
55                                 <para>The name of the contact to query.</para>
56                         </parameter>
57                         <parameter name="field" required="true">
58                                 <para>The configuration option for the contact to query for.
59                                 Supported options are those fields on the
60                                 <replaceable>contact</replaceable> object.</para>
61                                 <enumlist>
62                                         <configOptionToEnum>
63                                                 <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='contact']/configOption)"/>
64                                         </configOptionToEnum>
65                                 </enumlist>
66                         </parameter>
67                 </syntax>
68         </function>
69 ***/
70
71 static int contact_function_get_permanent(void *obj, void *arg, int flags)
72 {
73         const char *id = arg;
74
75         if (!strcmp(ast_sorcery_object_get_id(obj), id)) {
76                 return CMP_MATCH | CMP_STOP;
77         }
78
79         return 0;
80 }
81
82 static int pjsip_contact_function_read(struct ast_channel *chan,
83         const char *cmd, char *data, struct ast_str **buf, ssize_t len)
84 {
85         struct ast_sorcery *pjsip_sorcery;
86         char *parsed_data = ast_strdupa(data);
87         char *contact_name;
88         RAII_VAR(struct ast_sip_contact *, contact_obj, NULL, ao2_cleanup);
89         RAII_VAR(struct ast_sip_contact_status *, contact_status, NULL, ao2_cleanup);
90         int res = 0;
91
92         AST_DECLARE_APP_ARGS(args,
93                 AST_APP_ARG(contact_name);
94                 AST_APP_ARG(field_name);
95         );
96
97         /* Check for zero arguments */
98         if (ast_strlen_zero(parsed_data)) {
99                 ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
100                 return -1;
101         }
102
103         AST_STANDARD_APP_ARGS(args, parsed_data);
104
105         if (ast_strlen_zero(args.contact_name)) {
106                 ast_log(AST_LOG_ERROR, "Cannot call %s without a contact name to query\n", cmd);
107                 return -1;
108         }
109
110         if (ast_strlen_zero(args.field_name)) {
111                 ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
112                 return -1;
113         }
114
115         pjsip_sorcery = ast_sip_get_sorcery();
116         if (!pjsip_sorcery) {
117                 ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
118                 return -1;
119         }
120
121         /* Determine if this is a permanent contact or a normal contact */
122         if ((contact_name = strstr(args.contact_name, "@@"))) {
123                 size_t aor_name_len = contact_name - args.contact_name;
124                 char aor_name[aor_name_len + 1];
125                 RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
126
127                 /* Grab only the AOR name so we can retrieve the AOR which will give us the contact */
128                 strncpy(aor_name, args.contact_name, aor_name_len);
129                 aor_name[aor_name_len] = '\0';
130
131                 aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", aor_name);
132                 if (!aor_obj) {
133                         ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
134                         return -1;
135                 }
136
137                 contact_obj = ao2_callback(aor_obj->permanent_contacts, 0, contact_function_get_permanent, args.contact_name);
138         } else {
139                 contact_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "contact", args.contact_name);
140         }
141
142         if (!contact_obj) {
143                 ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s'\n", args.contact_name);
144                 return -1;
145         }
146
147         contact_status = ast_sorcery_retrieve_by_id(pjsip_sorcery, CONTACT_STATUS, ast_sorcery_object_get_id(contact_obj));
148
149         if (!strcmp(args.field_name, "status")) {
150                 if (!contact_status) {
151                         ast_str_set(buf, len, "%s", "Unknown");
152                 } else if (contact_status->status == UNAVAILABLE) {
153                         ast_str_set(buf, len, "%s", "Unreachable");
154                 } else if (contact_status->status == AVAILABLE) {
155                         ast_str_set(buf, len, "%s", "Reachable");
156                 }
157         } else if (!strcmp(args.field_name, "rtt")) {
158                 if (!contact_status) {
159                         ast_str_set(buf, len, "%s", "N/A");
160                 } else {
161                         ast_str_set(buf, len, "%" PRId64, contact_status->rtt);
162                 }
163         } else {
164                 struct ast_variable *change_set;
165                 struct ast_variable *it_change_set;
166
167                 change_set = ast_sorcery_objectset_create(pjsip_sorcery, contact_obj);
168
169                 if (!change_set) {
170                         ast_log(AST_LOG_WARNING, "Failed to retrieve information for contact '%s': change set is NULL\n", args.contact_name);
171                         return -1;
172                 }
173
174                 for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
175                         if (!strcmp(it_change_set->name, args.field_name)) {
176                                 ast_str_set(buf, len, "%s", it_change_set->value);
177                                 break;
178                         }
179                 }
180
181                 if (!it_change_set) {
182                         ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP contact\n", args.field_name);
183
184                         res = 1;
185                 }
186
187                 ast_variables_destroy(change_set);
188         }
189
190         return res;
191 }
192
193
194 static struct ast_custom_function pjsip_contact_function = {
195         .name = "PJSIP_CONTACT",
196         .read2 = pjsip_contact_function_read,
197 };
198
199 static int unload_module(void)
200 {
201         return ast_custom_function_unregister(&pjsip_contact_function);
202 }
203
204 static int load_module(void)
205 {
206         return ast_custom_function_register(&pjsip_contact_function);
207 }
208
209 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP contact");