Пример #1
0
/*!
 * \brief [lua_CFunction] Return a lua 'variable' object (for access from lua, don't call
 * directly)
 * 
 * This function is called to lookup a variable construct a 'variable' object.
 * It would be called in the following example as would be seen in
 * extensions.lua.
 *
 * \code
 * channel.variable
 * \endcode
 */
static int lua_get_variable(lua_State *L)
{
	struct ast_channel *chan;
	const char *name = luaL_checkstring(L, 2);
	char *value = NULL;
	char *workspace = alloca(LUA_BUF_SIZE);
	workspace[0] = '\0';
	
	lua_getfield(L, LUA_REGISTRYINDEX, "channel");
	chan = lua_touserdata(L, -1);
	lua_pop(L, 1);

	lua_pushvalue(L, 2);
	lua_push_variable_table(L);
	
	/* if this is not a request for a dialplan funciton attempt to retrieve
	 * the value of the variable */
	if (!ast_strlen_zero(name) && name[strlen(name) - 1] != ')') {
		pbx_retrieve_variable(chan, name, &value, workspace, LUA_BUF_SIZE, ast_channel_varshead(chan));
	}

	if (value) {
		lua_pushstring(L, value);
		lua_setfield(L, -2, "value");
	}

	return 1;	
}
Пример #2
0
/*!
 * \brief [lua_CFunction] Create a 'variable' object for accessing a dialplan
 * function (for access from lua, don't call directly)
 * 
 * This function is called to create a 'variable' object to access a dialplan
 * function.  It would be called in the following example as would be seen in
 * extensions.lua.
 *
 * \code
 * channel.func("arg1", "arg2", "arg3")
 * \endcode
 *
 * To actually do anything with the resulting value you must use the 'get()'
 * and 'set()' methods (the reason is the resulting value is not a value, but
 * an object in the form of a lua table).
 */
static int lua_func_read(lua_State *L)
{
	int nargs = lua_gettop(L);
	char fullname[LUA_EXT_DATA_SIZE] = "";
	char *fullname_next = fullname, *name;
	size_t fullname_left = sizeof(fullname);
	
	lua_getfield(L, 1, "name");
	name = ast_strdupa(lua_tostring(L, -1));
	lua_pop(L, 1);

	ast_build_string(&fullname_next, &fullname_left, "%s(", name);
	
	if (nargs > 1) {
		int i;

		if (!lua_isnil(L, 2))
			ast_build_string(&fullname_next, &fullname_left, "%s", luaL_checkstring(L, 2));

		for (i = 3; i <= nargs; i++) {
			if (lua_isnil(L, i))
				ast_build_string(&fullname_next, &fullname_left, ",");
			else
				ast_build_string(&fullname_next, &fullname_left, ",%s", luaL_checkstring(L, i));
		}
	}

	ast_build_string(&fullname_next, &fullname_left, ")");
	
	lua_push_variable_table(L, fullname);
	
	return 1;
}
Пример #3
0
/*!
 * \brief [lua_CFunction] Create a 'variable' object for accessing a dialplan
 * function (for access from lua, don't call directly)
 * 
 * This function is called to create a 'variable' object to access a dialplan
 * function.  It would be called in the following example as would be seen in
 * extensions.lua.
 *
 * \code
 * channel.func("arg1", "arg2", "arg3")
 * \endcode
 *
 * To actually do anything with the resulting value you must use the 'get()'
 * and 'set()' methods (the reason is the resulting value is not a value, but
 * an object in the form of a lua table).
 */
static int lua_func_read(lua_State *L)
{
	int nargs = lua_gettop(L);

	/* build a string in the form of "func_name(arg1,arg2,arg3)" */
	lua_getfield(L, 1, "name");
	lua_pushliteral(L, "(");
	lua_concat_args(L, 2, nargs);
	lua_pushliteral(L, ")");
	lua_concat(L, 4);

	lua_push_variable_table(L);
	return 1;
}