]> git.ipfire.org Git - people/ms/libloc.git/blame - src/lua/location.c
lua: Add a Database object with a dummy lookup function
[people/ms/libloc.git] / src / lua / location.c
CommitLineData
6ae775f9
MT
1/*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2024 IPFire Development Team <info@ipfire.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15*/
16
f51350bb
MT
17#include <errno.h>
18#include <string.h>
19
6ae775f9 20#include <lua.h>
03b9c3a4
MT
21#include <lauxlib.h>
22#include <lualib.h>
6ae775f9 23
f51350bb
MT
24#include <libloc/libloc.h>
25
6ae775f9 26#include "location.h"
7eaabd10 27#include "database.h"
6ae775f9 28
f51350bb
MT
29struct loc_ctx* ctx = NULL;
30
03b9c3a4
MT
31static int version(lua_State* L) {
32 lua_pushstring(L, PACKAGE_VERSION);
33 return 1;
34}
35
36static const struct luaL_Reg location_functions[] = {
37 { "version", version },
38 { NULL, NULL },
39};
40
6ae775f9 41int luaopen_location(lua_State* L) {
f51350bb
MT
42 int r;
43
44 // Initialize the context
45 r = loc_new(&ctx);
46 if (r)
47 return luaL_error(L,
48 "Could not initialize location context: %s\n", strerror(errno));
49
50 // Register functions
03b9c3a4
MT
51 luaL_newlib(L, location_functions);
52
7eaabd10
MT
53 // Register Database type
54 register_database(L);
55
56 lua_setfield(L, -2, "Database");
57
03b9c3a4 58 return 1;
6ae775f9 59}