]> git.sesse.net Git - vlc/blob - modules/meta_engine/luameta.c
Input access locking. Part one
[vlc] / modules / meta_engine / luameta.c
1 /*****************************************************************************
2  * luameta.c: Get meta/artwork using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2006 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 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc_input.h>
32 #include <vlc_playlist.h>
33 #include <vlc_meta.h>
34 #include <vlc_url.h>
35 #include <vlc_strings.h>
36 #include <vlc_stream.h>
37 #include <vlc_charset.h>
38
39 #ifdef HAVE_SYS_STAT_H
40 #   include <sys/stat.h>
41 #endif
42
43 #include <lua.h>        /* Low level lua C API */
44 #include <lauxlib.h>    /* Higher level C API */
45 #include <lualib.h>     /* Lua libs */
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int FindArt( vlc_object_t * );
51 static int FindMeta( vlc_object_t *p_this );
52
53 /*****************************************************************************
54  * Module descriptor
55  *****************************************************************************/
56
57 vlc_module_begin();
58     set_shortname( N_( "Lua Meta" ) );
59     set_description( _("Fetch Artwork using lua scripts") );
60     set_capability( "meta fetcher", 10 );
61     set_callbacks( FindMeta, NULL );
62     add_submodule();
63         set_capability( "art finder", 10 );
64         set_callbacks( FindArt, NULL );
65 vlc_module_end();
66
67 /*****************************************************************************
68  * Lua function bridge
69  *****************************************************************************/
70 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 /* Functions to register */
214 static luaL_Reg p_reg[] =
215 {
216     { "stream_new", vlclua_stream_new },
217     { "stream_read", vlclua_stream_read },
218     { "stream_readline", vlclua_stream_readline },
219     { "stream_delete", vlclua_stream_delete },
220     { "decode_uri", vlclua_decode_uri },
221     { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
222     { "msg_dbg", vlclua_msg_dbg },
223     { "msg_warn", vlclua_msg_warn },
224     { "msg_err", vlclua_msg_err },
225     { "msg_info", vlclua_msg_info },
226     { NULL, NULL }
227 };
228 /*****************************************************************************
229  *
230  *****************************************************************************/
231 static int file_select( const char *file )
232 {
233     int i = strlen( file );
234     return i > 4 && !strcmp( file+i-4, ".lua" );
235 }
236
237 static int file_compare( const char **a, const char **b )
238 {
239     return strcmp( *a, *b );
240 }
241
242 /*****************************************************************************
243  * Init lua
244  *****************************************************************************/
245 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
246 {
247     lua_State * p_state = luaL_newstate();
248     if( !p_state )
249     {
250         msg_Err( p_this, "Could not create new Lua State" );
251         return NULL;
252     }
253     char *psz_meta;
254
255     /* Load Lua libraries */
256     luaL_openlibs( p_state ); /* XXX: Don't open all the libs? */
257     
258     luaL_register( p_state, "vlc", p_reg );
259     
260     lua_pushlightuserdata( p_state, p_this );
261     lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
262    
263     psz_meta = input_item_GetName( p_item );
264     lua_pushstring( p_state, psz_meta );
265     lua_setfield( p_state, lua_gettop( p_state ) - 1, "name" );
266     free( psz_meta );
267    
268     psz_meta = input_item_GetTitle( p_item ) ;
269     lua_pushstring( p_state, psz_meta );
270     lua_setfield( p_state, lua_gettop( p_state ) - 1, "title" );
271     free( psz_meta );
272    
273     psz_meta = input_item_GetAlbum( p_item );
274     lua_pushstring( p_state, psz_meta );
275     lua_setfield( p_state, lua_gettop( p_state ) - 1, "album" );
276     free( psz_meta );
277
278     psz_meta = input_item_GetArtURL( p_item );
279     lua_pushstring( p_state, psz_meta );
280     lua_setfield( p_state, lua_gettop( p_state ) - 1, "arturl" );
281     free( psz_meta );
282     /* XXX: all should be passed ( could use macro ) */
283
284     return p_state;
285 }
286
287 /*****************************************************************************
288  * Will execute func on all scripts in luadirname, and stop if func returns
289  * success.
290  *****************************************************************************/
291 static int vlclua_scripts_batch_execute( vlc_object_t *p_this,
292                                          const char * luadirname,
293                                          int (*func)(vlc_object_t *, const char *, lua_State *, void *),
294                                          lua_State * p_state,
295                                          void * user_data)
296 {
297     int i_ret = VLC_EGENERIC;
298
299     DIR   *dir           = NULL;
300     char **ppsz_filelist = NULL;
301     char **ppsz_fileend  = NULL;
302     char **ppsz_file;
303
304     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
305     char **ppsz_dir;
306
307     if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP CONFIG_DIR DIR_SEP "%s", p_this->p_libvlc->psz_homedir,
308             luadirname ) < 0 )
309         return VLC_ENOMEM;
310
311 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
312     {
313         const char *psz_vlcpath = config_GetDataDir();
314         if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
315             return VLC_ENOMEM;
316
317         if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
318             return VLC_ENOMEM;
319     }
320 #   endif
321
322     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
323     {
324         int i_files;
325
326         if( ppsz_filelist )
327         {
328             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
329                  ppsz_file++ )
330                 free( *ppsz_file );
331             free( ppsz_filelist );
332             ppsz_filelist = NULL;
333         }
334
335         if( dir )
336         {
337             closedir( dir );
338         }
339
340         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
341         dir = utf8_opendir( *ppsz_dir );
342
343         if( !dir ) continue;
344         i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
345         if( i_files < 1 ) continue;
346         ppsz_fileend = ppsz_filelist + i_files;
347
348         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
349         {
350             char  *psz_filename;
351             asprintf( &psz_filename, "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file );
352             msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
353             
354             i_ret = func( p_this, psz_filename, p_state, user_data );
355             
356             free( psz_filename );
357
358             if( i_ret == VLC_SUCCESS ) break;
359         }
360         if( i_ret == VLC_SUCCESS ) break;
361     }
362
363     if( ppsz_filelist )
364     {
365         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
366              ppsz_file++ )
367             free( *ppsz_file );
368         free( ppsz_filelist );
369     }
370     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
371         free( *ppsz_dir );
372
373     if( dir ) closedir( dir );
374
375     return i_ret;
376 }
377
378 /*****************************************************************************
379  * Meta data setters utility.
380  *****************************************************************************/
381 static inline void read_meta_data( vlc_object_t *p_this,
382                                    lua_State *p_state, int o, int t,
383                                    input_item_t *p_input )
384 {
385     const char *psz_value;
386 #define TRY_META( a, b )                                    \
387     lua_getfield( p_state, o, a );                          \
388     if( lua_isstring( p_state, t ) )                        \
389     {                                                       \
390         psz_value = lua_tostring( p_state, t );             \
391         msg_Dbg( p_this, #b ": %s", psz_value );           \
392         input_item_Set ## b ( p_input, psz_value );   \
393     }                                                       \
394     lua_pop( p_state, 1 ); /* pop a */
395     TRY_META( "title", Title );
396     TRY_META( "artist", Artist );
397     TRY_META( "genre", Genre );
398     TRY_META( "copyright", Copyright );
399     TRY_META( "album", Album );
400     TRY_META( "tracknum", TrackNum );
401     TRY_META( "description", Description );
402     TRY_META( "rating", Rating );
403     TRY_META( "date", Date );
404     TRY_META( "setting", Setting );
405     TRY_META( "url", URL );
406     TRY_META( "language", Language );
407     TRY_META( "nowplaying", NowPlaying );
408     TRY_META( "publisher", Publisher );
409     TRY_META( "encodedby", EncodedBy );
410     TRY_META( "arturl", ArtURL );
411     TRY_META( "trackid", TrackID );
412 }
413
414 static inline void read_custom_meta_data( vlc_object_t *p_this,
415                                           lua_State *p_state, int o, int t,
416                                           input_item_t *p_input )
417 {
418     lua_getfield( p_state, o, "meta" );
419     if( lua_istable( p_state, t ) )
420     {
421         lua_pushnil( p_state );
422         while( lua_next( p_state, t ) )
423         {
424             if( !lua_isstring( p_state, t+1 ) )
425             {
426                 msg_Warn( p_this, "Custom meta data category name must be "
427                                    "a string" );
428             }
429             else if( !lua_istable( p_state, t+2 ) )
430             {
431                 msg_Warn( p_this, "Custom meta data category contents "
432                                    "must be a table" );
433             }
434             else
435             {
436                 const char *psz_meta_category = lua_tostring( p_state, t+1 );
437                 msg_Dbg( p_this, "Found custom meta data category: %s",
438                          psz_meta_category );
439                 lua_pushnil( p_state );
440                 while( lua_next( p_state, t+2 ) )
441                 {
442                     if( !lua_isstring( p_state, t+3 ) )
443                     {
444                         msg_Warn( p_this, "Custom meta category item name "
445                                            "must be a string." );
446                     }
447                     else if( !lua_isstring( p_state, t+4 ) )
448                     {
449                         msg_Warn( p_this, "Custom meta category item value "
450                                            "must be a string." );
451                     }
452                     else
453                     {
454                         const char *psz_meta_name =
455                             lua_tostring( p_state, t+3 );
456                         const char *psz_meta_value =
457                             lua_tostring( p_state, t+4 );
458                         msg_Dbg( p_this, "Custom meta %s, %s: %s",
459                                  psz_meta_category, psz_meta_name,
460                                  psz_meta_value );
461                         input_ItemAddInfo( p_input, psz_meta_category,
462                                            psz_meta_name, psz_meta_value );
463                     }
464                     lua_pop( p_state, 1 ); /* pop item */
465                 }
466             }
467             lua_pop( p_state, 1 ); /* pop category */
468         }
469     }
470     lua_pop( p_state, 1 ); /* pop "meta" */
471 }
472
473 /*****************************************************************************
474  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
475  * pointed by psz_filename.
476  *****************************************************************************/
477 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
478                       lua_State * p_state, void * user_data )
479 {
480     int i_ret = VLC_EGENERIC;
481     input_item_t * p_input = user_data;
482     int s;
483
484     /* Ugly hack to delete previous versions of the fetchart()
485     * functions. */
486     lua_pushnil( p_state );
487     lua_setglobal( p_state, "fetch_art" );
488     
489     /* Load and run the script(s) */
490     if( luaL_dofile( p_state, psz_filename ) )
491     {
492         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
493                   lua_tostring( p_state, lua_gettop( p_state ) ) );
494         lua_pop( p_state, 1 );
495         return VLC_EGENERIC;
496     }
497
498     lua_getglobal( p_state, "fetch_art" );
499
500     if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
501     {
502         msg_Warn( p_this, "Error while runing script %s, "
503                   "function fetch_art() not found", psz_filename );
504         lua_pop( p_state, 1 );
505         return VLC_EGENERIC;
506     }
507
508     if( lua_pcall( p_state, 0, 1, 0 ) )
509     {
510         msg_Warn( p_this, "Error while runing script %s, "
511                   "function fetch_art(): %s", psz_filename,
512                   lua_tostring( p_state, lua_gettop( p_state ) ) );
513         lua_pop( p_state, 1 );
514         return VLC_EGENERIC;
515     }
516
517     if((s = lua_gettop( p_state )))
518     {
519         const char * psz_value;
520
521         if( lua_isstring( p_state, s ) )
522         {
523             psz_value = lua_tostring( p_state, s );
524             if( psz_value && *psz_value != 0 )
525             {
526                 msg_Dbg( p_this, "setting arturl: %s", psz_value );
527                 input_item_SetArtURL ( p_input, psz_value );
528                 i_ret = VLC_SUCCESS;
529             }
530         }
531         else
532         {
533             msg_Err( p_this, "Lua playlist script %s: "
534                  "didn't return a string", psz_filename );
535         }
536     }
537     else
538     {
539         msg_Err( p_this, "Script went completely foobar" );
540     }
541
542     return i_ret;
543 }
544
545 /*****************************************************************************
546  * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
547  * pointed by psz_filename.
548  *****************************************************************************/
549 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
550                        lua_State * p_state, void * user_data )
551 {
552     input_item_t * p_input = user_data;
553     int t;
554
555     /* Ugly hack to delete previous versions of the fetchmeta()
556     * functions. */
557     lua_pushnil( p_state );
558     lua_setglobal( p_state, "fetch_meta" );
559     
560     /* Load and run the script(s) */
561     if( luaL_dofile( p_state, psz_filename ) )
562     {
563         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
564                   lua_tostring( p_state, lua_gettop( p_state ) ) );
565         lua_pop( p_state, 1 );
566         return VLC_EGENERIC;
567     }
568     
569     lua_getglobal( p_state, "fetch_meta" );
570     
571     if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
572     {
573         msg_Warn( p_this, "Error while runing script %s, "
574                   "function fetch_meta() not found", psz_filename );
575         lua_pop( p_state, 1 );
576         return VLC_EGENERIC;
577     }
578     
579     if( lua_pcall( p_state, 0, 1, 0 ) )
580     {
581         msg_Warn( p_this, "Error while runing script %s, "
582                   "function fetch_meta(): %s", psz_filename,
583                   lua_tostring( p_state, lua_gettop( p_state ) ) );
584         lua_pop( p_state, 1 );
585         return VLC_EGENERIC;
586     }
587     
588
589     if((t = lua_gettop( p_state )))
590     {
591         if( lua_istable( p_state, t ) )
592         {
593             read_meta_data( p_this, p_state, t, t+1, p_input );
594             read_custom_meta_data( p_this, p_state, t, t+1, p_input );
595         }
596         else
597         {
598             msg_Err( p_this, "Lua playlist script %s: "
599                  "didn't return a table", psz_filename );
600         }
601     }
602     else
603     {
604         msg_Err( p_this, "Script went completely foobar" );
605     }
606
607     /* We tell the batch thing to continue, hence all script
608      * will get the change to add its meta. This behaviour could
609      * be changed. */
610     return VLC_EGENERIC;
611 }
612
613 /*****************************************************************************
614  * Module entry point for meta.
615  *****************************************************************************/
616 static int FindMeta( vlc_object_t *p_this )
617 {
618     meta_engine_t *p_me = (meta_engine_t *)p_this;
619     input_item_t *p_item = p_me->p_item;
620     lua_State *p_state = vlclua_meta_init( p_this, p_item );
621     
622     int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_meta, p_state, p_item );
623     lua_close( p_state );
624     return i_ret;
625 }
626
627 /*****************************************************************************
628  * Module entry point for art.
629  *****************************************************************************/
630 static int FindArt( vlc_object_t *p_this )
631 {
632     playlist_t *p_playlist = (playlist_t *)p_this;
633     input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
634     lua_State *p_state = vlclua_meta_init( p_this, p_item );
635     
636     int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_art, p_state, p_item );
637     lua_close( p_state );
638     return i_ret;
639 }
640