]> git.sesse.net Git - vlc/blob - modules/lua/meta.c
Lua: code cosmetic
[vlc] / modules / lua / meta.c
1 /*****************************************************************************
2  * meta.c: Get meta/artwork using lua scripts
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 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include "vlc.h"
37 #include "libs.h"
38
39 /*****************************************************************************
40  * Init lua
41  *****************************************************************************/
42 static const luaL_Reg p_reg[] = { { NULL, NULL } };
43
44 static lua_State * init( vlc_object_t *p_this, input_item_t * p_item, const char *psz_filename )
45 {
46     lua_State * L = luaL_newstate();
47     if( !L )
48     {
49         msg_Err( p_this, "Could not create new Lua State" );
50         return NULL;
51     }
52
53     vlclua_set_this( L, p_this );
54
55     /* Load Lua libraries */
56     luaL_openlibs( L ); /* XXX: Don't open all the libs? */
57
58     luaL_register( L, "vlc", p_reg );
59
60     luaopen_msg( L );
61     luaopen_stream( L );
62     luaopen_strings( L );
63     luaopen_variables( L );
64     luaopen_object( L );
65     luaopen_xml( L );
66     luaopen_input_item( L, p_item );
67
68     if( vlclua_add_modules_path( L, psz_filename ) )
69     {
70         msg_Warn( p_this, "Error while setting the module search path for %s",
71                   psz_filename );
72         lua_close( L );
73         return NULL;
74     }
75
76     return L;
77 }
78
79 /*****************************************************************************
80  * Run a lua entry point function
81  *****************************************************************************/
82 static int run( vlc_object_t *p_this, const char * psz_filename,
83                 lua_State * L, const char *luafunction,
84                 const luabatch_context_t *p_context )
85 {
86     /* Ugly hack to delete previous versions of the fetchart()
87      * functions. */
88     lua_pushnil( L );
89     lua_setglobal( L, luafunction );
90
91     /* Load and run the script(s) */
92     if( luaL_dofile( L, psz_filename ) )
93     {
94         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
95                  lua_tostring( L, lua_gettop( L ) ) );
96         goto error;
97     }
98
99
100     meta_fetcher_scope_t e_scope = FETCHER_SCOPE_NETWORK; /* default to restricted one */
101     lua_getglobal( L, "descriptor" );
102     if( lua_isfunction( L, lua_gettop( L ) ) && !lua_pcall( L, 0, 1, 0 ) )
103     {
104         lua_getfield( L, -1, "scope" );
105         char *psz_scope = luaL_strdupornull( L, -1 );
106         if ( psz_scope && !strcmp( psz_scope, "local" ) )
107             e_scope = FETCHER_SCOPE_LOCAL;
108         free( psz_scope );
109         lua_pop( L, 1 );
110     }
111     lua_pop( L, 1 );
112
113     if ( p_context && p_context->pf_validator && !p_context->pf_validator( p_context, e_scope ) )
114     {
115         msg_Dbg( p_this, "skipping script (unmatched scope) %s", psz_filename );
116         goto error;
117     }
118
119     lua_getglobal( L, luafunction );
120
121     if( !lua_isfunction( L, lua_gettop( L ) ) )
122     {
123         msg_Warn( p_this, "Error while running script %s, "
124                  "function %s() not found", psz_filename, luafunction );
125         goto error;
126     }
127
128     if( lua_pcall( L, 0, 1, 0 ) )
129     {
130         msg_Warn( p_this, "Error while running script %s, "
131                  "function %s(): %s", psz_filename, luafunction,
132                  lua_tostring( L, lua_gettop( L ) ) );
133         goto error;
134     }
135     return VLC_SUCCESS;
136
137 error:
138     lua_pop( L, 1 );
139     return VLC_EGENERIC;
140 }
141
142 /*****************************************************************************
143  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
144  * pointed by psz_filename.
145  *****************************************************************************/
146 static bool validate_scope( const luabatch_context_t *p_context, meta_fetcher_scope_t e_scope )
147 {
148     if ( p_context->e_scope == FETCHER_SCOPE_ANY )
149         return true;
150     else
151         return ( p_context->e_scope == e_scope );
152 }
153
154 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
155                       const luabatch_context_t *p_context )
156 {
157     lua_State *L = init( p_this, p_context->p_item, psz_filename );
158     if( !L )
159         return VLC_EGENERIC;
160
161     int i_ret = run(p_this, psz_filename, L, "fetch_art", p_context);
162     if(i_ret != VLC_SUCCESS)
163     {
164         lua_close( L );
165         return i_ret;
166     }
167
168     if(lua_gettop( L ))
169     {
170         const char * psz_value;
171
172         if( lua_isstring( L, -1 ) )
173         {
174             psz_value = lua_tostring( L, -1 );
175             if( psz_value && *psz_value != 0 )
176             {
177                 lua_Dbg( p_this, "setting arturl: %s", psz_value );
178                 input_item_SetArtURL ( p_context->p_item, psz_value );
179                 lua_close( L );
180                 return VLC_SUCCESS;
181             }
182         }
183         else if( !lua_isnoneornil( L, -1 ) )
184         {
185             msg_Err( p_this, "Lua art fetcher script %s: "
186                  "didn't return a string", psz_filename );
187         }
188     }
189     else
190     {
191         msg_Err( p_this, "Script went completely foobar" );
192     }
193
194     lua_close( L );
195     return VLC_EGENERIC;
196 }
197
198 /*****************************************************************************
199  * Called through lua_scripts_batch_execute to call 'read_meta' on the script
200  * pointed by psz_filename.
201  *****************************************************************************/
202 static int read_meta( vlc_object_t *p_this, const char * psz_filename,
203                       const luabatch_context_t *p_context )
204 {
205     /* FIXME: merge with finder */
206     demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
207     VLC_UNUSED( p_context );
208
209     lua_State *L = init( p_this, p_demux_meta->p_item, psz_filename );
210     if( !L )
211         return VLC_EGENERIC;
212
213     int i_ret = run(p_this, psz_filename, L, "read_meta", NULL);
214     lua_close( L );
215
216     // Continue even if an error occurred: all "meta reader" are always run.
217     return i_ret == VLC_SUCCESS ? VLC_EGENERIC : i_ret;
218 }
219
220
221 /*****************************************************************************
222  * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
223  * pointed by psz_filename.
224  *****************************************************************************/
225 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
226                        const luabatch_context_t *p_context )
227 {
228     lua_State *L = init( p_this, p_context->p_item, psz_filename );
229     if( !L )
230         return VLC_EGENERIC;
231
232     int ret = run(p_this, psz_filename, L, "fetch_meta", p_context);
233     lua_close( L );
234
235     return ret;
236 }
237
238 /*****************************************************************************
239  * Read meta.
240  *****************************************************************************/
241
242 int ReadMeta( demux_meta_t *p_this )
243 {
244     return vlclua_scripts_batch_execute( VLC_OBJECT(p_this), "meta"DIR_SEP"reader",
245                                          (void*) &read_meta, NULL );
246 }
247
248
249 /*****************************************************************************
250  * Read meta.
251  *****************************************************************************/
252
253 int FetchMeta( meta_fetcher_t *p_finder )
254 {
255     luabatch_context_t context = { p_finder->p_item, p_finder->e_scope, validate_scope };
256
257     return vlclua_scripts_batch_execute( VLC_OBJECT(p_finder), "meta"DIR_SEP"fetcher",
258                                          &fetch_meta, (void*)&context );
259 }
260
261
262 /*****************************************************************************
263  * Module entry point for art.
264  *****************************************************************************/
265 int FindArt( meta_fetcher_t *p_finder )
266 {
267     luabatch_context_t context = { p_finder->p_item, p_finder->e_scope, validate_scope };
268
269     return vlclua_scripts_batch_execute( VLC_OBJECT(p_finder), "meta"DIR_SEP"art",
270                                          &fetch_art, (void*)&context );
271 }
272