]> git.sesse.net Git - vlc/blob - modules/misc/lua/vlclua.c
modules/misc/lua: Share some code between the modules that uses lua and put them...
[vlc] / modules / misc / lua / vlclua.c
1 /*****************************************************************************
2  * luameta.c: Get meta/artwork using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id: luameta.c 21327 2007-08-20 19:23:10Z courmisch $
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 #include <vlc/vlc.h>
33 #include <vlc_meta.h>
34
35 #include <lua.h>        /* Low level lua C API */
36 #include <lauxlib.h>    /* Higher level C API */
37 #include <lualib.h>     /* Lua libs */
38
39 #include "vlclua.h"
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44
45 vlc_module_begin();
46     add_shortcut( "lua" );
47     add_submodule();
48         add_shortcut( "luameta" );
49         set_shortname( N_( "Lua Meta" ) );
50         set_description( _("Fetch Artwork using lua scripts") );
51         set_capability( "meta fetcher", 10 );
52         set_callbacks( E_(FindMeta), NULL );
53         add_submodule();
54             set_capability( "art finder", 10 );
55             set_callbacks( E_(FindArt), NULL );
56     add_submodule();
57         add_shortcut( "luaplaylist" );
58         set_category( CAT_INPUT );
59         set_subcategory( SUBCAT_INPUT_DEMUX );
60
61         set_shortname( _("Lua Playlist") );
62         set_description( _("Lua Playlist Parser Interface") );
63         set_capability( "demux2", 9 );
64         set_callbacks( E_(Import_LuaPlaylist), E_(Close_LuaPlaylist) );
65 vlc_module_end();
66
67 /*****************************************************************************
68  * Lua function bridge
69  *****************************************************************************/
70 vlc_object_t * vlclua_get_this( lua_State *p_state )
71 {
72     vlc_object_t * p_this;
73     lua_getglobal( p_state, "vlc" );
74     lua_getfield( p_state, lua_gettop( p_state ), "private" );
75     p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
76     lua_pop( p_state, 2 );
77     return p_this;
78 }
79
80 int vlclua_stream_new( lua_State *p_state )
81 {
82     vlc_object_t * p_this = vlclua_get_this( p_state );
83     int i = lua_gettop( p_state );
84     stream_t * p_stream;
85     const char * psz_url;
86     if( !i ) return 0;
87     psz_url = lua_tostring( p_state, 1 );
88     lua_pop( p_state, i );
89     p_stream = stream_UrlNew( p_this, psz_url );
90     lua_pushlightuserdata( p_state, p_stream );
91     return 1;
92 }
93
94 int vlclua_stream_read( lua_State *p_state )
95 {
96     int i = lua_gettop( p_state );
97     stream_t * p_stream;
98     int n;
99     byte_t *p_read;
100     int i_read;
101     if( !i ) return 0;
102     p_stream = (stream_t *)lua_topointer( p_state, 1 );
103     n = lua_tonumber( p_state, 2 );
104     lua_pop( p_state, i );
105     p_read = malloc( n );
106     if( !p_read ) return 0;
107     i_read = stream_Read( p_stream, p_read, n );
108     lua_pushlstring( p_state, (const char *)p_read, i_read );
109     free( p_read );
110     return 1;
111 }
112
113 int vlclua_stream_readline( lua_State *p_state )
114 {
115     int i = lua_gettop( p_state );
116     stream_t * p_stream;
117     if( !i ) return 0;
118     p_stream = (stream_t *)lua_topointer( p_state, 1 );
119     lua_pop( p_state, i );
120     char *psz_line = stream_ReadLine( p_stream );
121     if( psz_line )
122     {
123         lua_pushstring( p_state, psz_line );
124         free( psz_line );
125     }
126     else
127     {
128         lua_pushnil( p_state );
129     }
130     return 1;
131 }
132
133 int vlclua_stream_delete( lua_State *p_state )
134 {
135     int i = lua_gettop( p_state );
136     stream_t * p_stream;
137     if( !i ) return 0;
138     p_stream = (stream_t *)lua_topointer( p_state, 1 );
139     lua_pop( p_state, i );
140     stream_Delete( p_stream );
141     return 1;
142 }
143
144 int vlclua_decode_uri( lua_State *p_state )
145 {
146     int i = lua_gettop( p_state );
147     if( !i ) return 0;
148     const char *psz_cstring = lua_tostring( p_state, 1 );
149     if( !psz_cstring ) return 0;
150     char *psz_string = strdup( psz_cstring );
151     lua_pop( p_state, i );
152     decode_URI( psz_string );
153     lua_pushstring( p_state, psz_string );
154     free( psz_string );
155     return 1;
156 }
157
158 int vlclua_resolve_xml_special_chars( lua_State *p_state )
159 {
160     int i = lua_gettop( p_state );
161     if( !i ) return 0;
162     const char *psz_cstring = lua_tostring( p_state, 1 );
163     if( !psz_cstring ) return 0;
164     char *psz_string = strdup( psz_cstring );
165     lua_pop( p_state, i );
166     resolve_xml_special_chars( psz_string );
167     lua_pushstring( p_state, psz_string );
168     free( psz_string );
169     return 1;
170 }
171
172 int vlclua_msg_dbg( lua_State *p_state )
173 {
174     vlc_object_t *p_this = vlclua_get_this( p_state );
175     int i = lua_gettop( p_state );
176     if( !i ) return 0;
177     const char *psz_cstring = lua_tostring( p_state, 1 );
178     if( !psz_cstring ) return 0;
179     msg_Dbg( p_this, "%s", psz_cstring );
180     return 0;
181 }
182 int vlclua_msg_warn( lua_State *p_state )
183 {
184     vlc_object_t *p_this = vlclua_get_this( p_state );
185     int i = lua_gettop( p_state );
186     if( !i ) return 0;
187     const char *psz_cstring = lua_tostring( p_state, 1 );
188     if( !psz_cstring ) return 0;
189     msg_Warn( p_this, "%s", psz_cstring );
190     return 0;
191 }
192 int vlclua_msg_err( lua_State *p_state )
193 {
194     vlc_object_t *p_this = vlclua_get_this( p_state );
195     int i = lua_gettop( p_state );
196     if( !i ) return 0;
197     const char *psz_cstring = lua_tostring( p_state, 1 );
198     if( !psz_cstring ) return 0;
199     msg_Err( p_this, "%s", psz_cstring );
200     return 0;
201 }
202 int vlclua_msg_info( lua_State *p_state )
203 {
204     vlc_object_t *p_this = vlclua_get_this( p_state );
205     int i = lua_gettop( p_state );
206     if( !i ) return 0;
207     const char *psz_cstring = lua_tostring( p_state, 1 );
208     if( !psz_cstring ) return 0;
209     msg_Info( p_this, "%s", psz_cstring );
210     return 0;
211 }
212
213 /*****************************************************************************
214  *
215  *****************************************************************************/
216 static int file_select( const char *file )
217 {
218     int i = strlen( file );
219     return i > 4 && !strcmp( file+i-4, ".lua" );
220 }
221
222 static int file_compare( const char **a, const char **b )
223 {
224     return strcmp( *a, *b );
225 }
226
227
228 /*****************************************************************************
229  * Will execute func on all scripts in luadirname, and stop if func returns
230  * success.
231  *****************************************************************************/
232 int vlclua_scripts_batch_execute( vlc_object_t *p_this,
233                                   const char * luadirname,
234                                   int (*func)(vlc_object_t *, const char *, lua_State *, void *),
235                                   lua_State * p_state,
236                                   void * user_data)
237 {
238     int i_ret = VLC_EGENERIC;
239
240     DIR   *dir           = NULL;
241     char **ppsz_filelist = NULL;
242     char **ppsz_fileend  = NULL;
243     char **ppsz_file;
244
245     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
246     char **ppsz_dir;
247
248     if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP CONFIG_DIR DIR_SEP "%s", p_this->p_libvlc->psz_homedir,
249             luadirname ) < 0 )
250         return VLC_ENOMEM;
251
252 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
253     {
254         const char *psz_vlcpath = config_GetDataDir();
255         if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
256             return VLC_ENOMEM;
257
258         if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
259             return VLC_ENOMEM;
260     }
261 #   else
262     {
263 #   ifdef HAVE_SYS_STAT_H
264         struct stat stat_info;
265         if( ( utf8_stat( "share/luaplaylist", &stat_info ) == -1 )
266             || !S_ISDIR( stat_info.st_mode ) )
267         {
268             if( asprintf( &ppsz_dir_list[1], 
269                           DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
270                 return VLC_ENOMEM;
271         }
272         else
273 #   endif
274         {
275             if( asprintf( &ppsz_dir_list[1], 
276                           "share" DIR_SEP "%s", luadirname ) < 0 )
277                 return VLC_ENOMEM;
278         }
279     }
280 #   endif
281
282     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
283     {
284         int i_files;
285
286         if( ppsz_filelist )
287         {
288             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
289                  ppsz_file++ )
290                 free( *ppsz_file );
291             free( ppsz_filelist );
292             ppsz_filelist = NULL;
293         }
294
295         if( dir )
296         {
297             closedir( dir );
298         }
299
300         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
301         dir = utf8_opendir( *ppsz_dir );
302
303         if( !dir ) continue;
304         i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
305         if( i_files < 1 ) continue;
306         ppsz_fileend = ppsz_filelist + i_files;
307
308         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
309         {
310             char  *psz_filename;
311             asprintf( &psz_filename, "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file );
312             msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
313             
314             i_ret = func( p_this, psz_filename, p_state, user_data );
315             
316             free( psz_filename );
317
318             if( i_ret == VLC_SUCCESS ) break;
319         }
320         if( i_ret == VLC_SUCCESS ) break;
321     }
322
323     if( ppsz_filelist )
324     {
325         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
326              ppsz_file++ )
327             free( *ppsz_file );
328         free( ppsz_filelist );
329     }
330     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
331         free( *ppsz_dir );
332
333     if( dir ) closedir( dir );
334
335     return i_ret;
336 }
337
338
339 /*****************************************************************************
340  * Meta data setters utility.
341  *****************************************************************************/
342 void vlclua_read_meta_data( vlc_object_t *p_this, lua_State *p_state,
343                             int o, int t, input_item_t *p_input )
344 {
345     const char *psz_value;
346 #define TRY_META( a, b )                                    \
347     lua_getfield( p_state, o, a );                          \
348     if( lua_isstring( p_state, t ) )                        \
349     {                                                       \
350         psz_value = lua_tostring( p_state, t );             \
351         msg_Dbg( p_this, #b ": %s", psz_value );           \
352         input_item_Set ## b ( p_input, psz_value );   \
353     }                                                       \
354     lua_pop( p_state, 1 ); /* pop a */
355     TRY_META( "title", Title );
356     TRY_META( "artist", Artist );
357     TRY_META( "genre", Genre );
358     TRY_META( "copyright", Copyright );
359     TRY_META( "album", Album );
360     TRY_META( "tracknum", TrackNum );
361     TRY_META( "description", Description );
362     TRY_META( "rating", Rating );
363     TRY_META( "date", Date );
364     TRY_META( "setting", Setting );
365     TRY_META( "url", URL );
366     TRY_META( "language", Language );
367     TRY_META( "nowplaying", NowPlaying );
368     TRY_META( "publisher", Publisher );
369     TRY_META( "encodedby", EncodedBy );
370     TRY_META( "arturl", ArtURL );
371     TRY_META( "trackid", TrackID );
372 }
373
374 void vlclua_read_custom_meta_data( vlc_object_t *p_this, lua_State *p_state,
375                                    int o, int t, input_item_t *p_input )
376 {
377     lua_getfield( p_state, o, "meta" );
378     if( lua_istable( p_state, t ) )
379     {
380         lua_pushnil( p_state );
381         while( lua_next( p_state, t ) )
382         {
383             if( !lua_isstring( p_state, t+1 ) )
384             {
385                 msg_Warn( p_this, "Custom meta data category name must be "
386                                    "a string" );
387             }
388             else if( !lua_istable( p_state, t+2 ) )
389             {
390                 msg_Warn( p_this, "Custom meta data category contents "
391                                    "must be a table" );
392             }
393             else
394             {
395                 const char *psz_meta_category = lua_tostring( p_state, t+1 );
396                 msg_Dbg( p_this, "Found custom meta data category: %s",
397                          psz_meta_category );
398                 lua_pushnil( p_state );
399                 while( lua_next( p_state, t+2 ) )
400                 {
401                     if( !lua_isstring( p_state, t+3 ) )
402                     {
403                         msg_Warn( p_this, "Custom meta category item name "
404                                            "must be a string." );
405                     }
406                     else if( !lua_isstring( p_state, t+4 ) )
407                     {
408                         msg_Warn( p_this, "Custom meta category item value "
409                                            "must be a string." );
410                     }
411                     else
412                     {
413                         const char *psz_meta_name =
414                             lua_tostring( p_state, t+3 );
415                         const char *psz_meta_value =
416                             lua_tostring( p_state, t+4 );
417                         msg_Dbg( p_this, "Custom meta %s, %s: %s",
418                                  psz_meta_category, psz_meta_name,
419                                  psz_meta_value );
420                         input_ItemAddInfo( p_input, psz_meta_category,
421                                            psz_meta_name, psz_meta_value );
422                     }
423                     lua_pop( p_state, 1 ); /* pop item */
424                 }
425             }
426             lua_pop( p_state, 1 ); /* pop category */
427         }
428     }
429     lua_pop( p_state, 1 ); /* pop "meta" */
430 }
431