Parsee/src/Events.c
LDA 147e430a47 [FIX] Do NOT use strlen in a loop
Never. Do. This. Never. It should never cross your mind. Doing so will
punish you. Basic computer science concepts will tell you it's O(n^2).
And it WILL actually matter. Never. Never. Never. Never. Never. Never.
            NEVER FUCKING EVER DO THAT EVER AGAIN.
            NEVER FUCKING EVER DO THAT EVER AGAIN.
            NEVER FUCKING EVER DO THAT EVER AGAIN.
            NEVER FUCKING EVER DO THAT EVER AGAIN.
            NEVER FUCKING EVER DO THAT EVER AGAIN.
Anyways, also fixes licensing year. On est en octobre et il comprend
toujours pas qu'on est plus en 2023.
2024-10-11 19:49:19 +02:00

236 lines
5.1 KiB
C

#include <Matrix.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <XEP393.h>
#include <string.h>
HashMap *
MatrixCreateNotice(char *body)
{
HashMap *map;
if (!body)
{
return NULL;
}
map = HashMapCreate();
HashMapSet(map, "msgtype", JsonValueString("m.notice"));
HashMapSet(map, "body", JsonValueString(body));
return map;
}
HashMap *
MatrixCreateMessage(char *body)
{
HashMap *map;
char *text = NULL;
if (!body)
{
return NULL;
}
map = HashMapCreate();
HashMapSet(map, "msgtype", JsonValueString("m.text"));
HashMapSet(map, "body", JsonValueString(body));
{
XEP393Element *e = XEP393(body);
text = XEP393ToXMLString(e);
XEP393FreeElement(e);
HashMapSet(map, "formatted_body", JsonValueString(text));
HashMapSet(map, "format", JsonValueString("org.matrix.custom.html"));
Free(text);
}
return map;
}
HashMap *
MatrixCreateNameState(char *name)
{
HashMap *map;
if (!name)
{
return NULL;
}
map = HashMapCreate();
HashMapSet(map, "name", JsonValueString(name));
return map;
}
HashMap *
MatrixCreateNickChange(char *nick)
{
HashMap *map;
if (!nick)
{
return NULL;
}
map = HashMapCreate();
HashMapSet(map, "displayname", JsonValueString(nick));
HashMapSet(map, "membership", JsonValueString("join"));
return map;
}
HashMap *
MatrixCreateMedia(char *mxc, char *body, char *mime, FileInfo *info)
{
HashMap *map;
char *mime_type = NULL, *matrix_type = NULL;
if (!mxc || !body)
{
return NULL;
}
matrix_type = "m.file";
if (mime)
{
size_t i, len;
mime_type = StrDuplicate(mime);
len = strlen(mime);
for (i = 0; i < len; i++)
{
if (mime_type[i] == '/')
{
mime_type[i] = '\0';
break;
}
}
if (StrEquals(mime_type, "image"))
{
matrix_type = "m.image";
}
else if (StrEquals(mime_type, "video"))
{
matrix_type = "m.video";
}
else if (StrEquals(mime_type, "audio"))
{
matrix_type = "m.audio";
}
Free(mime_type);
}
map = HashMapCreate();
JsonSet(map, JsonValueString(mime), 2, "info", "mimetype");
if (info && info->width && info->height)
{
JsonSet(map, JsonValueInteger(info->width), 2, "info", "w");
JsonSet(map, JsonValueInteger(info->height), 2, "info", "h");
}
HashMapSet(map, "msgtype", JsonValueString(matrix_type));
HashMapSet(map, "mimetype", JsonValueString(mime));
HashMapSet(map, "body", JsonValueString(body));
HashMapSet(map, "url", JsonValueString(mxc));
return map;
}
char *
MatrixGetReply(HashMap *event)
{
if (!event)
{
return NULL;
}
return StrDuplicate(JsonValueAsString(
JsonGet(event, 4, "content",
"m.relates_to", "m.in_reply_to",
"event_id")
));
}
char *
MatrixGetEdit(HashMap *event)
{
if (!event)
{
return NULL;
}
return StrDuplicate(JsonValueAsString(
JsonGet(event, 3, "content",
"m.relates_to", "event_id")
));
}
HashMap *
MatrixCreateReplace(char *event, char *body)
{
HashMap *map;
HashMap *new;
HashMap *rel;
if (!body || !event)
{
return NULL;
}
map = HashMapCreate();
HashMapSet(map, "msgtype", JsonValueString("m.text"));
HashMapSet(map, "body", JsonValueString(body));
new = HashMapCreate();
HashMapSet(new, "msgtype", JsonValueString("m.text"));
HashMapSet(new, "body", JsonValueString(body));
rel = HashMapCreate();
HashMapSet(rel, "rel_type", JsonValueString("m.replace"));
HashMapSet(rel, "event_id", JsonValueString(event));
HashMapSet(map, "m.new_content", JsonValueObject(new));
HashMapSet(map, "m.relates_to", JsonValueObject(rel));
{
XEP393Element *e = XEP393(body);
char *text = XEP393ToXMLString(e);
XEP393FreeElement(e);
HashMapSet(map, "formatted_body", JsonValueString(text));
HashMapSet(map, "format", JsonValueString("org.matrix.custom.html"));
HashMapSet(new, "formatted_body", JsonValueString(text));
HashMapSet(new, "format", JsonValueString("org.matrix.custom.html"));
Free(text);
}
return map;
}
HashMap *
MatrixCreateReact(char *event, char *body)
{
HashMap *map;
HashMap *rel;
if (!body || !event)
{
return NULL;
}
map = HashMapCreate();
rel = HashMapCreate();
HashMapSet(rel, "rel_type", JsonValueString("m.annotation"));
HashMapSet(rel, "event_id", JsonValueString(event));
HashMapSet(rel, "key", JsonValueString(body));
HashMapSet(map, "m.relates_to", JsonValueObject(rel));
return map;
}
void
MatrixSetReply(HashMap *map, char *event)
{
if (!map || !event)
{
return;
}
JsonValueFree(JsonSet(map, JsonValueString(event),
3, "m.relates_to", "m.in_reply_to", "event_id"
));
}