Beispiel #1
0
/*!
 * \internal
 * \brief Gets the number of messages that exist for the mailbox list.
 * \since 12.1.0
 *
 * \param mailboxes Comma or space delimited list of mailboxes.
 * \param newmsgs Where to put the count of new messages. (Can be NULL)
 * \param oldmsgs Where to put the count of old messages. (Can be NULL)
 *
 * \details
 * Simultaneously determines the count of new and old
 * messages.  The total messages would then be the sum of these.
 *
 * \retval 0 on success
 * \retval -1 on failure
 */
static int mwi_inboxcount(const char *mailboxes, int *newmsgs, int *oldmsgs)
{
	char *parse;
	char *mailbox_id;

	if (!newmsgs && !oldmsgs) {
		/* Nowhere to accumulate counts */
		return 0;
	}

	/* For each mailbox in the list. */
	parse = ast_strdupa(mailboxes);
	while ((mailbox_id = strsep(&parse, ", "))) {
		const struct ast_mwi_mailbox_object *mailbox;

		/* Get the specified mailbox. */
		mailbox = ast_mwi_mailbox_get(mailbox_id);
		if (!mailbox) {
			continue;
		}

		/* Accumulate the counts. */
		if (newmsgs) {
			*newmsgs += mailbox->msgs_new;
		}
		if (oldmsgs) {
			*oldmsgs += mailbox->msgs_old;
		}

		ast_mwi_mailbox_unref(mailbox);
	}

	return 0;
}
Beispiel #2
0
/*!
 * \internal
 * \brief Gets the number of messages that exist in a mailbox folder.
 * \since 12.1.0
 *
 * \param mailbox_id The mailbox name.
 * \param folder The folder to look in.  Default is INBOX if not provided.
 *
 * \return The number of messages in the mailbox folder (zero or more).
 */
static int mwi_messagecount(const char *mailbox_id, const char *folder)
{
	const struct ast_mwi_mailbox_object *mailbox;
	int num_msgs;
	enum folder_map which_folder;

	which_folder = mwi_folder_map(folder);
	if (which_folder == FOLDER_INVALID) {
		return 0;
	}

	mailbox = ast_mwi_mailbox_get(mailbox_id);
	if (!mailbox) {
		return 0;
	}
	num_msgs = 0;
	switch (which_folder) {
	case FOLDER_INVALID:
		break;
	case FOLDER_INBOX:
		num_msgs = mailbox->msgs_new;
		break;
	case FOLDER_OLD:
		num_msgs = mailbox->msgs_old;
		break;
	}
	ast_mwi_mailbox_unref(mailbox);

	return num_msgs;
}
Beispiel #3
0
int ast_mwi_mailbox_delete(const char *mailbox_id)
{
	const struct ast_mwi_mailbox_object *mailbox;

	if (ast_strlen_zero(mailbox_id)) {
		return -1;
	}

	mailbox = ast_mwi_mailbox_get(mailbox_id);
	if (mailbox) {
		mwi_mailbox_delete((struct ast_mwi_mailbox_object *) mailbox);
		ast_mwi_mailbox_unref(mailbox);
	}
	return 0;
}
Beispiel #4
0
/*!
 * \internal
 * \brief Determines if the given folder has messages.
 * \since 12.1.0
 *
 * \param mailboxes Comma or & delimited list of mailboxes.
 * \param folder The folder to look in.  Default is INBOX if not provided.
 *
 * \retval 1 if the folder has one or more messages.
 * \retval 0 otherwise.
 */
static int mwi_has_voicemail(const char *mailboxes, const char *folder)
{
	char *parse;
	char *mailbox_id;
	enum folder_map which_folder;

	which_folder = mwi_folder_map(folder);
	if (which_folder == FOLDER_INVALID) {
		return 0;
	}

	/* For each mailbox in the list. */
	parse = ast_strdupa(mailboxes);
	while ((mailbox_id = strsep(&parse, ",&"))) {
		const struct ast_mwi_mailbox_object *mailbox;
		int num_msgs;

		/* Get the specified mailbox. */
		mailbox = ast_mwi_mailbox_get(mailbox_id);
		if (!mailbox) {
			continue;
		}

		/* Done if the found mailbox has any messages. */
		num_msgs = 0;
		switch (which_folder) {
		case FOLDER_INVALID:
			break;
		case FOLDER_INBOX:
			num_msgs = mailbox->msgs_new;
			break;
		case FOLDER_OLD:
			num_msgs = mailbox->msgs_old;
			break;
		}
		ast_mwi_mailbox_unref(mailbox);
		if (num_msgs) {
			return 1;
		}
	}

	return 0;
}
Beispiel #5
0
static char *handle_mwi_show_mailbox(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	const struct ast_mwi_mailbox_object *mailbox;
	const char *mailbox_id;

	switch (cmd) {
	case CLI_INIT:
		e->command = "mwi show mailbox";
		e->usage =
			"Usage: mwi show mailbox <mailbox_id>\n"
			"       Show a specific external MWI mailbox.\n";
		return NULL;
	case CLI_GENERATE:
		if (a->pos == 3) {
			return complete_mailbox(a->word, a->n);
		}
		return NULL;
	}

	if (a->argc != 4) {
		return CLI_SHOWUSAGE;
	}
	mailbox_id = a->argv[3];

	mailbox = ast_mwi_mailbox_get(mailbox_id);
	if (mailbox) {
		ast_cli(a->fd,
			"Mailbox: %s\n"
			"NewMessages: %u\n"
			"OldMessages: %u\n",
			ast_sorcery_object_get_id(mailbox),
			mailbox->msgs_new,
			mailbox->msgs_old);

		ast_mwi_mailbox_unref(mailbox);
	} else {
		ast_cli(a->fd, "External MWI mailbox '%s' not found.\n", mailbox_id);
	}

	return CLI_SUCCESS;
}
enum stasis_mailbox_result stasis_app_mailbox_to_json(
	const char *name, struct ast_json **json)
{
	struct ast_json *mailbox_json;
	const struct ast_mwi_mailbox_object *mailbox;

	mailbox = ast_mwi_mailbox_get(name);
	if (!mailbox) {
		return STASIS_MAILBOX_MISSING;
	}

	mailbox_json = mailbox_to_json(mailbox);
	if (!mailbox_json) {
		ast_mwi_mailbox_unref(mailbox);
		return STASIS_MAILBOX_ERROR;
	}

	*json = mailbox_json;

	return STASIS_MAILBOX_OK;
}
enum stasis_mailbox_result stasis_app_mailbox_delete(
	const char *name)
{
	const struct ast_mwi_mailbox_object *mailbox;

	/* Make sure the mailbox actually exists before we delete it */
	mailbox = ast_mwi_mailbox_get(name);
	if (!mailbox) {
		return STASIS_MAILBOX_MISSING;
	}

	ast_mwi_mailbox_unref(mailbox);
	mailbox = NULL;

	/* Now delete the mailbox */
	if (ast_mwi_mailbox_delete(name)) {
		return STASIS_MAILBOX_ERROR;
	}

	return STASIS_MAILBOX_OK;
}
/*!
 * \internal
 * \brief Get the requested mailboxes.
 * \since 12.1.0
 *
 * \param s AMI session.
 * \param m AMI message.
 *
 * \retval 0 to keep AMI connection.
 * \retval -1 to disconnect AMI connection.
 */
static int mwi_mailbox_get(struct mansession *s, const struct message *m)
{
	char id_text[256];
	const char *id;
	const char *mailbox_id = astman_get_header(m, "Mailbox");
	const struct ast_mwi_mailbox_object *mailbox;
	struct ao2_container *mailboxes;
	unsigned count;
	struct ao2_iterator iter;

	if (ast_strlen_zero(mailbox_id)) {
		astman_send_error(s, m, "Missing mailbox parameter in request");
		return 0;
	}

	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) {
			mailbox = ast_mwi_mailbox_get(mailbox_id);
			if (mailbox) {
				if (!ao2_link(mailboxes, (void *) mailbox)) {
					ao2_ref(mailboxes, -1);
					mailboxes = NULL;
				}
				ast_mwi_mailbox_unref(mailbox);
			}
		}
	}
	if (!mailboxes) {
		astman_send_error(s, m, "Mailbox container creation failure");
		return 0;
	}

	astman_send_listack(s, m, "Mailboxes will follow", "start");

	id = astman_get_header(m, "ActionID");
	if (!ast_strlen_zero(id)) {
		snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
	} else {
		id_text[0] = '\0';
	}

	/* Output mailbox list. */
	count = 0;
	iter = ao2_iterator_init(mailboxes, AO2_ITERATOR_UNLINK);
	for (; (mailbox = ao2_iterator_next(&iter)); ast_mwi_mailbox_unref(mailbox)) {
		++count;
		astman_append(s,
			"Event: MWIGet\r\n"
			"Mailbox: %s\r\n"
			"OldMessages: %u\r\n"
			"NewMessages: %u\r\n"
			"%s"
			"\r\n",
			ast_mwi_mailbox_get_id(mailbox),
			ast_mwi_mailbox_get_msgs_old(mailbox),
			ast_mwi_mailbox_get_msgs_new(mailbox),
			id_text);
	}
	ao2_iterator_destroy(&iter);
	ao2_ref(mailboxes, -1);

	astman_send_list_complete_start(s, m, "MWIGetComplete", count);
	astman_send_list_complete_end(s);

	return 0;
}