[asterisk-commits] jpeeler: branch jpeeler/manager-configactions r101369 - in /team/jpeeler/mana...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Jan 30 18:30:53 CST 2008
Author: jpeeler
Date: Wed Jan 30 18:30:52 2008
New Revision: 101369
URL: http://svn.digium.com/view/asterisk?view=rev&rev=101369
Log:
AMI improvments: ListContexts and GetConfig with parameter context
Modified:
team/jpeeler/manager-configactions/include/asterisk/config.h
team/jpeeler/manager-configactions/main/config.c
team/jpeeler/manager-configactions/main/manager.c
Modified: team/jpeeler/manager-configactions/include/asterisk/config.h
URL: http://svn.digium.com/view/asterisk/team/jpeeler/manager-configactions/include/asterisk/config.h?view=diff&rev=101369&r1=101368&r2=101369
==============================================================================
--- team/jpeeler/manager-configactions/include/asterisk/config.h (original)
+++ team/jpeeler/manager-configactions/include/asterisk/config.h Wed Jan 30 18:30:52 2008
@@ -255,6 +255,7 @@
struct ast_category *ast_category_new(const char *name, const char *in_file, int lineno);
void ast_category_append(struct ast_config *config, struct ast_category *cat);
int ast_category_delete(struct ast_config *cfg, const char *category);
+int ast_category_empty(struct ast_config *cfg, const char *category);
void ast_category_destroy(struct ast_category *cat);
struct ast_variable *ast_category_detach_variables(struct ast_category *cat);
void ast_category_rename(struct ast_category *cat, const char *name);
Modified: team/jpeeler/manager-configactions/main/config.c
URL: http://svn.digium.com/view/asterisk/team/jpeeler/manager-configactions/main/config.c?view=diff&rev=101369&r1=101368&r2=101369
==============================================================================
--- team/jpeeler/manager-configactions/main/config.c (original)
+++ team/jpeeler/manager-configactions/main/config.c Wed Jan 30 18:30:52 2008
@@ -757,6 +757,26 @@
prev = cat;
cat = cat->next;
}
+ return -1;
+}
+
+int ast_category_empty(struct ast_config *cfg, const char *category)
+{
+ struct ast_category *cat;
+
+ cat = cfg->root;
+ while (cat) {
+ if (cat->name == category) {
+ return 0;
+ }
+
+ cat = cfg->root;
+ if (!strcasecmp(cat->name, category)) {
+ return 0;
+ }
+ cat = cat->next;
+ }
+
return -1;
}
Modified: team/jpeeler/manager-configactions/main/manager.c
URL: http://svn.digium.com/view/asterisk/team/jpeeler/manager-configactions/main/manager.c?view=diff&rev=101369&r1=101368&r2=101369
==============================================================================
--- team/jpeeler/manager-configactions/main/manager.c (original)
+++ team/jpeeler/manager-configactions/main/manager.c Wed Jan 30 18:30:52 2008
@@ -51,6 +51,8 @@
#include <sys/time.h>
#include <signal.h>
#include <sys/mman.h>
+#include <stdio.h>
+#include <unistd.h>
#include "asterisk/channel.h"
#include "asterisk/file.h"
@@ -1053,14 +1055,16 @@
static char mandescr_getconfig[] =
"Description: A 'GetConfig' action will dump the contents of a configuration\n"
-"file by category and contents.\n"
-"Variables:\n"
-" Filename: Configuration filename (e.g. foo.conf)\n";
+"file by category and contents or optionally by specified context only.\n"
+"Variables: (Names marked with * are required)\n"
+" *Filename: Configuration filename (e.g. foo.conf)\n"
+" Context: Context in configuration file\n";
static int action_getconfig(struct mansession *s, const struct message *m)
{
struct ast_config *cfg;
const char *fn = astman_get_header(m, "Filename");
+ const char *context = astman_get_header(m, "Context");
int catcount = 0;
int lineno = 0;
char *category=NULL;
@@ -1075,19 +1079,62 @@
astman_send_error(s, m, "Config file not found");
return 0;
}
+
astman_start_ack(s, m);
while ((category = ast_category_browse(cfg, category))) {
- lineno = 0;
- astman_append(s, "Category-%06d: %s\r\n", catcount, category);
- for (v = ast_variable_browse(cfg, category); v; v = v->next)
- astman_append(s, "Line-%06d-%06d: %s=%s\r\n", catcount, lineno++, v->name, v->value);
- catcount++;
- }
+ if (ast_strlen_zero(context) || (!ast_strlen_zero(context) && !strcmp(context, category))) {
+ lineno = 0;
+ astman_append(s, "Category-%06d: %s\r\n", catcount, category);
+ for (v = ast_variable_browse(cfg, category); v; v = v->next)
+ astman_append(s, "Line-%06d-%06d: %s=%s\r\n", catcount, lineno++, v->name, v->value);
+ catcount++;
+ }
+ }
+ if (!ast_strlen_zero(context) && catcount == 0) /* TODO: actually, a config with no categories doesn't even get loaded */
+ astman_append(s, "No contexts found");
ast_config_destroy(cfg);
astman_append(s, "\r\n");
return 0;
}
+
+static char mandescr_listcontexts[] =
+"Description: A 'ListContexts' action will dump the contents of a context\n"
+"in a given file.\n"
+"Variables:\n"
+" Filename: Configuration filename (e.g. foo.conf)\n";
+
+static int action_listcontexts(struct mansession *s, const struct message *m)
+{
+ struct ast_config *cfg;
+ const char *fn = astman_get_header(m, "Filename");
+ char *category=NULL;
+ struct ast_flags config_flags = { CONFIG_FLAG_WITHCOMMENTS | CONFIG_FLAG_NOCACHE };
+ int catcount = 0;
+
+ if (ast_strlen_zero(fn)) {
+ astman_send_error(s, m, "Filename not specified");
+ return 0;
+ }
+ if (!(cfg = ast_config_load(fn, config_flags))) {
+ astman_send_error(s, m, "Config file not found");
+ return 0;
+ }
+ astman_start_ack(s, m);
+ while ((category = ast_category_browse(cfg, category))) {
+ astman_append(s, "Category-%06d: %s\r\n", catcount, category);
+ catcount++;
+ }
+ if (catcount == 0) /* TODO: actually, a config with no categories doesn't even get loaded */
+ astman_append(s, "Error: no contexts found");
+ ast_config_destroy(cfg);
+ astman_append(s, "\r\n");
+
+ return 0;
+}
+
+
+
/*! The amount of space in out must be at least ( 2 * strlen(in) + 1 ) */
static void json_escape(char *out, const char *in)
@@ -1214,6 +1261,9 @@
} else if (!strcasecmp(action, "delcat")) {
if (!ast_strlen_zero(cat))
ast_category_delete(cfg, cat);
+ } else if (!strcasecmp(action, "emptycat")) {
+ if (!ast_strlen_zero(cat))
+ ast_category_empty(cfg, cat);
} else if (!strcasecmp(action, "update")) {
if (!ast_strlen_zero(cat) && !ast_strlen_zero(var) && (category = ast_category_get(cfg, cat)))
ast_variable_update(category, var, value, match, object);
@@ -1239,7 +1289,7 @@
" SrcFilename: Configuration filename to read(e.g. foo.conf)\n"
" DstFilename: Configuration filename to write(e.g. foo.conf)\n"
" Reload: Whether or not a reload should take place (or name of specific module)\n"
-" Action-XXXXXX: Action to Take (NewCat,RenameCat,DelCat,Update,Delete,Append)\n"
+" Action-XXXXXX: Action to Take (NewCat,RenameCat,DelCat,EmptyCat,Update,Delete,Append)\n"
" Cat-XXXXXX: Category to operate on\n"
" Var-XXXXXX: Variable to work on\n"
" Value-XXXXXX: Value to work on\n"
@@ -1258,10 +1308,11 @@
astman_send_error(s, m, "Filename not specified");
return 0;
}
- if (!(cfg = ast_config_load(sfn, config_flags))) {
- astman_send_error(s, m, "Config file not found");
- return 0;
- }
+ if (access(sfn, F_OK)) {
+ FILE *fp = fopen(sfn, "w");
+ fclose(fp);
+ }
+ cfg = ast_config_load(sfn, config_flags);
handle_updates(s, m, cfg, dfn);
ast_include_rename(cfg, sfn, dfn); /* change the include references from dfn to sfn, so things match up */
res = config_text_file_save(dfn, cfg, "Manager");
@@ -3475,6 +3526,7 @@
ast_manager_register2("GetConfig", EVENT_FLAG_SYSTEM | EVENT_FLAG_CONFIG, action_getconfig, "Retrieve configuration", mandescr_getconfig);
ast_manager_register2("GetConfigJSON", EVENT_FLAG_SYSTEM | EVENT_FLAG_CONFIG, action_getconfigjson, "Retrieve configuration (JSON format)", mandescr_getconfigjson);
ast_manager_register2("UpdateConfig", EVENT_FLAG_CONFIG, action_updateconfig, "Update basic configuration", mandescr_updateconfig);
+ ast_manager_register2("ListContexts", EVENT_FLAG_CONFIG, action_listcontexts, "List contexts in configuration file", mandescr_listcontexts);
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);
ast_manager_register2("Command", EVENT_FLAG_COMMAND, action_command, "Execute Asterisk CLI Command", mandescr_command );
More information about the asterisk-commits
mailing list