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