]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/vlc.c
lua_sd: warn in cas descriptor function is missing.
[vlc] / modules / misc / lua / vlc.c
index 181acd29748c8e8a448c73828788a7141a88b9a7..dc8daacf14dea46e4d81c3b9ad0b04db3befc4c6 100644 (file)
 
 #define CONFIG_TEXT N_("Lua interface configuration")
 #define CONFIG_LONGTEXT N_("Lua interface configuration string. Format is: '[\"<interface module name>\"] = { <option> = <value>, ...}, ...'.")
+#define HOST_TEXT N_( "Host address" )
+#define HOST_LONGTEXT N_( \
+    "Address and port the HTTP interface will listen on. It defaults to " \
+    "all network interfaces (0.0.0.0)." \
+    " If you want the HTTP interface to be available only on the local " \
+    "machine, enter 127.0.0.1" )
+#define SRC_TEXT N_( "Source directory" )
+#define SRC_LONGTEXT N_( "Source directory" )
+#define INDEX_TEXT N_( "Directory index" )
+#define INDEX_LONGTEXT N_( "Allow to build directory index" )
+
+#define TELNETHOST_TEXT N_( "Host" )
+#define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
+    "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
+    " If you want this interface to be available only on the local " \
+    "machine, enter \"127.0.0.1\"." )
+#define TELNETPORT_TEXT N_( "Port" )
+#define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
+    "interface will listen. It defaults to 4212." )
+#define TELNETPORT_DEFAULT 4212
+#define TELNETPWD_TEXT N_( "Password" )
+#define TELNETPWD_LONGTEXT N_( "A single administration password is used " \
+    "to protect this interface. The default value is \"admin\"." )
+#define TELNETPWD_DEFAULT "admin"
 
 static int vlc_sd_probe_Open( vlc_object_t * );
 
 vlc_module_begin ()
-        set_shortname( N_( "Lua Art" ) )
-        set_description( N_("Fetch artwork using lua scripts") )
-        set_capability( "art finder", 10 )
-        set_callbacks( FindArt, NULL )
+        set_shortname( N_("Lua Interface Module") )
+        set_description( N_("Interfaces implemented using lua scripts") )
+        add_shortcut( "luaintf" )
+        add_shortcut( "luahttp" )
+        /* add_shortcut( "http" ) */
+        add_shortcut( "luatelnet" )
+        add_shortcut( "telnet" )
+        add_shortcut( "luahotkeys" )
+        /* add_shortcut( "hotkeys" ) */
+        set_capability( "interface", 0 )
+        set_category( CAT_INTERFACE )
+        set_subcategory( SUBCAT_INTERFACE_CONTROL )
+        add_string( "lua-intf", "dummy", NULL,
+                    INTF_TEXT, INTF_LONGTEXT, false )
+        add_string( "lua-config", "", NULL,
+                    CONFIG_TEXT, CONFIG_LONGTEXT, false )
+        set_section( N_("Lua HTTP"), 0 )
+            add_string ( "http-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true )
+            add_string ( "http-src",  NULL, NULL, SRC_TEXT,  SRC_LONGTEXT,  true )
+            add_bool   ( "http-index", false, NULL, INDEX_TEXT, INDEX_LONGTEXT, true )
+        set_section( N_("Lua Telnet"), 0 )
+            add_string( "telnet-host", "localhost", NULL, TELNETHOST_TEXT,
+                        TELNETHOST_LONGTEXT, true )
+            add_integer( "telnet-port", TELNETPORT_DEFAULT, NULL, TELNETPORT_TEXT,
+                         TELNETPORT_LONGTEXT, true )
+            add_password( "telnet-password", TELNETPWD_DEFAULT, NULL, TELNETPWD_TEXT,
+                          TELNETPWD_LONGTEXT, true )
+
+        set_callbacks( Open_LuaIntf, Close_LuaIntf )
 
     add_submodule ()
         set_shortname( N_( "Lua Meta Fetcher" ) )
@@ -81,8 +130,6 @@ vlc_module_begin ()
 
     add_submodule ()
         add_shortcut( "luaplaylist" )
-        set_category( CAT_INPUT )
-        set_subcategory( SUBCAT_INPUT_DEMUX )
         set_shortname( N_("Lua Playlist") )
         set_description( N_("Lua Playlist Parser Interface") )
         set_capability( "demux", 2 )
@@ -96,20 +143,10 @@ vlc_module_begin ()
         set_callbacks( Open_LuaIntf, Close_LuaIntf )
 
     add_submodule ()
-        set_description( N_("Lua Interface Module") )
-        add_shortcut( "luaintf" )
-        add_shortcut( "luahttp" )
-        add_shortcut( "http" )
-        add_shortcut( "luatelnet" )
-        add_shortcut( "telnet" )
-        add_shortcut( "luahotkeys" )
-        /* add_shortcut( "hotkeys" ) */
-        set_capability( "interface", 0 )
-        add_string( "lua-intf", "dummy", NULL,
-                    INTF_TEXT, INTF_LONGTEXT, false )
-        add_string( "lua-config", "", NULL,
-                    CONFIG_TEXT, CONFIG_LONGTEXT, false )
-        set_callbacks( Open_LuaIntf, Close_LuaIntf )
+        set_shortname( N_( "Lua Art" ) )
+        set_description( N_("Fetch artwork using lua scripts") )
+        set_capability( "art finder", 10 )
+        set_callbacks( FindArt, NULL )
 
     add_submodule ()
         set_shortname( N_("Lua Extension") )
@@ -121,8 +158,10 @@ vlc_module_begin ()
         set_description( N_("Lua SD Module") )
         add_shortcut( "luasd" )
         set_capability( "services_discovery", 0 )
-        add_string( "lua-sd", "", NULL, "", "", false )
-        add_string( "lua-longname", "", NULL, "", "", false )
+        add_string( "lua-sd", "", NULL, NULL, NULL, false )
+            change_volatile()
+        add_string( "lua-longname", "", NULL, NULL, NULL, false )
+            change_volatile()
         set_callbacks( Open_LuaSD, Close_LuaSD )
 
     add_submodule ()
@@ -194,7 +233,7 @@ int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
                               psz_datapath, luadirname ) != -1) )
             i++;
 
-#if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
+#if defined(__APPLE__) || defined(SYS_BEOS)
         if( likely(asprintf( &ppsz_dir_list[i],
                              "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
                              psz_datapath, luadirname ) != -1) )
@@ -320,7 +359,8 @@ void __vlclua_read_meta_data( vlc_object_t *p_this, lua_State *L,
 {
 #define TRY_META( a, b )                                        \
     lua_getfield( L, -1, a );                                   \
-    if( lua_isstring( L, -1 ) )                                 \
+    if( lua_isstring( L, -1 ) &&                                \
+        strcmp( lua_tostring( L, -1 ), "" ) )                   \
     {                                                           \
         char *psz_value = strdup( lua_tostring( L, -1 ) );      \
         EnsureUTF8( psz_value );                                \
@@ -619,13 +659,15 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
             if( !L )
             {
                 msg_Err( probe, "Could not create new Lua State" );
-                return VLC_EGENERIC;
+                free( psz_filename );
+                goto error;
             }
             luaL_openlibs( L );
             if( vlclua_add_modules_path( probe, L, psz_filename ) )
             {
                 msg_Err( probe, "Error while setting the module search path for %s",
                           psz_filename );
+                free( psz_filename );
                 goto error;
             }
             if( luaL_dofile( L, psz_filename ) )
@@ -634,7 +676,9 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
                 msg_Err( probe, "Error loading script %s: %s", psz_filename,
                           lua_tostring( L, lua_gettop( L ) ) );
                 lua_pop( L, 1 );
-                goto error;
+                free( psz_filename );
+                lua_close( L );
+                continue;
             }
             char *psz_longname;
             char *temp = strchr( *ppsz_file, '.' );
@@ -643,6 +687,7 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
             lua_getglobal( L, "descriptor" );
             if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
             {
+                msg_Warn( probe, "No 'descriptor' function in '%s'", psz_filename );
                 lua_pop( L, 1 );
                 if( !( psz_longname = strdup( *ppsz_file ) ) )
                 {
@@ -660,13 +705,20 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
                     goto error;
                 }
             }
-            if( asprintf( &psz_name, "lua{sd=%s,longname=%s}",
-                          *ppsz_file, psz_longname ) < 0 )
+
+            char *psz_file_esc = config_StringEscape( *ppsz_file );
+            char *psz_longname_esc = config_StringEscape( psz_longname );
+            if( asprintf( &psz_name, "lua{sd='%s',longname='%s'}",
+                          psz_file_esc, psz_longname_esc ) < 0 )
             {
+                free( psz_file_esc );
+                free( psz_longname_esc );
                 free( psz_filename );
                 free( psz_longname );
                 goto error;
             }
+            free( psz_file_esc );
+            free( psz_longname_esc );
             vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
             free( psz_name );
             free( psz_longname );
@@ -699,23 +751,15 @@ error:
 
 static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
 {
-    /* FIXME: don't use luaL_dostring */
-    char *psz_command = NULL;
-    if( asprintf( &psz_command,
-                  "package.path =[[%s"DIR_SEP"modules"DIR_SEP"?.lua;]]..package.path",
-                  psz_path ) < 0 )
+    int count = 0;
+    for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
     {
-        return 1;
-    }
-
-    if( luaL_dostring( L, psz_command ) )
-    {
-        free( psz_command );
-        return 1;
+        lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
+                         psz_path, *ppsz_ext );
+        count ++;
     }
-    free( psz_command );
 
-    return 0;
+    return count;
 }
 
 int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
@@ -725,7 +769,6 @@ int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_
      *   * "The script's parent directory"/modules
      *   * and so on for all the next directories in the directory list
      */
-
     char *psz_path = strdup( psz_filename );
     if( !psz_path )
         return 1;
@@ -747,20 +790,16 @@ int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_
     }
     *psz_char = '\0';
 
+    /* Push package on stack */
+    int count = 0;
+    lua_getglobal( L, "package" );
+
     /* psz_path now holds the file's parent directory */
-    if( vlclua_add_modules_path_inner( L, psz_path ) )
-    {
-        free( psz_path );
-        return 1;
-    }
+    count += vlclua_add_modules_path_inner( L, psz_path );
     *psz_char = DIR_SEP_CHAR;
 
     /* psz_path now holds the file's directory */
-    if( vlclua_add_modules_path_inner( L, psz_path ) )
-    {
-        free( psz_path );
-        return 1;
-    }
+    count += vlclua_add_modules_path_inner( L, psz_path );
 
     char **ppsz_dir_list = NULL;
     vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
@@ -772,24 +811,27 @@ int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_
     for( ; *ppsz_dir; ppsz_dir++ )
     {
         psz_path = *ppsz_dir;
+        /* FIXME: doesn't work well with meta/... modules due to the double
+         * directory depth */
         psz_char = strrchr( psz_path, DIR_SEP_CHAR );
         if( !psz_char )
-            goto exit;
+        {
+            vlclua_dir_list_free( ppsz_dir_list );
+            return 1;
+        }
 
         *psz_char = '\0';
-        if( vlclua_add_modules_path_inner( L, psz_path ) )
-            goto exit;
+        count += vlclua_add_modules_path_inner( L, psz_path );
         *psz_char = DIR_SEP_CHAR;
-
-        if( vlclua_add_modules_path_inner( L, psz_path ) )
-            goto exit;
+        count += vlclua_add_modules_path_inner( L, psz_path );
     }
 
-    vlclua_dir_list_free( ppsz_dir_list );
-    return 0;
+    lua_getfield( L, -(count+1), "path" ); /* Get package.path */
+    lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
+    lua_setfield( L, -2, "path"); /* Set package.path */
+    lua_pop( L, 1 ); /* Pop the package module */
 
-    exit:
     vlclua_dir_list_free( ppsz_dir_list );
-    return 1;
+    return 0;
 }