]> git.sesse.net Git - vlc/blob - modules/lua/vlc.c
lua playlist: add custom metadata to extra metadata instead of input item information.
[vlc] / modules / lua / vlc.c
1 /*****************************************************************************
2  * vlc.c: Generic lua interface functions
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <assert.h>
37
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_meta.h>
41 #include <vlc_charset.h>
42 #include <vlc_fs.h>
43 #include <vlc_aout.h>
44 #include <vlc_services_discovery.h>
45 #include <vlc_stream.h>
46 #include <sys/stat.h>
47
48 #include <lua.h>        /* Low level lua C API */
49 #include <lauxlib.h>    /* Higher level C API */
50 #include <lualib.h>     /* Lua libs */
51
52 #include "vlc.h"
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 #define INTF_TEXT N_("Lua interface")
58 #define INTF_LONGTEXT N_("Lua interface module to load")
59
60 #define CONFIG_TEXT N_("Lua interface configuration")
61 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
62 #define HOST_TEXT N_( "Host address" )
63 #define HOST_LONGTEXT N_( \
64     "Address and port the HTTP interface will listen on. It defaults to " \
65     "all network interfaces (0.0.0.0)." \
66     " If you want the HTTP interface to be available only on the local " \
67     "machine, enter 127.0.0.1" )
68 #define SRC_TEXT N_( "Source directory" )
69 #define SRC_LONGTEXT N_( "Source directory" )
70 #define INDEX_TEXT N_( "Directory index" )
71 #define INDEX_LONGTEXT N_( "Allow to build directory index" )
72
73 #define TELNETHOST_TEXT N_( "Host" )
74 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
75     "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
76     " If you want this interface to be available only on the local " \
77     "machine, enter \"127.0.0.1\"." )
78 #define TELNETPORT_TEXT N_( "Port" )
79 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
80     "interface will listen. It defaults to 4212." )
81 #define TELNETPWD_TEXT N_( "Password" )
82 #define TELNETPWD_LONGTEXT N_( "A single administration password is used " \
83     "to protect this interface. The default value is \"admin\"." )
84 #define TELNETPWD_DEFAULT "admin"
85 #define RCHOST_TEXT N_("TCP command input")
86 #define RCHOST_LONGTEXT N_("Accept commands over a socket rather than stdin. " \
87             "You can set the address and port the interface will bind to." )
88 #define CLIHOST_TEXT N_("CLI input")
89 #define CLIHOST_LONGTEXT N_( "Accept commands from this source. " \
90     "The CLI defaults to stdin (\"*console\"), but can also bind to a " \
91     "plain TCP socket (\"localhost:4212\") or use the telnet protocol " \
92     "(\"telnet://0.0.0.0:4212\")" )
93
94 static int vlc_sd_probe_Open( vlc_object_t * );
95
96 vlc_module_begin ()
97         set_shortname( N_("Lua Interface Module") )
98         set_description( N_("Interfaces implemented using lua scripts") )
99         add_shortcut( "luaintf" )
100         add_shortcut( "luahttp" )
101         add_shortcut( "http" )
102         add_shortcut( "luatelnet" )
103         add_shortcut( "telnet" )
104         add_shortcut( "luahotkeys" )
105         /* add_shortcut( "hotkeys" ) */
106         set_capability( "interface", 0 )
107         set_category( CAT_INTERFACE )
108         set_subcategory( SUBCAT_INTERFACE_CONTROL )
109         add_string( "lua-intf", "dummy",
110                     INTF_TEXT, INTF_LONGTEXT, false )
111         add_string( "lua-config", "",
112                     CONFIG_TEXT, CONFIG_LONGTEXT, false )
113         set_section( N_("Lua HTTP"), 0 )
114             add_string ( "http-host", NULL, HOST_TEXT, HOST_LONGTEXT, true )
115             add_string ( "http-src",  NULL, SRC_TEXT,  SRC_LONGTEXT,  true )
116             add_bool   ( "http-index", false, INDEX_TEXT, INDEX_LONGTEXT, true )
117         set_section( N_("Lua CLI"), 0 )
118             add_string( "rc-host", NULL, RCHOST_TEXT, RCHOST_LONGTEXT, true )
119             add_string( "cli-host", NULL, CLIHOST_TEXT, CLIHOST_LONGTEXT, true )
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             add_password( "telnet-password", TELNETPWD_DEFAULT, TELNETPWD_TEXT,
126                           TELNETPWD_LONGTEXT, true )
127
128         set_callbacks( Open_LuaIntf, Close_LuaIntf )
129
130     add_submodule ()
131         set_shortname( N_( "Lua Meta Fetcher" ) )
132         set_description( N_("Fetch meta data using lua scripts") )
133         set_capability( "meta fetcher", 10 )
134         set_callbacks( FetchMeta, NULL )
135
136     add_submodule ()
137         set_shortname( N_( "Lua Meta Reader" ) )
138         set_description( N_("Read meta data using lua scripts") )
139         set_capability( "meta reader", 10 )
140         set_callbacks( ReadMeta, NULL )
141
142     add_submodule ()
143         add_shortcut( "luaplaylist" )
144         set_shortname( N_("Lua Playlist") )
145         set_description( N_("Lua Playlist Parser Interface") )
146         set_capability( "demux", 2 )
147         set_callbacks( Import_LuaPlaylist, Close_LuaPlaylist )
148
149     add_submodule ()
150         set_description( N_("Lua Interface Module (shortcuts)") )
151         add_shortcut( "luacli" )
152         add_shortcut( "luarc" )
153 #ifndef WIN32
154         add_shortcut( "cli" )
155         add_shortcut( "rc" )
156 #endif
157         set_capability( "interface", 25 )
158         set_callbacks( Open_LuaIntf, Close_LuaIntf )
159
160     add_submodule ()
161         set_shortname( N_( "Lua Art" ) )
162         set_description( N_("Fetch artwork using lua scripts") )
163         set_capability( "art finder", 10 )
164         set_callbacks( FindArt, NULL )
165
166     add_submodule ()
167         set_shortname( N_("Lua Extension") )
168         add_shortcut( "luaextension" )
169         set_capability( "extension", 1 )
170         set_callbacks( Open_Extension, Close_Extension )
171
172     add_submodule ()
173         set_description( N_("Lua SD Module") )
174         add_shortcut( "luasd" )
175         set_capability( "services_discovery", 0 )
176         add_string( "lua-sd", "", NULL, NULL, false )
177             change_volatile()
178         add_string( "lua-longname", "", NULL, NULL, false )
179             change_volatile()
180         set_callbacks( Open_LuaSD, Close_LuaSD )
181
182     add_submodule ()
183         set_description( N_("Freebox TV") )
184         add_shortcut( "freebox" )
185         set_capability( "services_discovery", 0 )
186         set_callbacks( Open_LuaSD, Close_LuaSD )
187
188     add_submodule ()
189         set_description( N_("French TV") )
190         add_shortcut( "frenchtv" )
191         set_capability( "services_discovery", 0 )
192         set_callbacks( Open_LuaSD, Close_LuaSD )
193
194     VLC_SD_PROBE_SUBMODULE
195
196 vlc_module_end ()
197
198 /*****************************************************************************
199  *
200  *****************************************************************************/
201 static const char *ppsz_lua_exts[] = { ".luac", ".lua", ".vle", NULL };
202 static int file_select( const char *file )
203 {
204     int i = strlen( file );
205     int j;
206     for( j = 0; ppsz_lua_exts[j]; j++ )
207     {
208         int l = strlen( ppsz_lua_exts[j] );
209         if( i >= l && !strcmp( file+i-l, ppsz_lua_exts[j] ) )
210             return 1;
211     }
212     return 0;
213 }
214
215 static int file_compare( const char **a, const char **b )
216 {
217     return strcmp( *a, *b );
218 }
219
220 int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
221                      char ***pppsz_dir_list )
222 {
223 #define MAX_DIR_LIST_SIZE 5
224     *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
225     if (!*pppsz_dir_list)
226         return VLC_EGENERIC;
227     char **ppsz_dir_list = *pppsz_dir_list;
228
229     int i = 0;
230     char *datadir = config_GetUserDir( VLC_DATA_DIR );
231
232     if( likely(datadir != NULL)
233      && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
234                          datadir, luadirname ) != -1) )
235         i++;
236     free( datadir );
237
238 #if !(defined(__APPLE__) || defined(WIN32))
239     if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
240                          config_GetLibDir(), luadirname ) != -1) )
241             i++;
242 #endif
243
244     char *psz_datapath = config_GetDataDir( p_this );
245     if( likely(psz_datapath != NULL) )
246     {
247         if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
248                               psz_datapath, luadirname ) != -1) )
249             i++;
250
251 #if defined(__APPLE__)
252         if( likely(asprintf( &ppsz_dir_list[i],
253                              "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
254                              psz_datapath, luadirname ) != -1) )
255             i++;
256 #endif
257         free( psz_datapath );
258     }
259
260     ppsz_dir_list[i] = NULL;
261
262     assert( i < MAX_DIR_LIST_SIZE);
263
264     return VLC_SUCCESS;
265 }
266
267 void vlclua_dir_list_free( char **ppsz_dir_list )
268 {
269     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
270         free( *ppsz_dir );
271     free( ppsz_dir_list );
272 }
273
274 /*****************************************************************************
275  * Will execute func on all scripts in luadirname, and stop if func returns
276  * success.
277  *****************************************************************************/
278 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
279                                   const char * luadirname,
280                                   int (*func)(vlc_object_t *, const char *, void *),
281                                   void * user_data)
282 {
283     char **ppsz_dir_list = NULL;
284     int i_ret;
285
286     if( (i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list )) != VLC_SUCCESS)
287         return i_ret;
288
289     i_ret = VLC_EGENERIC;
290     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
291     {
292         char **ppsz_filelist;
293
294         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
295         int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
296                                    file_compare );
297         if( i_files < 0 )
298             continue;
299
300         char **ppsz_file = ppsz_filelist;
301         char **ppsz_fileend = ppsz_filelist + i_files;
302
303         while( ppsz_file < ppsz_fileend )
304         {
305             char *psz_filename;
306
307             if( asprintf( &psz_filename,
308                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
309                 psz_filename = NULL;
310             free( *(ppsz_file++) );
311
312             if( likely(psz_filename != NULL) )
313             {
314                 msg_Dbg( p_this, "Trying Lua playlist script %s",
315                          psz_filename );
316                 i_ret = func( p_this, psz_filename, user_data );
317                 free( psz_filename );
318                 if( i_ret == VLC_SUCCESS )
319                     break;
320             }
321         }
322
323         while( ppsz_file < ppsz_fileend )
324             free( *(ppsz_file++) );
325         free( ppsz_filelist );
326
327         if( i_ret == VLC_SUCCESS )
328             break;
329     }
330     vlclua_dir_list_free( ppsz_dir_list );
331     return i_ret;
332 }
333
334 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
335 {
336     char **ppsz_dir_list = NULL;
337     vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
338
339     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
340     {
341         for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
342         {
343             char *psz_filename;
344             struct stat st;
345
346             if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
347                           psz_name, *ppsz_ext ) < 0 )
348             {
349                 vlclua_dir_list_free( ppsz_dir_list );
350                 return NULL;
351             }
352
353             if( vlc_stat( psz_filename, &st ) == 0
354                 && S_ISREG( st.st_mode ) )
355             {
356                 vlclua_dir_list_free( ppsz_dir_list );
357                 return psz_filename;
358             }
359             free( psz_filename );
360         }
361     }
362     vlclua_dir_list_free( ppsz_dir_list );
363     return NULL;
364 }
365
366 /*****************************************************************************
367  * Meta data setters utility.
368  * Playlist item table should be on top of the stack when these are called
369  *****************************************************************************/
370 void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
371                               input_item_t *p_input )
372 {
373 #define TRY_META( a, b )                                        \
374     lua_getfield( L, -1, a );                                   \
375     if( lua_isstring( L, -1 ) &&                                \
376         strcmp( lua_tostring( L, -1 ), "" ) )                   \
377     {                                                           \
378         char *psz_value = strdup( lua_tostring( L, -1 ) );      \
379         EnsureUTF8( psz_value );                                \
380         msg_Dbg( p_this, #b ": %s", psz_value );                \
381         input_item_Set ## b ( p_input, psz_value );             \
382         free( psz_value );                                      \
383     }                                                           \
384     lua_pop( L, 1 ); /* pop a */
385     TRY_META( "title", Title );
386     TRY_META( "artist", Artist );
387     TRY_META( "genre", Genre );
388     TRY_META( "copyright", Copyright );
389     TRY_META( "album", Album );
390     TRY_META( "tracknum", TrackNum );
391     TRY_META( "description", Description );
392     TRY_META( "rating", Rating );
393     TRY_META( "date", Date );
394     TRY_META( "setting", Setting );
395     TRY_META( "url", URL );
396     TRY_META( "language", Language );
397     TRY_META( "nowplaying", NowPlaying );
398     TRY_META( "publisher", Publisher );
399     TRY_META( "encodedby", EncodedBy );
400     TRY_META( "arturl", ArtURL );
401     TRY_META( "trackid", TrackID );
402 }
403
404 void __vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
405                                      input_item_t *p_input )
406 {
407     /* Lock the input item and create the meta table if needed */
408     vlc_mutex_lock( &p_input->lock );
409
410     if( !p_input->p_meta )
411         p_input->p_meta = vlc_meta_New();
412
413     /* ... item */
414     lua_getfield( L, -1, "meta" );
415     /* ... item meta */
416     if( lua_istable( L, -1 ) )
417     {
418         lua_pushnil( L );
419         /* ... item meta nil */
420         while( lua_next( L, -2 ) )
421         {
422             /* ... item meta key value */
423             printf("%s => %s\n", lua_typename(L, lua_type(L, -2)),
424                    lua_typename(L, lua_type(L, -1)));
425             if( !lua_isstring( L, -2 ) || !lua_isstring( L, -1 ) )
426             {
427                 msg_Err( p_this, "'meta' keys and values must be strings");
428                 lua_pop( L, 1 ); /* pop "value" */
429                 continue;
430             }
431             const char *psz_key = lua_tostring( L, -2 );
432             const char *psz_value = lua_tostring( L, -1 );
433
434             vlc_meta_AddExtra( p_input->p_meta, psz_key, psz_value );
435
436             lua_pop( L, 1 ); /* pop "value" */
437         }
438     }
439     lua_pop( L, 1 ); /* pop "meta" */
440     /* ... item -> back to original stack */
441
442     vlc_mutex_unlock( &p_input->lock );
443 }
444
445 /*****************************************************************************
446  * Playlist utilities
447  ****************************************************************************/
448 /**
449  * Playlist item table should be on top of the stack when this is called
450  */
451 void __vlclua_read_options( vlc_object_t *p_this, lua_State *L,
452                             int *pi_options, char ***pppsz_options )
453 {
454     lua_getfield( L, -1, "options" );
455     if( lua_istable( L, -1 ) )
456     {
457         lua_pushnil( L );
458         while( lua_next( L, -2 ) )
459         {
460             if( lua_isstring( L, -1 ) )
461             {
462                 char *psz_option = strdup( lua_tostring( L, -1 ) );
463                 msg_Dbg( p_this, "Option: %s", psz_option );
464                 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
465                              psz_option );
466             }
467             else
468             {
469                 msg_Warn( p_this, "Option should be a string" );
470             }
471             lua_pop( L, 1 ); /* pop option */
472         }
473     }
474     lua_pop( L, 1 ); /* pop "options" */
475 }
476
477 int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
478                                     playlist_t *p_playlist,
479                                     input_item_t *p_parent, bool b_play )
480 {
481     int i_count = 0;
482     input_item_node_t *p_parent_node = NULL;
483
484     assert( p_parent || p_playlist );
485
486     /* playlist */
487     if( lua_istable( L, -1 ) )
488     {
489         if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
490         lua_pushnil( L );
491         /* playlist nil */
492         while( lua_next( L, -2 ) )
493         {
494             /* playlist key item */
495             /* <Parse playlist item> */
496             if( lua_istable( L, -1 ) )
497             {
498                 lua_getfield( L, -1, "path" );
499                 /* playlist key item path */
500                 if( lua_isstring( L, -1 ) )
501                 {
502                     const char   *psz_path     = NULL;
503                     const char   *psz_name     = NULL;
504                     char        **ppsz_options = NULL;
505                     int           i_options    = 0;
506                     mtime_t       i_duration   = -1;
507                     input_item_t *p_input;
508
509                     /* Read path and name */
510                     psz_path = lua_tostring( L, -1 );
511                     msg_Dbg( p_this, "Path: %s", psz_path );
512                     lua_getfield( L, -2, "name" );
513                     /* playlist key item path name */
514                     if( lua_isstring( L, -1 ) )
515                     {
516                         psz_name = lua_tostring( L, -1 );
517                         msg_Dbg( p_this, "Name: %s", psz_name );
518                     }
519                     else
520                     {
521                         if( !lua_isnil( L, -1 ) )
522                             msg_Warn( p_this, "Playlist item name should be a string." );
523                         psz_name = psz_path;
524                     }
525
526                     /* Read duration */
527                     lua_getfield( L, -3, "duration" );
528                     /* playlist key item path name duration */
529                     if( lua_isnumber( L, -1 ) )
530                     {
531                         i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
532                     }
533                     else if( !lua_isnil( L, -1 ) )
534                     {
535                         msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
536                     }
537                     lua_pop( L, 1 ); /* pop "duration" */
538
539                     /* playlist key item path name */
540
541                     /* Read options: item must be on top of stack */
542                     lua_pushvalue( L, -3 );
543                     /* playlist key item path name item */
544                     vlclua_read_options( p_this, L, &i_options, &ppsz_options );
545
546                     /* Create input item */
547                     p_input = input_item_NewExt( p_playlist, psz_path,
548                                                 psz_name, i_options,
549                                                 (const char **)ppsz_options,
550                                                 VLC_INPUT_OPTION_TRUSTED,
551                                                 i_duration );
552                     lua_pop( L, 3 ); /* pop "path name item" */
553                     /* playlist key item */
554
555                     /* Read meta data: item must be on top of stack */
556                     vlclua_read_meta_data( p_this, L, p_input );
557
558                     /* Read custom meta data: item must be on top of stack*/
559                     vlclua_read_custom_meta_data( p_this, L, p_input );
560
561                     /* Append item to playlist */
562                     if( p_parent ) /* Add to node */
563                     {
564                         input_item_CopyOptions( p_parent, p_input );
565                         input_item_node_AppendItem( p_parent_node, p_input );
566                     }
567                     else /* Play or Enqueue (preparse) */
568                         /* FIXME: playlist_AddInput() can fail */
569                         playlist_AddInput( p_playlist, p_input,
570                                PLAYLIST_APPEND |
571                                ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
572                                PLAYLIST_END, true, false );
573                     i_count ++; /* increment counter */
574                     vlc_gc_decref( p_input );
575                     while( i_options > 0 )
576                         free( ppsz_options[--i_options] );
577                     free( ppsz_options );
578                 }
579                 else
580                 {
581                     lua_pop( L, 1 ); /* pop "path" */
582                     msg_Warn( p_this,
583                              "Playlist item's path should be a string" );
584                 }
585                 /* playlist key item */
586             }
587             else
588             {
589                 msg_Warn( p_this, "Playlist item should be a table" );
590             }
591             /* <Parse playlist item> */
592             lua_pop( L, 1 ); /* pop the value, keep the key for
593                               * the next lua_next() call */
594             /* playlist key */
595         }
596         /* playlist */
597         if( p_parent )
598         {
599             if( i_count ) input_item_node_PostAndDelete( p_parent_node );
600             else input_item_node_Delete( p_parent_node );
601         }
602     }
603     else
604     {
605         msg_Warn( p_this, "Playlist should be a table." );
606     }
607     return i_count;
608 }
609
610 static int vlc_sd_probe_Open( vlc_object_t *obj )
611 {
612     vlc_probe_t *probe = (vlc_probe_t *)obj;
613     char **ppsz_filelist = NULL;
614     char **ppsz_fileend  = NULL;
615     char **ppsz_file;
616     char *psz_name;
617     char **ppsz_dir_list = NULL;
618     char **ppsz_dir;
619     lua_State *L = NULL;
620     vlclua_dir_list( obj, "sd", &ppsz_dir_list );
621     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
622     {
623         int i_files;
624         if( ppsz_filelist )
625         {
626             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
627                  ppsz_file++ )
628                 free( *ppsz_file );
629             free( ppsz_filelist );
630             ppsz_filelist = NULL;
631         }
632         i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
633                                 file_compare );
634         if( i_files < 1 ) continue;
635         ppsz_fileend = ppsz_filelist + i_files;
636         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
637         {
638             char  *psz_filename;
639             if( asprintf( &psz_filename,
640                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
641             {
642                 goto error;
643             }
644             L = luaL_newstate();
645             if( !L )
646             {
647                 msg_Err( probe, "Could not create new Lua State" );
648                 free( psz_filename );
649                 goto error;
650             }
651             luaL_openlibs( L );
652             if( vlclua_add_modules_path( probe, L, psz_filename ) )
653             {
654                 msg_Err( probe, "Error while setting the module search path for %s",
655                           psz_filename );
656                 free( psz_filename );
657                 goto error;
658             }
659             if( luaL_dofile( L, psz_filename ) )
660             {
661
662                 msg_Err( probe, "Error loading script %s: %s", psz_filename,
663                           lua_tostring( L, lua_gettop( L ) ) );
664                 lua_pop( L, 1 );
665                 free( psz_filename );
666                 lua_close( L );
667                 continue;
668             }
669             char *psz_longname;
670             char *temp = strchr( *ppsz_file, '.' );
671             if( temp )
672                 *temp = '\0';
673             lua_getglobal( L, "descriptor" );
674             if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
675             {
676                 msg_Warn( probe, "No 'descriptor' function in '%s'", psz_filename );
677                 lua_pop( L, 1 );
678                 if( !( psz_longname = strdup( *ppsz_file ) ) )
679                 {
680                     free( psz_filename );
681                     goto error;
682                 }
683             }
684             else
685             {
686                 lua_getfield( L, -1, "title" );
687                 if( !lua_isstring( L, -1 ) ||
688                     !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
689                 {
690                     free( psz_filename );
691                     goto error;
692                 }
693             }
694
695             char *psz_file_esc = config_StringEscape( *ppsz_file );
696             char *psz_longname_esc = config_StringEscape( psz_longname );
697             if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
698                           psz_file_esc, psz_longname_esc ) < 0 )
699             {
700                 free( psz_file_esc );
701                 free( psz_longname_esc );
702                 free( psz_filename );
703                 free( psz_longname );
704                 goto error;
705             }
706             free( psz_file_esc );
707             free( psz_longname_esc );
708             vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
709             free( psz_name );
710             free( psz_longname );
711             free( psz_filename );
712             lua_close( L );
713         }
714     }
715     if( ppsz_filelist )
716     {
717         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
718              ppsz_file++ )
719             free( *ppsz_file );
720         free( ppsz_filelist );
721     }
722     vlclua_dir_list_free( ppsz_dir_list );
723     return VLC_PROBE_CONTINUE;
724 error:
725     if( ppsz_filelist )
726     {
727         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
728              ppsz_file++ )
729             free( *ppsz_file );
730         free( ppsz_filelist );
731     }
732     if( L )
733         lua_close( L );
734     vlclua_dir_list_free( ppsz_dir_list );
735     return VLC_ENOMEM;
736 }
737
738 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
739 {
740     int count = 0;
741     for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
742     {
743         lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
744                          psz_path, *ppsz_ext );
745         count ++;
746     }
747
748     return count;
749 }
750
751 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
752 {
753     /* Setup the module search path:
754      *   * "The script's directory"/modules
755      *   * "The script's parent directory"/modules
756      *   * and so on for all the next directories in the directory list
757      */
758     char *psz_path = strdup( psz_filename );
759     if( !psz_path )
760         return 1;
761
762     char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
763     if( !psz_char )
764     {
765         free( psz_path );
766         return 1;
767     }
768     *psz_char = '\0';
769
770     /* psz_path now holds the file's directory */
771     psz_char = strrchr( psz_path, DIR_SEP_CHAR );
772     if( !psz_char )
773     {
774         free( psz_path );
775         return 1;
776     }
777     *psz_char = '\0';
778
779     /* Push package on stack */
780     int count = 0;
781     lua_getglobal( L, "package" );
782
783     /* psz_path now holds the file's parent directory */
784     count += vlclua_add_modules_path_inner( L, psz_path );
785     *psz_char = DIR_SEP_CHAR;
786
787     /* psz_path now holds the file's directory */
788     count += vlclua_add_modules_path_inner( L, psz_path );
789
790     char **ppsz_dir_list = NULL;
791     vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
792     char **ppsz_dir = ppsz_dir_list;
793
794     for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
795     free( psz_path );
796
797     for( ; *ppsz_dir; ppsz_dir++ )
798     {
799         psz_path = *ppsz_dir;
800         /* FIXME: doesn't work well with meta/... modules due to the double
801          * directory depth */
802         psz_char = strrchr( psz_path, DIR_SEP_CHAR );
803         if( !psz_char )
804         {
805             vlclua_dir_list_free( ppsz_dir_list );
806             return 1;
807         }
808
809         *psz_char = '\0';
810         count += vlclua_add_modules_path_inner( L, psz_path );
811         *psz_char = DIR_SEP_CHAR;
812         count += vlclua_add_modules_path_inner( L, psz_path );
813     }
814
815     lua_getfield( L, -(count+1), "path" ); /* Get package.path */
816     lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
817     lua_setfield( L, -2, "path"); /* Set package.path */
818     lua_pop( L, 1 ); /* Pop the package module */
819
820     vlclua_dir_list_free( ppsz_dir_list );
821     return 0;
822 }
823
824 /** Replacement for luaL_dofile, using VLC's input capabilities */
825 int vlclua_dofile( vlc_object_t *p_this, lua_State *L, const char *uri )
826 {
827     if( !strstr( uri, "://" ) )
828         return luaL_dofile( L, uri );
829     if( !strncasecmp( uri, "file://", 7 ) )
830         return luaL_dofile( L, uri + 7 );
831     stream_t *s = stream_UrlNew( p_this, uri );
832     if( !s )
833     {
834         return 1;
835     }
836     int64_t i_size = stream_Size( s );
837     char *p_buffer = ( i_size > 0 ) ? malloc( i_size ) : NULL;
838     if( !p_buffer )
839     {
840         // FIXME: read the whole stream until we reach the end (if no size)
841         stream_Delete( s );
842         return 1;
843     }
844     int64_t i_read = stream_Read( s, p_buffer, (int) i_size );
845     int i_ret = ( i_read == i_size ) ? 0 : 1;
846     if( !i_ret )
847         i_ret = luaL_loadbuffer( L, p_buffer, (size_t) i_size, uri );
848     if( !i_ret )
849         i_ret = lua_pcall( L, 0, LUA_MULTRET, 0 );
850     stream_Delete( s );
851     free( p_buffer );
852     return i_ret;
853 }