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