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 vlclua_fd_init( p_sys );
228 p_sys->psz_filename = vlclua_find_file( "intf", name );
229 if( !p_sys->psz_filename )
231 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
235 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
240 msg_Err( p_intf, "Could not create new Lua State" );
244 vlclua_set_this( L, p_intf );
245 vlclua_set_playlist_internal( L, pl_Get(p_intf) );
249 /* register our functions */
250 luaL_register( L, "vlc", p_reg );
252 /* register submodules */
258 luaopen_net_intf( L );
261 luaopen_playlist( L );
264 luaopen_strings( L );
265 luaopen_variables( L );
269 luaopen_gettext( L );
271 luaopen_equalizer( L );
272 #if defined(_WIN32) && !VLC_WINSTORE_APP
279 /* Setup the module search path */
280 if( vlclua_add_modules_path( L, p_sys->psz_filename ) )
282 msg_Warn( p_intf, "Error while setting the module search path for %s",
283 p_sys->psz_filename );
289 * Get the lua-config string.
290 * If the string is empty, try with the old http-* or telnet-* options
291 * and build the right configuration line
293 bool b_config_set = false;
294 char *psz_config = var_InheritString( p_intf, "lua-config" );
296 psz_config = MakeConfig( p_intf, name );
301 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
303 char *psz_log = StripPasswords( psz_buffer );
304 if( psz_log != NULL )
306 msg_Dbg( p_intf, "Setting config variable: %s", psz_log );
310 if( luaL_dostring( L, psz_buffer ) == 1 )
311 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
313 lua_getglobal( L, "config" );
314 if( lua_istable( L, -1 ) )
316 if( !strcmp( name, "cli" ) )
318 lua_getfield( L, -1, "rc" );
319 if( lua_istable( L, -1 ) )
321 /* msg_Warn( p_intf, "The `rc' lua interface script "
322 "was renamed `cli', please update "
323 "your configuration!" ); */
324 lua_setfield( L, -2, "cli" );
329 lua_getfield( L, -1, name );
330 if( lua_istable( L, -1 ) )
332 lua_setglobal( L, "config" );
343 lua_setglobal( L, "config" );
346 /* Wrapper for legacy telnet config */
347 if ( !strcmp( name, "telnet" ) )
349 /* msg_Warn( p_intf, "The `telnet' lua interface script was replaced "
350 "by `cli', please update your configuration!" ); */
352 char *wrapped_file = vlclua_find_file( "intf", "cli" );
355 msg_Err( p_intf, "Couldn't find lua interface script \"cli\", "
356 "needed by telnet wrapper" );
357 lua_close( p_sys->L );
360 lua_pushstring( L, wrapped_file );
361 lua_setglobal( L, "wrapped_file" );
362 free( wrapped_file );
368 if( vlc_pipe( p_sys->fd ) )
370 lua_close( p_sys->L );
374 # define close(fd) (void)0
377 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
379 close( p_sys->fd[1] );
380 close( p_sys->fd[0] );
381 lua_close( p_sys->L );
387 free( p_sys->psz_filename );
389 free( p_intf->psz_header );
390 p_intf->psz_header = NULL;
394 void Close_LuaIntf( vlc_object_t *p_this )
396 intf_thread_t *p_intf = (intf_thread_t*)p_this;
397 intf_sys_t *p_sys = p_intf->p_sys;
399 close( p_sys->fd[1] );
400 vlc_join( p_sys->thread, NULL );
402 lua_close( p_sys->L );
403 close( p_sys->fd[0] );
404 vlclua_fd_destroy( p_sys );
405 free( p_sys->psz_filename );
409 static void *Run( void *data )
411 intf_thread_t *p_intf = data;
412 intf_sys_t *p_sys = p_intf->p_sys;
413 lua_State *L = p_sys->L;
415 if( luaL_dofile( L, p_sys->psz_filename ) )
417 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
418 lua_tostring( L, lua_gettop( L ) ) );
424 int Open_LuaIntf( vlc_object_t *p_this )
426 return Start_LuaIntf( p_this, NULL );
429 int Open_LuaHTTP( vlc_object_t *p_this )
431 return Start_LuaIntf( p_this, "http" );
434 int Open_LuaCLI( vlc_object_t *p_this )
436 return Start_LuaIntf( p_this, "cli" );
439 int Open_LuaTelnet( vlc_object_t *p_this )
441 char *pw = var_CreateGetNonEmptyString( p_this, "telnet-password" );
444 msg_Err( p_this, "password not configured" );
445 msg_Info( p_this, "Please specify the password in the preferences." );
449 return Start_LuaIntf( p_this, "telnet" );