1 /*****************************************************************************
2 * intf.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_interface.h>
37 #include <sys/types.h>
41 #include <lua.h> /* Low level lua C API */
42 #include <lauxlib.h> /* Higher level C API */
43 #include <lualib.h> /* Lua libs */
48 /*****************************************************************************
50 *****************************************************************************/
51 static void *Run( void * );
53 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
55 /*****************************************************************************
57 *****************************************************************************/
58 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
62 luaL_register( L, NULL, l );
63 lua_setfield( L, -2, psz_name );
66 static char *StripPasswords( const char *psz_config )
69 const char *p = psz_config;
70 while ((p = strstr(p, "password=")) != NULL)
76 return strdup(psz_config);
78 char *psz_log = malloc(strlen(psz_config) + n * strlen("******") + 1);
83 for (p = psz_config; ; )
85 const char *pwd = strstr(p, "password=");
88 /* Copy the last, ending bit */
92 pwd += strlen("password=");
95 if (*pwd == '\'' || *pwd == '"')
101 strncat(psz_log, p, pwd - p);
102 strcat(psz_log, "******");
104 /* Advance to the delimiter at the end of the password */
108 p = strpbrk(p + 1, delim);
110 /* Oops, unbalanced quotes or brackets */
113 while (*(p - 1) == '\\');
118 static const luaL_Reg p_reg[] = { { NULL, NULL } };
120 static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
122 intf_thread_t *p_intf = (intf_thread_t*)p_this;
126 assert( name != NULL );
127 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
130 bool b_config_set = false;
132 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
135 p_sys = p_intf->p_sys;
136 p_sys->psz_filename = vlclua_find_file( p_this, "intf", name );
137 if( !p_sys->psz_filename )
139 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
143 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
148 msg_Err( p_intf, "Could not create new Lua State" );
152 vlclua_set_this( L, p_intf );
153 vlclua_set_intf( L, p_sys );
157 /* register our functions */
158 luaL_register( L, "vlc", p_reg );
160 /* register submodules */
171 luaopen_playlist( L );
174 luaopen_strings( L );
175 luaopen_variables( L );
179 luaopen_gettext( L );
182 luaopen_equalizer( L );
187 /* Setup the module search path */
188 if( vlclua_add_modules_path( p_intf, L, p_sys->psz_filename ) )
190 msg_Warn( p_intf, "Error while setting the module search path for %s",
191 p_sys->psz_filename );
197 * Get the lua-config string.
198 * If the string is empty, try with the old http-* or telnet-* options
199 * and build the right configuration line
201 psz_config = var_CreateGetNonEmptyString( p_intf, "lua-config" );
204 if( !strcmp( name, "http" ) )
206 char *psz_http_src = var_CreateGetNonEmptyString( p_intf, "http-src" );
207 bool b_http_index = var_CreateGetBool( p_intf, "http-index" );
210 char *psz_esc = config_StringEscape( psz_http_src );
214 asprintf( &psz_tmp, "%s,dir='%s'", psz_config, psz_esc );
216 psz_config = psz_tmp;
219 asprintf( &psz_config, "http={dir='%s'", psz_esc );
221 free( psz_http_src );
226 asprintf( &psz_tmp, "%s,no_index=%s}", psz_config, b_http_index ? "true" : "false" );
228 psz_config = psz_tmp;
231 asprintf( &psz_config, "http={no_index=%s}", b_http_index ? "true" : "false" );
233 else if( !strcmp( name, "telnet" ) )
235 char *psz_telnet_host = var_CreateGetString( p_intf, "telnet-host" );
236 if( !strcmp( psz_telnet_host, "*console" ) )
241 vlc_UrlParse( &url, psz_telnet_host, 0 );
242 int i_telnet_port = var_CreateGetInteger( p_intf, "telnet-port" );
243 if ( url.i_port != 0 )
245 if ( i_telnet_port == TELNETPORT_DEFAULT )
246 i_telnet_port = url.i_port;
247 else if ( url.i_port != i_telnet_port )
248 msg_Warn( p_intf, "ignoring port %d (using %d)", url.i_port, i_telnet_port );
251 char *psz_esc_host = config_StringEscape( url.psz_host );
252 free( psz_telnet_host );
253 vlc_UrlClean( &url );
255 asprintf( &psz_telnet_host, "telnet://%s:%d", psz_esc_host ? psz_esc_host : "", i_telnet_port );
256 free( psz_esc_host );
259 char *psz_telnet_passwd = var_CreateGetString( p_intf, "telnet-password" );
261 char *psz_esc_passwd = config_StringEscape( psz_telnet_passwd );
263 asprintf( &psz_config, "telnet={host='%s',password='%s'}", psz_telnet_host, psz_esc_passwd );
265 free( psz_esc_passwd );
266 free( psz_telnet_passwd );
267 free( psz_telnet_host );
269 else if( !strcmp( name, "cli" ) )
271 char *psz_rc_host = var_CreateGetNonEmptyString( p_intf, "rc-host" );
273 psz_rc_host = var_CreateGetNonEmptyString( p_intf, "cli-host" );
276 char *psz_esc_host = config_StringEscape( psz_rc_host );
277 asprintf( &psz_config, "cli={host='%s'}", psz_esc_host );
279 free( psz_esc_host );
288 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
290 char *psz_log = StripPasswords( psz_buffer );
291 if( psz_log != NULL )
293 msg_Dbg( p_intf, "Setting config variable: %s", psz_log );
297 if( luaL_dostring( L, psz_buffer ) == 1 )
298 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
300 lua_getglobal( L, "config" );
301 if( lua_istable( L, -1 ) )
303 if( !strcmp( name, "cli" ) )
305 lua_getfield( L, -1, "rc" );
306 if( lua_istable( L, -1 ) )
308 /* msg_Warn( p_intf, "The `rc' lua interface script "
309 "was renamed `cli', please update "
310 "your configuration!" ); */
311 lua_setfield( L, -2, "cli" );
316 lua_getfield( L, -1, name );
317 if( lua_istable( L, -1 ) )
319 lua_setglobal( L, "config" );
330 lua_setglobal( L, "config" );
333 /* Wrapper for legacy telnet config */
334 if ( !strcmp( name, "telnet" ) )
336 /* msg_Warn( p_intf, "The `telnet' lua interface script was replaced "
337 "by `cli', please update your configuration!" ); */
339 char *wrapped_file = vlclua_find_file( p_this, "intf", "cli" );
342 msg_Err( p_intf, "Couldn't find lua interface script \"cli\", "
343 "needed by telnet wrapper" );
344 lua_close( p_sys->L );
347 lua_pushstring( L, wrapped_file );
348 lua_setglobal( L, "wrapped_file" );
349 free( wrapped_file );
354 /* Cleaned up by vlc_object_release() */
355 p_intf->psz_header = strdup( name );
357 vlc_mutex_init( &p_sys->lock );
358 vlc_cond_init( &p_sys->wait );
359 p_sys->exiting = false;
361 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
363 free( p_intf->psz_header );
364 p_intf->psz_header = NULL;
365 vlc_cond_destroy( &p_sys->wait );
366 vlc_mutex_destroy( &p_sys->lock );
367 lua_close( p_sys->L );
373 free( p_sys->psz_filename );
378 void Close_LuaIntf( vlc_object_t *p_this )
380 intf_thread_t *p_intf = (intf_thread_t*)p_this;
381 intf_sys_t *p_sys = p_intf->p_sys;
383 vlc_cancel( p_sys->thread );
385 vlc_mutex_lock( &p_sys->lock );
386 p_sys->exiting = true;
387 vlc_cond_signal( &p_sys->wait );
388 vlc_mutex_unlock( &p_sys->lock );
389 vlc_join( p_sys->thread, NULL );
390 vlc_cond_destroy( &p_sys->wait );
391 vlc_mutex_destroy( &p_sys->lock );
393 lua_close( p_sys->L );
395 free( p_sys->psz_filename );
399 static void *Run( void *data )
401 intf_thread_t *p_intf = data;
402 intf_sys_t *p_sys = p_intf->p_sys;
403 lua_State *L = p_sys->L;
405 if( luaL_dofile( L, p_sys->psz_filename ) )
407 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
408 lua_tostring( L, lua_gettop( L ) ) );
414 int Open_LuaIntf( vlc_object_t *p_this )
416 char *name = var_InheritString( p_this, "lua-intf" );
417 if( unlikely(name == NULL) )
420 int ret = Start_LuaIntf( p_this, name );
425 int Open_LuaHTTP( vlc_object_t *p_this )
427 return Start_LuaIntf( p_this, "http" );
430 int Open_LuaCLI( vlc_object_t *p_this )
432 return Start_LuaIntf( p_this, "cli" );
435 int Open_LuaTelnet( vlc_object_t *p_this )
437 return Start_LuaIntf( p_this, "telnet" );