func_pjsip_aor.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2015, Digium, Inc.
00005  *
00006  * Joshua Colp <jcolp@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Get information about a PJSIP AOR
00022  *
00023  * \author \verbatim Joshua Colp <jcolp@digium.com> \endverbatim
00024  *
00025  * \ingroup functions
00026  *
00027  */
00028 
00029 /*** MODULEINFO
00030    <support_level>core</support_level>
00031    <depend>pjproject</depend>
00032    <depend>res_pjsip</depend>
00033  ***/
00034 
00035 #include "asterisk.h"
00036 
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 430180 $")
00038 
00039 #include <pjsip.h>
00040 #include <pjlib.h>
00041 
00042 #include "asterisk/app.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/module.h"
00045 #include "asterisk/sorcery.h"
00046 #include "asterisk/res_pjsip.h"
00047 
00048 /*** DOCUMENTATION
00049    <function name="PJSIP_AOR" language="en_US">
00050       <synopsis>
00051          Get information about a PJSIP AOR
00052       </synopsis>
00053       <syntax>
00054          <parameter name="name" required="true">
00055             <para>The name of the AOR to query.</para>
00056          </parameter>
00057          <parameter name="field" required="true">
00058             <para>The configuration option for the AOR to query for.
00059             Supported options are those fields on the
00060             <replaceable>aor</replaceable> object in
00061             <filename>pjsip.conf</filename>.</para>
00062             <enumlist>
00063                <configOptionToEnum>
00064                   <xi:include xpointer="xpointer(/docs/configInfo[@name='res_pjsip']/configFile[@name='pjsip.conf']/configObject[@name='aor']/configOption)"/>
00065                </configOptionToEnum>
00066             </enumlist>
00067          </parameter>
00068       </syntax>
00069    </function>
00070 ***/
00071 
00072 static int pjsip_aor_function_read(struct ast_channel *chan,
00073    const char *cmd, char *data, struct ast_str **buf, ssize_t len)
00074 {
00075    struct ast_sorcery *pjsip_sorcery;
00076    char *parsed_data = ast_strdupa(data);
00077    RAII_VAR(struct ast_sip_aor *, aor_obj, NULL, ao2_cleanup);
00078    int res = 0;
00079 
00080    AST_DECLARE_APP_ARGS(args,
00081       AST_APP_ARG(aor_name);
00082       AST_APP_ARG(field_name);
00083    );
00084 
00085    /* Check for zero arguments */
00086    if (ast_strlen_zero(parsed_data)) {
00087       ast_log(AST_LOG_ERROR, "Cannot call %s without arguments\n", cmd);
00088       return -1;
00089    }
00090 
00091    AST_STANDARD_APP_ARGS(args, parsed_data);
00092 
00093    if (ast_strlen_zero(args.aor_name)) {
00094       ast_log(AST_LOG_ERROR, "Cannot call %s without an AOR name to query\n", cmd);
00095       return -1;
00096    }
00097 
00098    if (ast_strlen_zero(args.field_name)) {
00099       ast_log(AST_LOG_ERROR, "Cannot call %s with an empty field name to query\n", cmd);
00100       return -1;
00101    }
00102 
00103    pjsip_sorcery = ast_sip_get_sorcery();
00104    if (!pjsip_sorcery) {
00105       ast_log(AST_LOG_ERROR, "Unable to retrieve PJSIP configuration: sorcery object is NULL\n");
00106       return -1;
00107    }
00108 
00109    aor_obj = ast_sorcery_retrieve_by_id(pjsip_sorcery, "aor", args.aor_name);
00110    if (!aor_obj) {
00111       ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s'\n", args.aor_name);
00112       return -1;
00113    }
00114 
00115    if (!strcmp(args.field_name, "contact")) {
00116       /* The multiple fields handler for contact does not provide a list of contact object names, which is what we want, so we
00117        * handle contact specifically to provide this.
00118        */
00119       RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
00120       struct ao2_iterator i;
00121       struct ast_sip_contact *contact;
00122       int first = 1;
00123 
00124       contacts = ast_sip_location_retrieve_aor_contacts(aor_obj);
00125       if (!contacts) {
00126          ast_log(LOG_WARNING, "Failed to retrieve contacts for AOR '%s'\n", args.aor_name);
00127          return -1;
00128       }
00129 
00130       i = ao2_iterator_init(contacts, 0);
00131       while ((contact = ao2_iterator_next(&i))) {
00132          if (!first) {
00133             ast_str_append(buf, len, "%s", ",");
00134          }
00135 
00136          ast_str_append(buf, len, "%s", ast_sorcery_object_get_id(contact));
00137          first = 0;
00138       }
00139       ao2_iterator_destroy(&i);
00140    } else {
00141       struct ast_variable *change_set;
00142       struct ast_variable *it_change_set;
00143 
00144       change_set = ast_sorcery_objectset_create(pjsip_sorcery, aor_obj);
00145       if (!change_set) {
00146          ast_log(AST_LOG_WARNING, "Failed to retrieve information for AOR '%s': change set is NULL\n", args.aor_name);
00147          return -1;
00148       }
00149 
00150       for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
00151          if (!strcmp(it_change_set->name, args.field_name)) {
00152             ast_str_set(buf, len, "%s", it_change_set->value);
00153             break;
00154          }
00155       }
00156 
00157       if (!it_change_set) {
00158          ast_log(AST_LOG_WARNING, "Unknown property '%s' for PJSIP AOR\n", args.field_name);
00159          res = 1;
00160       }
00161 
00162       ast_variables_destroy(change_set);
00163    }
00164 
00165    return res;
00166 }
00167 
00168 
00169 static struct ast_custom_function pjsip_aor_function = {
00170    .name = "PJSIP_AOR",
00171    .read2 = pjsip_aor_function_read,
00172 };
00173 
00174 static int unload_module(void)
00175 {
00176    return ast_custom_function_unregister(&pjsip_aor_function);
00177 }
00178 
00179 static int load_module(void)
00180 {
00181    return ast_custom_function_register(&pjsip_aor_function);
00182 }
00183 
00184 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Get information about a PJSIP AOR");

Generated on Thu Apr 16 06:27:36 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6