[asterisk-commits] rmudgett: branch rmudgett/external_mwi r403433 - in /team/rmudgett/external_m...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Dec 6 19:59:12 CST 2013


Author: rmudgett
Date: Fri Dec  6 19:59:10 2013
New Revision: 403433

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=403433
Log:
* Replaced operations working by mailbox context with regex operations.

* Changed external MWI AMI commands to optionally take regular
expressions.

* Changed external MWI CLI commands to use regular expressions.

* Completed external MWI CLI command tab completion.

Modified:
    team/rmudgett/external_mwi/include/asterisk/res_mwi_external.h
    team/rmudgett/external_mwi/res/res_mwi_external.c
    team/rmudgett/external_mwi/res/res_mwi_external_ami.c

Modified: team/rmudgett/external_mwi/include/asterisk/res_mwi_external.h
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/external_mwi/include/asterisk/res_mwi_external.h?view=diff&rev=403433&r1=403432&r2=403433
==============================================================================
--- team/rmudgett/external_mwi/include/asterisk/res_mwi_external.h (original)
+++ team/rmudgett/external_mwi/include/asterisk/res_mwi_external.h Fri Dec  6 19:59:10 2013
@@ -168,19 +168,29 @@
 int ast_mwi_mailbox_update(struct ast_mwi_mailbox_object *mailbox);
 
 /*!
- * \brief Delete external MWI object(s).
+ * \brief Delete matching external MWI object.
  * \since 13.0.0
  *
  * \param user Part of user at context.
  * \param context Part of user at context.  Defaults to 'default' if empty.
  *
- * \note If user is empty then deletes all mailboxes in the
- * specified context.
- *
  * \retval 0 on success.
  * \retval -1 on error.
  */
 int ast_mwi_mailbox_delete(const char *user, const char *context);
+
+/*!
+ * \brief Delete all external MWI objects selected by the regular expression.
+ * \since 13.0.0
+ *
+ * \param regex Regular expression in extended syntax.  (NULL is same as "")
+ *
+ * \note The provided regex is treated as extended case sensitive.
+ *
+ * \retval 0 on success.
+ * \retval -1 on error.
+ */
+int ast_mwi_mailbox_delete_by_regex(const char *regex);
 
 /*!
  * \brief Delete all external MWI objects.
@@ -206,17 +216,19 @@
 const struct ast_mwi_mailbox_object *ast_mwi_mailbox_get(const char *user, const char *context);
 
 /*!
- * \brief Get all external MWI objects in the specified context.
- * \since 13.0.0
- *
- * \param context Part of user at context.  Defaults to 'default' if empty.
+ * \brief Get all external MWI objects selected by the regular expression.
+ * \since 13.0.0
+ *
+ * \param regex Regular expression in extended syntax.  (NULL is same as "")
+ *
+ * \note The provided regex is treated as extended case sensitive.
  *
  * \retval container of struct ast_mwi_mailbox_object on success.
  * \retval NULL on error.
  *
  * \note The objects in the container must be treated as read-only.
  */
-struct ao2_container *ast_mwi_mailbox_get_by_context(const char *context);
+struct ao2_container *ast_mwi_mailbox_get_by_regex(const char *regex);
 
 /*!
  * \brief Get all external MWI objects.

Modified: team/rmudgett/external_mwi/res/res_mwi_external.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/external_mwi/res/res_mwi_external.c?view=diff&rev=403433&r1=403432&r2=403433
==============================================================================
--- team/rmudgett/external_mwi/res/res_mwi_external.c (original)
+++ team/rmudgett/external_mwi/res/res_mwi_external.c Fri Dec  6 19:59:10 2013
@@ -200,18 +200,9 @@
 		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
 }
 
-struct ao2_container *ast_mwi_mailbox_get_by_context(const char *context)
-{
-	char *regex;
-
-	if (ast_strlen_zero(context)) {
-		context = MWI_MAILBOX_CONTEXT_DEFAULT;
-	}
-
-	regex = ast_alloca(5 + strlen(context));
-	sprintf(regex, ".*@%s$", context);/* Safe */
-
-	return ast_sorcery_retrieve_by_regex(mwi_sorcery, MWI_MAILBOX_TYPE, regex);
+struct ao2_container *ast_mwi_mailbox_get_by_regex(const char *regex)
+{
+	return ast_sorcery_retrieve_by_regex(mwi_sorcery, MWI_MAILBOX_TYPE, regex ?: "");
 }
 
 const struct ast_mwi_mailbox_object *ast_mwi_mailbox_get(const char *user, const char *context)
@@ -351,40 +342,42 @@
 	ao2_iterator_destroy(&iter);
 }
 
-int ast_mwi_mailbox_delete(const char *user, const char *context)
+int ast_mwi_mailbox_delete_all(void)
 {
 	struct ao2_container *mailboxes;
 
-	if (!ast_strlen_zero(user)) {
-		const struct ast_mwi_mailbox_object *mailbox;
-
-		/* Delete a single mailbox. */
-		mailbox = ast_mwi_mailbox_get(user, context);
-		if (mailbox) {
-			mwi_mailbox_delete((struct ast_mwi_mailbox_object *) mailbox);
-			ast_mwi_mailbox_unref(mailbox);
-		}
-		return 0;
-	}
-
-	/* Delete all mailboxes in the context */
-	mailboxes = ast_mwi_mailbox_get_by_context(context);
-	if (mailboxes) {
-		mwi_mailbox_delete_all(mailboxes);
-		ao2_ref(mailboxes, -1);
-	}
-	return 0;
-}
-
-int ast_mwi_mailbox_delete_all(void)
-{
-	struct ao2_container *mailboxes;
-
-	/* Delete all mailboxs. */
 	mailboxes = ast_mwi_mailbox_get_all();
 	if (mailboxes) {
 		mwi_mailbox_delete_all(mailboxes);
 		ao2_ref(mailboxes, -1);
+	}
+	return 0;
+}
+
+int ast_mwi_mailbox_delete_by_regex(const char *regex)
+{
+	struct ao2_container *mailboxes;
+
+	mailboxes = ast_mwi_mailbox_get_by_regex(regex);
+	if (mailboxes) {
+		mwi_mailbox_delete_all(mailboxes);
+		ao2_ref(mailboxes, -1);
+	}
+	return 0;
+}
+
+int ast_mwi_mailbox_delete(const char *user, const char *context)
+{
+	const struct ast_mwi_mailbox_object *mailbox;
+
+	if (ast_strlen_zero(user)) {
+		return -1;
+	}
+
+	mailbox = ast_mwi_mailbox_get(user, context);
+	if (mailbox) {
+		mwi_mailbox_delete((struct ast_mwi_mailbox_object *) mailbox);
+		ast_mwi_mailbox_unref(mailbox);
 	}
 	return 0;
 }
@@ -623,6 +616,37 @@
 	.messagecount = mwi_messagecount,
 };
 
+static char *complete_mailbox(const char *word, int state)
+{
+	struct ao2_iterator iter;
+	int wordlen = strlen(word);
+	int which = 0;
+	char *ret = NULL;
+	char *regex;
+	const struct ast_mwi_mailbox_object *mailbox;
+	RAII_VAR(struct ao2_container *, mailboxes, NULL, ao2_cleanup);
+
+	regex = ast_alloca(4 + wordlen);
+	sprintf(regex, "^%s.*", word);/* Safe */
+
+	mailboxes = ast_mwi_mailbox_get_by_regex(regex);
+	if (!mailboxes) {
+		return NULL;
+	}
+
+	iter = ao2_iterator_init(mailboxes, 0);
+	for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
+		if (++which > state) {
+			ret = ast_strdup(ast_sorcery_object_get_id(mailbox));
+			ast_mwi_mailbox_unref(mailbox);
+			break;
+		}
+	}
+	ao2_iterator_destroy(&iter);
+
+	return ret;
+}
+
 static char *handle_mwi_external_delete_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	switch (cmd) {
@@ -641,29 +665,28 @@
 	return CLI_SUCCESS;
 }
 
-static char *handle_mwi_external_delete_context(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
-	const char *context;
+static char *handle_mwi_external_delete_like(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	const char *regex;
 
 	switch (cmd) {
 	case CLI_INIT:
-		e->command = "mwi external delete context";
+		e->command = "mwi external delete like";
 		e->usage =
-			"Usage: mwi external delete context <context>\n"
-			"       Delete all external MWI mailboxes in a context.\n";
+			"Usage: mwi external delete like <pattern>\n"
+			"       Delete external MWI mailboxes matching a regular expression.\n";
 		return NULL;
 	case CLI_GENERATE:
-/* BUGBUG Generate context help. */
 		return NULL;
 	}
 
 	if (a->argc != 5) {
 		return CLI_SHOWUSAGE;
 	}
-	context = a->argv[4];
-
-	ast_mwi_mailbox_delete(NULL, context);
-	ast_cli(a->fd, "Deleted all external MWI mailboxes in context '%s'.\n", context);
+	regex = a->argv[4];
+
+	ast_mwi_mailbox_delete_by_regex(regex);
+	ast_cli(a->fd, "Deleted external MWI mailboxes matching '%s'.\n", regex);
 	return CLI_SUCCESS;
 }
 
@@ -682,7 +705,9 @@
 			"       If context is not present then '" MWI_MAILBOX_CONTEXT_DEFAULT "' is used.\n";
 		return NULL;
 	case CLI_GENERATE:
-/* BUGBUG Generate mailbox help. */
+		if (a->pos == 4) {
+			return complete_mailbox(a->word, a->n);
+		}
 		return NULL;
 	}
 
@@ -778,29 +803,28 @@
 	return CLI_SUCCESS;
 }
 
-static char *handle_mwi_external_list_context(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+static char *handle_mwi_external_list_like(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	struct ao2_container *mailboxes;
-	const char *context;
+	const char *regex;
 
 	switch (cmd) {
 	case CLI_INIT:
-		e->command = "mwi external list context";
+		e->command = "mwi external list like";
 		e->usage =
-			"Usage: mwi external list context <context>\n"
-			"       List all external MWI mailboxes in a context.\n";
+			"Usage: mwi external list like <pattern>\n"
+			"       List external MWI mailboxes matching a regular expression.\n";
 		return NULL;
 	case CLI_GENERATE:
-/* BUGBUG Generate context help. */
 		return NULL;
 	}
 
 	if (a->argc != 5) {
 		return CLI_SHOWUSAGE;
 	}
-	context = a->argv[4];
-
-	mailboxes = ast_mwi_mailbox_get_by_context(context);
+	regex = a->argv[4];
+
+	mailboxes = ast_mwi_mailbox_get_by_regex(regex);
 	if (!mailboxes) {
 		ast_cli(a->fd, "Failed to retrieve external MWI mailboxes.\n");
 		return CLI_SUCCESS;
@@ -826,7 +850,9 @@
 			"       If context is not present then '" MWI_MAILBOX_CONTEXT_DEFAULT "' is used.\n";
 		return NULL;
 	case CLI_GENERATE:
-/* BUGBUG Generate mailbox help. */
+		if (a->pos == 4) {
+			return complete_mailbox(a->word, a->n);
+		}
 		return NULL;
 	}
 
@@ -881,7 +907,9 @@
 			"       If context is not present then '" MWI_MAILBOX_CONTEXT_DEFAULT "' is used.\n";
 		return NULL;
 	case CLI_GENERATE:
-/* BUGBUG Generate mailbox help. */
+		if (a->pos == 4) {
+			return complete_mailbox(a->word, a->n);
+		}
 		return NULL;
 	}
 
@@ -937,10 +965,10 @@
 
 static struct ast_cli_entry mwi_external_cli[] = {
 	AST_CLI_DEFINE(handle_mwi_external_delete_all, "Delete all external MWI mailboxes"),
-	AST_CLI_DEFINE(handle_mwi_external_delete_context, "Delete all external MWI mailboxes in a context"),
+	AST_CLI_DEFINE(handle_mwi_external_delete_like, "Delete external MWI mailboxes matching regex"),
 	AST_CLI_DEFINE(handle_mwi_external_delete_mailbox, "Delete a specific external MWI mailbox"),
 	AST_CLI_DEFINE(handle_mwi_external_list_all, "List all external MWI mailboxes"),
-	AST_CLI_DEFINE(handle_mwi_external_list_context, "List all external MWI mailboxes in a context"),
+	AST_CLI_DEFINE(handle_mwi_external_list_like, "List external MWI mailboxes matching regex"),
 	AST_CLI_DEFINE(handle_mwi_external_show_mailbox, "Show a specific external MWI mailbox"),
 	AST_CLI_DEFINE(handle_mwi_external_update_mailbox, "Update a specific external MWI mailbox"),
 };

Modified: team/rmudgett/external_mwi/res/res_mwi_external_ami.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/external_mwi/res/res_mwi_external_ami.c?view=diff&rev=403433&r1=403432&r2=403433
==============================================================================
--- team/rmudgett/external_mwi/res/res_mwi_external_ami.c (original)
+++ team/rmudgett/external_mwi/res/res_mwi_external_ami.c Fri Dec  6 19:59:10 2013
@@ -41,10 +41,11 @@
 			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
 			<parameter name="Mailbox" required="true">
 				<para>Mailbox ID in the form of
-				[<replaceable>box</replaceable>][@[<replaceable>context</replaceable>]].  The
+				<replaceable>box</replaceable>[@[<replaceable>context</replaceable>]]
+				for a specific mailbox or /<replaceable>regex</replaceable>/ for all
+				mailboxes matching the regular expression.  The
 				<replaceable>context</replaceable> defaults to <literal>default</literal> if
-				missing.  If the <replaceable>box</replaceable> is missing then all mailboxes
-				in the <replaceable>context</replaceable> are selected.</para>
+				missing.</para>
 			</parameter>
 		</syntax>
 		<description>
@@ -156,8 +157,6 @@
 	char id_text[256];
 	const char *id;
 	const char *mailbox_id = astman_get_header(m, "Mailbox");
-	char *user;
-	char *context;
 	const struct ast_mwi_mailbox_object *mailbox;
 	struct ao2_container *mailboxes;
 	unsigned count;
@@ -168,15 +167,41 @@
 		return 0;
 	}
 
-	/* Split mailbox id user at context. */
-	context = ast_strdupa(mailbox_id);
-	user = strsep(&context, "@");
-
-	if (ast_strlen_zero(user)) {
-		mailboxes = ast_mwi_mailbox_get_by_context(context);
+	if (*mailbox_id == '/') {
+		struct ast_str *regex_string;
+
+		regex_string = ast_str_create(strlen(mailbox_id) + 1);
+		if (!regex_string) {
+			astman_send_error(s, m, "Memory Allocation Failure");
+			return 0;
+		}
+
+		/* Make "/regex/" into "regex" */
+		if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
+			astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
+			ast_free(regex_string);
+			return 0;
+		}
+
+		mailboxes = ast_mwi_mailbox_get_by_regex(ast_str_buffer(regex_string));
+		ast_free(regex_string);
 	} else {
 		mailboxes = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, NULL, NULL);
 		if (mailboxes) {
+			char *user;
+			char *context;
+
+			/* Split mailbox id user at context. */
+			context = ast_strdupa(mailbox_id);
+			user = strsep(&context, "@");
+
+			if (ast_strlen_zero(user)) {
+				astman_send_error_va(s, m, "Mailbox missing box part of box at context in: %s",
+					mailbox_id);
+				ao2_ref(mailboxes, -1);
+				return 0;
+			}
+
 			mailbox = ast_mwi_mailbox_get(user, context);
 			if (mailbox) {
 				if (!ao2_link(mailboxes, (void *) mailbox)) {
@@ -244,19 +269,46 @@
 static int mwi_mailbox_delete(struct mansession *s, const struct message *m)
 {
 	const char *mailbox_id = astman_get_header(m, "Mailbox");
-	char *user;
-	char *context;
 
 	if (ast_strlen_zero(mailbox_id)) {
 		astman_send_error(s, m, "Missing mailbox parameter in request");
 		return 0;
 	}
 
-	/* Split mailbox id user at context. */
-	context = ast_strdupa(mailbox_id);
-	user = strsep(&context, "@");
-
-	ast_mwi_mailbox_delete(user, context);
+	if (*mailbox_id == '/') {
+		struct ast_str *regex_string;
+
+		regex_string = ast_str_create(strlen(mailbox_id) + 1);
+		if (!regex_string) {
+			astman_send_error(s, m, "Memory Allocation Failure");
+			return 0;
+		}
+
+		/* Make "/regex/" into "regex" */
+		if (ast_regex_string_to_regex_pattern(mailbox_id, &regex_string) != 0) {
+			astman_send_error_va(s, m, "Mailbox regex format invalid in: %s", mailbox_id);
+			ast_free(regex_string);
+			return 0;
+		}
+
+		ast_mwi_mailbox_delete_by_regex(ast_str_buffer(regex_string));
+		ast_free(regex_string);
+	} else {
+		char *user;
+		char *context;
+
+		/* Split mailbox id user at context. */
+		context = ast_strdupa(mailbox_id);
+		user = strsep(&context, "@");
+
+		if (ast_strlen_zero(user)) {
+			astman_send_error_va(s, m, "Mailbox missing box part of box at context in: %s",
+				mailbox_id);
+			return 0;
+		}
+
+		ast_mwi_mailbox_delete(user, context);
+	}
 
 	astman_send_ack(s, m, NULL);
 	return 0;
@@ -294,7 +346,7 @@
 	user = strsep(&context, "@");
 
 	if (ast_strlen_zero(user)) {
-		astman_send_error_va(s, m, "Missing box part of box at context Mailbox ID: %s",
+		astman_send_error_va(s, m, "Mailbox missing box part of box at context in: %s",
 			mailbox_id);
 		return 0;
 	}
@@ -317,7 +369,7 @@
 
 	mailbox = ast_mwi_mailbox_alloc(user, context);
 	if (!mailbox) {
-		astman_send_error(s, m, "Internal error");
+		astman_send_error(s, m, "Mailbox object creation failure");
 		return 0;
 	}
 




More information about the asterisk-commits mailing list