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>
35 #include <vlc_common.h>
36 #include <vlc_interface.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static void *Run( void * );
47 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
49 /*****************************************************************************
51 *****************************************************************************/
52 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
56 luaL_register( L, NULL, l );
57 lua_setfield( L, -2, psz_name );
60 static char *MakeConfig( intf_thread_t *p_intf, const char *name )
62 char *psz_config = NULL;
64 if( !strcmp( name, "http" ) )
66 char *psz_http_src = var_InheritString( p_intf, "http-src" );
67 bool b_http_index = var_InheritBool( p_intf, "http-index" );
70 char *psz_esc = config_StringEscape( psz_http_src );
72 if( asprintf( &psz_config, "http={dir='%s',no_index=%s}", psz_esc,
73 b_http_index ? "true" : "false" ) == -1 )
80 if( asprintf( &psz_config, "http={no_index=%s}",
81 b_http_index ? "true" : "false" ) == -1 )
85 else if( !strcmp( name, "telnet" ) )
87 char *psz_host = var_InheritString( p_intf, "telnet-host" );
88 if( !strcmp( psz_host, "*console" ) )
93 vlc_UrlParse( &url, psz_host, 0 );
94 unsigned i_port = var_InheritInteger( p_intf, "telnet-port" );
95 if ( url.i_port != 0 )
97 if ( i_port == TELNETPORT_DEFAULT )
99 else if ( url.i_port != i_port )
100 msg_Warn( p_intf, "ignoring port %d (using %d)",
101 url.i_port, i_port );
104 char *psz_esc_host = config_StringEscape( url.psz_host );
106 vlc_UrlClean( &url );
108 if( asprintf( &psz_host, "telnet://%s:%d",
109 psz_esc_host ? psz_esc_host : "", i_port ) == -1 )
111 free( psz_esc_host );
114 char *psz_passwd = var_InheritString( p_intf, "telnet-password" );
116 char *psz_esc_passwd = config_StringEscape( psz_passwd );
118 if( asprintf( &psz_config, "telnet={host='%s',password='%s'}",
119 psz_host, psz_esc_passwd ) == -1 )
122 free( psz_esc_passwd );
126 else if( !strcmp( name, "cli" ) )
128 char *psz_rc_host = var_InheritString( p_intf, "rc-host" );
130 psz_rc_host = var_InheritString( p_intf, "cli-host" );
133 char *psz_esc_host = config_StringEscape( psz_rc_host );
135 if( asprintf( &psz_config, "cli={host='%s'}", psz_esc_host ) == -1 )
137 free( psz_esc_host );
145 static char *StripPasswords( const char *psz_config )
148 const char *p = psz_config;
149 while ((p = strstr(p, "password=")) != NULL)
155 return strdup(psz_config);
157 char *psz_log = malloc(strlen(psz_config) + n * strlen("******") + 1);
162 for (p = psz_config; ; )
164 const char *pwd = strstr(p, "password=");
167 /* Copy the last, ending bit */
171 pwd += strlen("password=");
173 char delim[3] = ",}";
174 if (*pwd == '\'' || *pwd == '"')
180 strncat(psz_log, p, pwd - p);
181 strcat(psz_log, "******");
183 /* Advance to the delimiter at the end of the password */
187 p = strpbrk(p + 1, delim);
189 /* Oops, unbalanced quotes or brackets */
192 while (*(p - 1) == '\\');
197 static const luaL_Reg p_reg[] = { { NULL, NULL } };
199 static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
201 intf_thread_t *p_intf = (intf_thread_t*)p_this;
204 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
208 char *n = var_InheritString( p_this, "lua-intf" );
209 if( unlikely(n == NULL) )
211 name = p_intf->psz_header = n;
214 /* Cleaned up by vlc_object_release() */
215 p_intf->psz_header = strdup( name );
217 intf_sys_t *p_sys = malloc( sizeof(*p_sys) );
218 if( unlikely(p_sys == NULL) )
220 free( p_intf->psz_header );
221 p_intf->psz_header = NULL;
224 p_intf->p_sys = p_sys;
226 p_sys->psz_filename = vlclua_find_file( "intf", name );
227 if( !p_sys->psz_filename )
229 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
233 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
238 msg_Err( p_intf, "Could not create new Lua State" );
242 vlclua_set_this( L, p_intf );
243 vlclua_set_playlist_internal( L, pl_Get(p_intf) );
247 /* register our functions */
248 luaL_register( L, "vlc", p_reg );
250 /* register submodules */
256 if( vlclua_fd_init( L, &p_sys->dtable ) )
263 luaopen_playlist( L );
264 luaopen_sd_intf( L );
266 luaopen_strings( L );
267 luaopen_variables( L );
271 luaopen_gettext( L );
273 luaopen_equalizer( L );
274 #if defined(_WIN32) && !VLC_WINSTORE_APP
281 /* Setup the module search path */
282 if( vlclua_add_modules_path( L, p_sys->psz_filename ) )
284 msg_Warn( p_intf, "Error while setting the module search path for %s",
285 p_sys->psz_filename );
291 * Get the lua-config string.
292 * If the string is empty, try with the old http-* or telnet-* options
293 * and build the right configuration line
295 bool b_config_set = false;
296 char *psz_config = var_InheritString( p_intf, "lua-config" );
298 psz_config = MakeConfig( p_intf, name );
303 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
305 char *psz_log = StripPasswords( psz_buffer );
306 if( psz_log != NULL )
308 msg_Dbg( p_intf, "Setting config variable: %s", psz_log );
312 if( luaL_dostring( L, psz_buffer ) == 1 )
313 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
315 lua_getglobal( L, "config" );
316 if( lua_istable( L, -1 ) )
318 if( !strcmp( name, "cli" ) )
320 lua_getfield( L, -1, "rc" );
321 if( lua_istable( L, -1 ) )
323 /* msg_Warn( p_intf, "The `rc' lua interface script "
324 "was renamed `cli', please update "
325 "your configuration!" ); */
326 lua_setfield( L, -2, "cli" );
331 lua_getfield( L, -1, name );
332 if( lua_istable( L, -1 ) )
334 lua_setglobal( L, "config" );
345 lua_setglobal( L, "config" );
348 /* Wrapper for legacy telnet config */
349 if ( !strcmp( name, "telnet" ) )
351 /* msg_Warn( p_intf, "The `telnet' lua interface script was replaced "
352 "by `cli', please update your configuration!" ); */
354 char *wrapped_file = vlclua_find_file( "intf", "cli" );
357 msg_Err( p_intf, "Couldn't find lua interface script \"cli\", "
358 "needed by telnet wrapper" );
359 lua_close( p_sys->L );
362 lua_pushstring( L, wrapped_file );
363 lua_setglobal( L, "wrapped_file" );
364 free( wrapped_file );
369 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
371 vlclua_fd_cleanup( &p_sys->dtable );
372 lua_close( p_sys->L );
378 free( p_sys->psz_filename );
380 free( p_intf->psz_header );
381 p_intf->psz_header = NULL;
385 void Close_LuaIntf( vlc_object_t *p_this )
387 intf_thread_t *p_intf = (intf_thread_t*)p_this;
388 intf_sys_t *p_sys = p_intf->p_sys;
390 vlclua_fd_interrupt( &p_sys->dtable );
391 vlc_join( p_sys->thread, NULL );
393 vlclua_fd_cleanup( &p_sys->dtable );
394 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 return Start_LuaIntf( p_this, NULL );
419 int Open_LuaHTTP( vlc_object_t *p_this )
421 return Start_LuaIntf( p_this, "http" );
424 int Open_LuaCLI( vlc_object_t *p_this )
426 return Start_LuaIntf( p_this, "cli" );
429 int Open_LuaTelnet( vlc_object_t *p_this )
431 char *pw = var_CreateGetNonEmptyString( p_this, "telnet-password" );
434 msg_Err( p_this, "password not configured" );
435 msg_Info( p_this, "Please specify the password in the preferences." );
439 return Start_LuaIntf( p_this, "telnet" );