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>
38 #include <vlc_interface.h>
40 #include <sys/types.h>
43 #include <lua.h> /* Low level lua C API */
44 #include <lauxlib.h> /* Higher level C API */
45 #include <lualib.h> /* Lua libs */
50 /*****************************************************************************
52 *****************************************************************************/
53 static void *Run( void * );
55 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
57 /*****************************************************************************
59 *****************************************************************************/
60 static inline void luaL_register_submodule( lua_State *L, const char *psz_name,
64 luaL_register( L, NULL, l );
65 lua_setfield( L, -2, psz_name );
70 const char *psz_shortcut;
79 { "luahotkeys", "hotkeys" },
80 /* { "hotkeys", "hotkeys" }, */
81 { "luatelnet", "telnet" },
82 { "telnet", "telnet" },
83 { "luahttp", "http" },
87 static const char *WordInList( const char *psz_list, const char *psz_word )
91 const char *end = strchr( psz_list, ',' );
95 if( !strncmp( psz_list, psz_word, end - psz_list ) )
99 return !strcmp( psz_list, psz_word ) ? psz_list : NULL;
102 static char *GetModuleName( intf_thread_t *p_intf )
105 const char *psz_intf;
106 /*if( *p_intf->psz_intf == '$' )
107 psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
109 psz_intf = p_intf->psz_intf;
111 int i_candidate = -1;
112 const char *psz_candidate = NULL;
113 for( i = 0; pp_shortcuts[i].psz_name; i++ )
115 const char *psz_match;
116 if( ( psz_match = WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) )
118 if( !psz_candidate || psz_match < psz_candidate )
120 psz_candidate = psz_match;
126 if( i_candidate >= 0 )
127 return strdup( pp_shortcuts[i_candidate].psz_name );
129 return var_CreateGetString( p_intf, "lua-intf" );
132 static char *StripPasswords( const char *psz_config )
135 const char *p = psz_config;
136 while ((p = strstr(p, "password=")) != NULL)
142 return strdup(psz_config);
144 char *psz_log = malloc(strlen(psz_config) + n * strlen("******") + 1);
149 for (p = psz_config; ; )
151 const char *pwd = strstr(p, "password=");
154 /* Copy the last, ending bit */
158 pwd += strlen("password=");
160 char delim[3] = ",}";
161 if (*pwd == '\'' || *pwd == '"')
167 strncat(psz_log, p, pwd - p);
168 strcat(psz_log, "******");
170 /* Advance to the delimiter at the end of the password */
174 p = strpbrk(p + 1, delim);
176 /* Oops, unbalanced quotes or brackets */
179 while (*(p - 1) == '\\');
184 static const luaL_Reg p_reg[] = { { NULL, NULL } };
186 int Open_LuaIntf( vlc_object_t *p_this )
188 intf_thread_t *p_intf = (intf_thread_t*)p_this;
192 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
193 char *psz_name = NULL;
195 if( !p_intf->psz_intf || !*p_intf->psz_intf )
196 psz_name = strdup( "rc" );
198 psz_name = GetModuleName( p_intf );
200 if( !psz_name ) psz_name = strdup( "dummy" );
203 bool b_config_set = false;
205 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
211 p_sys = p_intf->p_sys;
212 p_sys->psz_filename = vlclua_find_file( p_this, "intf", psz_name );
213 if( !p_sys->psz_filename )
215 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
219 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
224 msg_Err( p_intf, "Could not create new Lua State" );
228 vlclua_set_this( L, p_intf );
229 vlclua_set_intf( L, p_sys );
233 /* register our functions */
234 luaL_register( L, "vlc", p_reg );
236 /* register submodules */
247 luaopen_playlist( L );
250 luaopen_strings( L );
251 luaopen_variables( L );
255 luaopen_gettext( L );
258 luaopen_equalizer( L );
263 /* Setup the module search path */
264 if( vlclua_add_modules_path( p_intf, L, p_sys->psz_filename ) )
266 msg_Warn( p_intf, "Error while setting the module search path for %s",
267 p_sys->psz_filename );
273 * Get the lua-config string.
274 * If the string is empty, try with the old http-* or telnet-* options
275 * and build the right configuration line
277 psz_config = var_CreateGetNonEmptyString( p_intf, "lua-config" );
280 if( !strcmp( psz_name, "http" ) )
282 char *psz_http_host = var_CreateGetNonEmptyString( p_intf, "http-host" );
283 char *psz_http_src = var_CreateGetNonEmptyString( p_intf, "http-src" );
284 bool b_http_index = var_CreateGetBool( p_intf, "http-index" );
287 char *psz_esc = config_StringEscape( psz_http_host );
288 asprintf( &psz_config, "http={host='%s'", psz_esc );
290 free( psz_http_host );
294 char *psz_esc = config_StringEscape( psz_http_src );
298 asprintf( &psz_tmp, "%s,dir='%s'", psz_config, psz_esc );
300 psz_config = psz_tmp;
303 asprintf( &psz_config, "http={dir='%s'", psz_esc );
305 free( psz_http_src );
310 asprintf( &psz_tmp, "%s,no_index=%s}", psz_config, b_http_index ? "true" : "false" );
312 psz_config = psz_tmp;
315 asprintf( &psz_config, "http={no_index=%s}", b_http_index ? "true" : "false" );
317 else if( !strcmp( psz_name, "telnet" ) )
319 char *psz_telnet_host = var_CreateGetString( p_intf, "telnet-host" );
320 if( !strcmp( psz_telnet_host, "*console" ) )
325 vlc_UrlParse( &url, psz_telnet_host, 0 );
326 int i_telnet_port = var_CreateGetInteger( p_intf, "telnet-port" );
327 if ( url.i_port != 0 )
329 if ( i_telnet_port == TELNETPORT_DEFAULT )
330 i_telnet_port = url.i_port;
331 else if ( url.i_port != i_telnet_port )
332 msg_Warn( p_intf, "ignoring port %d (using %d)", url.i_port, i_telnet_port );
335 char *psz_esc_host = config_StringEscape( url.psz_host );
336 free( psz_telnet_host );
337 vlc_UrlClean( &url );
339 asprintf( &psz_telnet_host, "telnet://%s:%d", psz_esc_host ? psz_esc_host : "", i_telnet_port );
340 free( psz_esc_host );
343 char *psz_telnet_passwd = var_CreateGetString( p_intf, "telnet-password" );
345 char *psz_esc_passwd = config_StringEscape( psz_telnet_passwd );
347 asprintf( &psz_config, "telnet={host='%s',password='%s'}", psz_telnet_host, psz_esc_passwd );
349 free( psz_esc_passwd );
350 free( psz_telnet_passwd );
351 free( psz_telnet_host );
353 else if( !strcmp( psz_name, "cli" ) )
355 char *psz_rc_host = var_CreateGetNonEmptyString( p_intf, "rc-host" );
357 psz_rc_host = var_CreateGetNonEmptyString( p_intf, "cli-host" );
360 char *psz_esc_host = config_StringEscape( psz_rc_host );
361 asprintf( &psz_config, "cli={host='%s'}", psz_esc_host );
363 free( psz_esc_host );
372 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
374 char *psz_log = StripPasswords( psz_buffer );
375 if( psz_log != NULL )
377 msg_Dbg( p_intf, "Setting config variable: %s", psz_log );
381 if( luaL_dostring( L, psz_buffer ) == 1 )
382 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
384 lua_getglobal( L, "config" );
385 if( lua_istable( L, -1 ) )
387 if( !strcmp( psz_name, "cli" ) )
389 lua_getfield( L, -1, "rc" );
390 if( lua_istable( L, -1 ) )
392 /* msg_Warn( p_intf, "The `rc' lua interface script "
393 "was renamed `cli', please update "
394 "your configuration!" ); */
395 lua_setfield( L, -2, "cli" );
400 lua_getfield( L, -1, psz_name );
401 if( lua_istable( L, -1 ) )
403 lua_setglobal( L, "config" );
414 lua_setglobal( L, "config" );
417 /* Wrapper for legacy telnet config */
418 if ( !strcmp( psz_name, "telnet" ) )
420 /* msg_Warn( p_intf, "The `telnet' lua interface script was replaced "
421 "by `cli', please update your configuration!" ); */
423 char *wrapped_file = vlclua_find_file( p_this, "intf", "cli" );
426 msg_Err( p_intf, "Couldn't find lua interface script \"cli\", "
427 "needed by telnet wrapper" );
428 p_intf->psz_header = NULL;
429 lua_close( p_sys->L );
432 lua_pushstring( L, wrapped_file );
433 lua_setglobal( L, "wrapped_file" );
434 free( wrapped_file );
439 p_intf->psz_header = psz_name;
440 /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
442 vlc_mutex_init( &p_sys->lock );
443 vlc_cond_init( &p_sys->wait );
444 p_sys->exiting = false;
446 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
448 p_intf->psz_header = NULL;
449 vlc_cond_destroy( &p_sys->wait );
450 vlc_mutex_destroy( &p_sys->lock );
451 lua_close( p_sys->L );
457 free( p_sys->psz_filename );
463 void Close_LuaIntf( vlc_object_t *p_this )
465 intf_thread_t *p_intf = (intf_thread_t*)p_this;
466 intf_sys_t *p_sys = p_intf->p_sys;
468 vlc_cancel( p_sys->thread );
470 vlc_mutex_lock( &p_sys->lock );
471 p_sys->exiting = true;
472 vlc_cond_signal( &p_sys->wait );
473 vlc_mutex_unlock( &p_sys->lock );
474 vlc_join( p_sys->thread, NULL );
475 vlc_cond_destroy( &p_sys->wait );
476 vlc_mutex_destroy( &p_sys->lock );
478 lua_close( p_sys->L );
480 free( p_sys->psz_filename );
484 static void *Run( void *data )
486 intf_thread_t *p_intf = data;
487 intf_sys_t *p_sys = p_intf->p_sys;
488 lua_State *L = p_sys->L;
490 if( luaL_dofile( L, p_sys->psz_filename ) )
492 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
493 lua_tostring( L, lua_gettop( L ) ) );