]> git.sesse.net Git - vlc/blob - modules/lua/vlc.c
3e3047b34e068f5db3114638b9150096d2f14600
[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_("Command-line 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     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( vlc_object_t *p_this, const char *luadirname,
202                      char ***pppsz_dir_list )
203 {
204 #define MAX_DIR_LIST_SIZE 5
205     *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
206     if (!*pppsz_dir_list)
207         return VLC_EGENERIC;
208     char **ppsz_dir_list = *pppsz_dir_list;
209
210     int i = 0;
211     char *datadir = config_GetUserDir( VLC_DATA_DIR );
212
213     if( likely(datadir != NULL)
214      && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
215                          datadir, luadirname ) != -1) )
216         i++;
217     free( datadir );
218
219 #if !(defined(__APPLE__) || defined(WIN32) || defined(__OS2__))
220     char *psz_libpath = config_GetLibDir();
221     if( likely(psz_libpath != NULL) )
222     {
223         if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
224                              psz_libpath, luadirname ) != -1) )
225             i++;
226         free( psz_libpath );
227     }
228 #endif
229
230     char *psz_datapath = config_GetDataDir( p_this );
231     if( likely(psz_datapath != NULL) )
232     {
233         if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
234                               psz_datapath, luadirname ) != -1) )
235             i++;
236
237 #if defined(__APPLE__)
238         if( likely(asprintf( &ppsz_dir_list[i],
239                              "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
240                              psz_datapath, luadirname ) != -1) )
241             i++;
242 #endif
243         free( psz_datapath );
244     }
245
246     ppsz_dir_list[i] = NULL;
247
248     assert( i < MAX_DIR_LIST_SIZE);
249
250     return VLC_SUCCESS;
251 }
252
253 void vlclua_dir_list_free( char **ppsz_dir_list )
254 {
255     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
256         free( *ppsz_dir );
257     free( ppsz_dir_list );
258 }
259
260 /*****************************************************************************
261  * Will execute func on all scripts in luadirname, and stop if func returns
262  * success.
263  *****************************************************************************/
264 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
265                                   const char * luadirname,
266                                   int (*func)(vlc_object_t *, const char *, void *),
267                                   void * user_data)
268 {
269     char **ppsz_dir_list = NULL;
270     int i_ret;
271
272     if( (i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list )) != VLC_SUCCESS)
273         return i_ret;
274
275     i_ret = VLC_EGENERIC;
276     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
277     {
278         char **ppsz_filelist;
279
280         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
281         int i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
282                                    file_compare );
283         if( i_files < 0 )
284             continue;
285
286         char **ppsz_file = ppsz_filelist;
287         char **ppsz_fileend = ppsz_filelist + i_files;
288
289         while( ppsz_file < ppsz_fileend )
290         {
291             char *psz_filename;
292
293             if( asprintf( &psz_filename,
294                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
295                 psz_filename = NULL;
296             free( *(ppsz_file++) );
297
298             if( likely(psz_filename != NULL) )
299             {
300                 msg_Dbg( p_this, "Trying Lua playlist script %s",
301                          psz_filename );
302                 i_ret = func( p_this, psz_filename, user_data );
303                 free( psz_filename );
304                 if( i_ret == VLC_SUCCESS )
305                     break;
306             }
307         }
308
309         while( ppsz_file < ppsz_fileend )
310             free( *(ppsz_file++) );
311         free( ppsz_filelist );
312
313         if( i_ret == VLC_SUCCESS )
314             break;
315     }
316     vlclua_dir_list_free( ppsz_dir_list );
317     return i_ret;
318 }
319
320 char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
321 {
322     char **ppsz_dir_list = NULL;
323     vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
324
325     for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
326     {
327         for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
328         {
329             char *psz_filename;
330             struct stat st;
331
332             if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
333                           psz_name, *ppsz_ext ) < 0 )
334             {
335                 vlclua_dir_list_free( ppsz_dir_list );
336                 return NULL;
337             }
338
339             if( vlc_stat( psz_filename, &st ) == 0
340                 && S_ISREG( st.st_mode ) )
341             {
342                 vlclua_dir_list_free( ppsz_dir_list );
343                 return psz_filename;
344             }
345             free( psz_filename );
346         }
347     }
348     vlclua_dir_list_free( ppsz_dir_list );
349     return NULL;
350 }
351
352 /*****************************************************************************
353  * Meta data setters utility.
354  * Playlist item table should be on top of the stack when these are called
355  *****************************************************************************/
356 #undef vlclua_read_meta_data
357 void vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
358                             input_item_t *p_input )
359 {
360 #define TRY_META( a, b )                                        \
361     lua_getfield( L, -1, a );                                   \
362     if( lua_isstring( L, -1 ) &&                                \
363         strcmp( lua_tostring( L, -1 ), "" ) )                   \
364     {                                                           \
365         char *psz_value = strdup( lua_tostring( L, -1 ) );      \
366         EnsureUTF8( psz_value );                                \
367         msg_Dbg( p_this, #b ": %s", psz_value );                \
368         input_item_Set ## b ( p_input, psz_value );             \
369         free( psz_value );                                      \
370     }                                                           \
371     lua_pop( L, 1 ); /* pop a */
372     TRY_META( "title", Title );
373     TRY_META( "artist", Artist );
374     TRY_META( "genre", Genre );
375     TRY_META( "copyright", Copyright );
376     TRY_META( "album", Album );
377     TRY_META( "tracknum", TrackNum );
378     TRY_META( "description", Description );
379     TRY_META( "rating", Rating );
380     TRY_META( "date", Date );
381     TRY_META( "setting", Setting );
382     TRY_META( "url", URL );
383     TRY_META( "language", Language );
384     TRY_META( "nowplaying", NowPlaying );
385     TRY_META( "publisher", Publisher );
386     TRY_META( "encodedby", EncodedBy );
387     TRY_META( "arturl", ArtURL );
388     TRY_META( "trackid", TrackID );
389 }
390
391 #undef vlclua_read_custom_meta_data
392 void vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *L,
393                                      input_item_t *p_input )
394 {
395     /* Lock the input item and create the meta table if needed */
396     vlc_mutex_lock( &p_input->lock );
397
398     if( !p_input->p_meta )
399         p_input->p_meta = vlc_meta_New();
400
401     /* ... item */
402     lua_getfield( L, -1, "meta" );
403     /* ... item meta */
404     if( lua_istable( L, -1 ) )
405     {
406         lua_pushnil( L );
407         /* ... item meta nil */
408         while( lua_next( L, -2 ) )
409         {
410             /* ... item meta key value */
411             if( !lua_isstring( L, -2 ) || !lua_isstring( L, -1 ) )
412             {
413                 msg_Err( p_this, "'meta' keys and values must be strings");
414                 lua_pop( L, 1 ); /* pop "value" */
415                 continue;
416             }
417             const char *psz_key = lua_tostring( L, -2 );
418             const char *psz_value = lua_tostring( L, -1 );
419
420             vlc_meta_AddExtra( p_input->p_meta, psz_key, psz_value );
421
422             lua_pop( L, 1 ); /* pop "value" */
423         }
424     }
425     lua_pop( L, 1 ); /* pop "meta" */
426     /* ... item -> back to original stack */
427
428     vlc_mutex_unlock( &p_input->lock );
429 }
430
431 /*****************************************************************************
432  * Playlist utilities
433  ****************************************************************************/
434 /**
435  * Playlist item table should be on top of the stack when this is called
436  */
437 #undef vlclua_read_options
438 void vlclua_read_options( vlc_object_t *p_this, lua_State *L,
439                             int *pi_options, char ***pppsz_options )
440 {
441     lua_getfield( L, -1, "options" );
442     if( lua_istable( L, -1 ) )
443     {
444         lua_pushnil( L );
445         while( lua_next( L, -2 ) )
446         {
447             if( lua_isstring( L, -1 ) )
448             {
449                 char *psz_option = strdup( lua_tostring( L, -1 ) );
450                 msg_Dbg( p_this, "Option: %s", psz_option );
451                 INSERT_ELEM( *pppsz_options, *pi_options, *pi_options,
452                              psz_option );
453             }
454             else
455             {
456                 msg_Warn( p_this, "Option should be a string" );
457             }
458             lua_pop( L, 1 ); /* pop option */
459         }
460     }
461     lua_pop( L, 1 ); /* pop "options" */
462 }
463
464 #undef vlclua_playlist_add_internal
465 int vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
466                                     playlist_t *p_playlist,
467                                     input_item_t *p_parent, bool b_play )
468 {
469     int i_count = 0;
470     input_item_node_t *p_parent_node = NULL;
471
472     assert( p_parent || p_playlist );
473
474     /* playlist */
475     if( lua_istable( L, -1 ) )
476     {
477         if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
478         lua_pushnil( L );
479         /* playlist nil */
480         while( lua_next( L, -2 ) )
481         {
482             /* playlist key item */
483             /* <Parse playlist item> */
484             if( lua_istable( L, -1 ) )
485             {
486                 lua_getfield( L, -1, "path" );
487                 /* playlist key item path */
488                 if( lua_isstring( L, -1 ) )
489                 {
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                     psz_path = lua_tostring( L, -1 );
500                     msg_Dbg( p_this, "Path: %s", psz_path );
501                     lua_getfield( L, -2, "name" );
502                     /* playlist key item path name */
503                     if( lua_isstring( L, -1 ) )
504                     {
505                         psz_name = lua_tostring( L, -1 );
506                         msg_Dbg( p_this, "Name: %s", psz_name );
507                     }
508                     else
509                     {
510                         if( !lua_isnil( L, -1 ) )
511                             msg_Warn( p_this, "Playlist item name should be a string." );
512                         psz_name = NULL;
513                     }
514
515                     /* Read duration */
516                     lua_getfield( L, -3, "duration" );
517                     /* playlist key item path name duration */
518                     if( lua_isnumber( L, -1 ) )
519                     {
520                         i_duration = (mtime_t)(lua_tonumber( L, -1 )*1e6);
521                     }
522                     else if( !lua_isnil( L, -1 ) )
523                     {
524                         msg_Warn( p_this, "Playlist item duration should be a number (in seconds)." );
525                     }
526                     lua_pop( L, 1 ); /* pop "duration" */
527
528                     /* playlist key item path name */
529
530                     /* Read options: item must be on top of stack */
531                     lua_pushvalue( L, -3 );
532                     /* playlist key item path name item */
533                     vlclua_read_options( p_this, L, &i_options, &ppsz_options );
534
535                     /* Create input item */
536                     p_input = input_item_NewExt( psz_path, psz_name, i_options,
537                                                 (const char **)ppsz_options,
538                                                 VLC_INPUT_OPTION_TRUSTED,
539                                                 i_duration );
540                     lua_pop( L, 3 ); /* pop "path name item" */
541                     /* playlist key item */
542
543                     /* Read meta data: item must be on top of stack */
544                     vlclua_read_meta_data( p_this, L, p_input );
545
546                     /* Read custom meta data: item must be on top of stack*/
547                     vlclua_read_custom_meta_data( p_this, L, p_input );
548
549                     /* Append item to playlist */
550                     if( p_parent ) /* Add to node */
551                     {
552                         input_item_CopyOptions( p_parent, p_input );
553                         input_item_node_AppendItem( p_parent_node, p_input );
554                     }
555                     else /* Play or Enqueue (preparse) */
556                         /* FIXME: playlist_AddInput() can fail */
557                         playlist_AddInput( p_playlist, p_input,
558                                PLAYLIST_APPEND |
559                                ( b_play ? PLAYLIST_GO : PLAYLIST_PREPARSE ),
560                                PLAYLIST_END, true, false );
561                     i_count ++; /* increment counter */
562                     vlc_gc_decref( p_input );
563                     while( i_options > 0 )
564                         free( ppsz_options[--i_options] );
565                     free( ppsz_options );
566                     free( psz_u8path );
567                 }
568                 else
569                 {
570                     lua_pop( L, 1 ); /* pop "path" */
571                     msg_Warn( p_this,
572                              "Playlist item's path should be a string" );
573                 }
574                 /* playlist key item */
575             }
576             else
577             {
578                 msg_Warn( p_this, "Playlist item should be a table" );
579             }
580             /* <Parse playlist item> */
581             lua_pop( L, 1 ); /* pop the value, keep the key for
582                               * the next lua_next() call */
583             /* playlist key */
584         }
585         /* playlist */
586         if( p_parent )
587         {
588             if( i_count ) input_item_node_PostAndDelete( p_parent_node );
589             else input_item_node_Delete( p_parent_node );
590         }
591     }
592     else
593     {
594         msg_Warn( p_this, "Playlist should be a table." );
595     }
596     return i_count;
597 }
598
599 static int vlc_sd_probe_Open( vlc_object_t *obj )
600 {
601     vlc_probe_t *probe = (vlc_probe_t *)obj;
602     char **ppsz_filelist = NULL;
603     char **ppsz_fileend  = NULL;
604     char **ppsz_file;
605     char *psz_name;
606     char **ppsz_dir_list = NULL;
607     char **ppsz_dir;
608     lua_State *L = NULL;
609     vlclua_dir_list( obj, "sd", &ppsz_dir_list );
610     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
611     {
612         int i_files;
613         if( ppsz_filelist )
614         {
615             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
616                  ppsz_file++ )
617                 free( *ppsz_file );
618             free( ppsz_filelist );
619             ppsz_filelist = NULL;
620         }
621         i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
622                                 file_compare );
623         if( i_files < 1 ) continue;
624         ppsz_fileend = ppsz_filelist + i_files;
625         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
626         {
627             char  *psz_filename;
628             if( asprintf( &psz_filename,
629                           "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
630             {
631                 goto error;
632             }
633             L = luaL_newstate();
634             if( !L )
635             {
636                 msg_Err( probe, "Could not create new Lua State" );
637                 free( psz_filename );
638                 goto error;
639             }
640             luaL_openlibs( L );
641             if( vlclua_add_modules_path( probe, L, psz_filename ) )
642             {
643                 msg_Err( probe, "Error while setting the module search path for %s",
644                           psz_filename );
645                 free( psz_filename );
646                 goto error;
647             }
648             if( luaL_dofile( L, psz_filename ) )
649             {
650
651                 msg_Err( probe, "Error loading script %s: %s", psz_filename,
652                           lua_tostring( L, lua_gettop( L ) ) );
653                 lua_pop( L, 1 );
654                 free( psz_filename );
655                 lua_close( L );
656                 continue;
657             }
658             char *psz_longname;
659             char *temp = strchr( *ppsz_file, '.' );
660             if( temp )
661                 *temp = '\0';
662             lua_getglobal( L, "descriptor" );
663             if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
664             {
665                 msg_Warn( probe, "No 'descriptor' function in '%s'", psz_filename );
666                 lua_pop( L, 1 );
667                 if( !( psz_longname = strdup( *ppsz_file ) ) )
668                 {
669                     free( psz_filename );
670                     goto error;
671                 }
672             }
673             else
674             {
675                 lua_getfield( L, -1, "title" );
676                 if( !lua_isstring( L, -1 ) ||
677                     !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
678                 {
679                     free( psz_filename );
680                     goto error;
681                 }
682             }
683
684             char *psz_file_esc = config_StringEscape( *ppsz_file );
685             char *psz_longname_esc = config_StringEscape( psz_longname );
686             if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
687                           psz_file_esc, psz_longname_esc ) < 0 )
688             {
689                 free( psz_file_esc );
690                 free( psz_longname_esc );
691                 free( psz_filename );
692                 free( psz_longname );
693                 goto error;
694             }
695             free( psz_file_esc );
696             free( psz_longname_esc );
697             vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
698             free( psz_name );
699             free( psz_longname );
700             free( psz_filename );
701             lua_close( L );
702         }
703     }
704     if( ppsz_filelist )
705     {
706         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
707              ppsz_file++ )
708             free( *ppsz_file );
709         free( ppsz_filelist );
710     }
711     vlclua_dir_list_free( ppsz_dir_list );
712     return VLC_PROBE_CONTINUE;
713 error:
714     if( ppsz_filelist )
715     {
716         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
717              ppsz_file++ )
718             free( *ppsz_file );
719         free( ppsz_filelist );
720     }
721     if( L )
722         lua_close( L );
723     vlclua_dir_list_free( ppsz_dir_list );
724     return VLC_ENOMEM;
725 }
726
727 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
728 {
729     int count = 0;
730     for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
731     {
732         lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
733                          psz_path, *ppsz_ext );
734         count ++;
735     }
736
737     return count;
738 }
739
740 #undef vlclua_add_modules_path
741 int vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
742 {
743     /* Setup the module search path:
744      *   * "The script's directory"/modules
745      *   * "The script's parent directory"/modules
746      *   * and so on for all the next directories in the directory list
747      */
748     char *psz_path = strdup( psz_filename );
749     if( !psz_path )
750         return 1;
751
752     char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
753     if( !psz_char )
754     {
755         free( psz_path );
756         return 1;
757     }
758     *psz_char = '\0';
759
760     /* psz_path now holds the file's directory */
761     psz_char = strrchr( psz_path, DIR_SEP_CHAR );
762     if( !psz_char )
763     {
764         free( psz_path );
765         return 1;
766     }
767     *psz_char = '\0';
768
769     /* Push package on stack */
770     int count = 0;
771     lua_getglobal( L, "package" );
772
773     /* psz_path now holds the file's parent directory */
774     count += vlclua_add_modules_path_inner( L, psz_path );
775     *psz_char = DIR_SEP_CHAR;
776
777     /* psz_path now holds the file's directory */
778     count += vlclua_add_modules_path_inner( L, psz_path );
779
780     char **ppsz_dir_list = NULL;
781     vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
782     char **ppsz_dir = ppsz_dir_list;
783
784     for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
785     free( psz_path );
786
787     for( ; *ppsz_dir; ppsz_dir++ )
788     {
789         psz_path = *ppsz_dir;
790         /* FIXME: doesn't work well with meta/... modules due to the double
791          * directory depth */
792         psz_char = strrchr( psz_path, DIR_SEP_CHAR );
793         if( !psz_char )
794         {
795             vlclua_dir_list_free( ppsz_dir_list );
796             return 1;
797         }
798
799         *psz_char = '\0';
800         count += vlclua_add_modules_path_inner( L, psz_path );
801         *psz_char = DIR_SEP_CHAR;
802         count += vlclua_add_modules_path_inner( L, psz_path );
803     }
804
805     lua_getfield( L, -(count+1), "path" ); /* Get package.path */
806     lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
807     lua_setfield( L, -2, "path"); /* Set package.path */
808     lua_pop( L, 1 ); /* Pop the package module */
809
810     vlclua_dir_list_free( ppsz_dir_list );
811     return 0;
812 }
813
814 /** Replacement for luaL_dofile, using VLC's input capabilities */
815 int vlclua_dofile( vlc_object_t *p_this, lua_State *L, const char *uri )
816 {
817     if( !strstr( uri, "://" ) )
818         return luaL_dofile( L, uri );
819     if( !strncasecmp( uri, "file://", 7 ) )
820         return luaL_dofile( L, uri + 7 );
821     stream_t *s = stream_UrlNew( p_this, uri );
822     if( !s )
823     {
824         return 1;
825     }
826     int64_t i_size = stream_Size( s );
827     char *p_buffer = ( i_size > 0 ) ? malloc( i_size ) : NULL;
828     if( !p_buffer )
829     {
830         // FIXME: read the whole stream until we reach the end (if no size)
831         stream_Delete( s );
832         return 1;
833     }
834     int64_t i_read = stream_Read( s, p_buffer, (int) i_size );
835     int i_ret = ( i_read == i_size ) ? 0 : 1;
836     if( !i_ret )
837         i_ret = luaL_loadbuffer( L, p_buffer, (size_t) i_size, uri );
838     if( !i_ret )
839         i_ret = lua_pcall( L, 0, LUA_MULTRET, 0 );
840     stream_Delete( s );
841     free( p_buffer );
842     return i_ret;
843 }