Parsee/src/Events.c

229 lines
4.8 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)
{
HashMap *map;
char *mime_type = NULL, *matrix_type = NULL;
if (!mxc || !body)
{
return NULL;
}
matrix_type = "m.file";
if (mime)
{
size_t i;
mime_type = StrDuplicate(mime);
for (i = 0; i < strlen(mime); 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();
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"
));
}