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;
77 { "luahotkeys", "hotkeys" },
78 /* { "hotkeys", "hotkeys" }, */
79 { "luatelnet", "telnet" },
80 { "telnet", "telnet" },
81 { "luahttp", "http" },
82 /* { "http", "http" }, */
85 static const char *WordInList( const char *psz_list, const char *psz_word )
89 const char *end = strchr( psz_list, ',' );
93 if( !strncmp( psz_list, psz_word, end - psz_list ) )
97 return !strcmp( psz_list, psz_word ) ? psz_list : NULL;
100 static char *GetModuleName( intf_thread_t *p_intf )
103 const char *psz_intf;
104 /*if( *p_intf->psz_intf == '$' )
105 psz_intf = var_GetString( p_intf, p_intf->psz_intf+1 );
107 psz_intf = p_intf->psz_intf;
109 int i_candidate = -1;
110 const char *psz_candidate = NULL;
111 for( i = 0; pp_shortcuts[i].psz_name; i++ )
113 const char *psz_match;
114 if( ( psz_match = WordInList( psz_intf, pp_shortcuts[i].psz_shortcut ) ) )
116 if( !psz_candidate || psz_match < psz_candidate )
118 psz_candidate = psz_match;
124 if( i_candidate >= 0 )
125 return strdup( pp_shortcuts[i_candidate].psz_name );
127 return var_CreateGetString( p_intf, "lua-intf" );
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 int Open_LuaIntf( vlc_object_t *p_this )
186 intf_thread_t *p_intf = (intf_thread_t*)p_this;
190 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
191 char *psz_name = NULL;
193 if( !p_intf->psz_intf || !*p_intf->psz_intf )
194 psz_name = strdup( "rc" );
196 psz_name = GetModuleName( p_intf );
198 if( !psz_name ) psz_name = strdup( "dummy" );
201 bool b_config_set = false;
203 p_intf->p_sys = (intf_sys_t*)malloc( sizeof(intf_sys_t) );
209 p_sys = p_intf->p_sys;
210 p_sys->psz_filename = vlclua_find_file( p_this, "intf", psz_name );
211 if( !p_sys->psz_filename )
213 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
217 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
222 msg_Err( p_intf, "Could not create new Lua State" );
226 vlclua_set_this( L, p_intf );
227 vlclua_set_intf( L, p_sys );
231 /* register our functions */
232 luaL_register( L, "vlc", p_reg );
234 /* register submodules */
245 luaopen_playlist( L );
248 luaopen_strings( L );
249 luaopen_variables( L );
253 luaopen_gettext( L );
260 /* Setup the module search path */
261 if( vlclua_add_modules_path( p_intf, L, p_sys->psz_filename ) )
263 msg_Warn( p_intf, "Error while setting the module search path for %s",
264 p_sys->psz_filename );
270 * Get the lua-config string.
271 * If the string is empty, try with the old http-* or telnet-* options
272 * and build the right configuration line
274 psz_config = var_CreateGetNonEmptyString( p_intf, "lua-config" );
277 if( !strcmp( psz_name, "http" ) )
279 char *psz_http_host = var_CreateGetNonEmptyString( p_intf, "http-host" );
280 char *psz_http_src = var_CreateGetNonEmptyString( p_intf, "http-src" );
281 bool b_http_index = var_CreateGetBool( p_intf, "http-index" );
284 char *psz_esc = config_StringEscape( psz_http_host );
285 asprintf( &psz_config, "http={host='%s'", psz_esc );
287 free( psz_http_host );
291 char *psz_esc = config_StringEscape( psz_http_src );
295 asprintf( &psz_tmp, "%s,dir='%s'", psz_config, psz_esc );
297 psz_config = psz_tmp;
300 asprintf( &psz_config, "http={dir='%s'", psz_esc );
302 free( psz_http_src );
307 asprintf( &psz_tmp, "%s,no_index=%s}", psz_config, b_http_index ? "true" : "false" );
309 psz_config = psz_tmp;
312 asprintf( &psz_config, "http={no_index=%s}", b_http_index ? "true" : "false" );
314 else if( !strcmp( psz_name, "telnet" ) )
316 char *psz_telnet_host = var_CreateGetString( p_intf, "telnet-host" );
317 if( !strcmp( psz_telnet_host, "*console" ) )
322 vlc_UrlParse( &url, psz_telnet_host, 0 );
323 int i_telnet_port = var_CreateGetInteger( p_intf, "telnet-port" );
324 if ( url.i_port != 0 )
326 if ( i_telnet_port == TELNETPORT_DEFAULT )
327 i_telnet_port = url.i_port;
328 else if ( url.i_port != i_telnet_port )
329 msg_Warn( p_intf, "ignoring port %d (using %d)", url.i_port, i_telnet_port );
332 char *psz_esc_host = config_StringEscape( url.psz_host );
333 free( psz_telnet_host );
334 vlc_UrlClean( &url );
336 asprintf( &psz_telnet_host, "%s:%d", psz_esc_host ? psz_esc_host : "", i_telnet_port );
337 free( psz_esc_host );
340 char *psz_telnet_passwd = var_CreateGetString( p_intf, "telnet-password" );
342 char *psz_esc_passwd = config_StringEscape( psz_telnet_passwd );
344 asprintf( &psz_config, "telnet={host='%s',password='%s'}", psz_telnet_host, psz_esc_passwd );
346 free( psz_esc_passwd );
347 free( psz_telnet_passwd );
348 free( psz_telnet_host );
350 else if( !strcmp( psz_name, "rc" ) )
352 char *psz_rc_host = var_CreateGetNonEmptyString( p_intf, "rc-host" );
355 char *psz_esc_host = config_StringEscape( psz_rc_host );
356 asprintf( &psz_config, "rc={host='%s'}", psz_esc_host );
358 free( psz_esc_host );
367 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
369 char *psz_log = StripPasswords( psz_buffer );
370 if( psz_log != NULL )
372 msg_Dbg( p_intf, "Setting config variable: %s", psz_log );
376 if( luaL_dostring( L, psz_buffer ) == 1 )
377 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
379 lua_getglobal( L, "config" );
380 if( lua_istable( L, -1 ) )
382 lua_getfield( L, -1, psz_name );
383 if( lua_istable( L, -1 ) )
385 lua_setglobal( L, "config" );
396 lua_setglobal( L, "config" );
401 p_intf->psz_header = psz_name;
402 /* ^^ Do I need to clean that up myself in Close_LuaIntf? */
404 vlc_mutex_init( &p_sys->lock );
405 vlc_cond_init( &p_sys->wait );
406 p_sys->exiting = false;
408 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
410 p_intf->psz_header = NULL;
411 vlc_cond_destroy( &p_sys->wait );
412 vlc_mutex_destroy( &p_sys->lock );
413 lua_close( p_sys->L );
419 free( p_sys->psz_filename );
425 void Close_LuaIntf( vlc_object_t *p_this )
427 intf_thread_t *p_intf = (intf_thread_t*)p_this;
428 intf_sys_t *p_sys = p_intf->p_sys;
430 vlc_cancel( p_sys->thread );
432 vlc_mutex_lock( &p_sys->lock );
433 p_sys->exiting = true;
434 vlc_cond_signal( &p_sys->wait );
435 vlc_mutex_unlock( &p_sys->lock );
436 vlc_join( p_sys->thread, NULL );
437 vlc_cond_destroy( &p_sys->wait );
438 vlc_mutex_destroy( &p_sys->lock );
440 lua_close( p_sys->L );
442 free( p_sys->psz_filename );
446 static void *Run( void *data )
448 intf_thread_t *p_intf = data;
449 intf_sys_t *p_sys = p_intf->p_sys;
450 lua_State *L = p_sys->L;
452 if( luaL_dofile( L, p_sys->psz_filename ) )
454 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
455 lua_tostring( L, lua_gettop( L ) ) );