sout: remove fmt storage from sout packetizers
[vlc.git] / modules / lua / intf.c
bloba9f27e563ab017d4fe7deaefc82923e349fc6024
1 /*****************************************************************************
2 * intf.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
35 #include <vlc_common.h>
36 #include <vlc_interface.h>
37 #include <vlc_fs.h>
39 #include "vlc.h"
40 #include "libs.h"
42 /*****************************************************************************
43 * Prototypes
44 *****************************************************************************/
45 static void *Run( void * );
47 static const char * const ppsz_intf_options[] = { "intf", "config", NULL };
49 /*****************************************************************************
50 * Local structures
51 *****************************************************************************/
52 struct intf_sys_t
54 char *psz_filename;
55 lua_State *L;
56 vlc_thread_t thread;
57 vlclua_dtable_t dtable;
59 /*****************************************************************************
61 *****************************************************************************/
63 static char *MakeConfig( intf_thread_t *p_intf, const char *name )
65 char *psz_config = NULL;
67 if( !strcmp( name, "http" ) )
69 char *psz_http_src = var_InheritString( p_intf, "http-src" );
70 bool b_http_index = var_InheritBool( p_intf, "http-index" );
71 if( psz_http_src )
73 char *psz_esc = config_StringEscape( psz_http_src );
75 if( asprintf( &psz_config, "http={dir='%s',no_index=%s}", psz_esc,
76 b_http_index ? "true" : "false" ) == -1 )
77 psz_config = NULL;
78 free( psz_esc );
79 free( psz_http_src );
81 else
83 if( asprintf( &psz_config, "http={no_index=%s}",
84 b_http_index ? "true" : "false" ) == -1 )
85 psz_config = NULL;
88 else if( !strcmp( name, "telnet" ) )
90 char *psz_host = var_InheritString( p_intf, "telnet-host" );
91 if( !strcmp( psz_host, "*console" ) )
93 else
95 vlc_url_t url;
96 vlc_UrlParse( &url, psz_host );
97 unsigned i_port = var_InheritInteger( p_intf, "telnet-port" );
98 if ( url.i_port != 0 )
100 if ( i_port == TELNETPORT_DEFAULT )
101 i_port = url.i_port;
102 else if ( url.i_port != i_port )
103 msg_Warn( p_intf, "ignoring port %d (using %d)",
104 url.i_port, i_port );
107 char *psz_esc_host = config_StringEscape( url.psz_host );
108 free( psz_host );
109 vlc_UrlClean( &url );
111 if( asprintf( &psz_host, "telnet://%s:%d",
112 psz_esc_host ? psz_esc_host : "", i_port ) == -1 )
113 psz_host = NULL;
114 free( psz_esc_host );
117 char *psz_passwd = var_InheritString( p_intf, "telnet-password" );
119 char *psz_esc_passwd = config_StringEscape( psz_passwd );
121 if( asprintf( &psz_config, "telnet={host='%s',password='%s'}",
122 psz_host, psz_esc_passwd ) == -1 )
123 psz_config = NULL;
125 free( psz_esc_passwd );
126 free( psz_passwd );
127 free( psz_host );
129 else if( !strcmp( name, "cli" ) )
131 char *psz_rc_host = var_InheritString( p_intf, "rc-host" );
132 if( !psz_rc_host )
133 psz_rc_host = var_InheritString( p_intf, "cli-host" );
134 if( psz_rc_host )
136 char *psz_esc_host = config_StringEscape( psz_rc_host );
138 if( asprintf( &psz_config, "cli={host='%s'}", psz_esc_host ) == -1 )
139 psz_config = NULL;
140 free( psz_esc_host );
141 free( psz_rc_host );
145 return psz_config;
148 static char *StripPasswords( const char *psz_config )
150 unsigned n = 0;
151 const char *p = psz_config;
152 while ((p = strstr(p, "password=")) != NULL)
154 n++;
155 p++;
157 if (n == 0)
158 return strdup(psz_config);
160 char *psz_log = malloc(strlen(psz_config) + n * strlen("******") + 1);
161 if (psz_log == NULL)
162 return NULL;
163 psz_log[0] = '\0';
165 for (p = psz_config; ; )
167 const char *pwd = strstr(p, "password=");
168 if (pwd == NULL)
170 /* Copy the last, ending bit */
171 strcat(psz_log, p);
172 break;
174 pwd += strlen("password=");
176 char delim[3] = ",}";
177 if (*pwd == '\'' || *pwd == '"')
179 delim[0] = *pwd++;
180 delim[1] = '\0';
183 strncat(psz_log, p, pwd - p);
184 strcat(psz_log, "******");
186 /* Advance to the delimiter at the end of the password */
187 p = pwd - 1;
190 p = strpbrk(p + 1, delim);
191 if (p == NULL)
192 /* Oops, unbalanced quotes or brackets */
193 return psz_log;
195 while (*(p - 1) == '\\');
197 return psz_log;
200 static const luaL_Reg p_reg[] = { { NULL, NULL } };
202 static int Start_LuaIntf( vlc_object_t *p_this, const char *name )
204 intf_thread_t *p_intf = (intf_thread_t*)p_this;
205 lua_State *L;
207 config_ChainParse( p_intf, "lua-", ppsz_intf_options, p_intf->p_cfg );
209 if( name == NULL )
211 char *n = var_InheritString( p_this, "lua-intf" );
212 if( unlikely(n == NULL) )
213 return VLC_EGENERIC;
214 name = p_intf->obj.header = n;
216 else
217 /* Cleaned up by vlc_object_release() */
218 p_intf->obj.header = strdup( name );
220 intf_sys_t *p_sys = malloc( sizeof(*p_sys) );
221 if( unlikely(p_sys == NULL) )
223 free( p_intf->obj.header );
224 p_intf->obj.header = NULL;
225 return VLC_ENOMEM;
227 p_intf->p_sys = p_sys;
229 p_sys->psz_filename = vlclua_find_file( "intf", name );
230 if( !p_sys->psz_filename )
232 msg_Err( p_intf, "Couldn't find lua interface script \"%s\".",
233 name );
234 goto error;
236 msg_Dbg( p_intf, "Found lua interface script: %s", p_sys->psz_filename );
238 L = luaL_newstate();
239 if( !L )
241 msg_Err( p_intf, "Could not create new Lua State" );
242 goto error;
245 vlclua_set_this( L, p_intf );
246 vlclua_set_playlist_internal( L, pl_Get(p_intf) );
248 luaL_openlibs( L );
250 /* register our functions */
251 luaL_register_namespace( L, "vlc", p_reg );
253 /* register submodules */
254 luaopen_config( L );
255 luaopen_httpd( L );
256 luaopen_input( L );
257 luaopen_msg( L );
258 luaopen_misc( L );
259 if( vlclua_fd_init( L, &p_sys->dtable ) )
261 lua_close( L );
262 goto error;
264 luaopen_object( L );
265 luaopen_osd( L );
266 luaopen_playlist( L );
267 luaopen_sd_intf( L );
268 luaopen_stream( L );
269 luaopen_strings( L );
270 luaopen_variables( L );
271 luaopen_video( L );
272 luaopen_vlm( L );
273 luaopen_volume( L );
274 luaopen_gettext( L );
275 luaopen_xml( L );
276 luaopen_equalizer( L );
277 #if defined(_WIN32) && !VLC_WINSTORE_APP
278 luaopen_win( L );
279 #endif
281 /* clean up */
282 lua_pop( L, 1 );
284 /* Setup the module search path */
285 if( vlclua_add_modules_path( L, p_sys->psz_filename ) )
287 msg_Warn( p_intf, "Error while setting the module search path for %s",
288 p_sys->psz_filename );
289 lua_close( L );
290 goto error;
294 * Get the lua-config string.
295 * If the string is empty, try with the old http-* or telnet-* options
296 * and build the right configuration line
298 bool b_config_set = false;
299 char *psz_config = var_InheritString( p_intf, "lua-config" );
300 if( !psz_config )
301 psz_config = MakeConfig( p_intf, name );
303 if( psz_config )
305 char *psz_buffer;
306 if( asprintf( &psz_buffer, "config={%s}", psz_config ) != -1 )
308 char *psz_log = StripPasswords( psz_buffer );
309 if( psz_log != NULL )
311 msg_Dbg( p_intf, "Setting config variable: %s", psz_log );
312 free( psz_log );
315 if( luaL_dostring( L, psz_buffer ) == 1 )
316 msg_Err( p_intf, "Error while parsing \"lua-config\"." );
317 free( psz_buffer );
318 lua_getglobal( L, "config" );
319 if( lua_istable( L, -1 ) )
321 if( !strcmp( name, "cli" ) )
323 lua_getfield( L, -1, "rc" );
324 if( lua_istable( L, -1 ) )
326 /* msg_Warn( p_intf, "The `rc' lua interface script "
327 "was renamed `cli', please update "
328 "your configuration!" ); */
329 lua_setfield( L, -2, "cli" );
331 else
332 lua_pop( L, 1 );
334 lua_getfield( L, -1, name );
335 if( lua_istable( L, -1 ) )
337 lua_setglobal( L, "config" );
338 b_config_set = true;
342 free( psz_config );
345 if( !b_config_set )
347 lua_newtable( L );
348 lua_setglobal( L, "config" );
351 /* Wrapper for legacy telnet config */
352 if ( !strcmp( name, "telnet" ) )
354 /* msg_Warn( p_intf, "The `telnet' lua interface script was replaced "
355 "by `cli', please update your configuration!" ); */
357 char *wrapped_file = vlclua_find_file( "intf", "cli" );
358 if( !wrapped_file )
360 msg_Err( p_intf, "Couldn't find lua interface script \"cli\", "
361 "needed by telnet wrapper" );
362 lua_close( p_sys->L );
363 goto error;
365 lua_pushstring( L, wrapped_file );
366 lua_setglobal( L, "wrapped_file" );
367 free( wrapped_file );
370 p_sys->L = L;
372 if( vlc_clone( &p_sys->thread, Run, p_intf, VLC_THREAD_PRIORITY_LOW ) )
374 vlclua_fd_cleanup( &p_sys->dtable );
375 lua_close( p_sys->L );
376 goto error;
379 return VLC_SUCCESS;
380 error:
381 free( p_sys->psz_filename );
382 free( p_sys );
383 free( p_intf->obj.header );
384 p_intf->obj.header = NULL;
385 return VLC_EGENERIC;
388 void Close_LuaIntf( vlc_object_t *p_this )
390 intf_thread_t *p_intf = (intf_thread_t*)p_this;
391 intf_sys_t *p_sys = p_intf->p_sys;
393 vlclua_fd_interrupt( &p_sys->dtable );
394 vlc_join( p_sys->thread, NULL );
396 lua_close( p_sys->L );
397 vlclua_fd_cleanup( &p_sys->dtable );
398 free( p_sys->psz_filename );
399 free( p_sys );
402 static void *Run( void *data )
404 intf_thread_t *p_intf = data;
405 intf_sys_t *p_sys = p_intf->p_sys;
406 lua_State *L = p_sys->L;
408 if( vlclua_dofile( VLC_OBJECT(p_intf), L, p_sys->psz_filename ) )
410 msg_Err( p_intf, "Error loading script %s: %s", p_sys->psz_filename,
411 lua_tostring( L, lua_gettop( L ) ) );
412 lua_pop( L, 1 );
414 return NULL;
417 int Open_LuaIntf( vlc_object_t *p_this )
419 return Start_LuaIntf( p_this, NULL );
422 int Open_LuaHTTP( vlc_object_t *p_this )
424 return Start_LuaIntf( p_this, "http" );
427 int Open_LuaCLI( vlc_object_t *p_this )
429 return Start_LuaIntf( p_this, "cli" );
432 int Open_LuaTelnet( vlc_object_t *p_this )
434 char *pw = var_CreateGetNonEmptyString( p_this, "telnet-password" );
435 if( pw == NULL )
437 msg_Err( p_this, "password not configured" );
438 msg_Info( p_this, "Please specify the password in the preferences." );
439 return VLC_EGENERIC;
441 free( pw );
442 return Start_LuaIntf( p_this, "telnet" );