2cbd3d87c169e04e45920cab439b13a77fc4e748
[asterisk/asterisk.git] / funcs / func_config.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008, Digium, Inc.
5  *
6  * Russell Bryant <russell@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 A function to retrieve variables from an Asterisk configuration file
22  *
23  * \author Russell Bryant <russell@digium.com>
24  * 
25  * \ingroup functions
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/module.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/pbx.h"
35 #include "asterisk/app.h"
36
37 static int config_function_read(struct ast_channel *chan, const char *cmd, char *data, 
38         char *buf, size_t len) 
39 {
40         struct ast_config *cfg;
41         struct ast_flags cfg_flags = { 0 };
42         const char *val;
43         char *parse;
44         AST_DECLARE_APP_ARGS(args,
45                 AST_APP_ARG(filename);
46                 AST_APP_ARG(category);
47                 AST_APP_ARG(variable);
48                 AST_APP_ARG(index);
49         );
50
51         if (ast_strlen_zero(data)) {
52                 ast_log(LOG_ERROR, "AST_CONFIG() requires an argument\n");
53                 return -1;
54         }
55
56         parse = ast_strdupa(data);
57         AST_STANDARD_APP_ARGS(args, parse);
58
59         if (ast_strlen_zero(args.filename)) {
60                 ast_log(LOG_ERROR, "AST_CONFIG() requires a filename\n");
61                 return -1;
62         }
63
64         if (ast_strlen_zero(args.category)) {
65                 ast_log(LOG_ERROR, "AST_CONFIG() requires a category\n");
66                 return -1;
67         }
68         
69         if (ast_strlen_zero(args.variable)) {
70                 ast_log(LOG_ERROR, "AST_CONFIG() requires a variable\n");
71                 return -1;
72         }
73
74         if (!(cfg = ast_config_load(args.filename, cfg_flags))) {
75                 return -1;
76         }
77
78         if (!(val = ast_variable_retrieve(cfg, args.category, args.variable))) {
79                 ast_log(LOG_ERROR, "'%s' not found in [%s] of '%s'\n", args.variable, 
80                         args.category, args.filename);
81                 return -1;
82         }
83
84         ast_copy_string(buf, val, len);
85
86         ast_config_destroy(cfg);
87
88         return 0;
89 }
90
91 static struct ast_custom_function config_function = {
92         .name = "AST_CONFIG",
93         .syntax = "AST_CONFIG(config_file,category,variable_name[,index])",
94         .synopsis = "Retrieve a variable from a configuration file",
95         .desc = 
96         "   This function reads a variable from an Asterisk configuration file.\n"
97         "The optional index parameter would be used in the case that a variable\n"
98         "exists more than once in a category.  The index is zero-based, so an\n"
99         "index of 0 returns the first instance of the variable.  Also, if the\n"
100         "word \"count\" in the index field, the number of instances of that\n"
101         "variable will be returned.\n"
102         "",
103         .read = config_function_read,
104 };
105
106 static int unload_module(void)
107 {
108         return ast_custom_function_unregister(&config_function);
109 }
110
111 static int load_module(void)
112 {
113         return ast_custom_function_register(&config_function);
114 }
115
116 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Asterisk configuration file variable access");