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
, false )
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 TELNETPWD_LONGTEXT
, true )
129 set_capability( "interface", 0 )
130 set_callbacks( Open_LuaTelnet
, Close_LuaIntf
)
131 set_description( N_("Lua Telnet") )
132 add_shortcut( "luatelnet", "telnet" )
135 set_shortname( N_( "Lua Meta Fetcher" ) )
136 set_description( N_("Fetch meta data using lua scripts") )
137 set_capability( "meta fetcher", 10 )
138 set_callbacks( FetchMeta
, NULL
)
141 set_shortname( N_( "Lua Meta Reader" ) )
142 set_description( N_("Read meta data using lua scripts") )
143 set_capability( "meta reader", 10 )
144 set_callbacks( ReadMeta
, NULL
)
147 add_shortcut( "luaplaylist" )
148 set_shortname( N_("Lua Playlist") )
149 set_description( N_("Lua Playlist Parser Interface") )
150 set_capability( "stream_filter", 2 )
151 set_callbacks( Import_LuaPlaylist
, Close_LuaPlaylist
)
154 set_shortname( N_( "Lua Art" ) )
155 set_description( N_("Fetch artwork using lua scripts") )
156 set_capability( "art finder", 10 )
157 set_callbacks( FindArt
, NULL
)
160 set_shortname( N_("Lua Extension") )
161 set_description( N_("Lua Extension") )
162 add_shortcut( "luaextension" )
163 set_capability( "extension", 1 )
164 set_callbacks( Open_Extension
, Close_Extension
)
167 set_description( N_("Lua SD Module") )
168 add_shortcut( "luasd" )
169 set_capability( "services_discovery", 0 )
170 add_string( "lua-sd", "", NULL
, NULL
, false )
172 set_callbacks( Open_LuaSD
, Close_LuaSD
)
174 VLC_SD_PROBE_SUBMODULE
178 /*****************************************************************************
180 *****************************************************************************/
181 static const char *ppsz_lua_exts
[] = { ".luac", ".lua", ".vle", NULL
};
182 static int file_select( const char *file
)
184 int i
= strlen( file
);
186 for( j
= 0; ppsz_lua_exts
[j
]; j
++ )
188 int l
= strlen( ppsz_lua_exts
[j
] );
189 if( i
>= l
&& !strcmp( file
+i
-l
, ppsz_lua_exts
[j
] ) )
195 static int file_compare( const char **a
, const char **b
)
197 return strcmp( *a
, *b
);
200 int vlclua_dir_list( const char *luadirname
, char ***pppsz_dir_list
)
202 #define MAX_DIR_LIST_SIZE 5
203 *pppsz_dir_list
= malloc(MAX_DIR_LIST_SIZE
*sizeof(char *));
204 if (!*pppsz_dir_list
)
206 char **ppsz_dir_list
= *pppsz_dir_list
;
209 char *datadir
= config_GetUserDir( VLC_DATA_DIR
);
211 if( likely(datadir
!= NULL
)
212 && likely(asprintf( &ppsz_dir_list
[i
], "%s"DIR_SEP
"lua"DIR_SEP
"%s",
213 datadir
, luadirname
) != -1) )
217 char *psz_datapath
= config_GetDataDir();
218 if( likely(psz_datapath
!= NULL
) )
220 if( likely(asprintf( &ppsz_dir_list
[i
], "%s"DIR_SEP
"lua"DIR_SEP
"%s",
221 psz_datapath
, luadirname
) != -1) )
223 free( psz_datapath
);
226 ppsz_dir_list
[i
] = NULL
;
228 assert( i
< MAX_DIR_LIST_SIZE
);
233 void vlclua_dir_list_free( char **ppsz_dir_list
)
235 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
237 free( ppsz_dir_list
);
240 /*****************************************************************************
241 * Will execute func on all scripts in luadirname, and stop if func returns
243 *****************************************************************************/
244 int vlclua_scripts_batch_execute( vlc_object_t
*p_this
,
245 const char * luadirname
,
246 int (*func
)(vlc_object_t
*, const char *, const luabatch_context_t
*),
249 char **ppsz_dir_list
= NULL
;
252 if( (i_ret
= vlclua_dir_list( luadirname
, &ppsz_dir_list
)) != VLC_SUCCESS
)
255 i_ret
= VLC_EGENERIC
;
256 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
258 char **ppsz_filelist
;
260 msg_Dbg( p_this
, "Trying Lua scripts in %s", *ppsz_dir
);
261 int i_files
= vlc_scandir( *ppsz_dir
, &ppsz_filelist
, file_select
,
266 char **ppsz_file
= ppsz_filelist
;
267 char **ppsz_fileend
= ppsz_filelist
+ i_files
;
269 while( ppsz_file
< ppsz_fileend
)
273 if( asprintf( &psz_filename
,
274 "%s" DIR_SEP
"%s", *ppsz_dir
, *ppsz_file
) == -1 )
276 free( *(ppsz_file
++) );
278 if( likely(psz_filename
!= NULL
) )
280 msg_Dbg( p_this
, "Trying Lua playlist script %s", psz_filename
);
281 i_ret
= func( p_this
, psz_filename
, user_data
);
282 free( psz_filename
);
283 if( i_ret
== VLC_SUCCESS
)
288 while( ppsz_file
< ppsz_fileend
)
289 free( *(ppsz_file
++) );
290 free( ppsz_filelist
);
292 if( i_ret
== VLC_SUCCESS
)
295 vlclua_dir_list_free( ppsz_dir_list
);
299 char *vlclua_find_file( const char *psz_luadirname
, const char *psz_name
)
301 char **ppsz_dir_list
= NULL
;
302 vlclua_dir_list( psz_luadirname
, &ppsz_dir_list
);
304 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
306 for( const char **ppsz_ext
= ppsz_lua_exts
; *ppsz_ext
; ppsz_ext
++ )
311 if( asprintf( &psz_filename
, "%s"DIR_SEP
"%s%s", *ppsz_dir
,
312 psz_name
, *ppsz_ext
) < 0 )
314 vlclua_dir_list_free( ppsz_dir_list
);
318 if( vlc_stat( psz_filename
, &st
) == 0
319 && S_ISREG( st
.st_mode
) )
321 vlclua_dir_list_free( ppsz_dir_list
);
324 free( psz_filename
);
327 vlclua_dir_list_free( ppsz_dir_list
);
331 /*****************************************************************************
332 * Meta data setters utility.
333 * Playlist item table should be on top of the stack when these are called
334 *****************************************************************************/
335 #undef vlclua_read_meta_data
336 void vlclua_read_meta_data( vlc_object_t
*p_this
, lua_State
*L
,
337 input_item_t
*p_input
)
339 #define TRY_META( a, b ) \
340 lua_getfield( L, -1, a ); \
341 if( lua_isstring( L, -1 ) && \
342 strcmp( lua_tostring( L, -1 ), "" ) ) \
344 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
345 EnsureUTF8( psz_value ); \
346 msg_Dbg( p_this, #b ": %s", psz_value ); \
347 input_item_Set ## b ( p_input, psz_value ); \
350 lua_pop( L, 1 ); /* pop a */
351 TRY_META( "title", Title
);
352 TRY_META( "artist", Artist
);
353 TRY_META( "genre", Genre
);
354 TRY_META( "copyright", Copyright
);
355 TRY_META( "album", Album
);
356 TRY_META( "tracknum", TrackNum
);
357 TRY_META( "description", Description
);
358 TRY_META( "rating", Rating
);
359 TRY_META( "date", Date
);
360 TRY_META( "setting", Setting
);
361 TRY_META( "url", URL
);
362 TRY_META( "language", Language
);
363 TRY_META( "nowplaying", NowPlaying
);
364 TRY_META( "publisher", Publisher
);
365 TRY_META( "encodedby", EncodedBy
);
366 TRY_META( "arturl", ArtURL
);
367 TRY_META( "trackid", TrackID
);
368 TRY_META( "director", Director
);
369 TRY_META( "season", Season
);
370 TRY_META( "episode", Episode
);
371 TRY_META( "show_name", ShowName
);
372 TRY_META( "actors", Actors
);
375 #undef vlclua_read_custom_meta_data
376 void vlclua_read_custom_meta_data( vlc_object_t
*p_this
, lua_State
*L
,
377 input_item_t
*p_input
)
379 /* Lock the input item and create the meta table if needed */
380 vlc_mutex_lock( &p_input
->lock
);
382 if( !p_input
->p_meta
)
383 p_input
->p_meta
= vlc_meta_New();
386 lua_getfield( L
, -1, "meta" );
388 if( lua_istable( L
, -1 ) )
391 /* ... item meta nil */
392 while( lua_next( L
, -2 ) )
394 /* ... item meta key value */
395 if( !lua_isstring( L
, -2 ) || !lua_isstring( L
, -1 ) )
397 msg_Err( p_this
, "'meta' keys and values must be strings");
398 lua_pop( L
, 1 ); /* pop "value" */
401 const char *psz_key
= lua_tostring( L
, -2 );
402 const char *psz_value
= lua_tostring( L
, -1 );
404 vlc_meta_AddExtra( p_input
->p_meta
, psz_key
, psz_value
);
406 lua_pop( L
, 1 ); /* pop "value" */
409 lua_pop( L
, 1 ); /* pop "meta" */
410 /* ... item -> back to original stack */
412 vlc_mutex_unlock( &p_input
->lock
);
415 /*****************************************************************************
417 ****************************************************************************/
419 * Playlist item table should be on top of the stack when this is called
421 #undef vlclua_read_options
422 void vlclua_read_options( vlc_object_t
*p_this
, lua_State
*L
,
423 int *pi_options
, char ***pppsz_options
)
425 lua_getfield( L
, -1, "options" );
426 if( lua_istable( L
, -1 ) )
429 while( lua_next( L
, -2 ) )
431 if( lua_isstring( L
, -1 ) )
433 char *psz_option
= strdup( lua_tostring( L
, -1 ) );
434 msg_Dbg( p_this
, "Option: %s", psz_option
);
435 TAB_APPEND( *pi_options
, *pppsz_options
, psz_option
);
439 msg_Warn( p_this
, "Option should be a string" );
441 lua_pop( L
, 1 ); /* pop option */
444 lua_pop( L
, 1 ); /* pop "options" */
447 input_item_t
*vlclua_read_input_item(vlc_object_t
*obj
, lua_State
*L
)
449 if (!lua_istable(L
, -1))
451 msg_Warn(obj
, "Playlist item should be a table" );
455 lua_getfield(L
, -1, "path");
457 /* playlist key item path */
458 if (!lua_isstring(L
, -1))
460 lua_pop(L
, 1); /* pop "path" */
461 msg_Warn(obj
, "Playlist item's path should be a string");
465 /* Read path and name */
466 const char *path
= lua_tostring(L
, -1);
467 msg_Dbg(obj
, "Path: %s", path
);
469 const char *name
= NULL
;
470 lua_getfield(L
, -2, "name");
471 if (lua_isstring(L
, -1))
473 name
= lua_tostring(L
, -1);
474 msg_Dbg(obj
, "Name: %s", name
);
476 else if (!lua_isnil(L
, -1))
477 msg_Warn(obj
, "Playlist item name should be a string" );
480 mtime_t duration
= -1;
482 lua_getfield( L
, -3, "duration" );
483 if (lua_isnumber(L
, -1))
484 duration
= (mtime_t
)(lua_tonumber(L
, -1) * (CLOCK_FREQ
* 1.));
485 else if (!lua_isnil(L
, -1))
486 msg_Warn(obj
, "Playlist item duration should be a number (seconds)");
487 lua_pop( L
, 1 ); /* pop "duration" */
489 /* Read options: item must be on top of stack */
493 lua_pushvalue(L
, -3);
494 vlclua_read_options(obj
, L
, &optc
, &optv
);
496 /* Create input item */
497 input_item_t
*item
= input_item_NewExt(path
, name
, duration
,
500 if (unlikely(item
== NULL
))
503 input_item_AddOptions(item
, optc
, (const char **)optv
,
504 VLC_INPUT_OPTION_TRUSTED
);
505 lua_pop(L
, 3); /* pop "path name item" */
506 /* playlist key item */
508 /* Read meta data: item must be on top of stack */
509 vlclua_read_meta_data(obj
, L
, item
);
511 /* copy the psz_name to the meta data, if "Title" is still empty */
512 char* title
= input_item_GetTitle(item
);
514 input_item_SetTitle(item
, name
);
517 /* Read custom meta data: item must be on top of stack*/
518 vlclua_read_custom_meta_data(obj
, L
, item
);
527 static int vlc_sd_probe_Open( vlc_object_t
*obj
)
529 vlc_dictionary_t name_d
;
531 char **ppsz_dir_list
;
532 if( vlclua_dir_list( "sd", &ppsz_dir_list
) )
535 vlc_dictionary_init( &name_d
, 32 );
536 for( char **ppsz_dir
= ppsz_dir_list
; *ppsz_dir
; ppsz_dir
++ )
538 char **ppsz_filelist
;
539 int i_files
= vlc_scandir( *ppsz_dir
, &ppsz_filelist
, file_select
,
541 if( i_files
< 1 ) continue;
543 for( char **ppsz_file
= ppsz_filelist
;
544 ppsz_file
< ppsz_filelist
+ i_files
; ppsz_file
++ )
546 char *temp
= strchr( *ppsz_file
, '.' );
550 if( !vlc_dictionary_has_key( &name_d
, *ppsz_file
) )
551 vlc_dictionary_insert( &name_d
, *ppsz_file
, &name_d
);
554 free( ppsz_filelist
);
556 vlclua_dir_list_free( ppsz_dir_list
);
558 int r
= VLC_PROBE_CONTINUE
;
559 char **names
= vlc_dictionary_all_keys( &name_d
);
562 for( char **name
= names
; *name
; ++name
)
564 r
= vlclua_probe_sd( obj
, *name
);
565 if( r
!= VLC_PROBE_CONTINUE
)
569 for( char **name
= names
; *name
; ++name
)
574 vlc_dictionary_clear( &name_d
, NULL
, NULL
);
579 static int vlclua_add_modules_path_inner( lua_State
*L
, const char *psz_path
)
582 for( const char **ppsz_ext
= ppsz_lua_exts
; *ppsz_ext
; ppsz_ext
++ )
584 lua_pushfstring( L
, "%s"DIR_SEP
"modules"DIR_SEP
"?%s;",
585 psz_path
, *ppsz_ext
);
592 int vlclua_add_modules_path( lua_State
*L
, const char *psz_filename
)
594 /* Setup the module search path:
595 * * "The script's directory"/modules
596 * * "The script's parent directory"/modules
597 * * and so on for all the next directories in the directory list
599 char *psz_path
= strdup( psz_filename
);
603 char *psz_char
= strrchr( psz_path
, DIR_SEP_CHAR
);
611 /* psz_path now holds the file's directory */
612 psz_char
= strrchr( psz_path
, DIR_SEP_CHAR
);
620 /* Push package on stack */
622 lua_getglobal( L
, "package" );
624 /* psz_path now holds the file's parent directory */
625 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
626 *psz_char
= DIR_SEP_CHAR
;
628 /* psz_path now holds the file's directory */
629 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
631 char **ppsz_dir_list
= NULL
;
632 vlclua_dir_list( psz_char
+1/* gruik? */, &ppsz_dir_list
);
633 char **ppsz_dir
= ppsz_dir_list
;
635 for( ; *ppsz_dir
&& strcmp( *ppsz_dir
, psz_path
); ppsz_dir
++ );
638 for( ; *ppsz_dir
; ppsz_dir
++ )
640 psz_path
= *ppsz_dir
;
641 /* FIXME: doesn't work well with meta/... modules due to the double
643 psz_char
= strrchr( psz_path
, DIR_SEP_CHAR
);
646 vlclua_dir_list_free( ppsz_dir_list
);
651 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
652 *psz_char
= DIR_SEP_CHAR
;
653 count
+= vlclua_add_modules_path_inner( L
, psz_path
);
656 lua_getfield( L
, -(count
+1), "path" ); /* Get package.path */
657 lua_concat( L
, count
+1 ); /* Concat vlc module paths and package.path */
658 lua_setfield( L
, -2, "path"); /* Set package.path */
659 lua_pop( L
, 1 ); /* Pop the package module */
661 vlclua_dir_list_free( ppsz_dir_list
);
665 /** Replacement for luaL_dofile, using VLC's input capabilities */
666 int vlclua_dofile( vlc_object_t
*p_this
, lua_State
*L
, const char *curi
)
668 char *uri
= ToLocaleDup( curi
);
669 if( !strstr( uri
, "://" ) ) {
670 int ret
= luaL_dofile( L
, uri
);
674 if( !strncasecmp( uri
, "file://", 7 ) ) {
675 int ret
= luaL_dofile( L
, uri
+ 7 );
679 stream_t
*s
= vlc_stream_NewURL( p_this
, uri
);
685 int64_t i_size
= stream_Size( s
);
686 char *p_buffer
= ( i_size
> 0 ) ? malloc( i_size
) : NULL
;
689 // FIXME: read the whole stream until we reach the end (if no size)
690 vlc_stream_Delete( s
);
694 int64_t i_read
= vlc_stream_Read( s
, p_buffer
, (int) i_size
);
695 int i_ret
= ( i_read
== i_size
) ? 0 : 1;
697 i_ret
= luaL_loadbuffer( L
, p_buffer
, (size_t) i_size
, uri
);
699 i_ret
= lua_pcall( L
, 0, LUA_MULTRET
, 0 );
700 vlc_stream_Delete( s
);