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 *****************************************************************************/
31 #include <sys/types.h>
34 #include <vlc_common.h>
35 #include <vlc_interface.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static void *Run( void * );
45 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
47 /*****************************************************************************
49 *****************************************************************************/
50 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
54 luaL_register( L, NULL, l );
55 lua_setfield( L, -2, psz_name );
58 static char *MakeConfig( intf_thread_t *p_intf, const char *name )
60 char *psz_config = NULL;
62 if( !strcmp( name, "http" ) )
64 char *psz_http_src = var_CreateGetNonEmptyString( p_intf, "http-src" );
65 bool b_http_index = var_CreateGetBool( p_intf, "http-index" );
68 char *psz_esc = config_StringEscape( psz_http_src );
69 asprintf( &psz_config, "http={dir='%s',no_index=%s}", psz_esc, b_http_index ? "true" : "false" );
74 asprintf( &psz_config, "http={no_index=%s}", b_http_index ? "true" : "false" );
76 else if( !strcmp( name, "telnet" ) )
78 char *psz_telnet_host = var_CreateGetString( p_intf, "telnet-host" );
79 if( !strcmp( psz_telnet_host, "*console" ) )
84 vlc_UrlParse( &url, psz_telnet_host, 0 );
85 int i_telnet_port = var_CreateGetInteger( p_intf, "telnet-port" );
86 if ( url.i_port != 0 )
88 if ( i_telnet_port == TELNETPORT_DEFAULT )
89 i_telnet_port = url.i_port;
90 else if ( url.i_port != i_telnet_port )
91 msg_Warn( p_intf, "ignoring port %d (using %d)", url.i_port, i_telnet_port );
94 char *psz_esc_host = config_StringEscape( url.psz_host );
95 free( psz_telnet_host );
98 asprintf( &psz_telnet_host, "telnet://%s:%d", psz_esc_host ? psz_esc_host : "", i_telnet_port );
102 char *psz_telnet_passwd = var_CreateGetString( p_intf, "telnet-password" );
104 char *psz_esc_passwd = config_StringEscape( psz_telnet_passwd );
106 asprintf( &psz_config, "telnet={host='%s',password='%s'}", psz_telnet_host, psz_esc_passwd );
108 free( psz_esc_passwd );
109 free( psz_telnet_passwd );
110 free( psz_telnet_host );
112 else if( !strcmp( name, "cli" ) )
114 char *psz_rc_host = var_CreateGetNonEmptyString( p_intf, "rc-host" );
116 psz_rc_host = var_CreateGetNonEmptyString( p_intf, "cli-host" );
119 char *psz_esc_host = config_StringEscape( psz_rc_host );
120 asprintf( &psz_config, "cli={host='%s'}", psz_esc_host );
122 free( psz_esc_host );
130 static char *StripPasswords( const char *psz_config )
133 const char *p = psz_config;
134 while ((p = strstr(p, "password=")) != NULL)
140 return strdup(psz_config);
142 char *psz_log = malloc(strlen(psz_config) + n * strlen("******") + 1);
147 for (p = psz_config; ; )
149 const char *pwd = strstr(p, "password=");
152 /* Copy the last, ending bit */
156 pwd += strlen("password=");
158 char delim[3] = ",}";
159 if (*pwd == '\'' || *pwd == '"')
165 strncat(psz_log, p, pwd - p);
166 strcat(psz_log, "******");
168 /* Advance to the delimiter at the end of the password */
172 p = strpbrk(p + 1, delim);
174 /* Oops, unbalanced quotes or brackets */
177 while (*(p - 1) == '\\');
182 static const luaL_Reg p_reg[] = { { NULL, NULL } };
184 static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
186 intf_thread_t *p_intf = (intf_thread_t*)p_this;
190 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
194 char *n = var_InheritString( p_this, "lua-intf" );
195 if( unlikely(n == NULL) )
197 name = p_intf->psz_header = n;
200 /* Cleaned up by vlc_object_release() */
201 p_intf->psz_header = strdup( name );
203 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
206 free( p_intf->psz_header );
207 p_intf->psz_header = NULL;
210 p_sys = p_intf->p_sys;
211 p_sys->psz_filename = vlclua_find_file( p_this, "intf", name );
212 if( !p_sys->psz_filename )
214 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
218 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
223 msg_Err( p_intf, "Could not create new Lua State" );
227 vlclua_set_this( L, p_intf );
228 vlclua_set_intf( L, p_sys );
232 /* register our functions */
233 luaL_register( L, "vlc", p_reg );
235 /* register submodules */
246 luaopen_playlist( L );
249 luaopen_strings( L );
250 luaopen_variables( L );
254 luaopen_gettext( L );
256 luaopen_equalizer( L );
264 /* Setup the module search path */
265 if( vlclua_add_modules_path( p_intf, L, p_sys->psz_filename ) )
267 msg_Warn( p_intf, "Error while setting the module search path for %s",
268 p_sys->psz_filename );
274 * Get the lua-config string.
275 * If the string is empty, try with the old http-* or telnet-* options
276 * and build the right configuration line
278 bool b_config_set = false;
279 char *psz_config = var_InheritString( p_intf, "lua-config" );
281 psz_config = MakeConfig( p_intf, name );
286 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
288 char *psz_log = StripPasswords( psz_buffer );
289 if( psz_log != NULL )
291 msg_Dbg( p_intf, "Setting config variable: %s", psz_log );
295 if( luaL_dostring( L, psz_buffer ) == 1 )
296 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
298 lua_getglobal( L, "config" );
299 if( lua_istable( L, -1 ) )
301 if( !strcmp( name, "cli" ) )
303 lua_getfield( L, -1, "rc" );
304 if( lua_istable( L, -1 ) )
306 /* msg_Warn( p_intf, "The `rc' lua interface script "
307 "was renamed `cli', please update "
308 "your configuration!" ); */
309 lua_setfield( L, -2, "cli" );
314 lua_getfield( L, -1, name );
315 if( lua_istable( L, -1 ) )
317 lua_setglobal( L, "config" );
328 lua_setglobal( L, "config" );
331 /* Wrapper for legacy telnet config */
332 if ( !strcmp( name, "telnet" ) )
334 /* msg_Warn( p_intf, "The `telnet' lua interface script was replaced "
335 "by `cli', please update your configuration!" ); */
337 char *wrapped_file = vlclua_find_file( p_this, "intf", "cli" );
340 msg_Err( p_intf, "Couldn't find lua interface script \"cli\", "
341 "needed by telnet wrapper" );
342 lua_close( p_sys->L );
345 lua_pushstring( L, wrapped_file );
346 lua_setglobal( L, "wrapped_file" );
347 free( wrapped_file );
352 vlc_mutex_init( &p_sys->lock );
353 vlc_cond_init( &p_sys->wait );
354 p_sys->exiting = false;
356 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
358 vlc_cond_destroy( &p_sys->wait );
359 vlc_mutex_destroy( &p_sys->lock );
360 lua_close( p_sys->L );
366 free( p_sys->psz_filename );
368 free( p_intf->psz_header );
369 p_intf->psz_header = NULL;
373 void Close_LuaIntf( vlc_object_t *p_this )
375 intf_thread_t *p_intf = (intf_thread_t*)p_this;
376 intf_sys_t *p_sys = p_intf->p_sys;
378 vlc_cancel( p_sys->thread );
380 vlc_mutex_lock( &p_sys->lock );
381 p_sys->exiting = true;
382 vlc_cond_signal( &p_sys->wait );
383 vlc_mutex_unlock( &p_sys->lock );
384 vlc_join( p_sys->thread, NULL );
385 vlc_cond_destroy( &p_sys->wait );
386 vlc_mutex_destroy( &p_sys->lock );
388 lua_close( p_sys->L );
390 free( p_sys->psz_filename );
394 static void *Run( void *data )
396 intf_thread_t *p_intf = data;
397 intf_sys_t *p_sys = p_intf->p_sys;
398 lua_State *L = p_sys->L;
400 if( luaL_dofile( L, p_sys->psz_filename ) )
402 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
403 lua_tostring( L, lua_gettop( L ) ) );
409 int Open_LuaIntf( vlc_object_t *p_this )
411 return Start_LuaIntf( p_this, NULL );
414 int Open_LuaHTTP( vlc_object_t *p_this )
416 return Start_LuaIntf( p_this, "http" );
419 int Open_LuaCLI( vlc_object_t *p_this )
421 return Start_LuaIntf( p_this, "cli" );
424 int Open_LuaTelnet( vlc_object_t *p_this )
426 return Start_LuaIntf( p_this, "telnet" );