mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 12:15:12 +00:00
[MOD] Read the config file for tool DB access
This commit is contained in:
parent
5d268a71a5
commit
9775bd8d4e
6 changed files with 157 additions and 115 deletions
|
|
@ -13,12 +13,7 @@
|
|||
* Under CC0, as its a rather useful example of a Parsee tool.
|
||||
* See LICENSE for more information about Parsee's licensing. */
|
||||
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Json.h>
|
||||
#include <Cytoplasm/Log.h>
|
||||
#include <Cytoplasm/Db.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "common.h"
|
||||
|
||||
static void
|
||||
AddAdmin(Db *parsee, char *glob)
|
||||
|
|
@ -86,14 +81,14 @@ Main(Array *args, HashMap *env)
|
|||
|
||||
if (ArraySize(args) < 2)
|
||||
{
|
||||
Log(LOG_ERR, "Usage: %s [DB path] [glob]", exec);
|
||||
Log(LOG_ERR, "Usage: %s [config] <glob>", exec);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
db_path = ArrayGet(args, 1);
|
||||
glob = ArrayGet(args, 2);
|
||||
|
||||
parsee = DbOpenLMDB(db_path, 8 * 1024 * 1024);
|
||||
parsee = GetDB(db_path);
|
||||
if (parsee)
|
||||
{
|
||||
|
||||
|
|
|
|||
118
tools/common.h
Normal file
118
tools/common.h
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/* Single-header tools for Parsee tools. */
|
||||
|
||||
#include <Cytoplasm/HttpClient.h>
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Json.h>
|
||||
#include <Cytoplasm/Args.h>
|
||||
#include <Cytoplasm/Util.h>
|
||||
#include <Cytoplasm/Log.h>
|
||||
#include <Cytoplasm/Uri.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
#include <Cytoplasm/Db.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define GrabString(obj, ...) JsonValueAsString(JsonGet(obj, __VA_ARGS__))
|
||||
#define GrabInteger(obj, ...) JsonValueAsInteger(JsonGet(obj, __VA_ARGS__))
|
||||
#define GrabBoolean(obj, ...) JsonValueAsBoolean(JsonGet(obj, __VA_ARGS__))
|
||||
#define GrabObject(obj, ...) JsonValueAsObject(JsonGet(obj, __VA_ARGS__))
|
||||
#define GrabArray(obj, ...) JsonValueAsArray(JsonGet(obj, __VA_ARGS__))
|
||||
|
||||
Uri *
|
||||
DelegateServer(char *raw)
|
||||
{
|
||||
HttpClientContext *ctx = NULL;
|
||||
Stream *stream = NULL;
|
||||
HashMap *reply = NULL;
|
||||
Uri *ret = NULL;
|
||||
|
||||
if (!raw)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ctx = HttpRequest(
|
||||
HTTP_GET,
|
||||
HTTP_FLAG_TLS, 443,
|
||||
raw, "/.well-known/matrix/client"
|
||||
);
|
||||
HttpRequestSendHeaders(ctx);
|
||||
|
||||
switch (HttpRequestSend(ctx))
|
||||
{
|
||||
case HTTP_OK:
|
||||
stream = HttpClientStream(ctx);
|
||||
reply = JsonDecode(stream);
|
||||
if (!reply)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = UriParse(JsonValueAsString(JsonGet(
|
||||
reply, 2,
|
||||
"m.homeserver", "base_url"
|
||||
)));
|
||||
break;
|
||||
default:
|
||||
/* TODO */
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret && !ret->port && StrEquals(ret->proto, "https"))
|
||||
{
|
||||
ret->port = 443;
|
||||
}
|
||||
if (ret && !ret->port)
|
||||
{
|
||||
ret->port = 80;
|
||||
}
|
||||
|
||||
end:
|
||||
HttpClientContextFree(ctx);
|
||||
JsonFree(reply);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Db *
|
||||
GetDB(char *config)
|
||||
{
|
||||
size_t lmdb_size;
|
||||
Db *ret = NULL;
|
||||
char *db_path;
|
||||
HashMap *json;
|
||||
Stream *file;
|
||||
if (!config)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
file = StreamOpen(config, "r");
|
||||
json = JsonDecode(file);
|
||||
StreamClose(file);
|
||||
if (!json)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lmdb_size = GrabInteger(json, 1, "db_size");
|
||||
db_path = GrabString(json, 1, "db");
|
||||
|
||||
if (!db_path)
|
||||
{
|
||||
JsonFree(json);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (lmdb_size)
|
||||
{
|
||||
ret = DbOpenLMDB(db_path, lmdb_size);
|
||||
}
|
||||
if (!ret)
|
||||
{
|
||||
ret = DbOpen(db_path, 0);
|
||||
}
|
||||
|
||||
JsonFree(json);
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -10,73 +10,7 @@
|
|||
* Under CC0, as its a rather useful example of a Parsee tool.
|
||||
* See LICENSE for more information about Parsee's licensing. */
|
||||
|
||||
#include <Cytoplasm/HttpClient.h>
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Json.h>
|
||||
#include <Cytoplasm/Args.h>
|
||||
#include <Cytoplasm/Util.h>
|
||||
#include <Cytoplasm/Log.h>
|
||||
#include <Cytoplasm/Uri.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
#include <Cytoplasm/Db.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static Uri *
|
||||
DelegateServer(char *raw)
|
||||
{
|
||||
HttpClientContext *ctx = NULL;
|
||||
Stream *stream = NULL;
|
||||
HashMap *reply = NULL;
|
||||
Uri *ret = NULL;
|
||||
|
||||
if (!raw)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ctx = HttpRequest(
|
||||
HTTP_GET,
|
||||
HTTP_FLAG_TLS, 443,
|
||||
raw, "/.well-known/matrix/client"
|
||||
);
|
||||
HttpRequestSendHeaders(ctx);
|
||||
|
||||
switch (HttpRequestSend(ctx))
|
||||
{
|
||||
case HTTP_OK:
|
||||
stream = HttpClientStream(ctx);
|
||||
reply = JsonDecode(stream);
|
||||
if (!reply)
|
||||
{
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = UriParse(JsonValueAsString(JsonGet(
|
||||
reply, 2,
|
||||
"m.homeserver", "base_url"
|
||||
)));
|
||||
break;
|
||||
default:
|
||||
/* TODO */
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret && !ret->port && StrEquals(ret->proto, "https"))
|
||||
{
|
||||
ret->port = 443;
|
||||
}
|
||||
if (ret && !ret->port)
|
||||
{
|
||||
ret->port = 80;
|
||||
}
|
||||
|
||||
end:
|
||||
HttpClientContextFree(ctx);
|
||||
JsonFree(reply);
|
||||
|
||||
return ret;
|
||||
}
|
||||
#include "common.h"
|
||||
|
||||
int
|
||||
Main(Array *args, HashMap *env)
|
||||
|
|
|
|||
|
|
@ -3,12 +3,7 @@
|
|||
* Under CC0, as its a rather useful example of a Parsee tool.
|
||||
* See LICENSE for more information about Parsee's licensing. */
|
||||
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Json.h>
|
||||
#include <Cytoplasm/Log.h>
|
||||
#include <Cytoplasm/Db.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "common.h"
|
||||
|
||||
int
|
||||
Main(Array *args, HashMap *env)
|
||||
|
|
@ -20,13 +15,13 @@ Main(Array *args, HashMap *env)
|
|||
|
||||
if (ArraySize(args) < 2)
|
||||
{
|
||||
Log(LOG_ERR, "Usage: %s [DB path]", exec);
|
||||
Log(LOG_ERR, "Usage: %s [config]", exec);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
db_path = ArrayGet(args, 1);
|
||||
|
||||
parsee = DbOpenLMDB(db_path, 8 * 1024 * 1024);
|
||||
parsee = GetDB(db_path);
|
||||
if (parsee)
|
||||
{
|
||||
DbDelete(parsee, 1, "avatars");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue