[ADD/WIP] Start making a simple SAX parser, ASwerk

This commit is contained in:
LDA 2024-06-15 12:29:34 +02:00
commit 79217d3608
14 changed files with 1066 additions and 26 deletions

130
src/AS.c
View file

@ -54,3 +54,133 @@ ASAuthenticateRequest(ParseeConfig *data, HttpClientContext *ctx)
HttpRequestHeader(ctx, "Authorization", bearer);
Free(bearer);
}
bool
ASRegisterUser(ParseeConfig *conf, char *user)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
HttpStatus status;
if (!conf || !user)
{
return false;
}
/* Create user. We don't actually care about the value as we can
* masquerade, as long as it exists. */
ctx = ParseeCreateRequest(
conf,
HTTP_POST, "/_matrix/client/v3/register"
);
json = HashMapCreate();
HashMapSet(json,"type",JsonValueString("m.login.application_service"));
HashMapSet(json,"username",JsonValueString(user));
ASAuthenticateRequest(conf, ctx);
status = ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
return status == HTTP_OK;
}
void
ASPing(ParseeConfig *conf)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path;
if (!conf)
{
return;
}
Log(LOG_NOTICE, "Pinging...");
path = StrConcat(3,
"/_matrix/client/v1/appservice/",
"Parsee%20XMPP",
"/ping"
);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
}
void
ASJoin(ParseeConfig *conf, char *id, char *masquerade)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL, *params_obj;
char *path, *params;
if (!conf || !id)
{
return;
}
params_obj = HashMapCreate();
if (masquerade)
{
HashMapSet(params_obj, "user_id", masquerade);
}
params = HttpParamEncode(params_obj);
HashMapFree(params_obj);
path = StrConcat(4,
"/_matrix/client/v3/rooms/", id, "/join?",
params
);
Free(params);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
}
void
ASSend(ParseeConfig *conf, char *id, char *user, char *type, HashMap *c)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL, *params_obj;
char *path, *params;
char *txn;
if (!conf || !id || !type || !c)
{
return;
}
txn = StrRandom(16);
params_obj = HashMapCreate();
if (user)
{
HashMapSet(params_obj, "user_id", user);
}
params = HttpParamEncode(params_obj);
HashMapFree(params_obj);
path = StrConcat(8,
"/_matrix/client/v3/rooms/",
id, "/send/", type, "/", txn, "?",
params
);
Log(LOG_INFO, "Sending %s", path);
Free(params);
Free(txn);
ctx = ParseeCreateRequest(conf, HTTP_PUT, path);
Free(path);
json = HashMapCreate();
ASAuthenticateRequest(conf, ctx);
Log(LOG_INFO, "%d", ParseeSetRequestJSON(ctx, c));
HttpClientContextFree(ctx);
JsonFree(c);
}