[ADD/WIP] XMPP->Matrix avatar, start bridging bans

I still hate XEP-0084.
This commit is contained in:
LDA 2024-07-05 03:10:43 +02:00
commit 1f658ece76
9 changed files with 587 additions and 27 deletions

171
src/AS.c
View file

@ -188,6 +188,41 @@ ASBan(const ParseeConfig *conf, char *id, char *banned)
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->homeserver_host
);
path = StrConcat(5,
"/_matrix/client/v3/rooms/", id, "/kick",
"?user_id=", bridge
);
Free(bridge);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
HashMapSet(json, "user_id", JsonValueString(banned));
HashMapSet(json, "reason", JsonValueString("Parsee felt jealous."));
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
}
void
ASJoin(const ParseeConfig *conf, char *id, char *masquerade)
{
HttpClientContext *ctx = NULL;
@ -331,6 +366,35 @@ ASCreateRoom(const ParseeConfig *conf, char *by, char *alias)
return id;
}
void
ASSetAvatar(const ParseeConfig *conf, char *user, char *mxc)
{
HttpClientContext *ctx = NULL;
HashMap *json;
char *path;
if (!conf || !user || !mxc)
{
return;
}
user = HttpUrlEncode(user);
path = StrConcat(6,
"/_matrix/client/v3/profile/",
user, "/avatar_url", "?",
"user_id=", user
);
json = HashMapCreate();
HashMapSet(json, "avatar_url", JsonValueString(mxc));
ctx = ParseeCreateRequest(conf, HTTP_PUT, path);
Free(path);
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
HttpClientContextFree(ctx);
JsonFree(json);
Free(user);
}
void
ASSetName(const ParseeConfig *conf, char *user, char *name)
{
HttpClientContext *ctx = NULL;
@ -510,6 +574,7 @@ ASUpload(const ParseeConfig *c, Stream *from, unsigned int size)
HashMap *reply;
if (!c || !from)
{
Log(LOG_INFO, "Obvious upload fail");
return NULL;
}
@ -526,13 +591,26 @@ ASUpload(const ParseeConfig *c, Stream *from, unsigned int size)
}
HttpRequestSendHeaders(ctx);
StreamCopy(from, HttpClientStream(ctx));
for (i = 0; i < size; i++)
{
int ch = StreamGetc(from);
if (ch == EOF)
{
break;
}
StreamPutc(HttpClientStream(ctx), ch);
}
HttpRequestSend(ctx);
reply = JsonDecode(HttpClientStream(ctx));
ret = StrDuplicate(
JsonValueAsString(HashMapGet(reply, "content_uri"))
);
if (!ret)
{
JsonEncode(reply, StreamStdout(), JSON_PRETTY);
StreamFlush(StreamStdout());
Log(LOG_INFO, "Less obvious upload fail");
}
HttpClientContextFree(ctx);
JsonFree(reply);
Free(size_str);
@ -632,3 +710,92 @@ ASType(const ParseeConfig *c, char *user, char *room, bool status)
HttpClientContextFree(ctx);
Free(user);
}
HashMap *
ASGetUserConfig(const ParseeConfig *c, char *user, char *key)
{
HttpClientContext *ctx = NULL;
HashMap *json;
char *path;
if (!c || !key)
{
return NULL;
}
if (!user)
{
char *raw = StrConcat(4,
"@", c->sender_localpart,
":", c->homeserver_host
);
user = HttpUrlEncode(raw);
Free(raw);
}
else
{
user = HttpUrlEncode(user);
}
path = StrConcat(7,
"/_matrix/client/v3/user/",
user, "/account_data/", key, "?",
"user_id=", user
);
ctx = ParseeCreateRequest(c, HTTP_GET, path);
Free(path);
ASAuthenticateRequest(c, ctx);
HttpRequestSendHeaders(ctx);
HttpRequestSend(ctx);
json = JsonDecode(HttpClientStream(ctx));
HttpClientContextFree(ctx);
Free(user);
return json;
}
void
ASSetUserConfig(const ParseeConfig *c, char *user, char *key, HashMap *map)
{
HttpClientContext *ctx = NULL;
HashMap *json;
char *path;
if (!c || !key || !map)
{
JsonFree(map);
return;
}
if (!user)
{
char *raw = StrConcat(4,
"@", c->sender_localpart,
":", c->homeserver_host
);
user = HttpUrlEncode(raw);
Free(raw);
}
else
{
user = HttpUrlEncode(user);
}
path = StrConcat(7,
"/_matrix/client/v3/user/",
user, "/account_data/", key, "?",
"user_id=", user
);
ctx = ParseeCreateRequest(c, HTTP_PUT, path);
Free(path);
ASAuthenticateRequest(c, ctx);
ParseeSetRequestJSON(ctx, map);
HttpClientContextFree(ctx);
Free(user);
JsonFree(map);
return;
}