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