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