1 /*****************************************************************************
2 * vlc.c: Generic lua interface functions
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
39 #include <vlc_plugin.h>
40 #include <vlc_arrays.h>
41 #include <vlc_charset.h>
43 #include <vlc_services_discovery.h>
44 #include <vlc_stream.h>
46 /*****************************************************************************
48 *****************************************************************************/
49 #define INTF_TEXT N_("Lua interface")
50 #define INTF_LONGTEXT N_("Lua interface module to load")
52 #define CONFIG_TEXT N_("Lua interface configuration")
53 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
54 #define PASS_TEXT N_( "Password" )
55 #define PASS_LONGTEXT N_( "A single password restricts access " \
56 "to this interface." )
57 #define SRC_TEXT N_( "Source directory" )
58 #define SRC_LONGTEXT N_( "Source directory" )
59 #define INDEX_TEXT N_( "Directory index" )
60 #define INDEX_LONGTEXT N_( "Allow to build directory index" )
62 #define TELNETHOST_TEXT N_( "Host" )
63 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
64 "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
65 " If you want this interface to be available only on the local " \
66 "machine, enter \"127.0.0.1\"." )
67 #define TELNETPORT_TEXT N_( "Port" )
68 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
69 "interface will listen. It defaults to 4212." )
70 #define TELNETPWD_TEXT N_( "Password" )
71 #define TELNETPWD_LONGTEXT N_( "A single password restricts access " \
72 "to this interface." )
73 #define RCHOST_TEXT N_("TCP command input")
74 #define RCHOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
75 "You can set the address and port the interface will bind to." )
76 #define CLIHOST_TEXT N_("CLI input")
77 #define CLIHOST_LONGTEXT N_( "Accept commands from this source. " \
78 "The CLI defaults to stdin (\"*console\"), but can also bind to a " \
79 "plain TCP socket (\"localhost:4212\") or use the telnet protocol " \
80 "(\"telnet://0.0.0.0:4212\")" )
82 static int vlc_sd_probe_Open( vlc_object_t
* );
85 set_shortname( N_("Lua") )
86 set_description( N_("Lua interpreter") )
87 set_category( CAT_INTERFACE
)
88 set_subcategory( SUBCAT_INTERFACE_MAIN
)
90 add_string( "lua-intf", "dummy", INTF_TEXT
, INTF_LONGTEXT
, false )
91 add_string( "lua-config", "", CONFIG_TEXT
, CONFIG_LONGTEXT
, false )
92 set_capability( "interface", 0 )
93 set_callbacks( Open_LuaIntf
, Close_LuaIntf
)
94 add_shortcut( "luaintf" )
97 set_section( N_("Lua HTTP"), 0 )
98 add_password("http-password", NULL
, PASS_TEXT
, PASS_LONGTEXT
)
99 add_string ( "http-src", NULL
, SRC_TEXT
, SRC_LONGTEXT
, true )
100 add_bool ( "http-index", false, INDEX_TEXT
, INDEX_LONGTEXT
, true )
101 set_capability( "interface", 0 )
102 set_callbacks( Open_LuaHTTP
, Close_LuaIntf
)
103 add_shortcut( "luahttp", "http" )
104 set_description( N_("Lua HTTP") )
107 set_section( N_("Lua CLI"), 0 )
108 add_string( "rc-host", NULL
, RCHOST_TEXT
, RCHOST_LONGTEXT
, true )
109 add_string( "cli-host", NULL
, CLIHOST_TEXT
, CLIHOST_LONGTEXT
, true )
110 set_capability( "interface", 25 )
111 set_description( N_("Command-line interface") )
112 set_callbacks( Open_LuaCLI
, Close_LuaIntf
)
114 add_shortcut( "luacli", "luarc", "cli", "rc" )
116 add_shortcut( "luacli", "luarc" )
120 set_section( N_("Lua Telnet"), 0 )
121 add_string( "telnet-host", "localhost", TELNETHOST_TEXT
,
122 TELNETHOST_LONGTEXT
, true )
123 add_integer( "telnet-port", TELNETPORT_DEFAULT
, TELNETPORT_TEXT
,
124 TELNETPORT_LONGTEXT
, true )
125 change_integer_range( 1, 65535 )
126 add_password("telnet-password", NULL
, TELNETPWD_TEXT
,
128 set_capability( "interface", 0 )
129 set_callbacks( Open_LuaTelnet
, Close_LuaIntf
)
130 set_description( N_("Lua Telnet") )
131 add_shortcut( "luatelnet", "telnet" )
134 set_shortname( N_( "Lua Meta Fetcher" ) )
135 set_description( N_("Fetch meta data using lua scripts") )
136 set_capability( "meta fetcher", 10 )
137 set_callbacks( FetchMeta
, NULL
)
140 set_shortname( N_( "Lua Meta Reader" ) )
141 set_description( N_("Read meta data using lua scripts") )
142 set_capability( "meta reader", 10 )
143 set_callbacks( ReadMeta
, NULL
)
146 add_shortcut( "luaplaylist" )
147 set_shortname( N_("Lua Playlist") )
148 set_description( N_("Lua Playlist Parser Interface") )
149 set_capability( "stream_filter", 302 )
150 set_callbacks( Import_LuaPlaylist
, Close_LuaPlaylist
)
153 set_shortname( N_( "Lua Art" ) )
154 set_description( N_("Fetch artwork using lua scripts") )
155 set_capability( "art finder", 10 )
156 set_callbacks( FindArt
, NULL
)
159 set_shortname( N_("Lua Extension") )
160 set_description( N_("Lua Extension") )
161 add_shortcut( "luaextension" )
162 set_capability( "extension", 1 )
163 set_callbacks( Open_Extension
, Close_Extension
)
166 set_description( N_("Lua SD Module") )
167 add_shortcut( "luasd" )
168 set_capability( "services_discovery", 0 )
169 add_string( "lua-sd", "", NULL
, NULL
, false )
171 set_callbacks( Open_LuaSD
, Close_LuaSD
)
173 VLC_SD_PROBE_SUBMODULE
177 /*****************************************************************************
179 *****************************************************************************/
180 static const char *ppsz_lua_exts
[] = { ".luac", ".lua", ".vle", NULL
};
181 static int file_select( const char *file
)
183 int i
= strlen( file
);
185 for( j
= 0; ppsz_lua_exts
[j
]; j
++ )
187 int l
= strlen( ppsz_lua_exts
[j
] );
188 if( i
>= l
&& !strcmp( file
+i
-l
, ppsz_lua_exts
[j
] ) )
194 static int file_compare( const char **a
, const char **b
)
196 return strcmp( *a
, *b
);
199 static char **vlclua_dir_list_append( char **restrict list
, char *basedir
,
200 const char *luadirname
)
202 if (unlikely(basedir
== NULL
))
205 if (likely(asprintf(list
, "%s"DIR_SEP
"lua"DIR_SEP
"%s",
206 basedir
, luadirname
) != -1))
213 int vlclua_dir_list(const char *luadirname
, char ***restrict listp
)
215 char **list
= malloc(4 * sizeof(char *));
216 if (unlikely(list
== NULL
))
221 /* Lua scripts in user-specific data directory */
222 list
= vlclua_dir_list_append(list
, config_GetUserDir(VLC_USERDATA_DIR
),
225 char *libdir
= config_GetSysPath(VLC_PKG_LIBEXEC_DIR
, NULL
);
226 char *datadir
= config_GetSysPath(VLC_PKG_DATA_DIR
, NULL
);
227 bool both
= libdir
!= NULL
&& datadir
!= NULL
&& strcmp(libdir
, datadir
);
229 /* Tokenized Lua scripts in architecture-specific data directory */
230 list
= vlclua_dir_list_append(list
, libdir
, luadirname
);
232 /* Source Lua Scripts in architecture-independent data directory */
233 if (both
|| libdir
== NULL
)
234 list
= vlclua_dir_list_append(list
, datadir
, luadirname
);
240 void vlclua_dir_list_free( char **ppsz_dir_list
)
242 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
244 free( ppsz_dir_list
);
247 /*****************************************************************************
248 * Will execute func on all scripts in luadirname, and stop if func returns
250 *****************************************************************************/
251 int vlclua_scripts_batch_execute( vlc_object_t
*p_this
,
252 const char * luadirname
,
253 int (*func
)(vlc_object_t
*, const char *, const luabatch_context_t
*),
256 char **ppsz_dir_list
= NULL
;
259 if( (i_ret
= vlclua_dir_list( luadirname
, &ppsz_dir_list
)) != VLC_SUCCESS
)
262 i_ret
= VLC_EGENERIC
;
263 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
265 char **ppsz_filelist
;
267 msg_Dbg( p_this
, "Trying Lua scripts in %s", *ppsz_dir
);
268 int i_files
= vlc_scandir( *ppsz_dir
, &ppsz_filelist
, file_select
,
273 char **ppsz_file
= ppsz_filelist
;
274 char **ppsz_fileend
= ppsz_filelist
+ i_files
;
276 while( ppsz_file
< ppsz_fileend
)
280 if( asprintf( &psz_filename
,
281 "%s" DIR_SEP
"%s", *ppsz_dir
, *ppsz_file
) == -1 )
283 free( *(ppsz_file
++) );
285 if( likely(psz_filename
!= NULL
) )
287 msg_Dbg( p_this
, "Trying Lua playlist script %s", psz_filename
);
288 i_ret
= func( p_this
, psz_filename
, user_data
);
289 free( psz_filename
);
290 if( i_ret
== VLC_SUCCESS
)
295 while( ppsz_file
< ppsz_fileend
)
296 free( *(ppsz_file
++) );
297 free( ppsz_filelist
);
299 if( i_ret
== VLC_SUCCESS
)
302 vlclua_dir_list_free( ppsz_dir_list
);
306 char *vlclua_find_file( const char *psz_luadirname
, const char *psz_name
)
308 char **ppsz_dir_list
= NULL
;
309 vlclua_dir_list( psz_luadirname
, &ppsz_dir_list
);
311 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
313 for( const char **ppsz_ext
= ppsz_lua_exts
; *ppsz_ext
; ppsz_ext
++ )
318 if( asprintf( &psz_filename
, "%s"DIR_SEP
"%s%s", *ppsz_dir
,
319 psz_name
, *ppsz_ext
) < 0 )
321 vlclua_dir_list_free( ppsz_dir_list
);
325 if( vlc_stat( psz_filename
, &st
) == 0
326 && S_ISREG( st
.st_mode
) )
328 vlclua_dir_list_free( ppsz_dir_list
);
331 free( psz_filename
);
334 vlclua_dir_list_free( ppsz_dir_list
);
338 /*****************************************************************************
339 * Meta data setters utility.
340 * Playlist item table should be on top of the stack when these are called
341 *****************************************************************************/
342 #undef vlclua_read_meta_data
343 void vlclua_read_meta_data( vlc_object_t
*p_this
, lua_State
*L
,
344 input_item_t
*p_input
)
346 #define TRY_META( a, b ) \
347 lua_getfield( L, -1, a ); \
348 if( lua_isstring( L, -1 ) && \
349 strcmp( lua_tostring( L, -1 ), "" ) ) \
351 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
352 EnsureUTF8( psz_value ); \
353 msg_Dbg( p_this, #b ": %s", psz_value ); \
354 input_item_Set ## b ( p_input, psz_value ); \
357 lua_pop( L, 1 ); /* pop a */
358 TRY_META( "title", Title
);
359 TRY_META( "artist", Artist
);
360 TRY_META( "genre", Genre
);
361 TRY_META( "copyright", Copyright
);
362 TRY_META( "album", Album
);
363 TRY_META( "tracknum", TrackNum
);
364 TRY_META( "description", Description
);
365 TRY_META( "rating", Rating
);
366 TRY_META( "date", Date
);
367 TRY_META( "setting", Setting
);
368 TRY_META( "url", URL
);
369 TRY_META( "language", Language
);
370 TRY_META( "nowplaying", NowPlaying
);
371 TRY_META( "publisher", Publisher
);
372 TRY_META( "encodedby", EncodedBy
);
373 TRY_META( "arturl", ArtURL
);
374 TRY_META( "trackid", TrackID
);
375 TRY_META( "director", Director
);
376 TRY_META( "season", Season
);
377 TRY_META( "episode", Episode
);
378 TRY_META( "show_name", ShowName
);
379 TRY_META( "actors", Actors
);
382 #undef vlclua_read_custom_meta_data
383 void vlclua_read_custom_meta_data( vlc_object_t
*p_this
, lua_State
*L
,
384 input_item_t
*p_input
)
386 /* Lock the input item and create the meta table if needed */
387 vlc_mutex_lock( &p_input
->lock
);
389 if( !p_input
->p_meta
)
390 p_input
->p_meta
= vlc_meta_New();
393 lua_getfield( L
, -1, "meta" );
395 if( lua_istable( L
, -1 ) )
398 /* ... item meta nil */
399 while( lua_next( L
, -2 ) )
401 /* ... item meta key value */
402 if( !lua_isstring( L
, -2 ) || !lua_isstring( L
, -1 ) )
404 msg_Err( p_this
, "'meta' keys and values must be strings");
405 lua_pop( L
, 1 ); /* pop "value" */
408 const char *psz_key
= lua_tostring( L
, -2 );
409 const char *psz_value
= lua_tostring( L
, -1 );
411 vlc_meta_AddExtra( p_input
->p_meta
, psz_key
, psz_value
);
413 lua_pop( L
, 1 ); /* pop "value" */
416 lua_pop( L
, 1 ); /* pop "meta" */
417 /* ... item -> back to original stack */
419 vlc_mutex_unlock( &p_input
->lock
);
422 /*****************************************************************************
424 ****************************************************************************/
426 * Playlist item table should be on top of the stack when this is called
428 #undef vlclua_read_options
429 void vlclua_read_options( vlc_object_t
*p_this
, lua_State
*L
,
430 int *pi_options
, char ***pppsz_options
)
432 lua_getfield( L
, -1, "options" );
433 if( lua_istable( L
, -1 ) )
436 while( lua_next( L
, -2 ) )
438 if( lua_isstring( L
, -1 ) )
440 char *psz_option
= strdup( lua_tostring( L
, -1 ) );
441 msg_Dbg( p_this
, "Option: %s", psz_option
);
442 TAB_APPEND( *pi_options
, *pppsz_options
, psz_option
);
446 msg_Warn( p_this
, "Option should be a string" );
448 lua_pop( L
, 1 ); /* pop option */
451 lua_pop( L
, 1 ); /* pop "options" */
454 input_item_t
*vlclua_read_input_item(vlc_object_t
*obj
, lua_State
*L
)
456 if (!lua_istable(L
, -1))
458 msg_Warn(obj
, "Playlist item should be a table" );
462 lua_getfield(L
, -1, "path");
464 /* playlist key item path */
465 if (!lua_isstring(L
, -1))
467 lua_pop(L
, 1); /* pop "path" */
468 msg_Warn(obj
, "Playlist item's path should be a string");
472 /* Read path and name */
473 const char *path
= lua_tostring(L
, -1);
474 msg_Dbg(obj
, "Path: %s", path
);
476 const char *name
= NULL
;
477 lua_getfield(L
, -2, "name");
478 if (lua_isstring(L
, -1))
480 name
= lua_tostring(L
, -1);
481 msg_Dbg(obj
, "Name: %s", name
);
483 else if (!lua_isnil(L
, -1))
484 msg_Warn(obj
, "Playlist item name should be a string" );
487 vlc_tick_t duration
= -1;
489 lua_getfield( L
, -3, "duration" );
490 if (lua_isnumber(L
, -1))
491 duration
= vlc_tick_from_sec( lua_tonumber(L
, -1) );
492 else if (!lua_isnil(L
, -1))
493 msg_Warn(obj
, "Playlist item duration should be a number (seconds)");
494 lua_pop( L
, 1 ); /* pop "duration" */
496 /* Read options: item must be on top of stack */
500 lua_pushvalue(L
, -3);
501 vlclua_read_options(obj
, L
, &optc
, &optv
);
503 /* Create input item */
504 input_item_t
*item
= input_item_NewExt(path
, name
, duration
,
507 if (unlikely(item
== NULL
))
510 input_item_AddOptions(item
, optc
, (const char **)optv
,
511 VLC_INPUT_OPTION_TRUSTED
);
512 lua_pop(L
, 3); /* pop "path name item" */
513 /* playlist key item */
515 /* Read meta data: item must be on top of stack */
516 vlclua_read_meta_data(obj
, L
, item
);
518 /* copy the psz_name to the meta data, if "Title" is still empty */
519 char* title
= input_item_GetTitle(item
);
521 input_item_SetTitle(item
, name
);
524 /* Read custom meta data: item must be on top of stack*/
525 vlclua_read_custom_meta_data(obj
, L
, item
);
534 static int vlc_sd_probe_Open( vlc_object_t
*obj
)
536 if( lua_Disabled( obj
) )
539 vlc_dictionary_t name_d
;
541 char **ppsz_dir_list
;
542 if( vlclua_dir_list( "sd", &ppsz_dir_list
) )
545 vlc_dictionary_init( &name_d
, 32 );
546 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
548 char **ppsz_filelist
;
549 int i_files
= vlc_scandir( *ppsz_dir
, &ppsz_filelist
, file_select
,
551 if( i_files
< 1 ) continue;
553 for( char **ppsz_file
= ppsz_filelist
;
554 ppsz_file
< ppsz_filelist
+ i_files
; ppsz_file
++ )
556 char *temp
= strchr( *ppsz_file
, '.' );
560 if( !vlc_dictionary_has_key( &name_d
, *ppsz_file
) )
561 vlc_dictionary_insert( &name_d
, *ppsz_file
, &name_d
);
564 free( ppsz_filelist
);
566 vlclua_dir_list_free( ppsz_dir_list
);
568 int r
= VLC_PROBE_CONTINUE
;
569 char **names
= vlc_dictionary_all_keys( &name_d
);
572 for( char **name
= names
; *name
; ++name
)
574 r
= vlclua_probe_sd( obj
, *name
);
575 if( r
!= VLC_PROBE_CONTINUE
)
579 for( char **name
= names
; *name
; ++name
)
584 vlc_dictionary_clear( &name_d
, NULL
, NULL
);
589 static int vlclua_add_modules_path_inner( lua_State
*L
, const char *psz_path
)
592 for( const char **ppsz_ext
= ppsz_lua_exts
; *ppsz_ext
; ppsz_ext
++ )
594 lua_pushfstring( L
, "%s"DIR_SEP
"modules"DIR_SEP
"?%s;",
595 psz_path
, *ppsz_ext
);
602 int vlclua_add_modules_path( lua_State
*L
, const char *psz_filename
)
604 /* Setup the module search path:
605 * * "The script's directory"/modules
606 * * "The script's parent directory"/modules
607 * * and so on for all the next directories in the directory list
609 char *psz_path
= strdup( psz_filename
);
613 char *psz_char
= strrchr( psz_path
, DIR_SEP_CHAR
);
621 /* psz_path now holds the file's directory */
622 psz_char
= strrchr( psz_path
, DIR_SEP_CHAR
);
630 /* Push package on stack */
632 lua_getglobal( L
, "package" );
634 /* psz_path now holds the file's parent directory */
635 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
636 *psz_char
= DIR_SEP_CHAR
;
638 /* psz_path now holds the file's directory */
639 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
641 char **ppsz_dir_list
= NULL
;
642 vlclua_dir_list( psz_char
+1/* gruik? */, &ppsz_dir_list
);
643 char **ppsz_dir
= ppsz_dir_list
;
645 for( ; *ppsz_dir
&& strcmp( *ppsz_dir
, psz_path
); ppsz_dir
++ );
648 for( ; *ppsz_dir
; ppsz_dir
++ )
650 psz_path
= *ppsz_dir
;
651 /* FIXME: doesn't work well with meta/... modules due to the double
653 psz_char
= strrchr( psz_path
, DIR_SEP_CHAR
);
656 vlclua_dir_list_free( ppsz_dir_list
);
661 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
662 *psz_char
= DIR_SEP_CHAR
;
663 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
666 lua_getfield( L
, -(count
+1), "path" ); /* Get package.path */
667 lua_concat( L
, count
+1 ); /* Concat vlc module paths and package.path */
668 lua_setfield( L
, -2, "path"); /* Set package.path */
669 lua_pop( L
, 1 ); /* Pop the package module */
671 vlclua_dir_list_free( ppsz_dir_list
);
675 /** Replacement for luaL_dofile, using VLC's input capabilities */
676 int vlclua_dofile( vlc_object_t
*p_this
, lua_State
*L
, const char *curi
)
678 char *uri
= ToLocaleDup( curi
);
679 if( !strstr( uri
, "://" ) ) {
680 int ret
= luaL_dofile( L
, uri
);
684 if( !strncasecmp( uri
, "file://", 7 ) ) {
685 int ret
= luaL_dofile( L
, uri
+ 7 );
689 stream_t
*s
= vlc_stream_NewURL( p_this
, uri
);
695 int64_t i_size
= stream_Size( s
);
696 char *p_buffer
= ( i_size
> 0 ) ? malloc( i_size
) : NULL
;
699 // FIXME: read the whole stream until we reach the end (if no size)
700 vlc_stream_Delete( s
);
704 int64_t i_read
= vlc_stream_Read( s
, p_buffer
, (int) i_size
);
705 int i_ret
= ( i_read
== i_size
) ? 0 : 1;
707 i_ret
= luaL_loadbuffer( L
, p_buffer
, (size_t) i_size
, uri
);
709 i_ret
= lua_pcall( L
, 0, LUA_MULTRET
, 0 );
710 vlc_stream_Delete( s
);