mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 13:45:10 +00:00
206 lines
4.7 KiB
C
206 lines
4.7 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
|
|
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;
|
|
HashMap *json;
|
|
char *path;
|
|
if (!conf || !user || !name)
|
|
{
|
|
return;
|
|
}
|
|
|
|
user = HttpUrlEncode(user);
|
|
path = StrConcat(6,
|
|
"/_matrix/client/v3/profile/",
|
|
user, "/displayname", "?",
|
|
"user_id=", user
|
|
);
|
|
|
|
json = HashMapCreate();
|
|
HashMapSet(json, "displayname", JsonValueString(name));
|
|
ctx = ParseeCreateRequest(conf, HTTP_PUT, path);
|
|
Free(path);
|
|
ASAuthenticateRequest(conf, ctx);
|
|
ParseeSetRequestJSON(ctx, json);
|
|
|
|
HttpClientContextFree(ctx);
|
|
JsonFree(json);
|
|
Free(user);
|
|
}
|
|
|
|
char *
|
|
ASGetName(const ParseeConfig *c, char *room, char *user)
|
|
{
|
|
HttpClientContext *ctx;
|
|
HashMap *reply;
|
|
char *path, *ret;
|
|
char *u2 = user;
|
|
if (!c || !user)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
if (!room)
|
|
{
|
|
user = HttpUrlEncode(user);
|
|
path = StrConcat(3,
|
|
"/_matrix/client/v3/profile/", user, "/displayname"
|
|
);
|
|
ctx = ParseeCreateRequest(c, HTTP_GET, path);
|
|
Free(user);
|
|
ASAuthenticateRequest(c, ctx);
|
|
HttpRequestSendHeaders(ctx);
|
|
HttpRequestSend(ctx);
|
|
|
|
reply = JsonDecode(HttpClientStream(ctx));
|
|
|
|
ret = StrDuplicate(
|
|
JsonValueAsString(HashMapGet(reply, "displayname"))
|
|
);
|
|
HttpClientContextFree(ctx);
|
|
JsonFree(reply);
|
|
Free(path);
|
|
|
|
if (!ret)
|
|
{
|
|
ret = StrDuplicate(u2);
|
|
}
|
|
return ret;
|
|
}
|
|
user = HttpUrlEncode(user);
|
|
room = HttpUrlEncode(room);
|
|
path = StrConcat(4,
|
|
"/_matrix/client/v3/rooms/", room,
|
|
"/state/m.room.member/", user
|
|
);
|
|
ctx = ParseeCreateRequest(c, HTTP_GET, path);
|
|
Free(user);
|
|
Free(room);
|
|
ASAuthenticateRequest(c, ctx);
|
|
HttpRequestSendHeaders(ctx);
|
|
HttpRequestSend(ctx);
|
|
|
|
reply = JsonDecode(HttpClientStream(ctx));
|
|
|
|
ret = StrDuplicate(
|
|
JsonValueAsString(HashMapGet(reply, "displayname"))
|
|
);
|
|
HttpClientContextFree(ctx);
|
|
JsonFree(reply);
|
|
Free(path);
|
|
|
|
if (!ret)
|
|
{
|
|
ret = StrDuplicate(u2);
|
|
}
|
|
return ret;
|
|
}
|
|
char *
|
|
ASGetAvatar(const ParseeConfig *c, char *room, char *user)
|
|
{
|
|
HttpClientContext *ctx;
|
|
HashMap *reply;
|
|
char *path = NULL, *ret = NULL;
|
|
char *u2 = user;
|
|
if (!c || !user)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
if (room)
|
|
{
|
|
user = HttpUrlEncode(user);
|
|
room = HttpUrlEncode(room);
|
|
path = StrConcat(4,
|
|
"/_matrix/client/v3/rooms/", room,
|
|
"/state/m.room.member/", user
|
|
);
|
|
ctx = ParseeCreateRequest(c, HTTP_GET, path);
|
|
Free(user);
|
|
Free(room);
|
|
ASAuthenticateRequest(c, ctx);
|
|
HttpRequestSendHeaders(ctx);
|
|
HttpRequestSend(ctx);
|
|
|
|
reply = JsonDecode(HttpClientStream(ctx));
|
|
|
|
ret = StrDuplicate(
|
|
JsonValueAsString(HashMapGet(reply, "avatar_url"))
|
|
);
|
|
HttpClientContextFree(ctx);
|
|
JsonFree(reply);
|
|
Free(path);
|
|
|
|
user = u2;
|
|
|
|
Log(LOG_DEBUG, "ASGetAvatar: trying to grab avatar from room, got %s", ret);
|
|
}
|
|
|
|
if (!ret)
|
|
{
|
|
user = HttpUrlEncode(user);
|
|
path = StrConcat(3,
|
|
"/_matrix/client/v3/profile/", user, "/avatar_url"
|
|
);
|
|
ctx = ParseeCreateRequest(c, HTTP_GET, path);
|
|
Free(user);
|
|
user = u2;
|
|
ASAuthenticateRequest(c, ctx);
|
|
HttpRequestSendHeaders(ctx);
|
|
HttpRequestSend(ctx);
|
|
|
|
reply = JsonDecode(HttpClientStream(ctx));
|
|
|
|
ret = StrDuplicate(
|
|
JsonValueAsString(HashMapGet(reply, "avatar_url"))
|
|
);
|
|
HttpClientContextFree(ctx);
|
|
JsonFree(reply);
|
|
Free(path);
|
|
|
|
Log(LOG_DEBUG, "ASGetAvatar: trying to grab avatar from profile, got %s", ret);
|
|
}
|
|
|
|
return ret;
|
|
}
|