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 *****************************************************************************/
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
41 #include <vlc_charset.h>
44 #include <vlc_services_discovery.h>
47 #include <lua.h> /* Low level lua C API */
48 #include <lauxlib.h> /* Higher level C API */
49 #include <lualib.h> /* Lua libs */
53 /*****************************************************************************
55 *****************************************************************************/
56 #define INTF_TEXT N_("Lua interface")
57 #define INTF_LONGTEXT N_("Lua interface module to load")
59 #define CONFIG_TEXT N_("Lua interface configuration")
60 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
61 #define HOST_TEXT N_( "Host address" )
62 #define HOST_LONGTEXT N_( \
63 "Address and port the HTTP interface will listen on. It defaults to " \
64 "all network interfaces (0.0.0.0)." \
65 " If you want the HTTP interface to be available only on the local " \
66 "machine, enter 127.0.0.1" )
67 #define SRC_TEXT N_( "Source directory" )
68 #define SRC_LONGTEXT N_( "Source directory" )
69 #define INDEX_TEXT N_( "Directory index" )
70 #define INDEX_LONGTEXT N_( "Allow to build directory index" )
72 static int vlc_sd_probe_Open( vlc_object_t * );
75 set_shortname( N_("Lua Interface Module") )
76 set_description( N_("Interfaces implemented using lua scripts") )
77 add_shortcut( "luaintf" )
78 add_shortcut( "luahttp" )
79 /* add_shortcut( "http" ) */
80 add_shortcut( "luatelnet" )
81 add_shortcut( "telnet" )
82 add_shortcut( "luahotkeys" )
83 /* add_shortcut( "hotkeys" ) */
84 set_capability( "interface", 0 )
85 set_category( CAT_INTERFACE )
86 set_subcategory( SUBCAT_INTERFACE_CONTROL )
87 add_string( "lua-intf", "dummy", NULL,
88 INTF_TEXT, INTF_LONGTEXT, false )
89 add_string( "lua-config", "", NULL,
90 CONFIG_TEXT, CONFIG_LONGTEXT, false )
91 set_section( N_("Lua HTTP" ), 0 )
92 add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true )
93 add_string ( "http-src", NULL, NULL, SRC_TEXT, SRC_LONGTEXT, true )
94 add_bool ( "http-index", false, NULL, INDEX_TEXT, INDEX_LONGTEXT, true )
95 set_callbacks( Open_LuaIntf, Close_LuaIntf )
98 set_shortname( N_( "Lua Meta Fetcher" ) )
99 set_description( N_("Fetch meta data using lua scripts") )
100 set_capability( "meta fetcher", 10 )
101 set_callbacks( FetchMeta, NULL )
104 set_shortname( N_( "Lua Meta Reader" ) )
105 set_description( N_("Read meta data using lua scripts") )
106 set_capability( "meta reader", 10 )
107 set_callbacks( ReadMeta, NULL )
110 add_shortcut( "luaplaylist" )
111 set_shortname( N_("Lua Playlist") )
112 set_description( N_("Lua Playlist Parser Interface") )
113 set_capability( "demux", 2 )
114 set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
117 set_description( N_("Lua Interface Module (shortcuts)") )
118 add_shortcut( "luarc" )
120 set_capability( "interface", 25 )
121 set_callbacks( Open_LuaIntf, Close_LuaIntf )
124 set_shortname( N_( "Lua Art" ) )
125 set_description( N_("Fetch artwork using lua scripts") )
126 set_capability( "art finder", 10 )
127 set_callbacks( FindArt, NULL )
130 set_shortname( N_("Lua Extension") )
131 add_shortcut( "luaextension" )
132 set_capability( "extension", 1 )
133 set_callbacks( Open_Extension, Close_Extension )
136 set_description( N_("Lua SD Module") )
137 add_shortcut( "luasd" )
138 set_capability( "services_discovery", 0 )
139 add_string( "lua-sd", "", NULL, NULL, NULL, false )
141 add_string( "lua-longname", "", NULL, NULL, NULL, false )
143 set_callbacks( Open_LuaSD, Close_LuaSD )
146 set_description( N_("Freebox TV") )
147 add_shortcut( "freebox" )
148 set_capability( "services_discovery", 0 )
149 set_callbacks( Open_LuaSD, Close_LuaSD )
152 set_description( N_("French TV") )
153 add_shortcut( "frenchtv" )
154 set_capability( "services_discovery", 0 )
155 set_callbacks( Open_LuaSD, Close_LuaSD )
157 VLC_SD_PROBE_SUBMODULE
161 /*****************************************************************************
163 *****************************************************************************/
164 static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
165 static int file_select( const char *file )
167 int i = strlen( file );
169 for( j = 0; ppsz_lua_exts[j]; j++ )
171 int l = strlen( ppsz_lua_exts[j] );
172 if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
178 static int file_compare( const char **a, const char **b )
180 return strcmp( *a, *b );
183 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
184 char ***pppsz_dir_list )
186 #define MAX_DIR_LIST_SIZE 5
187 *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
188 if (!*pppsz_dir_list)
190 char **ppsz_dir_list = *pppsz_dir_list;
193 char *datadir = config_GetUserDir( VLC_DATA_DIR );
195 if( likely(datadir != NULL)
196 && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
197 datadir, luadirname ) != -1) )
201 #if !(defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32))
202 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
203 config_GetLibDir(), luadirname ) != -1) )
207 char *psz_datapath = config_GetDataDir( p_this );
208 if( likely(psz_datapath != NULL) )
210 if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
211 psz_datapath, luadirname ) != -1) )
214 #if defined(__APPLE__) || defined(SYS_BEOS)
215 if( likely(asprintf( &ppsz_dir_list[i],
216 "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
217 psz_datapath, luadirname ) != -1) )
220 free( psz_datapath );
223 ppsz_dir_list[i] = NULL;
225 assert( i < MAX_DIR_LIST_SIZE);
230 void vlclua_dir_list_free( char **ppsz_dir_list )
233 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
235 free( ppsz_dir_list );
238 /*****************************************************************************
239 * Will execute func on all scripts in luadirname, and stop if func returns
241 *****************************************************************************/
242 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
243 const char * luadirname,
244 int (*func)(vlc_object_t *, const char *, void *),
247 char **ppsz_dir_list = NULL;
249 int i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list );
250 if( i_ret != VLC_SUCCESS )
252 i_ret = VLC_EGENERIC;
254 for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
256 char **ppsz_filelist;
259 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
260 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
265 char **ppsz_file = ppsz_filelist;
266 char **ppsz_fileend = ppsz_filelist + i_files;
268 while( ppsz_file < ppsz_fileend )
272 if( asprintf( &psz_filename,
273 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
275 free( *(ppsz_file++) );
277 if( likely(psz_filename != NULL) )
279 msg_Dbg( p_this, "Trying Lua playlist script %s",
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( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
301 char **ppsz_dir_list = NULL;
303 vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
304 for( 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 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
336 input_item_t *p_input )
338 #define TRY_META( a, b ) \
339 lua_getfield( L, -1, a ); \
340 if( lua_isstring( L, -1 ) && \
341 strcmp( lua_tostring( L, -1 ), "" ) ) \
343 char *psz_value = strdup( lua_tostring( L, -1 ) ); \
344 EnsureUTF8( psz_value ); \
345 msg_Dbg( p_this, #b ": %s", psz_value ); \
346 input_item_Set ## b ( p_input, psz_value ); \
349 lua_pop( L, 1 ); /* pop a */
350 TRY_META( "title", Title );
351 TRY_META( "artist", Artist );
352 TRY_META( "genre", Genre );
353 TRY_META( "copyright", Copyright );
354 TRY_META( "album", Album );
355 TRY_META( "tracknum", TrackNum );
356 TRY_META( "description", Description );
357 TRY_META( "rating", Rating );
358 TRY_META( "date", Date );
359 TRY_META( "setting", Setting );
360 TRY_META( "url", URL );
361 TRY_META( "language", Language );
362 TRY_META( "nowplaying", NowPlaying );
363 TRY_META( "publisher", Publisher );
364 TRY_META( "encodedby", EncodedBy );
365 TRY_META( "arturl", ArtURL );
366 TRY_META( "trackid", TrackID );
369 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
370 input_item_t *p_input )
373 lua_getfield( L, -1, "meta" );
375 if( lua_istable( L, -1 ) )
378 /* ... item meta nil */
379 while( lua_next( L, -2 ) )
381 /* ... item meta key value */
382 if( !lua_isstring( L, -2 ) )
384 msg_Warn( p_this, "Custom meta data category name must be "
387 else if( !lua_istable( L, -1 ) )
389 msg_Warn( p_this, "Custom meta data category contents "
394 const char *psz_meta_category = lua_tostring( L, -2 );
395 msg_Dbg( p_this, "Found custom meta data category: %s",
398 /* ... item meta key value nil */
399 while( lua_next( L, -2 ) )
401 /* ... item meta key value key2 value2 */
402 if( !lua_isstring( L, -2 ) )
404 msg_Warn( p_this, "Custom meta category item name "
405 "must be a string." );
407 else if( !lua_isstring( L, -1 ) )
409 msg_Warn( p_this, "Custom meta category item value "
410 "must be a string." );
414 const char *psz_meta_name =
415 lua_tostring( L, -2 );
416 const char *psz_meta_value =
417 lua_tostring( L, -1 );
418 msg_Dbg( p_this, "Custom meta %s, %s: %s",
419 psz_meta_category, psz_meta_name,
421 input_item_AddInfo( p_input, psz_meta_category,
422 psz_meta_name, "%s", psz_meta_value );
424 lua_pop( L, 1 ); /* pop item */
425 /* ... item meta key value key2 */
427 /* ... item meta key value */
429 lua_pop( L, 1 ); /* pop category */
430 /* ... item meta key */
434 lua_pop( L, 1 ); /* pop "meta" */
435 /* ... item -> back to original stack */
438 /*****************************************************************************
440 ****************************************************************************/
442 * Playlist item table should be on top of the stack when this is called
444 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
445 int *pi_options, char ***pppsz_options )
447 lua_getfield( L, -1, "options" );
448 if( lua_istable( L, -1 ) )
451 while( lua_next( L, -2 ) )
453 if( lua_isstring( L, -1 ) )
455 char *psz_option = strdup( lua_tostring( L, -1 ) );
456 msg_Dbg( p_this, "Option: %s", psz_option );
457 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
462 msg_Warn( p_this, "Option should be a string" );
464 lua_pop( L, 1 ); /* pop option */
467 lua_pop( L, 1 ); /* pop "options" */
470 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
471 playlist_t *p_playlist,
472 input_item_t *p_parent, bool b_play )
475 input_item_node_t *p_parent_node = NULL;
477 assert( p_parent || p_playlist );
480 if( lua_istable( L, -1 ) )
482 if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
485 while( lua_next( L, -2 ) )
487 /* playlist key item */
488 /* <Parse playlist item> */
489 if( lua_istable( L, -1 ) )
491 lua_getfield( L, -1, "path" );
492 /* playlist key item path */
493 if( lua_isstring( L, -1 ) )
495 const char *psz_path = NULL;
496 const char *psz_name = NULL;
497 char **ppsz_options = NULL;
499 mtime_t i_duration = -1;
500 input_item_t *p_input;
502 /* Read path and name */
503 psz_path = lua_tostring( L, -1 );
504 msg_Dbg( p_this, "Path: %s", psz_path );
505 lua_getfield( L, -2, "name" );
506 /* playlist key item path name */
507 if( lua_isstring( L, -1 ) )
509 psz_name = lua_tostring( L, -1 );
510 msg_Dbg( p_this, "Name: %s", psz_name );
514 if( !lua_isnil( L, -1 ) )
515 msg_Warn( p_this, "Playlist item name should be a string." );
520 lua_getfield( L, -3, "duration" );
521 /* playlist key item path name duration */
522 if( lua_isnumber( L, -1 ) )
524 i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
526 else if( !lua_isnil( L, -1 ) )
528 msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
530 lua_pop( L, 1 ); /* pop "duration" */
532 /* playlist key item path name */
534 /* Read options: item must be on top of stack */
535 lua_pushvalue( L, -3 );
536 /* playlist key item path name item */
537 vlclua_read_options( p_this, L, &i_options, &ppsz_options );
539 /* Create input item */
540 p_input = input_item_NewExt( p_playlist, psz_path,
542 (const char **)ppsz_options,
543 VLC_INPUT_OPTION_TRUSTED,
545 lua_pop( L, 3 ); /* pop "path name item" */
546 /* playlist key item */
548 /* Read meta data: item must be on top of stack */
549 vlclua_read_meta_data( p_this, L, p_input );
551 /* Read custom meta data: item must be on top of stack*/
552 vlclua_read_custom_meta_data( p_this, L, p_input );
554 /* Append item to playlist */
555 if( p_parent ) /* Add to node */
557 input_item_node_AppendItem( p_parent_node, p_input );
559 else /* Play or Enqueue (preparse) */
560 /* FIXME: playlist_AddInput() can fail */
561 playlist_AddInput( p_playlist, p_input,
563 ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
564 PLAYLIST_END, true, false );
565 i_count ++; /* increment counter */
566 vlc_gc_decref( p_input );
567 while( i_options > 0 )
568 free( ppsz_options[--i_options] );
569 free( ppsz_options );
573 lua_pop( L, 1 ); /* pop "path" */
575 "Playlist item's path should be a string" );
577 /* playlist key item */
581 msg_Warn( p_this, "Playlist item should be a table" );
583 /* <Parse playlist item> */
584 lua_pop( L, 1 ); /* pop the value, keep the key for
585 * the next lua_next() call */
591 if( i_count ) input_item_node_PostAndDelete( p_parent_node );
592 else input_item_node_Delete( p_parent_node );
597 msg_Warn( p_this, "Playlist should be a table." );
602 static int vlc_sd_probe_Open( vlc_object_t *obj )
604 vlc_probe_t *probe = (vlc_probe_t *)obj;
605 char **ppsz_filelist = NULL;
606 char **ppsz_fileend = NULL;
609 char **ppsz_dir_list = NULL;
612 vlclua_dir_list( obj, "sd", &ppsz_dir_list );
613 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
618 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
621 free( ppsz_filelist );
622 ppsz_filelist = NULL;
624 i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
626 if( i_files < 1 ) continue;
627 ppsz_fileend = ppsz_filelist + i_files;
628 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
631 if( asprintf( &psz_filename,
632 "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
639 msg_Err( probe, "Could not create new Lua State" );
640 free( psz_filename );
644 if( vlclua_add_modules_path( probe, L, psz_filename ) )
646 msg_Err( probe, "Error while setting the module search path for %s",
648 free( psz_filename );
651 if( luaL_dofile( L, psz_filename ) )
654 msg_Err( probe, "Error loading script %s: %s", psz_filename,
655 lua_tostring( L, lua_gettop( L ) ) );
657 free( psz_filename );
662 char *temp = strchr( *ppsz_file, '.' );
665 lua_getglobal( L, "descriptor" );
666 if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
669 if( !( psz_longname = strdup( *ppsz_file ) ) )
671 free( psz_filename );
677 lua_getfield( L, -1, "title" );
678 if( !lua_isstring( L, -1 ) ||
679 !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
681 free( psz_filename );
686 char *psz_file_esc = config_StringEscape( *ppsz_file );
687 char *psz_longname_esc = config_StringEscape( psz_longname );
688 if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
689 psz_file_esc, psz_longname_esc ) < 0 )
691 free( psz_file_esc );
692 free( psz_longname_esc );
693 free( psz_filename );
694 free( psz_longname );
697 free( psz_file_esc );
698 free( psz_longname_esc );
699 vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
701 free( psz_longname );
702 free( psz_filename );
708 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
711 free( ppsz_filelist );
713 vlclua_dir_list_free( ppsz_dir_list );
714 return VLC_PROBE_CONTINUE;
718 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
721 free( ppsz_filelist );
725 vlclua_dir_list_free( ppsz_dir_list );
729 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
732 for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
734 lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
735 psz_path, *ppsz_ext );
742 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
744 /* Setup the module search path:
745 * * "The script's directory"/modules
746 * * "The script's parent directory"/modules
747 * * and so on for all the next directories in the directory list
749 char *psz_path = strdup( psz_filename );
753 char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
761 /* psz_path now holds the file's directory */
762 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
770 /* Push package on stack */
772 lua_getglobal( L, "package" );
774 /* psz_path now holds the file's parent directory */
775 count += vlclua_add_modules_path_inner( L, psz_path );
776 *psz_char = DIR_SEP_CHAR;
778 /* psz_path now holds the file's directory */
779 count += vlclua_add_modules_path_inner( L, psz_path );
781 char **ppsz_dir_list = NULL;
782 vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
783 char **ppsz_dir = ppsz_dir_list;
785 for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
788 for( ; *ppsz_dir; ppsz_dir++ )
790 psz_path = *ppsz_dir;
791 /* FIXME: doesn't work well with meta/... modules due to the double
793 psz_char = strrchr( psz_path, DIR_SEP_CHAR );
796 vlclua_dir_list_free( ppsz_dir_list );
801 count += vlclua_add_modules_path_inner( L, psz_path );
802 *psz_char = DIR_SEP_CHAR;
803 count += vlclua_add_modules_path_inner( L, psz_path );
806 lua_getfield( L, -(count+1), "path" ); /* Get package.path */
807 lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
808 lua_setfield( L, -2, "path"); /* Set package.path */
809 lua_pop( L, 1 ); /* Pop the package module */
811 vlclua_dir_list_free( ppsz_dir_list );