mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 21:25:11 +00:00
There is still this fun issue with the blocking XML parser leaking memory on pthread_cancel, I'll try to deal with that later, maybe with a test stanza. I also apologise for swearing in the last commit. I promise it won't happen again, Prosody.
172 lines
3.6 KiB
C
172 lines
3.6 KiB
C
#include <AS.h>
|
|
|
|
#include <Cytoplasm/Memory.h>
|
|
#include <Cytoplasm/Str.h>
|
|
#include <Cytoplasm/Log.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <Matrix.h>
|
|
|
|
HashMap *
|
|
ASVerifyRequest(ParseeHttpArg *arg)
|
|
{
|
|
HashMap *ret = NULL;
|
|
HashMap *params;
|
|
char *authorisation;
|
|
if (!arg)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
params = HttpRequestHeaders(arg->ctx);
|
|
authorisation = HashMapGet(params, "authorization");
|
|
|
|
if (!authorisation || strncmp(authorisation, "Bearer ", 7))
|
|
{
|
|
HttpResponseStatus(arg->ctx, HTTP_FORBIDDEN);
|
|
ret = MatrixCreateError("M_MISSING_TOKEN", "No 'hs_token' given in.");
|
|
goto end;
|
|
}
|
|
|
|
authorisation += 7;
|
|
if (!StrEquals(authorisation, arg->data->config->hs_token))
|
|
{
|
|
HttpResponseStatus(arg->ctx, HTTP_FORBIDDEN);
|
|
ret = MatrixCreateError("M_FORBIDDEN","Incorrect authorisation given");
|
|
goto end;
|
|
}
|
|
end:
|
|
return ret;
|
|
}
|
|
|
|
void
|
|
ASAuthenticateRequest(const ParseeConfig *data, HttpClientContext *ctx)
|
|
{
|
|
char *bearer;
|
|
if (!data || !ctx)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bearer = StrConcat(2, "Bearer ", data->as_token);
|
|
HttpRequestHeader(ctx, "Authorization", bearer);
|
|
Free(bearer);
|
|
}
|
|
bool
|
|
ASRegisterUser(const 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"));
|
|
|
|
user = ParseeGetLocal(user);
|
|
HashMapSet(json,"username",JsonValueString(user));
|
|
|
|
ASAuthenticateRequest(conf, ctx);
|
|
status = ParseeSetRequestJSON(ctx, json);
|
|
|
|
HttpClientContextFree(ctx);
|
|
JsonFree(json);
|
|
|
|
Free(user);
|
|
|
|
return status == HTTP_OK;
|
|
}
|
|
|
|
void
|
|
ASPing(const ParseeConfig *conf)
|
|
{
|
|
HttpClientContext *ctx = NULL;
|
|
HashMap *json = NULL;
|
|
char *path;
|
|
if (!conf)
|
|
{
|
|
return;
|
|
}
|
|
|
|
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(const ParseeConfig *conf, char *id, char *masquerade)
|
|
{
|
|
HttpClientContext *ctx = NULL;
|
|
HashMap *json = NULL;
|
|
char *path;
|
|
if (!conf || !id || !masquerade)
|
|
{
|
|
return;
|
|
}
|
|
|
|
path = StrConcat(5,
|
|
"/_matrix/client/v3/rooms/", id, "/join?",
|
|
"user_id=", masquerade
|
|
);
|
|
|
|
ctx = ParseeCreateRequest(
|
|
conf,
|
|
HTTP_POST, path
|
|
);
|
|
Free(path);
|
|
json = HashMapCreate();
|
|
ASAuthenticateRequest(conf, ctx);
|
|
ParseeSetRequestJSON(ctx, json);
|
|
|
|
HttpClientContextFree(ctx);
|
|
JsonFree(json);
|
|
}
|
|
void
|
|
ASSend(const ParseeConfig *conf, char *id, char *user, char *type, HashMap *c)
|
|
{
|
|
HttpClientContext *ctx = NULL;
|
|
char *path, *params;
|
|
char *txn;
|
|
if (!conf || !id || !type || !user || !c)
|
|
{
|
|
return;
|
|
}
|
|
|
|
txn = StrRandom(16);
|
|
path = StrConcat(9,
|
|
"/_matrix/client/v3/rooms/",
|
|
id, "/send/", type, "/", txn, "?",
|
|
"user_id=", user
|
|
);
|
|
Free(txn);
|
|
|
|
ctx = ParseeCreateRequest(conf, HTTP_PUT, path);
|
|
Free(path);
|
|
ASAuthenticateRequest(conf, ctx);
|
|
ParseeSetRequestJSON(ctx, c);
|
|
|
|
HttpClientContextFree(ctx);
|
|
JsonFree(c);
|
|
}
|