Parsee/src/AS/Room.c

366 lines
7.9 KiB
C

#include <AS.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <Cytoplasm/Uri.h>
#include <string.h>
#include <stdlib.h>
#include <Matrix.h>
void
ASInvite(const ParseeConfig *conf, char *id, char *invited)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path, *bridge;
if (!conf || !id || !invited)
{
return;
}
bridge = StrConcat(4,
"@", conf->sender_localpart,
":", conf->server_base
);
id = HttpUrlEncode(id);
path = StrConcat(5,
"/_matrix/client/v3/rooms/", id, "/invite",
"?user_id=", bridge
);
Free(bridge);
Free(id);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
HashMapSet(json, "user_id", JsonValueString(invited));
HashMapSet(json, "reason", JsonValueString("Pass over."));
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
}
void
ASBan(const ParseeConfig *conf, char *id, char *banned)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path, *bridge;
if (!conf || !id || !banned)
{
return;
}
bridge = StrConcat(4,
"@", conf->sender_localpart,
":", conf->server_base
);
id = HttpUrlEncode(id);
path = StrConcat(5,
"/_matrix/client/v3/rooms/", id, "/ban",
"?user_id=", bridge
);
Free(bridge);
Free(id);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
HashMapSet(json, "user_id", JsonValueString(banned));
HashMapSet(json, "reason", JsonValueString(NAME " felt jealous."));
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
}
void
ASKick(const ParseeConfig *conf, char *id, char *banned)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path, *bridge;
if (!conf || !id || !banned)
{
return;
}
bridge = StrConcat(4,
"@", conf->sender_localpart,
":", conf->server_base
);
id = HttpUrlEncode(id);
path = StrConcat(5,
"/_matrix/client/v3/rooms/", id, "/kick",
"?user_id=", bridge
);
Free(bridge);
Free(id);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
HashMapSet(json, "user_id", JsonValueString(banned));
HashMapSet(json, "reason", JsonValueString(NAME " felt jealous."));
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
}
char *
ASJoin(const ParseeConfig *conf, char *id, char *masquerade)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path, *ret, *serv;
int status;
if (!conf || !id)
{
return NULL;
}
if (!masquerade)
{
char *raw = StrConcat(4,
"@", conf->sender_localpart,
":", conf->server_base
);
masquerade = HttpUrlEncode(raw);
Free(raw);
}
else
{
masquerade = HttpUrlEncode(masquerade);
}
serv = strchr(id, ':');
if (serv)
{
serv = serv + 1;
}
id = HttpUrlEncode(id);
path = StrConcat(5,
"/_matrix/client/v3/join/", id, "?",
"user_id=", masquerade
);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
ASAuthenticateRequest(conf, ctx);
status = ParseeSetRequestJSON(ctx, json);
JsonFree(json);
json = JsonDecode(HttpClientStream(ctx));
ret = StrDuplicate(GrabString(json, 1, "room_id"));
JsonFree(json);
HttpClientContextFree(ctx);
Free(masquerade);
Free(id);
(void) serv; // TODO
return ret;
}
void
ASLeave(const ParseeConfig *conf, char *id, char *masquerade)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path;
if (!conf || !id)
{
return;
}
if (!masquerade)
{
char *raw = StrConcat(4,
"@", conf->sender_localpart,
":", conf->server_base
);
masquerade = HttpUrlEncode(raw);
Free(raw);
}
else
{
masquerade = HttpUrlEncode(masquerade);
}
id = HttpUrlEncode(id);
path = StrConcat(5,
"/_matrix/client/v3/rooms/", id, "/leave?",
"user_id=", masquerade
);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
JsonFree(json);
HttpClientContextFree(ctx);
Free(masquerade);
Free(id);
}
char *
ASCreateRoom(const ParseeConfig *conf, char *by, char *alias)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path, *id;
if (!conf || !by)
{
return NULL;
}
path = StrConcat(3,
"/_matrix/client/v3/createRoom",
"?user_id=", by
);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
if (alias)
{
char *trimmed = StrDuplicate(alias);
if (*alias == '#')
{
char *tmp, cb[2] = { 0 };
alias++;
Free(trimmed);
trimmed = NULL;
while (*alias && *alias != ':')
{
cb[0] = *alias;
tmp = trimmed;
trimmed = StrConcat(2, trimmed, cb);
Free(tmp);
alias ++;
}
}
HashMapSet(json, "room_alias_name", JsonValueString(trimmed));
Free(trimmed);
}
HashMapSet(json, "visibility", JsonValueString("public"));
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
JsonFree(json);
json = JsonDecode(HttpClientStream(ctx));
id = StrDuplicate(JsonValueAsString(HashMapGet(json, "room_id")));
HttpClientContextFree(ctx);
JsonFree(json);
return id;
}
char *
ASCreateDM(const ParseeConfig *conf, char *by, char *with)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path, *id;
if (!conf || !by || !with)
{
return NULL;
}
path = StrConcat(3,
"/_matrix/client/v3/createRoom",
"?user_id=", by
);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
{
Array *invitees = ArrayCreate();
ArrayAdd(invitees, JsonValueString(with));
HashMapSet(json, "invite", JsonValueArray(invitees));
HashMapSet(json, "is_direct", JsonValueBoolean(true));
}
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
JsonFree(json);
json = JsonDecode(HttpClientStream(ctx));
id = StrDuplicate(JsonValueAsString(HashMapGet(json, "room_id")));
HttpClientContextFree(ctx);
JsonFree(json);
return id;
}
HashMap *
ASGetPL(const ParseeConfig *c, char *room)
{
char *path;
HttpClientContext *ctx;
HashMap *reply;
if (!c || !room)
{
return NULL;
}
room = HttpUrlEncode(room);
path = StrConcat(4,
"/_matrix/client/v3/rooms/", room,
"/state/m.room.power_levels/", ""
);
ctx = ParseeCreateRequest(c, HTTP_GET, path);
Free(room);
ASAuthenticateRequest(c, ctx);
HttpRequestSendHeaders(ctx);
HttpRequestSend(ctx);
reply = JsonDecode(HttpClientStream(ctx));
HttpClientContextFree(ctx);
Free(path);
return reply;
}
void
ASSetPL(const ParseeConfig *conf, char *id, HashMap *m)
{
char *user;
if (!conf || !id || !m)
{
return;
}
user = StrConcat(4,
"@",conf->sender_localpart,
":",conf->server_base
);
ASSetState(conf, id, "m.room.power_levels", "", user, m);
Free(user);
}