diff --git a/src/Events.c b/src/Events.c index 85b1e37..a78e756 100644 --- a/src/Events.c +++ b/src/Events.c @@ -79,7 +79,7 @@ MatrixCreateNickChange(char *nick) return map; } HashMap * -MatrixCreateMedia(char *mxc, char *body, char *mime) +MatrixCreateMedia(char *mxc, char *body, char *mime, FileInfo *info) { HashMap *map; char *mime_type = NULL, *matrix_type = NULL; @@ -120,6 +120,11 @@ MatrixCreateMedia(char *mxc, char *body, char *mime) 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)); diff --git a/src/FileInfo.c b/src/FileInfo.c new file mode 100644 index 0000000..e242e9d --- /dev/null +++ b/src/FileInfo.c @@ -0,0 +1,66 @@ +#include + +#include +#include + +#include +#include + +static int +GetField(XMLElement *elem) +{ + XMLElement *child; + if (!elem || ArraySize(elem->children) != 1) + { + return 0; + } + + child = ArrayGet(elem->children, 0); + + return strtol(child->data, NULL, 10); +} + +FileInfo * +FileInfoFromXMPP(XMLElement *stanza) +{ + FileInfo *info; + XMLElement *reference, *sims, *file; + + if (!stanza) + { + return NULL; + } + + reference = XMLookForTKV(stanza, + "reference", "xmlns", "urn:xmpp:reference:0" + ); + sims = XMLookForTKV(reference, + "media-sharing", "xmlns", "urn:xmpp:sims:1" + ); + file = XMLookForTKV(sims, + "file", "xmlns", "urn:xmpp:jingle:apps:file-transfer:5" + ); + if (!file) + { + return NULL; + } + + info = Malloc(sizeof(*info)); + + + info->width = GetField(XMLookForUnique(file, "width")); + info->height = GetField(XMLookForUnique(file, "height")); + info->size = GetField(XMLookForUnique(file, "size")); + return info; +} + +void +FileInfoFree(FileInfo *info) +{ + if (!info) + { + return; + } + + Free(info); +} diff --git a/src/XMPPThread/Stanzas/Message.c b/src/XMPPThread/Stanzas/Message.c index ae5f345..7cb82b3 100644 --- a/src/XMPPThread/Stanzas/Message.c +++ b/src/XMPPThread/Stanzas/Message.c @@ -419,10 +419,13 @@ end_error: if (oob_data) { + FileInfo *info = FileInfoFromXMPP(stanza); mxc = ASReupload(args->config, oob_data->data, &mime); if (mxc) { - content = MatrixCreateMedia(mxc, data->data, mime); + content = MatrixCreateMedia( + mxc, data->data, mime, info + ); HashMapSet(content, "at.kappach.at.parsee.external", @@ -436,6 +439,7 @@ end_error: ); Free(mxc); } + FileInfoFree(info); Free(mime); } } diff --git a/src/include/FileInfo.h b/src/include/FileInfo.h new file mode 100644 index 0000000..3d98c9b --- /dev/null +++ b/src/include/FileInfo.h @@ -0,0 +1,23 @@ +#ifndef PARSEE_FILEINFO_H +#define PARSEE_FILEINFO_H + +#include "XML.h" + +typedef struct FileInfo { + int width; + int height; + int size; +} FileInfo; + +/** Grab file metadata through SIMS. + * ---------------- + * Returns: File information(SIMS)[HEAP] + * Thrasher: FileInfoFree */ +extern FileInfo * FileInfoFromXMPP(XMLElement *stanza); + +/** Frees all information related with file metadata. + * -------------- + * Thrashes: info */ +extern void FileInfoFree(FileInfo *info); + +#endif diff --git a/src/include/Matrix.h b/src/include/Matrix.h index 0ffb7ec..a408389 100644 --- a/src/include/Matrix.h +++ b/src/include/Matrix.h @@ -3,6 +3,8 @@ #include +#include "FileInfo.h" + /* A simple representation of everything. It is not as complex as * Telodendria's CommonID parser, simply because Parsee does not * need such complexity. */ @@ -33,7 +35,7 @@ extern HashMap * MatrixCreateReact(char *event, char *body); extern HashMap * MatrixCreateReplace(char *event, char *body); /* Creates the content for a media file. */ -extern HashMap * MatrixCreateMedia(char *mxc, char *body, char *mime); +extern HashMap * MatrixCreateMedia(char *mxc, char *body, char *mime, FileInfo *info); /* Creates the content for a m.room.name state event */ extern HashMap * MatrixCreateNameState(char *name);