Esempio n. 1
0
int delete_queue(const char* uuid)
{
	struct ast_json* j_tmp;
	char* tmp;
	char* sql;
	int ret;

	if(uuid == NULL) {
		// invalid parameter.
		return false;
	}

	j_tmp = ast_json_object_create();
	tmp = get_utc_timestamp();
	ast_json_object_set(j_tmp, "tm_delete", ast_json_string_create(tmp));
	ast_json_object_set(j_tmp, "in_use", ast_json_integer_create(E_DL_USE_NO));
	ast_free(tmp);

	tmp = db_get_update_str(j_tmp);
	AST_JSON_UNREF(j_tmp);
	ast_asprintf(&sql, "update queue set %s where uuid=\"%s\";", tmp, uuid);
	ast_free(tmp);

	ret = db_exec(sql);
	ast_free(sql);
	if(ret == false) {
		ast_log(LOG_WARNING, "Could not delete queue. uuid[%s]\n", uuid);
		return false;
	}

	// send notification
	send_manager_evt_out_queue_delete(uuid);

	return true;
}
Esempio n. 2
0
/*!
 * \internal
 * Shared code for all handlers
 */
static void handler(const char *name,
		    int response_code,
		    struct ast_variable *get_params,
		    struct ast_variable *path_vars,
		    struct ast_variable *headers,
		    struct ast_ari_response *response)
{
	struct ast_json *message = ast_json_pack("{s: s, s: {}, s: {}, s: {}}",
						 "name", name,
						 "get_params",
						 "path_vars",
						 "headers");
	struct ast_json *get_params_obj = ast_json_object_get(message, "get_params");
	struct ast_json *path_vars_obj = ast_json_object_get(message, "path_vars");
	struct ast_json *headers_obj = ast_json_object_get(message, "headers");

	for (; get_params != NULL; get_params = get_params->next) {
		ast_json_object_set(get_params_obj, get_params->name, ast_json_string_create(get_params->value));
	}

	for (; path_vars != NULL; path_vars = path_vars->next) {
		ast_json_object_set(path_vars_obj, path_vars->name, ast_json_string_create(path_vars->value));
	}

	for (; headers != NULL; headers = headers->next) {
		ast_json_object_set(headers_obj, headers->name, ast_json_string_create(headers->value));
	}

	++invocation_count;
	response->response_code = response_code;
	response->message = message;
}
Esempio n. 3
0
struct ast_json *ast_json_party_id(struct ast_party_id *party)
{
	RAII_VAR(struct ast_json *, json_party_id, NULL, ast_json_unref);
	int pres;

	/* Combined party presentation */
	pres = ast_party_id_presentation(party);
	json_party_id = ast_json_pack("{s: i, s: s}",
		"presentation", pres,
		"presentation_txt", ast_describe_caller_presentation(pres));
	if (!json_party_id) {
		return NULL;
	}

	/* Party number */
	if (party->number.valid && ast_json_object_set(json_party_id, "number", json_party_number(&party->number))) {
		return NULL;
	}

	/* Party name */
	if (party->name.valid && ast_json_object_set(json_party_id, "name", json_party_name(&party->name))) {
		return NULL;
	}

	/* Party subaddress */
	if (party->subaddress.valid && ast_json_object_set(json_party_id, "subaddress", json_party_subaddress(&party->subaddress))) {
		return NULL;
	}

	return ast_json_ref(json_party_id);
}
struct ast_json *stasis_app_recording_to_json(
	const struct stasis_app_recording *recording)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);

	if (recording == NULL) {
		return NULL;
	}

	json = ast_json_pack("{s: s, s: s, s: s, s: s}",
		"name", recording->options->name,
		"format", recording->options->format,
		"state", state_to_string(recording->state),
		"target_uri", recording->options->target);
	if (json && recording->duration.total > -1) {
		ast_json_object_set(json, "duration",
			ast_json_integer_create(recording->duration.total));
	}
	if (json && recording->duration.energy_only > -1) {
		ast_json_object_set(json, "talking_duration",
			ast_json_integer_create(recording->duration.energy_only));
		ast_json_object_set(json, "silence_duration",
			ast_json_integer_create(recording->duration.total - recording->duration.energy_only));
	}

	return ast_json_ref(json);
}
Esempio n. 5
0
struct ast_json *stasis_app_event_filter_to_json(struct stasis_app *app, struct ast_json *json)
{
	if (!app || !json) {
		return json;
	}

	ast_json_object_set(json, "events_allowed", app->events_allowed ?
		ast_json_ref(app->events_allowed) : ast_json_array_create());
	ast_json_object_set(json, "events_disallowed", app->events_disallowed ?
		ast_json_ref(app->events_disallowed) : ast_json_array_create());

	return json;
}
Esempio n. 6
0
/*! \internal \brief convert multi object blob to ari json */
static struct ast_json *multi_user_event_to_json(
	struct stasis_message *message,
	const struct stasis_message_sanitizer *sanitize)
{
	RAII_VAR(struct ast_json *, out, NULL, ast_json_unref);
	struct ast_multi_object_blob *multi = stasis_message_data(message);
	struct ast_json *blob = multi->blob;
	const struct timeval *tv = stasis_message_timestamp(message);
	enum stasis_user_multi_object_snapshot_type type;
	int i;

	out = ast_json_object_create();
	if (!out) {
		return NULL;
	}

	ast_json_object_set(out, "type", ast_json_string_create("ChannelUserevent"));
	ast_json_object_set(out, "timestamp", ast_json_timeval(*tv, NULL));
	ast_json_object_set(out, "eventname", ast_json_string_create(ast_json_string_get((ast_json_object_get(blob, "eventname")))));
	ast_json_object_set(out, "userevent", ast_json_deep_copy(blob));

	for (type = 0; type < STASIS_UMOS_MAX; ++type) {
		for (i = 0; i < AST_VECTOR_SIZE(&multi->snapshots[type]); ++i) {
			struct ast_json *json_object = NULL;
			char *name = NULL;
			void *snapshot = AST_VECTOR_GET(&multi->snapshots[type], i);

			switch (type) {
			case STASIS_UMOS_CHANNEL:
				json_object = ast_channel_snapshot_to_json(snapshot, sanitize);
				name = "channel";
				break;
			case STASIS_UMOS_BRIDGE:
				json_object = ast_bridge_snapshot_to_json(snapshot, sanitize);
				name = "bridge";
				break;
			case STASIS_UMOS_ENDPOINT:
				json_object = ast_endpoint_snapshot_to_json(snapshot, sanitize);
				name = "endpoint";
				break;
			}
			if (json_object) {
				ast_json_object_set(out, name, json_object);
			}
		}
	}
	return ast_json_ref(out);
}
Esempio n. 7
0
/*!
 * \brief Send a message to the given application.
 * \param app App to send the message to.
 * \param message Message to send.
 */
void app_send(struct stasis_app *app, struct ast_json *message)
{
	stasis_app_cb handler;
	char eid[20];
	void *data;

	if (ast_json_object_set(message, "asterisk_id", ast_json_string_create(
			ast_eid_to_str(eid, sizeof(eid), &ast_eid_default)))) {
		ast_log(AST_LOG_WARNING, "Failed to append EID to outgoing event %s\n",
			ast_json_string_get(ast_json_object_get(message, "type")));
	}

	/* Copy off mutable state with lock held */
	ao2_lock(app);
	handler = app->handler;
	data = ao2_bump(app->data);
	ao2_unlock(app);
	/* Name is immutable; no need to copy */

	if (handler) {
		handler(data, app->name, message);
	} else {
		ast_verb(3,
			"Inactive Stasis app '%s' missed message\n", app->name);
	}
	ao2_cleanup(data);
}
Esempio n. 8
0
static void sub_bridge_update_handler(void *data,
	struct stasis_subscription *sub,
	struct stasis_message *message)
{
	struct ast_json *json = NULL;
	struct stasis_app *app = data;
	struct ast_bridge_snapshot_update *update;
	const struct timeval *tv;

	update = stasis_message_data(message);

	tv = stasis_message_timestamp(message);

	if (!update->new_snapshot) {
		json = simple_bridge_event("BridgeDestroyed", update->old_snapshot, tv);
	} else if (!update->old_snapshot) {
		json = simple_bridge_event("BridgeCreated", update->new_snapshot, tv);
	} else if (update->new_snapshot && update->old_snapshot
		&& strcmp(update->new_snapshot->video_source_id, update->old_snapshot->video_source_id)) {
		json = simple_bridge_event("BridgeVideoSourceChanged", update->new_snapshot, tv);
		if (json && !ast_strlen_zero(update->old_snapshot->video_source_id)) {
			ast_json_object_set(json, "old_video_source_id",
				ast_json_string_create(update->old_snapshot->video_source_id));
		}
	}

	if (json) {
		app_send(app, json);
		ast_json_unref(json);
	}

	if (!update->new_snapshot && update->old_snapshot) {
		unsubscribe(app, "bridge", update->old_snapshot->uniqueid, 1);
	}
}
Esempio n. 9
0
int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other)
{
#if JANSSON_VERSION_HEX >= 0x020300
	return json_object_update_missing((json_t *)object, (json_t *)other);
#else
	struct ast_json_iter *iter = ast_json_object_iter(other);
	int ret = 0;

	if (object == NULL || other == NULL) {
		return -1;
	}

	while (iter != NULL && ret == 0) {
		const char *key = ast_json_object_iter_key(iter);

		if (ast_json_object_get(object, key) == NULL) {
			struct ast_json *value = ast_json_object_iter_value(iter);

			if (!value || ast_json_object_set(object, key, ast_json_ref(value))) {
				ret = -1;
			}
		}
		iter = ast_json_object_iter_next(other, iter);
	}
	return ret;
#endif
}
static void recording_publish(struct stasis_app_recording *recording, const char *cause)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);

	ast_assert(recording != NULL);

	json = stasis_app_recording_to_json(recording);
	if (json == NULL) {
		return;
	}

	if (!ast_strlen_zero(cause)) {
		struct ast_json *failure_cause = ast_json_string_create(cause);

		if (!failure_cause) {
			return;
		}

		if (ast_json_object_set(json, "cause", failure_cause)) {
			return;
		}
	}

	message = ast_channel_blob_create_from_cache(
		stasis_app_control_get_channel_id(recording->control),
		stasis_app_recording_snapshot_type(), json);
	if (message == NULL) {
		return;
	}

	stasis_app_control_publish(recording->control, message);
}
Esempio n. 11
0
/*!
 * \brief Send a message to the given application.
 * \param app App to send the message to.
 * \param message Message to send.
 */
void app_send(struct stasis_app *app, struct ast_json *message)
{
	stasis_app_cb handler;
	char eid[20];
	RAII_VAR(void *, data, NULL, ao2_cleanup);

	if (ast_json_object_set(message, "asterisk_id", ast_json_string_create(
			ast_eid_to_str(eid, sizeof(eid), &ast_eid_default)))) {
		ast_log(AST_LOG_WARNING, "Failed to append EID to outgoing event %s\n",
			ast_json_string_get(ast_json_object_get(message, "type")));
	}

	/* Copy off mutable state with lock held */
	{
		SCOPED_AO2LOCK(lock, app);
		handler = app->handler;
		if (app->data) {
			ao2_ref(app->data, +1);
			data = app->data;
		}
		/* Name is immutable; no need to copy */
	}

	if (!handler) {
		ast_verb(3,
			"Inactive Stasis app '%s' missed message\n", app->name);
		return;
	}

	handler(data, app->name, message);
}
Esempio n. 12
0
int create_queue(struct ast_json* j_queue)
{
	int ret;
	char* uuid;
	struct ast_json* j_tmp;

	if(j_queue == NULL) {
		return false;
	}

	j_tmp = ast_json_deep_copy(j_queue);
	uuid = gen_uuid();
	ast_json_object_set(j_tmp, "uuid", ast_json_string_create(uuid));

	ast_log(LOG_NOTICE, "Create queue. uuid[%s], name[%s]\n",
			ast_json_string_get(ast_json_object_get(j_tmp, "uuid")),
			ast_json_string_get(ast_json_object_get(j_tmp, "name"))
			);
	ret = db_insert("queue", j_tmp);
	AST_JSON_UNREF(j_tmp);
	if(ret == false) {
		ast_free(uuid);
		return false;
	}

	// send ami event
	j_tmp = get_queue(uuid);
	send_manager_evt_out_queue_create(j_tmp);
	AST_JSON_UNREF(j_tmp);

	return true;
}
Esempio n. 13
0
/*!
 * \brief Callback handler for Stasis application messages.
 */
static void app_handler(void *data, const char *app_name,
			struct ast_json *message)
{
	struct event_session *session = data;
	int res;
	const char *msg_type = S_OR(
		ast_json_string_get(ast_json_object_get(message, "type")),
		"");
	const char *msg_application = S_OR(
		ast_json_string_get(ast_json_object_get(message, "application")),
		"");
 
	/* Determine if we've been replaced */
	if (strcmp(msg_type, "ApplicationReplaced") == 0 &&
		strcmp(msg_application, app_name) == 0) {
		ao2_find(session->websocket_apps, msg_application,
			OBJ_UNLINK | OBJ_NODATA);
	}

	res = ast_json_object_set(message, "application",
				  ast_json_string_create(app_name));
	if(res != 0) {
		return;
	}

	ao2_lock(session);
	if (session->ws_session) {
		ast_ari_websocket_session_write(session->ws_session, message);
	}
	ao2_unlock(session);
}
Esempio n. 14
0
/**
 * Create plan.
 * @param j_plan
 * @return
 */
bool create_plan(const struct ast_json* j_plan)
{
	int ret;
	char* uuid;
	struct ast_json* j_tmp;
	char* tmp;

	if(j_plan == NULL) {
		return false;
	}

	j_tmp = ast_json_deep_copy(j_plan);
	uuid = gen_uuid();
	ast_json_object_set(j_tmp, "uuid", ast_json_string_create(uuid));

	tmp = get_utc_timestamp();
	ast_json_object_set(j_tmp, "tm_create", ast_json_string_create(tmp));
	ast_free(tmp);

	ast_log(LOG_NOTICE, "Create plan. uuid[%s], name[%s]\n",
			ast_json_string_get(ast_json_object_get(j_tmp, "uuid")),
			ast_json_string_get(ast_json_object_get(j_tmp, "name"))? : "<unknown>"
			);
	ret = db_insert("plan", j_tmp);
	AST_JSON_UNREF(j_tmp);
	if(ret == false) {
		ast_free(uuid);
		return false;
	}
	ast_log(LOG_VERBOSE, "Finished insert.\n");

	// send ami event
	j_tmp = get_plan(uuid);
	ast_log(LOG_VERBOSE, "Check plan info. uuid[%s]\n",
			ast_json_string_get(ast_json_object_get(j_tmp, "uuid"))
			);
	ast_free(uuid);
	if(j_tmp == NULL) {
		ast_log(LOG_ERROR, "Could not get created plan info.");
		return false;
	}
	send_manager_evt_out_plan_create(j_tmp);
	AST_JSON_UNREF(j_tmp);

	return true;
}
Esempio n. 15
0
struct ast_json *ast_json_channel_vars(struct varshead *channelvars)
{
	struct ast_json *ret;
	struct ast_var_t *var;

	ret = ast_json_object_create();
	AST_LIST_TRAVERSE(channelvars, var, entries) {
		ast_json_object_set(ret, var->name, ast_json_string_create(var->value));
	}
Esempio n. 16
0
static struct ast_json *channel_blob_to_json(
	struct stasis_message *message,
	const char *type,
	const struct stasis_message_sanitizer *sanitize)
{
	RAII_VAR(struct ast_json *, out, NULL, ast_json_unref);
	struct ast_channel_blob *channel_blob = stasis_message_data(message);
	struct ast_json *blob = channel_blob->blob;
	struct ast_channel_snapshot *snapshot = channel_blob->snapshot;
	const struct timeval *tv = stasis_message_timestamp(message);
	int res = 0;

	if (blob == NULL || ast_json_is_null(blob)) {
		out = ast_json_object_create();
	} else {
		/* blobs are immutable, so shallow copies are fine */
		out = ast_json_copy(blob);
	}

	if (!out) {
		return NULL;
	}

	res |= ast_json_object_set(out, "type", ast_json_string_create(type));
	res |= ast_json_object_set(out, "timestamp",
		ast_json_timeval(*tv, NULL));

	/* For global channel messages, the snapshot is optional */
	if (snapshot) {
		struct ast_json *json_channel = ast_channel_snapshot_to_json(snapshot, sanitize);

		if (!json_channel) {
			return NULL;
		}

		res |= ast_json_object_set(out, "channel", json_channel);
	}

	if (res != 0) {
		return NULL;
	}

	return ast_json_ref(out);
}
Esempio n. 17
0
static struct ast_json *dial_to_json(
	struct stasis_message *message,
	const struct stasis_message_sanitizer *sanitize)
{
	struct ast_multi_channel_blob *payload = stasis_message_data(message);
	struct ast_json *blob = ast_multi_channel_blob_get_json(payload);
	struct ast_json *caller_json = ast_channel_snapshot_to_json(ast_multi_channel_blob_get_channel(payload, "caller"), sanitize);
	struct ast_json *peer_json = ast_channel_snapshot_to_json(ast_multi_channel_blob_get_channel(payload, "peer"), sanitize);
	struct ast_json *forwarded_json = ast_channel_snapshot_to_json(ast_multi_channel_blob_get_channel(payload, "forwarded"), sanitize);
	struct ast_json *json;
	const struct timeval *tv = stasis_message_timestamp(message);
	int res = 0;

	json = ast_json_pack("{s: s, s: o, s: O, s: O, s: O}",
		"type", "Dial",
		"timestamp", ast_json_timeval(*tv, NULL),
		"dialstatus", ast_json_object_get(blob, "dialstatus"),
		"forward", ast_json_object_get(blob, "forward"),
		"dialstring", ast_json_object_get(blob, "dialstring"));
	if (!json) {
		ast_json_unref(caller_json);
		ast_json_unref(peer_json);
		ast_json_unref(forwarded_json);
		return NULL;
	}

	if (caller_json) {
		res |= ast_json_object_set(json, "caller", caller_json);
	}
	if (peer_json) {
		res |= ast_json_object_set(json, "peer", peer_json);
	}
	if (forwarded_json) {
		res |= ast_json_object_set(json, "forwarded", forwarded_json);
	}

	if (res) {
		ast_json_unref(json);
		return NULL;
	}

	return json;
}
Esempio n. 18
0
/*!
 * \brief Identify module by name and process resource information
 * \param module Resource name
 * \param description Resource description
 * \param usecnt Resource use count
 * \param status Resource running status
 * \param like
 * \param support_level Resource support level
 * \param data JSON body for resource
 * \param condition Name to match resource to
 *
 * \retval 0 if no resource exists
 * \retval 1 if resource exists
 */
static int identify_module(const char *module, const char *description, int usecnt,
                           const char *status, const char *like,
                           enum ast_module_support_level support_level, void *data,
                           const char *condition)
{
	int json_obj_set = 0;

	if (strcmp(condition, module) != 0) {
		return 0;
	}

	json_obj_set += ast_json_object_set(data, "name", ast_json_string_create(module));
	json_obj_set += ast_json_object_set(data, "description", ast_json_string_create(description));
	json_obj_set += ast_json_object_set(data, "use_count", ast_json_integer_create(usecnt));
	json_obj_set += ast_json_object_set(data, "status", ast_json_string_create(status));
	json_obj_set += ast_json_object_set(data, "support_level", ast_json_string_create(
	                                    ast_module_support_level_to_string(support_level)));

	if (json_obj_set != 0) {
		return 0;
	}

	return 1;
}
Esempio n. 19
0
struct ast_json *ast_endpoint_snapshot_to_json(
	const struct ast_endpoint_snapshot *snapshot,
	const struct stasis_message_sanitizer *sanitize)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	struct ast_json *channel_array;
	int i;

	json = ast_json_pack("{s: s, s: s, s: s, s: []}",
		"technology", snapshot->tech,
		"resource", snapshot->resource,
		"state", ast_endpoint_state_to_string(snapshot->state),
		"channel_ids");

	if (json == NULL) {
		return NULL;
	}

	if (snapshot->max_channels != -1) {
		int res = ast_json_object_set(json, "max_channels",
			ast_json_integer_create(snapshot->max_channels));
		if (res != 0) {
			return NULL;
		}
	}

	channel_array = ast_json_object_get(json, "channel_ids");
	ast_assert(channel_array != NULL);
	for (i = 0; i < snapshot->num_channels; ++i) {
		int res;

		if (sanitize && sanitize->channel_id
			&& sanitize->channel_id(snapshot->channel_ids[i])) {
			continue;
		}

		res = ast_json_array_append(channel_array,
			ast_json_string_create(snapshot->channel_ids[i]));
		if (res != 0) {
			return NULL;
		}
	}

	return ast_json_ref(json);
}
Esempio n. 20
0
static void sub_bridge_update_handler(void *data,
	struct stasis_subscription *sub,
	struct stasis_message *message)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	struct stasis_app *app = data;
	struct stasis_cache_update *update;
	struct ast_bridge_snapshot *new_snapshot;
	struct ast_bridge_snapshot *old_snapshot;
	const struct timeval *tv;

	ast_assert(stasis_message_type(message) == stasis_cache_update_type());

	update = stasis_message_data(message);

	ast_assert(update->type == ast_bridge_snapshot_type());

	new_snapshot = stasis_message_data(update->new_snapshot);
	old_snapshot = stasis_message_data(update->old_snapshot);
	tv = update->new_snapshot ?
		stasis_message_timestamp(update->new_snapshot) :
		stasis_message_timestamp(message);

	if (!new_snapshot) {
		json = simple_bridge_event("BridgeDestroyed", old_snapshot, tv);
	} else if (!old_snapshot) {
		json = simple_bridge_event("BridgeCreated", new_snapshot, tv);
	} else if (new_snapshot && old_snapshot
		&& strcmp(new_snapshot->video_source_id, old_snapshot->video_source_id)) {
		json = simple_bridge_event("BridgeVideoSourceChanged", new_snapshot, tv);
		if (json && !ast_strlen_zero(old_snapshot->video_source_id)) {
			ast_json_object_set(json, "old_video_source_id",
				ast_json_string_create(old_snapshot->video_source_id));
		}
	}

	if (json) {
		app_send(app, json);
	}

	if (!new_snapshot && old_snapshot) {
		unsubscribe(app, "bridge", old_snapshot->uniqueid, 1);
	}
}
Esempio n. 21
0
/*! \brief Helper function which converts from a sorcery object set to a json object */
static struct ast_json *sorcery_objectset_to_json(const struct ast_variable *objectset)
{
	struct ast_json *json = ast_json_object_create();
	const struct ast_variable *field;

	for (field = objectset; field; field = field->next) {
		struct ast_json *value = ast_json_string_create(field->value);

		if (!value) {
			ast_json_unref(json);
			return NULL;
		} else if (ast_json_object_set(json, field->name, value)) {
			ast_json_unref(json);
			return NULL;
		}
	}

	return json;
}
Esempio n. 22
0
/*!
 * \brief Callback handler for Stasis application messages.
 *
 * \internal
 *
 * \param data      Void pointer to the event session (\ref event_session).
 * \param app_name  Name of the Stasis application that dispatched the message.
 * \param message   The dispatched message.
 */
static void stasis_app_message_handler(
		void *data, const char *app_name, struct ast_json *message)
{
	struct event_session *session = data;
	const char *msg_type, *msg_application;

	ast_assert(session != NULL);

	ao2_lock(session);

	msg_type = S_OR(ast_json_string_get(ast_json_object_get(message, "type")), "");
	msg_application = S_OR(
		ast_json_string_get(ast_json_object_get(message, "application")), "");

	/* If we've been replaced, remove the application from our local
	   websocket_apps container */
	if (strcmp(msg_type, "ApplicationReplaced") == 0 &&
		strcmp(msg_application, app_name) == 0) {
		ao2_find(session->websocket_apps, msg_application,
			OBJ_UNLINK | OBJ_NODATA);
	}

	/* Now, we need to determine our state to see how we will handle the message */
	if (ast_json_object_set(message, "application", ast_json_string_create(app_name))) {
		/* We failed to add an application element to our json message */
		ast_log(LOG_WARNING,
		        "Failed to dispatch '%s' message from Stasis app '%s'; could not update message\n",
		        msg_type,
		        msg_application);
	} else if (!session->ws_session) {
			/* If the websocket is NULL, the message goes to the queue */
			AST_VECTOR_APPEND(&session->message_queue, message);
			ast_log(LOG_WARNING,
			        "Queued '%s' message for Stasis app '%s'; websocket is not ready\n",
			        msg_type,
			        msg_application);
	} else {
		/* We are ready to publish the message */
		ast_ari_websocket_session_write(session->ws_session, message);
	}

	ao2_unlock(session);
}
Esempio n. 23
0
struct ast_json* create_dial_plan_info(struct ast_json* j_plan)
{
	struct ast_json* j_res;

	if(j_plan == NULL) {
		ast_log(LOG_WARNING, "Wrong input parameter.\n");
		return NULL;
	}

	j_res = ast_json_pack("{s:I, s:s}",
			"dial_timeout", 	ast_json_integer_get(ast_json_object_get(j_plan, "dial_timeout")),
			"plan_variables",	ast_json_string_get(ast_json_object_get(j_plan, "variables"))? : ""
			);

	if(ast_json_string_get(ast_json_object_get(j_plan, "caller_id")) != NULL) {
		ast_json_object_set(j_res, "callerid", ast_json_ref(ast_json_object_get(j_plan, "caller_id")));
	}

	return j_res;

}
Esempio n. 24
0
void ast_ari_asterisk_get_info(struct ast_variable *headers,
	struct ast_ari_asterisk_get_info_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	int show_all = args->only_count == 0;
	int show_build = show_all;
	int show_system = show_all;
	int show_config = show_all;
	int show_status = show_all;
	size_t i;
	int res = 0;

	for (i = 0; i < args->only_count; ++i) {
		if (strcasecmp("build", args->only[i]) == 0) {
			show_build = 1;
		} else if (strcasecmp("system", args->only[i]) == 0) {
			show_system = 1;
		} else if (strcasecmp("config", args->only[i]) == 0) {
			show_config = 1;
		} else if (strcasecmp("status", args->only[i]) == 0) {
			show_status = 1;
		} else {
			ast_log(LOG_WARNING, "Unrecognized info section '%s'\n",
				args->only[i]);
		}
	}

	json = ast_json_object_create();

	if (show_build) {
		res |= ast_json_object_set(json, "build",
			ast_json_pack(
				"{ s: s, s: s, s: s,"
				"  s: s, s: s, s: s }",

				"os", ast_build_os,
				"kernel", ast_build_kernel,
				"machine", ast_build_machine,

				"options", AST_BUILDOPTS,
				"date", ast_build_date,
				"user", ast_build_user));
	}

	if (show_system) {
		char eid_str[128];

		ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);

		res |= ast_json_object_set(json, "system",
			ast_json_pack("{ s: s, s: s }",
				"version", ast_get_version(),
				"entity_id", eid_str));
	}

	if (show_config) {
		struct ast_json *config = ast_json_pack(
			"{ s: s, s: s,"
			" s: { s: s, s: s } }",

			"name", ast_config_AST_SYSTEM_NAME,
			"default_language", ast_defaultlanguage,

			"setid",
			"user", ast_config_AST_RUN_USER,
			"group", ast_config_AST_RUN_GROUP);

		res |= ast_json_object_set(json, "config", config);

		if (ast_option_maxcalls) {
			res |= ast_json_object_set(config, "max_channels",
				ast_json_integer_create(ast_option_maxcalls));
		}

		if (ast_option_maxfiles) {
			res |= ast_json_object_set(config, "max_open_files",
				ast_json_integer_create(ast_option_maxfiles));
		}

		if (ast_option_maxload) {
			res |= ast_json_object_set(config, "max_load",
				ast_json_real_create(ast_option_maxload));
		}
	}

	if (show_status) {
		res |= ast_json_object_set(json, "status",
			ast_json_pack("{ s: o, s: o }",
				"startup_time",
				ast_json_timeval(ast_startuptime, NULL),
				"last_reload_time",
				ast_json_timeval(ast_lastreloadtime, NULL)));
	}

	if (res != 0) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	ast_ari_response_ok(response, ast_json_ref(json));
}
Esempio n. 25
0
void ast_ari_get_docs(const char *uri, struct ast_variable *headers,
                      struct ast_ari_response *response)
{
    RAII_VAR(struct ast_str *, absolute_path_builder, NULL, ast_free);
    RAII_VAR(char *, absolute_api_dirname, NULL, ast_std_free);
    RAII_VAR(char *, absolute_filename, NULL, ast_std_free);
    struct ast_json *obj = NULL;
    struct ast_variable *host = NULL;
    struct ast_json_error error = {};
    struct stat file_stat;

    ast_debug(3, "%s(%s)\n", __func__, uri);

    absolute_path_builder = ast_str_create(80);
    if (absolute_path_builder == NULL) {
        ast_ari_response_alloc_failed(response);
        return;
    }

    /* absolute path to the rest-api directory */
    ast_str_append(&absolute_path_builder, 0, "%s", ast_config_AST_DATA_DIR);
    ast_str_append(&absolute_path_builder, 0, "/rest-api/");
    absolute_api_dirname = realpath(ast_str_buffer(absolute_path_builder), NULL);
    if (absolute_api_dirname == NULL) {
        ast_log(LOG_ERROR, "Error determining real directory for rest-api\n");
        ast_ari_response_error(
            response, 500, "Internal Server Error",
            "Cannot find rest-api directory");
        return;
    }

    /* absolute path to the requested file */
    ast_str_append(&absolute_path_builder, 0, "%s", uri);
    absolute_filename = realpath(ast_str_buffer(absolute_path_builder), NULL);
    if (absolute_filename == NULL) {
        switch (errno) {
        case ENAMETOOLONG:
        case ENOENT:
        case ENOTDIR:
            ast_ari_response_error(
                response, 404, "Not Found",
                "Resource not found");
            break;
        case EACCES:
            ast_ari_response_error(
                response, 403, "Forbidden",
                "Permission denied");
            break;
        default:
            ast_log(LOG_ERROR,
                    "Error determining real path for uri '%s': %s\n",
                    uri, strerror(errno));
            ast_ari_response_error(
                response, 500, "Internal Server Error",
                "Cannot find file");
            break;
        }
        return;
    }

    if (!ast_begins_with(absolute_filename, absolute_api_dirname)) {
        /* HACKERZ! */
        ast_log(LOG_ERROR,
                "Invalid attempt to access '%s' (not in %s)\n",
                absolute_filename, absolute_api_dirname);
        ast_ari_response_error(
            response, 404, "Not Found",
            "Resource not found");
        return;
    }

    if (stat(absolute_filename, &file_stat) == 0) {
        if (!(file_stat.st_mode & S_IFREG)) {
            /* Not a file */
            ast_ari_response_error(
                response, 403, "Forbidden",
                "Invalid access");
            return;
        }
    } else {
        /* Does not exist */
        ast_ari_response_error(
            response, 404, "Not Found",
            "Resource not found");
        return;
    }

    /* Load resource object from file */
    obj = ast_json_load_new_file(absolute_filename, &error);
    if (obj == NULL) {
        ast_log(LOG_ERROR, "Error parsing resource file: %s:%d(%d) %s\n",
                error.source, error.line, error.column, error.text);
        ast_ari_response_error(
            response, 500, "Internal Server Error",
            "Yikes! Cannot parse resource");
        return;
    }

    /* Update the basePath properly */
    if (ast_json_object_get(obj, "basePath") != NULL) {
        for (host = headers; host; host = host->next) {
            if (strcasecmp(host->name, "Host") == 0) {
                break;
            }
        }
        if (host != NULL) {
            ast_json_object_set(
                obj, "basePath",
                ast_json_stringf("http://%s/ari", host->value));
        } else {
            /* Without the host, we don't have the basePath */
            ast_json_object_del(obj, "basePath");
        }
    }

    ast_ari_response_ok(response, obj);
}
Esempio n. 26
0
/**
 * Update plan
 * @param j_plan
 * @return
 */
bool update_plan(const struct ast_json* j_plan)
{
	char* tmp;
	const char* tmp_const;
	char* sql;
	struct ast_json* j_tmp;
	int ret;
	char* uuid;

	if(j_plan == NULL) {
		return false;
	}

	j_tmp = ast_json_deep_copy(j_plan);
	if(j_tmp == NULL) {
		return false;
	}

	tmp_const = ast_json_string_get(ast_json_object_get(j_tmp, "uuid"));
	if(tmp_const == NULL) {
		ast_log(LOG_WARNING, "Could not get uuid.\n");
		AST_JSON_UNREF(j_tmp);
		return false;
	}
	uuid = ast_strdup(tmp_const);

	tmp = get_utc_timestamp();
	ast_json_object_set(j_tmp, "tm_update", ast_json_string_create(tmp));
	ast_free(tmp);

	tmp = db_get_update_str(j_tmp);
	if(tmp == NULL) {
		ast_log(LOG_WARNING, "Could not get update str.\n");
		AST_JSON_UNREF(j_tmp);
		ast_free(uuid);
		return false;
	}
	AST_JSON_UNREF(j_tmp);

	ast_asprintf(&sql, "update plan set %s where in_use=%d and uuid=\"%s\";", tmp, E_DL_USE_OK, uuid);
	ast_free(tmp);

	ret = db_exec(sql);
	ast_free(sql);
	if(ret == false) {
		ast_log(LOG_WARNING, "Could not update plan info. uuid[%s]\n", uuid);
		ast_free(uuid);
		return false;
	}

	j_tmp = get_plan(uuid);
	ast_free(uuid);
	if(j_tmp == NULL) {
		ast_log(LOG_WARNING, "Could not get updated plan info.\n");
		return false;
	}
	send_manager_evt_out_plan_update(j_tmp);
	AST_JSON_UNREF(j_tmp);

	return true;
}