Index: main/manager.c =================================================================== --- main/manager.c (revision 89462) +++ main/manager.c (working copy) @@ -1146,6 +1146,91 @@ return 0; } +static char mandescr_getcategory[] = +"Description: A 'GetCategory' action will grab all contents of a specific category from a config file\n" +"Variables:\n" +" Filename: Configuration filename (e.g. users.conf)\n" +" Category: The specific Context to grab (e.g. trunk_1)\n" +" Note: Category is CASE-SENSITIVE!\n"; + +static int action_getcategory(struct mansession *s, const struct message *m) +{ + int lineno = 0; + int res = 0; + int hascategory = 0; + char *category=NULL; + const char *filename = astman_get_header(m, "Filename"); + const char *m_category = astman_get_header(m, "Category"); + struct ast_config *cfg; + struct ast_variable *v; + struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE }; + + if (ast_strlen_zero(filename)) { + astman_send_error(s, m, "Filename not specified"); + return res; + } + if (!(cfg = ast_config_load(filename, config_flags))) { + astman_send_error(s, m, "Config file not found"); + return res; + } + + /* Comparison for grabbing a category is case-sensitive! */ + while ((category = ast_category_browse(cfg, category))) { + /* Search for category that matches the category in the manager header, and return it. */ + if(!strcasecmp(category, m_category)) { + /* Context found. Start ack (Response: Success) but only once */ + if(!hascategory) { astman_start_ack(s, m); } + hascategory = 1; + astman_append(s, "Category: %s\r\n", category); + for (v = ast_variable_browse(cfg, category); v; v = v->next) + astman_append(s, "Line-%06d: %s=%s\r\n", lineno++, v->name, v->value); + } + } + ast_config_destroy(cfg); + if(!hascategory) { + astman_send_error(s, m, "Context Not Found"); + return res; + } + + astman_append(s, "\r\n"); + + return res; +} + +static char mandescr_listcategories[] = +"Description: A 'ListCategories' action list all categories in a given config file.\n" +"Variables:\n" +" Filename: Configuration filename (e.g. users.conf)\n"; + +/*! List all Categories in a given config file */ +static int action_listcategories(struct mansession *s, const struct message *m) +{ + int lineno = 0; + int res = 0; + char *category=NULL; + const char *filename = astman_get_header(m, "Filename"); + struct ast_config *cfg; + struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE }; + + if (ast_strlen_zero(filename)) { + astman_send_error(s, m, "Filename not specified"); + return res; + } + if (!(cfg = ast_config_load(filename, config_flags))) { + astman_send_error(s, m, "Config file not found"); + return res; + } + + /* I used to check to make sure a category was found, but if there is not one, asterisk returns config file not found */ + while ((category = ast_category_browse(cfg, category))) { + astman_append(s, "Category-%06d: %s\r\n", lineno++, category); + } + ast_config_destroy(cfg); + astman_append(s, "\r\n"); + + return res; +} + /*! The amount of space in out must be at least ( 2 * strlen(in) + 1 ) */ static void json_escape(char *out, const char *in) { @@ -3307,6 +3392,8 @@ ast_manager_register2("Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar ); ast_manager_register2("GetConfig", EVENT_FLAG_CONFIG, action_getconfig, "Retrieve configuration", mandescr_getconfig); ast_manager_register2("GetConfigJSON", EVENT_FLAG_CONFIG, action_getconfigjson, "Retrieve configuration (JSON format)", mandescr_getconfigjson); + ast_manager_register2("GetCategory", EVENT_FLAG_CONFIG, action_getcategory, "Retrieve all contents of a category from a config file", mandescr_getcategory); + ast_manager_register2("ListCategories", EVENT_FLAG_CONFIG, action_listcategories, "List all categories for a specific file", mandescr_listcategories); ast_manager_register2("UpdateConfig", EVENT_FLAG_CONFIG, action_updateconfig, "Update basic configuration", mandescr_updateconfig); ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect ); ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate);