]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/acl.c
lua: remove "bla ? true : false"
[vlc] / modules / misc / lua / libs / acl.c
1 /*****************************************************************************
2  * acl.c: Access list related functions
3  *****************************************************************************
4  * Copyright (C) 2007-2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_acl.h>
37
38 #include <lua.h>        /* Low level lua C API */
39 #include <lauxlib.h>    /* Higher level C API */
40
41 #include "../vlc.h"
42 #include "../libs.h"
43
44 /*****************************************************************************
45  *
46  *****************************************************************************/
47 static int vlclua_acl_create_inner( lua_State *, vlc_acl_t * );
48 static int vlclua_acl_delete( lua_State * );
49 static int vlclua_acl_check( lua_State * );
50 static int vlclua_acl_duplicate( lua_State * );
51 static int vlclua_acl_add_host( lua_State * );
52 static int vlclua_acl_add_net( lua_State * );
53 static int vlclua_acl_load_file( lua_State * );
54
55 static const luaL_Reg vlclua_acl_reg[] = {
56     { "check", vlclua_acl_check },
57     { "duplicate", vlclua_acl_duplicate },
58     { "add_host", vlclua_acl_add_host },
59     { "add_net", vlclua_acl_add_net },
60     { "load_file", vlclua_acl_load_file },
61     { NULL, NULL }
62 };
63
64 static int vlclua_acl_create( lua_State *L )
65 {
66     vlc_object_t *p_this = vlclua_get_this( L );
67     bool b_allow = luaL_checkboolean( L, 1 );
68     vlc_acl_t *p_acl = ACL_Create( p_this, b_allow );
69     return vlclua_acl_create_inner( L, p_acl );
70 }
71
72 static int vlclua_acl_create_inner( lua_State *L, vlc_acl_t *p_acl )
73 {
74     if( !p_acl )
75         return luaL_error( L, "ACL creation failed." );
76
77     vlc_acl_t **pp_acl = lua_newuserdata( L, sizeof( vlc_acl_t * ) );
78     *pp_acl = p_acl;
79
80     if( luaL_newmetatable( L, "acl" ) )
81     {
82         lua_newtable( L );
83         luaL_register( L, NULL, vlclua_acl_reg );
84         lua_setfield( L, -2, "__index" );
85         lua_pushcfunction( L, vlclua_acl_delete );
86         lua_setfield( L, -2, "__gc" );
87     }
88
89     lua_setmetatable( L, -2 );
90     return 1;
91 }
92
93 static int vlclua_acl_delete( lua_State *L )
94 {
95     vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
96     ACL_Destroy( *pp_acl );
97     return 0;
98 }
99
100 static int vlclua_acl_check( lua_State *L )
101 {
102     vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
103     const char *psz_ip = luaL_checkstring( L, 2 );
104     lua_pushinteger( L, ACL_Check( *pp_acl, psz_ip ) );
105     return 1;
106 }
107
108 static int vlclua_acl_duplicate( lua_State *L )
109 {
110     vlc_object_t *p_this = vlclua_get_this( L );
111     vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
112     vlc_acl_t *p_acl_new = ACL_Duplicate( p_this, *pp_acl );
113     return vlclua_acl_create_inner( L, p_acl_new );
114 }
115
116 static int vlclua_acl_add_host( lua_State *L )
117 {
118     vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
119     const char *psz_ip = luaL_checkstring( L, 2 );
120     bool b_allow = luaL_checkboolean( L, 3 );
121     lua_pushinteger( L, ACL_AddHost( *pp_acl, psz_ip, b_allow ) );
122     return 1;
123 }
124
125 static int vlclua_acl_add_net( lua_State *L )
126 {
127     vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
128     const char *psz_ip = luaL_checkstring( L, 2 );
129     int i_len = luaL_checkint( L, 3 );
130     bool b_allow = luaL_checkboolean( L, 4 );
131     lua_pushinteger( L, ACL_AddNet( *pp_acl, psz_ip, i_len, b_allow ) );
132     return 1;
133 }
134
135 static int vlclua_acl_load_file( lua_State *L )
136 {
137     vlc_acl_t **pp_acl = (vlc_acl_t**)luaL_checkudata( L, 1, "acl" );
138     const char *psz_path = luaL_checkstring( L, 2 );
139     lua_pushinteger( L, ACL_LoadFile( *pp_acl, psz_path ) );
140     return 1;
141 }
142
143 void luaopen_acl( lua_State *L )
144 {
145     lua_pushcfunction( L, vlclua_acl_create );
146     lua_setfield( L, -2, "acl" );
147 }